package com.huaheng.pc.shipment.shipmentContainerHeader.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.domain.RetCode;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.shipmentPreference.service.ShipmentPreferenceService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.shipment.lockingWorkOrder.domain.LockingWorkOrder;
import com.huaheng.pc.shipment.lockingWorkOrder.service.LockingWorkOrderService;
import com.huaheng.pc.shipment.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentCombinationModel;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.shipment.shippingCombination.domain.ShippingSearch;
import com.huaheng.pc.shipment.shippingCombination.service.ShippingCombinationService;
import com.huaheng.pc.shipment.wave.domain.Wave;
import com.huaheng.pc.shipment.wave.service.WaveService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.ShipmentTaskCreateModel;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.ShipmentTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.mapper.ShipmentContainerHeaderMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ShipmentContainerHeaderServiceImpl extends ServiceImpl<ShipmentContainerHeaderMapper, ShipmentContainerHeader> implements ShipmentContainerHeaderService{

    @Autowired
    private ShipmentDetailService shipmentDetailService;
    @Autowired
    private InventoryDetailService inventoryDetailService;
    @Autowired
    private LocationService locationService;
    @Autowired
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentContainerHeaderMapper shipmentContainerHeaderMapper;
    @Autowired
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Autowired
    private MaterialService materialService;
    @Autowired
    private ShippingCombinationService shippingCombinationService;
    @Autowired
    private TaskHeaderService taskHeaderService;
    @Autowired
    private TaskDetailService taskDetailService;
    @Autowired
    private WaveService waveService;
    @Autowired
    private ShipmentPreferenceService shipmentPreferenceService;
    @Autowired
    private ContainerService containerService;
    @Resource
    private ShipmentTaskService shipmentTaskService;
    @Autowired
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Autowired
    private LockingWorkOrderService lockingWorkOrderService;


    @Override
    public Map<String, Integer> getShipmentContainerMaxAndMinStatusByShipmentID(int shipmentId) {
        return shipmentContainerHeaderMapper.getShipmentContainerMaxAndMinStatusByShipmentID(shipmentId);
    }



    /**
     * 1.检查基本属性
     * 2.更改库存明细
     * 3.更新单据明细的已出库数量
     * 4.自动判定出库任务状态,根据库存数量减去预定库存相等就是整盘出--预计任务状态
     * 5.增加出库组盘头
     * 6.增加出库组盘明细
     * 7.更新单据状态
     *
     * 出库组盘单条保存
     * @param shipmentCombinationModel
     * @return
     */
    @Transactional
    @Override
    public ShipmentContainerHeader combination(ShipmentCombinationModel shipmentCombinationModel) {

        //1.检查基本属性
        //校验
        if(shipmentCombinationModel.getShipQty().compareTo(new BigDecimal("0")) <= 0){
            throw new ServiceException("出库数量必须大于0");
        }
        ShipmentDetail shipmentDetail = shipmentDetailService.getById(shipmentCombinationModel.getShipmentDetailId());
        if(shipmentDetail == null){
            throw new ServiceException("出库明细未找到");
        }
        InventoryDetail inventoryDetail = inventoryDetailService.getById((shipmentCombinationModel.getInventoryDetailId()));
        if(inventoryDetail == null){
            throw new ServiceException("库存未找到");
        }
        String containerCode = inventoryDetail.getContainerCode();
        int unCompleteCombineNumber = receiptContainerHeaderService.getUnCompleteCombineNumber(containerCode);
        if(unCompleteCombineNumber > 0) {
            throw new ServiceException("托盘:" + containerCode + " 已经用于入库组盘");
        }
        Container container = containerService.getContainerByCode(containerCode);
        if(container != null) {
            if(container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
                throw new ServiceException("托盘已经锁定,不能再组盘");
            }
        }
        //校验数量是否超出
        if((shipmentDetail.getQty().subtract(shipmentDetail.getTaskQty())).compareTo(shipmentCombinationModel.getShipQty())<0){
            throw new ServiceException("录入数量超出明细待出数量");
        }
        //校验库存可用数量
        if(inventoryDetail.getQty().subtract(inventoryDetail.getTaskQty()).compareTo(shipmentCombinationModel.getShipQty())<0){
            throw new ServiceException("录入数量超出可出数量");
        }
        if(!shipmentDetail.getMaterialCode().equals(inventoryDetail.getMaterialCode())){
            throw new ServiceException("配盘物料不一致");
        }
        //库位
        LambdaQueryWrapper<Location> lambdaQueryWrapper= Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Location::getWarehouseCode,inventoryDetail.getWarehouseCode())
                .eq(Location::getCode,inventoryDetail.getLocationCode());
        Location location = locationService.getOne(lambdaQueryWrapper);
        if (location == null) {
            throw new ServiceException("库位 " + inventoryDetail.getLocationCode() + " 不存在");
        }

        //2.更新库存分配数
        inventoryDetail.setTaskQty(inventoryDetail.getTaskQty().add(shipmentCombinationModel.getShipQty()));
        inventoryDetailService.saveOrUpdate(inventoryDetail);
        //3.更新单据明细的已出库数量
        shipmentDetail.setTaskQty(shipmentDetail.getTaskQty().add(shipmentCombinationModel.getShipQty()));
        int i = shipmentDetail.getQty().compareTo(shipmentDetail.getTaskQty());
        if(i > 0){
            shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_WAVE);
        }else if(i == 0) {
            shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_GROUPDISK);
        }else if(i < 0) {
            throw new ServiceException("出库数量不能大于单据数量!");
        }
        shipmentDetailService.saveOrUpdate(shipmentDetail);

        //自动判定出库任务状态,根据库存数量减去预定库存相等就是整盘出--预计任务状态
        BigDecimal inventoryQty = inventoryDetail.getQty().subtract(inventoryDetail.getTaskQty());
        if(inventoryQty.compareTo(new BigDecimal(0)) <= 0) {
            shipmentCombinationModel.setTaskType(QuantityConstant.TASK_TYPE_WHOLESHIPMENT);
        } else {
            shipmentCombinationModel.setTaskType(QuantityConstant.TASK_TYPE_SORTINGSHIPMENT);
        }

        //5.增加出库组盘头
        ShipmentContainerHeader shipmentContainerHeader = ShipmentContainerHeaderAdd(location, shipmentDetail);
        //6.增加出库组盘明细
        ShipmentContainerDetail shipmentContainerDetail = ShipmentContainerDetailAdd(shipmentDetail, shipmentContainerHeader, shipmentCombinationModel);
        //7.更新单据状态
        shipmentHeaderService.updateShipmentStatus(shipmentDetail.getShipmentId());

        return  shipmentContainerHeader;
    }

    /**
     * 组盘头
     * 1.查看是否有状态小于等于20的组盘头,有就需新建组盘头,没有就新建
     * 2.没有符合条件的组盘头,新建组盘头
     *
     * @param location
     * @param shipmentDetail
     * @return
     */
    private ShipmentContainerHeader ShipmentContainerHeaderAdd(Location location,
                                                               ShipmentDetail shipmentDetail) {
        //1.查看是否有状态小于等于20的组盘头,有就需新建组盘头,没有就新建
        LambdaQueryWrapper<ShipmentContainerHeader> lambdaQueryWrapper=Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentContainerHeader::getLocationCode,location.getCode())
                .eq(ShipmentContainerHeader::getContainerCode,location.getContainerCode())
                .eq(ShipmentContainerHeader::getWarehouseCode,ShiroUtils.getWarehouseCode())
                .lt(ShipmentContainerHeader::getStatus,QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
        ShipmentContainerHeader shipmentContainerHeader = this.getOne(lambdaQueryWrapper);
        if(shipmentContainerHeader != null)  {
            return shipmentContainerHeader;
        }
        else {
            // 2.没有符合条件的组盘头,新建组盘头
            //找到容器类型
            LambdaQueryWrapper<Container> containerLam=Wrappers.lambdaQuery();
            containerLam.eq(Container::getCode,location.getContainerCode())
                    .eq(Container::getWarehouseCode,ShiroUtils.getWarehouseCode());
            Container container = containerService.getOne(containerLam);
            if(container == null) {
                throw new ServiceException("系统没有此容器编码");
            }
            LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode, container.getCode());
            List<InventoryDetail> inventoryDetailList =  inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
            int taskType = QuantityConstant.TASK_TYPE_WHOLESHIPMENT;
            if(inventoryDetailList != null && inventoryDetailList.size() > 0) {
                for(InventoryDetail inventoryDetail : inventoryDetailList) {
                    if(inventoryDetail.getQty().subtract(inventoryDetail.getTaskQty()).
                            compareTo(BigDecimal.ZERO) > 0) {
                        taskType = QuantityConstant.TASK_TYPE_SORTINGSHIPMENT;
                    }
                }
            }
            shipmentContainerHeader = new ShipmentContainerHeader();
            shipmentContainerHeader.setContainerCode(location.getContainerCode());
            shipmentContainerHeader.setLocationCode(location.getCode());
            shipmentContainerHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
            shipmentContainerHeader.setCompanyCode(shipmentDetail.getCompanyCode());
            shipmentContainerHeader.setPort(shipmentDetail.getPort());
            shipmentContainerHeader.setContainerType(container.getContainerType());
            shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
            shipmentContainerHeader.setTaskType(taskType);
            shipmentContainerHeader.setTaskCreated(0);
            shipmentContainerHeader.setCreatedBy(ShiroUtils.getLoginName());
            Boolean flag = this.save(shipmentContainerHeader);
            if (flag == false) {
                throw new ServiceException("新建组盘头失败,sql错误");
            }
            return shipmentContainerHeader;
        }
    }

    /**
     * 组盘明细
     * 1.查看是否有同一出库明细的物料需要出库
     * 2.有时修改阻盘明细的出库数量就行
     * 3.没有就新建明细
     * 4。查看组盘头状态,如果状态在10到20,则生成任务明细或修改任务明细的数量
     *
     * @param shipmentDetail
     * @param shipmentContainerHeader
     * @param shipmentCombinationModel
     * @return
     */
    private ShipmentContainerDetail ShipmentContainerDetailAdd(ShipmentDetail shipmentDetail,
                                                               ShipmentContainerHeader shipmentContainerHeader,
                                                               ShipmentCombinationModel shipmentCombinationModel) {

        Boolean flag =true;
        //1.查看是否有同一出库明细的物料需要出库
        LambdaQueryWrapper<ShipmentContainerDetail> lambdaQueryWrapper=Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentContainerDetail::getShippingContainerId, shipmentContainerHeader.getId())
                .eq(ShipmentContainerDetail::getLocationCode, shipmentContainerHeader.getLocationCode())
                .eq(ShipmentContainerDetail::getContainerCode, shipmentContainerHeader.getContainerCode())
                .eq(ShipmentContainerDetail::getShipmentDetailId, shipmentCombinationModel.getShipmentDetailId())
                .eq(ShipmentContainerDetail::getInventoryId, shipmentCombinationModel.getInventoryDetailId())
                .eq(ShipmentContainerDetail::getWarehouseCode, ShiroUtils.getWarehouseCode());
        ShipmentContainerDetail shipmentContainerDetail = shipmentContainerDetailService.getOne(lambdaQueryWrapper);
        ShipmentContainerDetail shipmentContainerDetaill = new ShipmentContainerDetail();

        //2.有时修改阻盘明细的出库数量就行
        if(shipmentContainerDetail != null)    {
            shipmentContainerDetail.setQty(shipmentContainerDetail.getQty().add(shipmentCombinationModel.getShipQty()));
            flag=shipmentContainerDetailService.saveOrUpdate(shipmentContainerDetail);
            if(flag == false){
                throw new ServiceException("修改组盘明细失败,sql错误");
            }
        }
        else  {

            //3.没有就新建明细
            LambdaQueryWrapper<Material> lam=Wrappers.lambdaQuery();
            lam.eq(Material::getCode,shipmentDetail.getMaterialCode())
                    .eq(Material::getWarehouseCode,ShiroUtils.getWarehouseCode());
            Material material = materialService.getOne(lam);
            if (material == null) {
                throw new ServiceException("出库单(" + shipmentDetail.getShipmentCode() + ")的物料(" + shipmentDetail.getMaterialCode() + ")不存在!");
            }
            shipmentContainerDetaill.setWarehouseCode(shipmentContainerHeader.getWarehouseCode());
            shipmentContainerDetaill.setCompanyCode(shipmentContainerHeader.getCompanyCode());
            shipmentContainerDetaill.setContainerCode(shipmentContainerHeader.getContainerCode());
            shipmentContainerDetaill.setLocationCode(shipmentContainerHeader.getLocationCode());
            shipmentContainerDetaill.setInventoryId(shipmentCombinationModel.getInventoryDetailId());
            shipmentContainerDetaill.setInventorySts(shipmentDetail.getInventorySts());
            shipmentContainerDetaill.setShippingContainerId(shipmentContainerHeader.getId());
            shipmentContainerDetaill.setShipmentCode(shipmentDetail.getShipmentCode());
            shipmentContainerDetaill.setShipmentId(shipmentDetail.getShipmentId());
            shipmentContainerDetaill.setShipmentDetailId(shipmentDetail.getId());
            shipmentContainerDetaill.setMaterialCode(shipmentDetail.getMaterialCode());
            shipmentContainerDetaill.setMaterialName(shipmentDetail.getMaterialName());
            shipmentContainerDetaill.setMaterialSpec(shipmentDetail.getMaterialSpec());
            shipmentContainerDetaill.setMaterialUnit(shipmentDetail.getMaterialUnit());
            shipmentContainerDetaill.setQty(shipmentCombinationModel.getShipQty());
            shipmentContainerDetaill.setWaveId(shipmentDetail.getWaveId());
            shipmentContainerDetaill.setTaskCreated(0);
            shipmentContainerDetaill.setStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
            shipmentContainerDetaill.setBatch(shipmentDetail.getBatch());
            shipmentContainerDetaill.setLot(shipmentDetail.getLot());
            shipmentContainerDetaill.setProjectNo(shipmentDetail.getProjectNo());
            shipmentContainerDetaill.setCreatedBy(ShiroUtils.getLoginName());
            flag = shipmentContainerDetailService.save(shipmentContainerDetaill);
            if(flag == false){
                throw new ServiceException("新建组盘明细失败,sql错误");
            }
        }

        //4.查看组盘头状态,如果状态在10到30,则生成任务明细或修改任务明细的数量
        //查看任务头
        int containerHeaderStatus = shipmentContainerHeader.getStatus().intValue();
        if(QuantityConstant.SHIPMENT_CONTAINER_TASK <= containerHeaderStatus &&
                containerHeaderStatus < QuantityConstant.SHIPMENT_CONTAINER_FINISHED){
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper=Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.eq(TaskHeader::getWarehouseCode,shipmentContainerHeader.getWarehouseCode())
                    .in(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT, QuantityConstant.TASK_TYPE_WHOLESHIPMENT)
                    .eq(TaskHeader::getAllocationHeadId,shipmentContainerHeader.getId());
            TaskHeader taskHeader=taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
            if(taskHeader==null){
                throw new ServiceException("有相同的组盘头,但找不到相应的任务头");
            }

            if(shipmentContainerDetaill == null) {
                //shipmentContainerDetaill为空时,说明组盘明细不是新建,so修改任务明细
                //查找任务明细
                LambdaQueryWrapper<TaskDetail> taskDetailLam = Wrappers.lambdaQuery();
                taskDetailLam.eq(TaskDetail::getWarehouseCode,shipmentContainerDetail.getWarehouseCode())
                        .eq(TaskDetail::getAllocationId,shipmentContainerDetail.getId())
                        .eq(TaskDetail::getTaskId,taskHeader.getId())
                        .in(TaskDetail::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT, QuantityConstant.TASK_TYPE_WHOLESHIPMENT);
                TaskDetail taskDetail = taskDetailService.getOne(taskDetailLam);
                if(taskDetail == null){
                    throw new ServiceException("找不到对应的任务明细");
                }
                taskDetail.setQty(taskDetail.getQty().add(shipmentCombinationModel.getShipQty()));
                flag=taskDetailService.saveOrUpdate(taskDetail);
                if(flag == false){
                    throw new ServiceException("修改任务明细失败,sql错误");
                }
            }else {
                //shipmentContainerDetaill不为空时,说明组盘明细是新建,so新建任务明细
                TaskDetail taskDetail = new TaskDetail();
                taskDetail.setTaskId(taskHeader.getId());
                taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
                taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());
                taskDetail.setCompanyCode(taskHeader.getCompanyCode());
                taskDetail.setTaskType(taskHeader.getTaskType());
                taskDetail.setAllocationId(shipmentContainerDetaill.getId());
                taskDetail.setBillCode(shipmentContainerDetaill.getShipmentCode());
                taskDetail.setBillDetailId(shipmentContainerDetaill.getShipmentDetailId());
                taskDetail.setMaterialCode(shipmentContainerDetaill.getMaterialCode());
                taskDetail.setMaterialName(shipmentContainerDetaill.getMaterialName());
                taskDetail.setMaterialSpec(shipmentContainerDetaill.getMaterialSpec());
                taskDetail.setMaterialUnit(shipmentContainerDetaill.getMaterialUnit());
                taskDetail.setFromInventoryId(shipmentContainerDetaill.getInventoryId());
                taskDetail.setQty(shipmentContainerDetaill.getQty());
                taskDetail.setContainerCode(taskHeader.getContainerCode());
                taskDetail.setFromLocation(taskHeader.getFromLocation());
                taskDetail.setToLocation(taskHeader.getToLocation());
                taskDetail.setLot(shipmentContainerDetaill.getLot());
                taskDetail.setWaveId(shipmentContainerDetaill.getWaveId());
                taskDetail.setBatch(shipmentContainerDetaill.getBatch());
                taskDetail.setProjectNo(shipmentContainerDetaill.getProjectNo());
                if (shipmentContainerHeader.getStatus().equals(QuantityConstant.SHIPMENT_CONTAINER_TASK)) {
                    taskDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
                } else {
                    taskDetail.setStatus(QuantityConstant.TASK_STATUS_RELEASE);
                }
                taskDetail.setCreatedBy(ShiroUtils.getLoginName());
                taskDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
                flag = taskDetailService.save(taskDetail);
                if(flag == false){
                    throw new ServiceException("新建任务明细失败,sql错误");
                }
            }
        }
        return shipmentContainerDetail;
    }


    /**
     * 取消组盘的一组明细
     * @param detailIds
     * @return
     */
    @Override
    public AjaxResult cancelCombinationDetail(List<Integer> detailIds)  {
        List<ShipmentContainerDetail> shipmentContainerDetails = new ArrayList<>();
        for(int detailId:detailIds){
            ShipmentContainerDetail shipmentContainerDetail = shipmentContainerDetailService.getById(detailId);
            shipmentContainerDetails.add(shipmentContainerDetail);
        }
        return cancelCombinationDetailInner(shipmentContainerDetails);
    }

    /**
     * 取消组盘的一组明细
     * @param shipmentContainerDetails
     * @return
     */
    @Override
    @Transactional
    public AjaxResult cancelCombinationDetailInner(List<ShipmentContainerDetail> shipmentContainerDetails)  {
        for(ShipmentContainerDetail shipmentContainerDetail:shipmentContainerDetails){
            //获取头
            ShipmentContainerHeader shipmentContainerHeader = this.getById(shipmentContainerDetail.getShippingContainerId());
            if(shipmentContainerHeader.getStatus()>= QuantityConstant.SHIPMENT_CONTAINER_TASK){
                return AjaxResult.error("容器"+shipmentContainerHeader.getContainerCode()+"非新建状态,不允许取消明细");
            }
            //恢复占用库存
            InventoryDetail inventoryDetail = inventoryDetailService.getById(shipmentContainerDetail.getInventoryId());
            if(inventoryDetail!=null){
                inventoryDetail.setTaskQty(inventoryDetail.getTaskQty().subtract(shipmentContainerDetail.getQty()));
                inventoryDetailService.saveOrUpdate(inventoryDetail);
            }
            //恢复单据发货数量
            ShipmentDetail shipmentDetail = shipmentDetailService.getById(shipmentContainerDetail.getShipmentDetailId());
            shipmentDetail.setTaskQty(shipmentDetail.getTaskQty().subtract(shipmentContainerDetail.getQty()));
            if(shipmentDetail.getTaskQty().compareTo(BigDecimal.ZERO) != 0 ){
                shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_WAVE);//明细状态恢复,如果删除后还有以出数量就是波次
            }else{
                shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_POOL );//明细状态
            }
            shipmentDetailService.saveOrUpdate(shipmentDetail);
            //删除这个配盘明细
            shipmentContainerDetailService.removeById(shipmentContainerDetail.getId());

            //查询头表下还有没有明细,如果没有,则删了这个头表
            ShipmentContainerDetail condition = new ShipmentContainerDetail();
            condition.setShippingContainerId(shipmentContainerDetail.getShippingContainerId());
            List<Map<String, Object>> list= shipmentContainerDetailService.selectListMapByEqual("id", condition);
            if(list==null||list.size() == 0){
                this.removeById(shipmentContainerHeader.getId());
                locationService.updateStatus(shipmentContainerHeader.getLocationCode(),
                        QuantityConstant.STATUS_LOCATION_EMPTY);
            }
            //更新单据状态
            shipmentHeaderService.updateShipmentStatus(shipmentDetail.getShipmentId());
        }
        return AjaxResult.success("");
    }

    /**
     * 取消组盘
     * @param shipmentContainerIds
     * @return
     */
    @Override
    public AjaxResult cancelCombination(List<Integer> shipmentContainerIds)  {

        AjaxResult result = checkForCancelCombination(shipmentContainerIds);
        if(result.getCode()!= RetCode.SUCCESS.getValue()){
            return result;
        }
        for(int id : shipmentContainerIds){
            LambdaQueryWrapper<ShipmentContainerDetail> lambdaQueryWrapper=Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(ShipmentContainerDetail::getShippingContainerId,id);
            List<ShipmentContainerDetail> shipmentContainerDetails = shipmentContainerDetailService.list(lambdaQueryWrapper);
            result = cancelCombinationDetailInner(shipmentContainerDetails);
            if(result.hasErr()){
                return result;
            }
        }

        return AjaxResult.success("");
    }

    /**
     *  出库自动组盘
     * @param shipmentCode
     * @return
     */
    @Override
    @Transactional
    public AjaxResult autoCombination(String shipmentCode) {
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLam = Wrappers.lambdaQuery();
        shipmentHeaderLam.eq(ShipmentHeader::getWarehouseCode,ShiroUtils.getWarehouseCode())
                .eq(ShipmentHeader::getCode,shipmentCode);
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(shipmentHeaderLam);
        if(shipmentHeader == null){
            throw new ServiceException("系统没有此单据");
        }
        shipmentPreferenceService.checkShipmentProcess(shipmentHeader.getId().toString(),100,shipmentCode);

        LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentDetail::getShipmentCode, shipmentCode)
                .eq(ShipmentDetail::getWarehouseCode, ShiroUtils.getWarehouseCode());
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(lambdaQueryWrapper);
        if (shipmentDetailList.isEmpty()) {
            return AjaxResult.error("找不到子单链");
        }
        for(ShipmentDetail shipmentDetail: shipmentDetailList){
            if(shipmentDetail.getWaveId() != 0){
                return AjaxResult.error(shipmentCode+"单号已加入波次"+shipmentDetail.getWaveId()+",无法自动组盘");
            }
        }
        return this.autoCombination(shipmentDetailList);
    }


    //出库自动组盘
    @Override
    public AjaxResult autoCombination(List<ShipmentDetail> shipmentDetailList){
        int num = 0;
        List<Integer> list = new ArrayList<>();
        for (ShipmentDetail item : shipmentDetailList) {
            //获取需要出库数量
            BigDecimal shipmentQty = item.getQty().subtract(item.getTaskQty());
            String userDef1 = item.getUserDef1();
            if (StringUtils.isNotEmpty(userDef1)) {
                shipmentQty = new BigDecimal(userDef1);
            }
            //判断是否还有需要出库的物料,如果没有就跳过该物料
            if (shipmentQty.compareTo(BigDecimal.ZERO) <= 0) {
                continue;
            }

            List<InventoryDetail> inventoryList = shippingCombinationService.getInventorys(item);
            //去除已锁的库存
            ArrayList<InventoryDetail> removeInventoryList = new ArrayList<>();
            List<TaskHeader> taskHeaderList = taskHeaderService.getUnCompleteTaskList();
            List<ReceiptContainerHeader> receiptContainerHeaderList =
                    receiptContainerHeaderService.getUnCompleteCombineList();
            boolean lock = false;
            for (InventoryDetail inventoryDetail : inventoryList) {
                // 校验锁定数量之前,先查询锁定工作令数据是否存在
//                LockingWorkOrder lockingWorkOrder = lockingWorkOrderService.selectLockingWorkOrder(item.getMoCode(), item.getMaterialCode(), item.getId().toString());
//                if (lockingWorkOrder == null){
//                    //校验库存是否有被锁定的数量
//                    LockingWorkOrder lockingWorkOrder1=new LockingWorkOrder();
//                    lockingWorkOrder1.setInventoryId(inventoryDetail.getId().toString());
//                    List<LockingWorkOrder> lockingWorkOrderList = lockingWorkOrderService.getListByDoamin(lockingWorkOrder1);
//                    BigDecimal qty = inventoryDetail.getQty().subtract(inventoryDetail.getTaskQty());
//                    if (lockingWorkOrderService.inventoryCheck(lockingWorkOrderList, qty, shipmentQty)){
//                        continue;
//                    }
//                }
                LambdaQueryWrapper<Container> containerLambdaQueryWrapper  = Wrappers.lambdaQuery();
                containerLambdaQueryWrapper.eq(Container::getCode, inventoryDetail.getContainerCode());
                Container container = containerService.getOne(containerLambdaQueryWrapper);
                if (QuantityConstant.STATUS_CONTAINER_LOCK.equals(container.getStatus())) {
                    removeInventoryList.add(inventoryDetail);
                    lock = true;
                }
                if(taskHeaderList != null) {
                    for(TaskHeader taskHeader : taskHeaderList) {
                        if(taskHeader.getContainerCode().equals(container.getCode())) {
                            removeInventoryList.add(inventoryDetail);
                        }
                    }
                }
                if(receiptContainerHeaderList != null) {
                    for(ReceiptContainerHeader receiptContainerHeader : receiptContainerHeaderList) {
                        if(receiptContainerHeader.getContainerCode().equals(container.getCode())) {
                            removeInventoryList.add(inventoryDetail);
                        }
                    }
                }
                // 判断锁定工作令数据是否需要修改
//                if (lockingWorkOrder != null){
//                    lockingWorkOrderService.checkLockingWorkOrder(lockingWorkOrder, shipmentQty);
//                }
            }
            inventoryList.removeAll(removeInventoryList);
            if (inventoryList.size()<1 && lock){
                return AjaxResult.error("'"+item.getMaterialName()+"' 所在托盘全部处于锁定状态,请完成或取消任务后再进行组盘");
            }

            if(inventoryList.size() < 1 && item.getWaveId() != 0){
                Wave wave = waveService.getById(item.getWaveId());
                wave.setStatus(QuantityConstant.WAVE_STATUS_ERROR);
                wave.setCurrentWaveStep(QuantityConstant.WAVE_STEP_ERROR);
                wave.setLastWaveStep(QuantityConstant.WAVE_STEP_BUILD);
                waveService.updateById(wave);
                throw new ServiceException("主单为"+item.getShipmentCode()+"子单id为"+item.getId() + "的单据没有库存,波次失败");
            }
            if (inventoryList.size() < 1) {
                num = num + 1;
            } else {
                for (InventoryDetail inventory : inventoryList) {
                    //判断是否组盘完毕,完毕就退出内循环
                    if (shipmentQty.compareTo(BigDecimal.ZERO) <= 0) {
                        break;
                    }

                    BigDecimal inventoryQty = inventory.getQty().subtract(inventory.getTaskQty());
                    ShipmentCombinationModel shipmentCombination = new ShipmentCombinationModel();
                    shipmentCombination.setInventoryDetailId(inventory.getId());
                    shipmentCombination.setShipmentDetailId(item.getId());
                    shipmentCombination.setType((short) QuantityConstant.TASK_TYPE_WHOLESHIPMENT);

                    //如果库存数 >= 待出库数,组盘数就是待出库数并且结束组盘,否则组盘数就是库存数
                    if (inventoryQty.compareTo(shipmentQty) > -1) {
                        shipmentCombination.setShipQty(shipmentQty);
                    } else {
                        shipmentCombination.setShipQty(inventoryQty);
                    }
                    ShipmentContainerHeader shipmentContainerHeader=this.combination(shipmentCombination);
                    list.add(shipmentContainerHeader.getId());
                    shipmentQty = shipmentQty.subtract(shipmentCombination.getShipQty());
                }
            }
        }

        if(num == shipmentDetailList.size()){
            //throw new ServiceException("单据物料的库存分配规则在此库区没有库存,无法出库");
            throw new ServiceException("此库区没有库存了,无法出库");
        }
        return AjaxResult.success("成功",list);
    }

    /**
     * 检查是否可以进行取消配盘
     * @param shipmentContainerIds
     * @return
     */
    @Override
    public AjaxResult checkForCancelCombination(List<Integer> shipmentContainerIds) {
        for(int id : shipmentContainerIds){
            ShipmentContainerHeader shipmentContainerHeader = this.getById(id);
            if(shipmentContainerHeader.getStatus()>=QuantityConstant.SHIPMENT_CONTAINER_TASK){
                return AjaxResult.error("存在已生成任务的记录,请先取消任务再取消组盘");
            }
        }
        return AjaxResult.success("");
    }

    /* 生成任务 */
    @Override
    @Transactional
    public AjaxResult createTask(List<Integer> idList) {
        ShipmentTaskCreateModel shipmentTask = new ShipmentTaskCreateModel();
        for (Integer id : idList)   {
            shipmentTask.setShipmentContainerHeaderIds(id);
            AjaxResult ajaxResult = shipmentTaskService.createTaskFromShipmentContainers(shipmentTask);
            if(ajaxResult.hasErr()){
                return ajaxResult;
            }
        }
        return AjaxResult.success("全部生成完毕");
    }

    /**
     * 取消出库任务
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public boolean cancelShipment(Integer combineHeaderId) {
        // 回滚组盘主表状态
        ShipmentContainerHeader shipmentContainerHeader = new ShipmentContainerHeader();
        shipmentContainerHeader.setId(combineHeaderId);
        shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
        if (!updateById(shipmentContainerHeader)){
            throw new ServiceException("更新出库组盘头表失败");
        }
        // 回滚组盘明细状态
        LambdaQueryWrapper<ShipmentContainerDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ShipmentContainerDetail::getShippingContainerId,combineHeaderId);
        List<ShipmentContainerDetail> shipmentContainerDetailList = shipmentContainerDetailService.list(queryWrapper);
        for (ShipmentContainerDetail shipmentContainerDetail : shipmentContainerDetailList) {
            shipmentContainerDetail.setStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
        }
        if (!shipmentContainerDetailService.updateBatchById(shipmentContainerDetailList)){
            throw new ServiceException("更新出库组盘明细失败");
        }
        return true;
    }

    @Override
    public AjaxResult getShipmentInfoByCode(String code) {
        LambdaQueryWrapper<ShipmentContainerDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentContainerDetail::getShipmentCode, code)
                .eq(ShipmentContainerDetail::getWarehouseCode, ShiroUtils.getWarehouseCode());

        List<ShipmentContainerDetail> shipmentContainerDetails = shipmentContainerDetailService.list(lambdaQueryWrapper);
        return AjaxResult.success(shipmentContainerDetails);
    }

    @Override
    public int getUnCompleteCombineNumber(String containerCode) {
        LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentContainerHeaderLambdaQueryWrapper.eq(ShipmentContainerHeader::getContainerCode, containerCode)
                .lt(ShipmentContainerHeader::getStatus, QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
        int number = 0;
        List<ShipmentContainerHeader> shipmentContainerHeaderList = list(shipmentContainerHeaderLambdaQueryWrapper);
        if(shipmentContainerHeaderList != null && shipmentContainerHeaderList.size() > 0) {
            number = shipmentContainerHeaderList.size();
        }
        return number;
    }

    @Override
    public List<ShipmentContainerHeader> getUnCompleteCombineList() {
        LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentContainerHeaderLambdaQueryWrapper.lt(ShipmentContainerHeader::getStatus,
                QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
        List<ShipmentContainerHeader> shipmentContainerHeaderList =
                list(shipmentContainerHeaderLambdaQueryWrapper);
        return  shipmentContainerHeaderList;
    }

}