package com.prolog.cs.base.gsp.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.prolog.cs.base.gsp.QueryDTO;
import com.prolog.cs.base.gsp.dao.DxiGspQualityReviewDMapper;
import com.prolog.cs.base.gsp.dao.DxiGspQualityReviewHMapper;
import com.prolog.cs.base.gsp.enums.GspQualityReviewAuditStatus;
import com.prolog.cs.base.gsp.enums.GspQualityReviewBillType;
import com.prolog.cs.base.gsp.model.DxiGspQualityReviewD;
import com.prolog.cs.base.gsp.model.DxiGspQualityReviewH;
import com.prolog.cs.base.gsp.model.GspUpkeepCheck;
import com.prolog.cs.base.gsp.resource.InventoryClient;
import com.prolog.cs.base.gsp.resource.ItemClient;
import com.prolog.cs.base.gsp.resource.WarehouseClient;
import com.prolog.cs.base.gsp.service.GspQualityReviewService;
import com.prolog.cs.base.gsp.vo.GspQualityCheckMoveItemVo;
import com.prolog.cs.base.gsp.vo.GspQualityCheckMoveVo;
import com.prolog.cs.basic.item.model.BaseMtlItemHealth;
import com.prolog.cs.basic.item.model.BaseMtlItemMst;
import com.prolog.cs.basic.warehouse.model.BaseWmsWarehouseMst;
import com.prolog.cs.data.exchange.client.ExchangeSender;
import com.prolog.cs.data.exchange.client.core.EtlMessage;
import com.prolog.cs.data.exchange.client.core.EtlTypeConstants;
import com.prolog.framework.authority.core.dto.UserDTO;
import com.prolog.framework.authority.user.UserTemplate;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.*;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.framework.toolkit.MapToolKit;
import com.prolog.framework.utils.*;
import com.prolog.upcloud.base.inventory.enums.StockStatusEnum;
import com.prolog.upcloud.base.inventory.model.WmsInvStockLot;
import com.prolog.upcloud.base.strategy.dto.stock.StockLocatorDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class GspQualityReviewServiceImpl implements GspQualityReviewService {

    @Autowired
    private UserTemplate userTemplate;

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private InventoryClient inventoryClient;

    @Autowired
    private WarehouseClient warehouseClient;

    @Autowired
    private DxiGspQualityReviewHMapper dxiGspQualityReviewHMapper;

    @Autowired
    private DxiGspQualityReviewDMapper dxiGspQualityReviewDMapper;

    @Autowired
    private ExchangeSender exchangeSender;


    @Override
    public Page<DxiGspQualityReviewH> listPage(String branchcode, String consignorcode, String requestBilltype, String billNo,String status, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<DxiGspQualityReviewH> list = dxiGspQualityReviewHMapper.findNoEnd(branchcode,consignorcode,requestBilltype,billNo,status);
        return PageUtils.getPage(list);
    }

    @Override
    public List<DxiGspQualityReviewD> findDetailByBillId(String billId) {
        //String[] pendStatus = {GspQualityReviewAuditStatus.START.getStatus(),GspQualityReviewAuditStatus.REVIEW.getStatus()};
        Criteria crtH = Criteria.forClass(DxiGspQualityReviewH.class);
        Restriction rH =  Restrictions.eq("billid", billId);
        crtH.setRestriction(rH);
        List<DxiGspQualityReviewH> list = dxiGspQualityReviewHMapper.findByCriteria(crtH);
        AssertUtils.isNotEmpty(list,"查询单据不存在");
        DxiGspQualityReviewH dxiGspQualityReviewH = list.get(0);

        String searchStatus = GspQualityReviewAuditStatus.START.getStatus();
        if(GspQualityReviewAuditStatus.REVIEW.getStatus().equals(dxiGspQualityReviewH.getStatus())){
            searchStatus = GspQualityReviewAuditStatus.REVIEW.getStatus();
        }
        Criteria crt = Criteria.forClass(DxiGspQualityReviewD.class);
        Restriction r1 =  Restrictions.eq("billid", billId);
        Restriction r2 =  Restrictions.in("endStatus",searchStatus);
        crt.setOrder(Order.newInstance().asc("lastmodifydate"));
        crt.setRestriction(Restrictions.and(r1,r2));
        List<DxiGspQualityReviewD> byCriteria = dxiGspQualityReviewDMapper.findByCriteria(crt);




        String consignorid = dxiGspQualityReviewH.getConsignorid();

        Set<String> warhouseCode = Sets.newHashSet();
        warhouseCode.add(dxiGspQualityReviewH.getBranchcode());
        String warhouseId = null;
        RestMessage<Map<String, BaseWmsWarehouseMst>> warehouseCodeMapRes = warehouseClient.getByWarehouseCode(warhouseCode);
        if (!warehouseCodeMapRes.isSuccess() || CollectionUtil.isEmpty(warehouseCodeMapRes.getData())) {
            throw new BizException("查询仓库信息失败");
        }
        Map<String, BaseWmsWarehouseMst> whData = warehouseCodeMapRes.getData();
        for (String id : whData.keySet()) {
            if(warhouseCode.iterator().next().equals(whData.get(id).getWarehouseCode())){
                warhouseId = id;
            }
        }


        Set<String> itemCodes = byCriteria.stream().map(DxiGspQualityReviewD::getItemcode).collect(Collectors.toSet());
        //请求商品信息
        RestMessage<List<BaseMtlItemMst>> itemResult = itemClient.getByItemCodes(consignorid,itemCodes);
        if (!itemResult.isSuccess() || CollectionUtil.isEmpty(itemResult.getData())) {
            throw new BizException("获取商品信息失败");
        }


        for (DxiGspQualityReviewD byCriterion : byCriteria) {
            byCriterion.setRefBilltype("1".equals(dxiGspQualityReviewH.getRefBilltype())?"ERP对接":"2".equals(dxiGspQualityReviewH.getRefBilltype())?"SAP对接":"OMS录入");
            byCriterion.setRequestBilltype(GspQualityReviewBillType.getByCode(dxiGspQualityReviewH.getRequestBilltype()));
            byCriterion.setOperator(dxiGspQualityReviewH.getOperator());
            byCriterion.setBillDate(dxiGspQualityReviewH.getBillDate());
            byCriterion.setReviewReason(dxiGspQualityReviewH.getReviewReason());

            //查询对应的库存信息
            BaseMtlItemMst baseMtlItemMst = itemResult.getData().stream().filter(item ->byCriterion.getItemcode().equals(item.getItemCode())).findFirst().orElse(null);
            if (baseMtlItemMst == null) {
                throw new BizException("获取商品信息失败");
            }
            BaseMtlItemHealth baseMtlItemHealth = baseMtlItemMst.getBaseMtlItemHealth();
            byCriterion.setItemHealthCategory(baseMtlItemHealth==null?"":baseMtlItemHealth.getItemHealthCategory());
            byCriterion.setItemHealthListedLicensor(baseMtlItemHealth==null?"":baseMtlItemHealth.getItemHealthListedLicensor());
            byCriterion.setItemHealthType(baseMtlItemHealth==null?"":baseMtlItemHealth.getItemHealthType());

            RestMessage<List<StockLocatorDTO>> itemLocators = inventoryClient.getItemLocators(warhouseId, consignorid, baseMtlItemMst.getItemId(), null, null);
            //RestMessage<List<StockLocatorDTO>> itemLocators = inventoryClient.getItemLocators("599581702716788736", "E23A4BUUPPW", "0021256304", null, null);
            if (itemLocators.isSuccess() && !CollectionUtil.isEmpty(itemLocators.getData())) {
                StockLocatorDTO stockLocatorDTO = itemLocators.getData().stream().filter(itemLocator -> itemLocator.getBatchNum().equals(byCriterion.getBatchNo())).findFirst().orElse(null);
                if(stockLocatorDTO != null){
                    BigDecimal availableQuantity = stockLocatorDTO.getAvailableQuantity();
                    BigDecimal[] bigDecimals = availableQuantity.divideAndRemainder(new BigDecimal(byCriterion.getPackSize()));
                    byCriterion.setCaseQuantity(bigDecimals[0]);
                    byCriterion.setOddPieceQuantity(bigDecimals[1]);
                    byCriterion.setStockQty(availableQuantity);
                }
            }
            //byCriterion.setItemid(baseMtlItemMst.getItemId());

            dxiGspQualityReviewDMapper.update(byCriterion);
        }

        Criteria crtUpdate = Criteria.forClass(DxiGspQualityReviewH.class);
        Restriction updateEndStatus = Restrictions.eq("billid", list.get(0).getBillid());
        crtUpdate.setRestriction(updateEndStatus);
        dxiGspQualityReviewHMapper.updateMapByCriteria(MapUtils.put("warehouseId",warhouseId)
                .put("lastmodifydate", new Date()).getMap(), crtUpdate);

        return byCriteria;
    }

    @Override
    public void batchAudit(List<DxiGspQualityReviewD> list) {

        if(CollectionUtils.isEmpty(list)){
            throw new BizException("操作失败");
        }

        UserDTO userDTO = userTemplate.getUserDTO();
        if(userDTO == null){
            throw new BizException("用户登录信息失效");
        }
        String username = userDTO.getUsername();
        String qualityReviewHStaus = GspQualityReviewAuditStatus.WMS_AUDIT_YES.getStatus();


        for (DxiGspQualityReviewD dxiGspQualityReviewD : list) {

            if(!StringUtils.isBlank(dxiGspQualityReviewD.getAuditStausManager())){
                dxiGspQualityReviewD.setAuditManager(username);
                //质量经理审核
                AssertUtils.isTrue(GspQualityReviewAuditStatus.REVIEW.getStatus().equals(dxiGspQualityReviewD.getAuditStausDirector()),"审核结论异常,质量主管意见不为待处理");
                if(GspQualityReviewAuditStatus.WMS_AUDIT_NO.getStatus().equals(dxiGspQualityReviewD.getAuditStausManager()) && StringUtils.isBlank(dxiGspQualityReviewD.getReviewQty())){
                    throw new BizException("不合格时必须填入数量");
                }
                dxiGspQualityReviewD.setAuditDateManager(new Date());

                if(GspQualityReviewAuditStatus.WMS_AUDIT_NO.getStatus().equals(dxiGspQualityReviewD.getAuditStausManager())){
                    dxiGspQualityReviewD.setEndStatus(GspQualityReviewAuditStatus.WMS_AUDIT_NO.getStatus());
                    //不合格移库后回传结果到OMS
                }

                if(GspQualityReviewAuditStatus.WMS_AUDIT_YES.getStatus().equals(dxiGspQualityReviewD.getAuditStausManager())){
                    dxiGspQualityReviewD.setEndStatus(GspQualityReviewAuditStatus.WMS_AUDIT_YES.getStatus());
                    //合格直接回传结果到OMS
                }

            }else if(!StringUtils.isBlank(dxiGspQualityReviewD.getAuditStausDirector())){
                //质量主管审核
                dxiGspQualityReviewD.setAuditDirector(username);
                AssertUtils.isNotTrue(dxiGspQualityReviewD.getAuditStausDirector() == GspQualityReviewAuditStatus.START.getStatus(),"审核状态异常");

                AssertUtils.isNotTrue( (GspQualityReviewAuditStatus.WMS_AUDIT_NO.getStatus().equals(dxiGspQualityReviewD.getAuditStausDirector())
                                || GspQualityReviewAuditStatus.REVIEW.getStatus().equals(dxiGspQualityReviewD.getAuditStausDirector()))
                                && StringUtils.isBlank(dxiGspQualityReviewD.getReviewQty())
                        ,"待处理或不合格时必须填入数量");

                dxiGspQualityReviewD.setAuditDateDirector(new Date());

                if(GspQualityReviewAuditStatus.REVIEW.getStatus().equals(dxiGspQualityReviewD.getAuditStausDirector())){
                    //只有待审核时更新未40  其他都跟新未已审核20
                    qualityReviewHStaus = GspQualityReviewAuditStatus.REVIEW.getStatus();
                    dxiGspQualityReviewD.setEndStatus(GspQualityReviewAuditStatus.REVIEW.getStatus());
                }

                if(GspQualityReviewAuditStatus.WMS_AUDIT_NO.getStatus().equals(dxiGspQualityReviewD.getAuditStausDirector())){
                    dxiGspQualityReviewD.setEndStatus(GspQualityReviewAuditStatus.WMS_AUDIT_NO.getStatus());
                    //不合格移库后回传结果到OMS
                }

                if(GspQualityReviewAuditStatus.WMS_AUDIT_YES.getStatus().equals(dxiGspQualityReviewD.getAuditStausDirector())){
                    dxiGspQualityReviewD.setEndStatus(GspQualityReviewAuditStatus.WMS_AUDIT_YES.getStatus());
                    //合格直接回传结果到OMS
                }
            }else{
                throw new BizException("审核状态为空");
            }
            //更新审核结果明细
            dxiGspQualityReviewDMapper.update(dxiGspQualityReviewD);
        }

        Criteria crtUpdate = Criteria.forClass(DxiGspQualityReviewH.class);
        Restriction updateEndStatus = Restrictions.eq("billid", list.get(0).getBillid());
        crtUpdate.setRestriction(updateEndStatus);
        dxiGspQualityReviewHMapper.updateMapByCriteria(MapUtils.put("status",qualityReviewHStaus)
                .put("lastmodifydate", new Date()).put("lastmodifyby",userDTO.getUsername()).getMap(), crtUpdate);


        //获取质量审核合格的发送OMS
        List<DxiGspQualityReviewD> collect = list.stream().filter(refiewD -> GspQualityReviewAuditStatus.WMS_AUDIT_YES.getStatus().equals(refiewD.getEndStatus())).collect(Collectors.toList());

        if(CollectionUtil.isNotEmpty(collect)){
            for (DxiGspQualityReviewD dxiGspQualityReviewD : collect) {
                toOMSMoved2(collect.get(0).getBillid(),dxiGspQualityReviewD.getLineid(),GspQualityReviewAuditStatus.WMS_AUDIT_YES.getStatus());
            }
        }
    }

    @Override
    public List<GspQualityCheckMoveVo> findQualityReview4MovePick(String warehouseId,String cargoOwnerId,String requestBilltype, String billno) {
        List<GspQualityCheckMoveVo> resultList = dxiGspQualityReviewDMapper.findQualityReview4MovePick(warehouseId,cargoOwnerId,requestBilltype,billno);
        return resultList;
    }



    @Override
    public List<GspQualityCheckMoveItemVo> findQualityReview4MoveItemPick(String billId) {

        List<GspQualityCheckMoveItemVo> itemVos = dxiGspQualityReviewDMapper.findQualityReview4MoveItemPick(billId);
        if(CollectionUtils.isEmpty(itemVos)){
            return null;
        }

        Set<String> itemCodes = itemVos.stream().map(GspQualityCheckMoveItemVo::getItemCode).collect(Collectors.toSet());
        //请求商品信息
        RestMessage<List<BaseMtlItemMst>> itemResult = itemClient.getByItemCodes(itemVos.get(0).getCargoOwnerId(),itemCodes);
        if (!itemResult.isSuccess() || CollectionUtil.isEmpty(itemResult.getData())) {
            throw new BizException("获取商品信息失败");
        }
        for (GspQualityCheckMoveItemVo qcVo : itemVos) {
            qcVo.setShowProductionDate(StringUtil.isNotEmpty(qcVo.getProductionDate())?qcVo.getProductionDate().replaceAll("-",""):"");
            qcVo.setShowExpiredDate(StringUtil.isNotEmpty(qcVo.getExpiredDate())?qcVo.getExpiredDate().replaceAll("-",""):"");

            qcVo.setRefBilltype("1".equals(qcVo.getRefBilltype())?"ERP对接":"2".equals(qcVo.getRefBilltype())?"SAP对接":"OMS录入");

            BaseMtlItemMst baseMtlItemMst = itemResult.getData().stream().filter(item -> qcVo.getItemCode().equals(item.getItemCode())).findFirst().orElse(null);
            if (baseMtlItemMst == null) {
                throw new BizException("获取商品信息失败");
            }
            qcVo.setItemId(baseMtlItemMst.getItemId());

            //查询对应的库存信息
            RestMessage<List<StockLocatorDTO>> itemLocators = inventoryClient.getItemLocators(qcVo.getWarehouseId(), qcVo.getCargoOwnerId(), qcVo.getItemId(), null, null);
            //RestMessage<List<StockLocatorDTO>> itemLocators = inventoryClient.getItemLocators("599581702716788736", "E23A4BUUPPW", "0021256304", null, null);
            if (itemLocators.isSuccess() &&  CollectionUtil.isNotEmpty(itemLocators.getData())) {
                StockLocatorDTO stockLocatorDTO = itemLocators.getData().stream().filter(itemLocator -> itemLocator.getBatchNum().equals(qcVo.getBatchNum())).findFirst().orElse(null);
                if(stockLocatorDTO != null){
                    BigDecimal availableQuantity = stockLocatorDTO.getAvailableQuantity();
                    //BigDecimal[] bigDecimals = availableQuantity.divideAndRemainder(qcVo.getPackSize());
                    //获取不合格移库数量
                    if(StringUtils.isEmpty(qcVo.getExpiredDate()) || StringUtils.isEmpty(qcVo.getProductionDate())){
                        qcVo.setProductionDate(DateUtils.format(stockLocatorDTO.getBatchProductionDate(),"yyyy-MM-dd hh:mm:ss"));
                        qcVo.setExpiredDate(DateUtils.format(stockLocatorDTO.getBatchExpiredDate(),"yyyy-MM-dd hh:mm:ss"));
                        qcVo.setShowProductionDate(stockLocatorDTO.getBatchProductionDateShow());
                        qcVo.setShowExpiredDate(stockLocatorDTO.getBatchExpiredDateShow());

                    }
                    BigDecimal[] bigDecimals = qcVo.getReviewQty().divideAndRemainder(qcVo.getPackSize());
                    qcVo.setCaseQuantity(bigDecimals[0]);
                    qcVo.setOddPieceQuantity(bigDecimals[1]);
                    qcVo.setBatchId(stockLocatorDTO.getBatchId());
                    qcVo.setStockQty(availableQuantity);
                    qcVo.setQuantity(qcVo.getReviewQty());
                }else{
                    throw new BizException("未获取到商品库存信息");
                }
            }

            qcVo.setInStockStatus("2");
            qcVo.setOutStockStatus("1");
        }
        return itemVos;
    }

    @Override
    public void confirmQualityReviewMove(String[] arrList) {


        Criteria crt = Criteria.forClass(DxiGspQualityReviewD.class);
        Restriction restriction = Restrictions.in("lineid", arrList);
        crt.setRestriction(restriction);
        List<DxiGspQualityReviewD> byCriterias = dxiGspQualityReviewDMapper.findByCriteria(crt);
        for (DxiGspQualityReviewD byCriteria : byCriterias) {
            byCriteria.setReviewMovedQty(byCriteria.getReviewQty());
            byCriteria.setLastmodifydate(new Date());
            dxiGspQualityReviewDMapper.update(byCriteria);
            toOMSMoved2(byCriteria.getBillid(),byCriteria.getLineid(),GspQualityReviewAuditStatus.WMS_AUDIT_NO.getStatus());
        }
    }


    private void toOMSMoved2(String billid, String lineid , String status) {
        if (!StringUtils.isBlank(lineid)) {
            EtlMessage em = new EtlMessage();
            em.setId(billid);
            em.setEtlType(EtlTypeConstants.gsp_quality_review_w2o);
            HashMap<String, String> omsMap = Maps.newHashMap();
            omsMap.put("status",status);
            omsMap.put("lineids",lineid);
            omsMap.put("dtId",String.valueOf(SnowFlakeUtils.getId()));

            em.setEtlData(omsMap);
            try {
                exchangeSender.send(em);
            } catch (Exception e) {
                //修改回传状态
                Criteria crt = Criteria.forClass(DxiGspQualityReviewD.class);
                crt.setRestriction(Restrictions.eq("lineid", lineid));
                dxiGspQualityReviewDMapper.updateMapByCriteria(MapUtils.put("infStatus", "E").put("lastmodifydate", new Date()).getMap(), crt);
                throw new BizException("同步OMS异常，请联系系统管理员");

            }
            //修改回传状态
            Criteria crt = Criteria.forClass(DxiGspQualityReviewD.class);
            crt.setRestriction(Restrictions.eq("lineid", lineid));
            dxiGspQualityReviewDMapper.updateMapByCriteria(MapUtils.put("infStatus", "Y").put("lastmodifydate", new Date()).getMap(), crt);

        }
    }


    private void toOMSMoved(String billid, String[] lineids , String status) {
        if (lineids != null) {
            EtlMessage em = new EtlMessage();
            /*em.setId(billid);*/
            em.setId("1");
            em.setEtlType(EtlTypeConstants.gsp_quality_review_w2o);
            HashMap<String, String> omsMap = Maps.newHashMap();
           /* omsMap.put("status",status);
            omsMap.put("lineids",String.join(",", lineids));*/
            omsMap.put("status","20");
            omsMap.put("lineids","1");
            em.setEtlData(omsMap);
            try {
                exchangeSender.send(em);
            } catch (Exception e) {
                throw new BizException("同步OMS异常，请联系系统管理员");
            }
        }
    }





    //备份
    public List<GspQualityCheckMoveItemVo> findQualityReview4MoveItemPickOri(String billId) {

        List<GspQualityCheckMoveItemVo> itemVos = dxiGspQualityReviewDMapper.findQualityReview4MoveItemPick(billId);
        if(itemVos==null){
            return null;
        }
        //warhousehid
        Set<String> codeIds = itemVos.stream().map(GspQualityCheckMoveItemVo::getWarehouseCode).collect(Collectors.toSet());
        RestMessage<Map<String, BaseWmsWarehouseMst>> warehouseCodeMapRes = warehouseClient.getByWarehouseCode(codeIds);
        if (!warehouseCodeMapRes.isSuccess() || CollectionUtil.isEmpty(warehouseCodeMapRes.getData())) {
            throw new BizException("查询仓库信息失败");
        }


        Set<String> itemCodes = itemVos.stream().map(GspQualityCheckMoveItemVo::getItemCode).collect(Collectors.toSet());
        //请求商品信息
        RestMessage<List<BaseMtlItemMst>> itemResult = itemClient.getByItemCodes(itemVos.get(0).getCargoOwnerId(),itemCodes);
        if (!itemResult.isSuccess() || CollectionUtil.isEmpty(itemResult.getData())) {
            throw new BizException("获取商品信息失败");
        }
        for (GspQualityCheckMoveItemVo qcVo : itemVos) {
            Map<String, BaseWmsWarehouseMst> whData = warehouseCodeMapRes.getData();
            for (String id : whData.keySet()) {
                if(qcVo.getWarehouseCode().equals(whData.get(id).getWarehouseCode())){
                    qcVo.setWarehouseId(id);
                }
            }

            BaseMtlItemMst baseMtlItemMst = itemResult.getData().stream().filter(item -> qcVo.getItemCode().equals(item.getItemCode())).findFirst().orElse(null);
            if (baseMtlItemMst == null) {
                throw new BizException("获取商品信息失败");
            }
            qcVo.setItemId(baseMtlItemMst.getItemId());
            //查询对应的库存信息
            RestMessage<List<StockLocatorDTO>> itemLocators = inventoryClient.getItemLocators(qcVo.getWarehouseId(), qcVo.getCargoOwnerId(), qcVo.getItemId(), null, null);
            //RestMessage<List<StockLocatorDTO>> itemLocators = inventoryClient.getItemLocators("599581702716788736", "E23A4BUUPPW", "0021256304", null, null);
            if (!itemLocators.isSuccess() || CollectionUtil.isEmpty(itemLocators.getData())) {
                throw new BizException("获取商品库存信息失败");
            }

            StockLocatorDTO stockLocatorDTO = itemLocators.getData().stream().filter(itemLocator -> itemLocator.getBatchNum().equals(qcVo.getBatchNum())).findFirst().orElse(null);
            if(stockLocatorDTO != null){
                BigDecimal availableQuantity = stockLocatorDTO.getAvailableQuantity();
                //BigDecimal[] bigDecimals = availableQuantity.divideAndRemainder(qcVo.getPackSize());
                //获取不合格移库数量
                BigDecimal[] bigDecimals = qcVo.getReviewQty().divideAndRemainder(qcVo.getPackSize());
                qcVo.setCaseQuantity(bigDecimals[0]);
                qcVo.setOddPieceQuantity(bigDecimals[1]);
                qcVo.setBatchId(stockLocatorDTO.getBatchId());
                qcVo.setStockQty(availableQuantity);
            }

            qcVo.setInStockStatus("2");
            qcVo.setOutStockStatus("1");
        }
        return itemVos;
    }


}
