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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.constant.msg.SaOrderMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.sale.ext.SaOrderSelfLogExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.common.domain.sale.order.SaOrderSelfLog;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.sale.order.controller.ParentOrderController;
import com.hzs.sale.order.param.SelfPickUpParam;
import com.hzs.sale.order.service.ISaOrderSelfLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 订单自助业务
 * @Author: sui q
 * @Time: 2022/10/25 11:38
 * @Classname: ApiFanSaOrderController
 * @PackageName: com.hzs.sale.order.controller.api
 */
@RestController
@RequestMapping("/api/order")
@Slf4j
public class ApiSaOrderBusinessController extends ParentOrderController {

    private ISaOrderSelfLogService saOrderSelfLogService;

    @Autowired
    public void setSaOrderSelfLogService(ISaOrderSelfLogService saOrderSelfLogService) {
        this.saOrderSelfLogService = saOrderSelfLogService;
    }

    /**
     * @param selfPickUpParam 订单参数
     * @Description: 订单申请自提
     * @return: AjaxResult
     * @Author: sui q
     * @Date: 2022/8/27 16:47
     */
    @PostMapping("/self-pick-up")
    public AjaxResult selfPickUpSaOrder(@RequestBody SelfPickUpParam selfPickUpParam) throws Exception{
        // 验证必输项
        if(StringUtils.isEmpty(selfPickUpParam.getPhone()) && StringUtils.isEmpty(selfPickUpParam.getEmail())){
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        if (StringUtils.isEmpty(selfPickUpParam.getOrderCode())) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        selfPickUpParam.setCenterCodeId(SecurityUtils.getUserId());
        selfPickUpParam.setPkSettleCountry(SecurityUtils.getPkCountry());
        SaOrder saOrder = getOrderService().querySaleOrderByOrderCode(SaOrder.builder().orderStatus(EOrderStatus.PAY.getValue()).orderCode(selfPickUpParam.getOrderCode()).build());
        QueryWrapper<SaOrderSelfLog> queryWrapper=new QueryWrapper();
        queryWrapper.eq("PK_ORDER",saOrder.getPkId());
        List<SaOrderSelfLog>  list=saOrderSelfLogService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.SELF_PICKUP_ORDER_REPEAT));
        }
        if(saOrder == null){
            return AjaxResult.error(TransactionUtils.getContent(SaOrderMsgConstants.ORDER_EMPTY));
        }else{
            // 验证订单只能是当天的
            if(!DateUtils.compareDateEqual(DateUtils.currentDate(), saOrder.getPayTime())){
                return AjaxResult.error(TransactionUtils.getContent(SaOrderMsgConstants.ORDER_CURRENT_DATE));
            }
            selfPickUpParam.setPkCreator(SecurityUtils.getUserId());
            saOrderSelfLogService.applySelfPickUpSaOrder(saOrder, selfPickUpParam);
            return AjaxResult.success(selfPickUpParam);
        }
    }

    /**
     * 查询订单列表
     *
     * @return
     */
    @PostMapping("/self-order-list")
    public TableDataInfo getQueryList(@RequestBody SelfPickUpParam selfPickUpParam) {
        startPage();
        selfPickUpParam.setPkSettleCountry(SecurityUtils.getPkCountry());
        if(selfPickUpParam.getApproveStatus() == null){
            selfPickUpParam.setApproveStatus(EApproveRechargeStatus.SUBMIT.getValue());
        }
        List<SaOrderSelfLogExt> orderSelfLogList = saOrderSelfLogService.queryMemberSaOrderSelfLog(selfPickUpParam);
        return getDataTable(orderSelfLogList);
    }
}
