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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.util.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;

/**
 * 订单自助业务
 */
@RestController
@RequestMapping("/api/order")
@Slf4j
public class ApiSaOrderBusinessController extends ParentOrderController {

    @Autowired
    private ISaOrderSelfLogService iSaOrderSelfLogService;

    /**
     * 订单申请自提
     *
     * @param selfPickUpParam 订单参数
     */
    @PostMapping("/self-pick-up")
    public AjaxResult selfPickUpSaOrder(@RequestBody SelfPickUpParam selfPickUpParam) throws Exception {
        // 验证必输项
        if (StringUtils.isEmpty(selfPickUpParam.getPhone()) && StringUtils.isEmpty(selfPickUpParam.getEmail())) {
            return AjaxResult.error(MemberMsgConstants.REQUIRED_NOT_EMPTY);
        }
        if (StringUtils.isEmpty(selfPickUpParam.getOrderCode())) {
            return AjaxResult.error(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());
        LambdaQueryWrapper<SaOrderSelfLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaOrderSelfLog::getPkOrder, saOrder.getPkId());
        List<SaOrderSelfLog> list = iSaOrderSelfLogService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.SELF_PICKUP_ORDER_REPEAT));
        }
        // 验证订单只能是当天的
        if (!DateUtils.compareDateEqual(DateUtils.currentDate(), saOrder.getPayTime())) {
            return AjaxResult.error(TransactionUtils.getContent(SaOrderMsgConstants.ORDER_CURRENT_DATE));
        }
        selfPickUpParam.setPkCreator(SecurityUtils.getUserId());
        iSaOrderSelfLogService.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 = iSaOrderSelfLogService.queryMemberSaOrderSelfLog(selfPickUpParam);
        return getDataTable(orderSelfLogList);
    }

}
