package org.springblade.modules.api.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.AfterSalesTypeEnum;
import org.springblade.common.enums.OrderStateEnum;
import org.springblade.common.enums.OrderTypeEnum;
import org.springblade.common.utils.SpringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.api.request.GroupOrderRequest;
import org.springblade.modules.api.request.SubmitOrderRequest;
import org.springblade.modules.api.response.GrouponOrderResponse;
import org.springblade.modules.api.response.SubmitOrderResponse;
import org.springblade.modules.api.service.SubmitOrderService;
import org.springblade.modules.api.service.impl.GrouponOrderServiceImpl;
import org.springblade.modules.finance.entity.Order;
import org.springblade.modules.finance.service.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * Author pangqijun
 * Date 2023/6/6
 * Description
 */
@RestController
@RequestMapping(CommonConstant.API_URL + "/order")
@Api(value = "订单接口", tags = "05.订单接口")
public class ApiOrderController extends BladeController {

    @Resource
    private GrouponOrderServiceImpl grouponOrderService;
    @Resource
    private IOrderService orderService;
    @Resource
    private IConfirmOrderService confirmOrderService;
    @Resource
    private IOrderGrouponService orderGrouponService;
    @Resource
    private IAfterSalesService afterSalesService;
    @Resource
    private IOrderGoodsService iOrderGoodsService;


    @ApiLog("通用下单")
    @PostMapping("/submit")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "通用下单")
    public R<SubmitOrderResponse> submitOrder(@Valid @RequestBody SubmitOrderRequest request) {
        if (StringUtil.isEmpty(request.getDistributeMode())) {
            request.setDistributeMode(1);
        }
        request.setUserId(AuthUtil.getUserId());
        SubmitOrderService submitOrderService = SpringUtils.getBean(OrderTypeEnum.getInstanceByValue(request.getOrderType()));
        Assert.notNull(submitOrderService, "获取实现类失败");
        String orderNo = submitOrderService.submitOrder(request);
        return data(new SubmitOrderResponse(orderNo));
    }

    @PostMapping("/groupon")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取拼团下单数据", notes = "传入groupon")
    public R<GrouponOrderResponse> getGroupOrderData(@Valid @RequestBody GroupOrderRequest request) {
        return R.data(grouponOrderService.getGroupOrderData(request));
    }

    @PostMapping("cancel")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "取消订单", notes = "传入订单号")
    public R orderCancel(Long orderId) {
        orderService.orderCancel(orderId);
        return R.success("订单已取消");
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/return-goods")
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "申请退货", notes = "传入订单号")
    public R afterSales(@RequestParam("orderId")Long orderId,
                        @RequestParam("reason") String reason) {
        Order order = orderService.getById(orderId);
        Assert.notNull(order, "订单数据不存在");
        afterSalesService.add(orderId, reason, AfterSalesTypeEnum.RETURN_GOODS);

        order.setOrderState(OrderStateEnum.AFTER_SALES.getValue());
        orderService.updateById(order);
        return success("操作成功");
    }
}
