package com.hzs.sale.order.controller.manager;


import com.hzs.common.core.constant.msg.SaOrderMsgConstants;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.sale.order.param.OrderQueryParam;
import com.hzs.sale.order.service.ISaOrderRevokeService;
import com.hzs.sale.order.service.ISaOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 撤销订单主表 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-10-19
 */
@RestController
@RequestMapping("/manager/revoke")
@Slf4j
public class SaOrderRevokeController extends BaseController {

    private ISaOrderService orderService;

    private ISaOrderRevokeService orderRevokeService;

    private UserTokenService userTokenService;

    @Autowired
    public void setOrderRevokeService(ISaOrderRevokeService orderRevokeService) {
        this.orderRevokeService = orderRevokeService;
    }

    @Autowired
    public void setOrderService(ISaOrderService orderService) {
        this.orderService = orderService;
    }

    @Autowired
    public void setUserTokenService(UserTokenService userTokenService) {
        this.userTokenService = userTokenService;
    }

    /**
     * @param orderQueryParam 订单参数
     * @Description: 撤销订单
     * @return: AjaxResult
     * @Author: sui q
     * @Date: 2022/8/27 16:56
     */
    @PostMapping("/cancel")
    public AjaxResult revokeOrder(@RequestBody OrderQueryParam orderQueryParam) {
        log.info(orderQueryParam.getOrderCode());
        // 验证订单编号、订单类型是否存在
        if (StringUtils.isEmpty(orderQueryParam.getOrderCode()) || orderQueryParam.getSpecialArea() == null) {
            return AjaxResult.error(TransactionUtils.getContent(SaOrderMsgConstants.ORDER_CODE_EMPTY));
        }
        SaOrder saOrder = SaOrder.builder()
                .orderCode(orderQueryParam.getOrderCode())
                .orderType(orderQueryParam.getSpecialArea()).build();
        saOrder.setPkCountry(SecurityUtils.getPkCountry());
        SaOrderExt saOrderExt = orderService.querySaleOrderExtByOrderCode(saOrder, orderQueryParam.getItemIdList());
        if (saOrderExt == null) {
            return AjaxResult.error(TransactionUtils.getContent(SaOrderMsgConstants.ORDER_EMPTY));
        }
        orderRevokeService.revokeSaOrder(saOrder, userTokenService.getLoginUser());
        return AjaxResult.success();
    }

    /**
     * @param orderQueryParam 订单参数
     * @Description: 撤销所有可以撤销订单
     * @return: AjaxResult
     * @Author: sui q
     * @Date: 2022/8/27 16:56
     */
    @PostMapping("/query-order")
    public AjaxResult queryOrder(@RequestBody OrderQueryParam orderQueryParam) {
        orderQueryParam.setPkCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(orderService.querySaleOrderByCondition(orderQueryParam));
    }

}
