package com.hzs.scm.ic.controller.manage;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.constant.msg.ScmMsgConstants;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.EScmOrderStatus;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.sale.classify.BdSpecs;
import com.hzs.common.domain.scm.ic.IcAvailableNum;
import com.hzs.common.domain.scm.ic.ext.IcAvailableNumExt;
import com.hzs.common.domain.scm.po.ext.PoOrderExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.scm.ic.param.AvailableNum;
import com.hzs.scm.ic.service.IIcAvailableNumService;
import com.hzs.scm.ic.vo.IcAvailableNumVo;
import com.hzs.scm.po.param.PoOrderParam;
import com.hzs.scm.po.vo.PoOrderVo;
import com.hzs.scm.pub.controller.ScmController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
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;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <p>
 * 可用量 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2023-12-09
 */
@RestController
@RequestMapping("/manage/available")
public class IcAvailableNumController extends ScmController {

    @Autowired
    private IIcAvailableNumService availableNumService;

    /**
     * 查询列表
     *
     * @return cubasdocParam 入参
     */
    @PostMapping("/list")
    @Log(module = EOperationModule.AVAILABLE_NUM, business = EOperationBusiness.AVAILABLE_NUM, method = EOperationMethod.SELECT)
    public TableDataInfo listAvailableNum(@RequestBody AvailableNum availableNum) {
        startPage();
        availableNum.setPkCountry(SecurityUtils.getPkCountry());
        List<IcAvailableNumExt> availableNumList = availableNumService.queryAvailableNumByCondition(availableNum);
        return getDataTable(availableNumList);
    }

    /**
     * 分页查询会员某个时间段内的奖金汇总
     */
    @PostMapping("/export-list")
    @Log(module = EOperationModule.AVAILABLE_NUM, business = EOperationBusiness.AVAILABLE_NUM, method = EOperationMethod.EXPORT)
    public void exportListAvailableNum(HttpServletResponse response, AvailableNum availableNum){
        availableNum.setPkCountry(SecurityUtils.getPkCountry());
        List<IcAvailableNumExt> availableNumList = availableNumService.queryAvailableNumByCondition(availableNum);
        List<IcAvailableNumVo> availableNumVoList = packageScmExportVo(availableNumList);
        ExcelUtil<IcAvailableNumVo> util = new ExcelUtil<>(IcAvailableNumVo.class);
        util.exportExcel(response, availableNumVoList, "可用量导出");
    }

    private List<IcAvailableNumVo> packageScmExportVo(List<IcAvailableNumExt> availableNumList) {
        List<IcAvailableNumVo> availableNumVoList = new ArrayList<>();
        for (IcAvailableNumExt availableNumExt : availableNumList) {
            IcAvailableNumVo availableNumVo = BeanUtil.copyProperties(availableNumExt, IcAvailableNumVo.class);
            availableNumVoList.add(availableNumVo);
        }
        return availableNumVoList;
    }

    /**
     * 新增采购入库
     *
     * @param availableNum 入参
     */
    @PostMapping("/insert")
    @Log(module = EOperationModule.AVAILABLE_NUM, business = EOperationBusiness.AVAILABLE_NUM, method = EOperationMethod.INSERT)
    public AjaxResult savePoOrder(@RequestBody AvailableNum availableNum) {
        int pkCountry = SecurityUtils.getPkCountry();
        // 验证供应商
        String msg = validateAvailableMsg(availableNum, pkCountry);
        if (ObjectUtil.isNotEmpty(msg)) {
            return AjaxResult.error(msg);
        }
        // 计算价税合计、总数量
        availableNum.setPkCountry(pkCountry);
        availableNumService.insertIcAvailableNumByParam(availableNum, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /*
     * @description: 验证订单相关信息
     * @author: sui q
     * @date: 2023/11/21 11:43
     * @param: null null
     **/
    private String validateAvailableMsg(AvailableNum availableNum, int pkCountry) {
        Set<Integer> storehouseIdSet = new HashSet<>();
        if (availableNum.getPkCorp() == null || availableNum.getAvailableNum() == null ||
                availableNum.getPkProduct() == null || availableNum.getPkProductBas() == null ||
                availableNum.getPkStorehouse() == null ||
                CollectionUtil.isEmpty(availableNum.getScmItemSpecsList())) {
            return MemberMsgConstants.REQUIRED_NOT_EMPTY;
        }
        // 验证供应商
        String msg = validateCuBasDoc(availableNum.getPkCubasdoc(), pkCountry);
        if(ObjectUtil.isNotEmpty(msg)){
            return msg;
        }
        // 验证采购组织
        msg = validateSysCorp(availableNum.getPkCorp(), pkCountry);
        if(ObjectUtil.isNotEmpty(msg)){
            return msg;
        }
        storehouseIdSet.add(availableNum.getPkStorehouse());
        // 验证仓库，根据采购组织验证
        msg = validateStorehouse(storehouseIdSet, pkCountry, availableNum.getPkCorp());
        if(ObjectUtil.isNotEmpty(msg)){
            return msg;
        }
        // 验证产品，根据产品
        // 验证规格、规格分类 需要根据产品查询 处理specsName
        Map<String, BdSpecs> specsMap = new HashMap<>();
        String message = validateProduct(Collections.singletonList(availableNum), specsMap);
        if(ObjectUtil.isNotEmpty(message)){
            return message;
        }
        // 可用量 同规格同产品同库存组织同仓库只能有一条
        List<IcAvailableNumExt> availableNumList = availableNumService.queryAvailableNumByCondition(availableNum);
        if(CollectionUtil.isNotEmpty(availableNumList)){
            return ScmMsgConstants.AVAILABLE_NUM_EXIST;
        }
        return null;
    }

    /**
     * 修改可用量
     *
     * @param  availableNum 可用量
     */
    @PostMapping("/update")
    @Log(module = EOperationModule.AVAILABLE_NUM, business = EOperationBusiness.AVAILABLE_NUM, method = EOperationMethod.UPDATE)
    public AjaxResult updateAvailableNum(@RequestBody AvailableNum availableNum) {
        if (availableNum.getPkId() == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        IcAvailableNum icAvailableNum= availableNumService.getById(availableNum.getPkId());
        if(icAvailableNum == null){
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.AVAILABLE_NUM_NOT_EXIST));
        }
        icAvailableNum.setAvailableNum(availableNum.getAvailableNum());
        icAvailableNum.setPkStorehouse(availableNum.getPkStorehouse());
        icAvailableNum.setPkCubasdoc(availableNum.getPkCubasdoc());
        icAvailableNum.setPkCorp(availableNum.getPkCorp());
        availableNumService.updateDeductIcAvailableNumByGeneralOut(icAvailableNum, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

}
