package com.dk.mdm.service.ivt.inbound;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dk.common.exception.BaseBusinessException;
import com.dk.common.infrastructure.annotaiton.Pagination;
import com.dk.common.infrastructure.constant.Constant;
import com.dk.common.infrastructure.enums.ErrorCodeEnum;
import com.dk.common.mapper.BaseMapper;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseCodeEnum;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.common.service.BaseService;
import com.dk.mdm.infrastructure.convert.ivt.InboundConvert;
import com.dk.mdm.infrastructure.convert.ivt.InboundItemConvert;
import com.dk.mdm.infrastructure.util.AuthUtils;
import com.dk.mdm.mapper.common.CommonMapper;
import com.dk.mdm.mapper.ivt.InboundItemMapper;
import com.dk.mdm.mapper.ivt.InboundMapper;
import com.dk.mdm.mapper.ivt.InventoryMapper;
import com.dk.mdm.mapper.pur.PurchaseItemMapper;
import com.dk.mdm.mapper.pur.PurchaseMapper;
import com.dk.mdm.model.pojo.ivt.Inbound;
import com.dk.mdm.model.pojo.ivt.InboundItem;
import com.dk.mdm.model.pojo.pur.Purchase;
import com.dk.mdm.model.pojo.pur.PurchaseItem;
import com.dk.mdm.model.query.ivt.InboundItemQuery;
import com.dk.mdm.model.query.ivt.InboundQuery;
import com.dk.mdm.model.query.ivt.InventoryQuery;
import com.dk.mdm.model.response.ivt.InboundItemResponse;
import com.dk.mdm.model.response.ivt.InboundResponse;
import com.dk.mdm.model.response.ivt.InventoryResponse;
import com.dk.mdm.model.response.pur.PurchaseItemResponse;
import com.dk.mdm.model.response.pur.PurchaseResponse;
import com.dk.mdm.model.vo.ivt.InboundItemVO;
import com.dk.mdm.model.vo.ivt.InboundVO;
import com.dk.mdm.service.common.CommonService;
import com.dk.mdm.service.ivt.inventory.InventoryService;
import com.dk.mdm.service.mac.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;


/**
 * @author : 寇珊珊
 * @desc : 采购入库业务层
 * @date : 2024/3/7 14:11
 */
@Service
public class InboundPurchaseService extends BaseService<Inbound> {

    @Override
    public BaseMapper<Inbound> getRepository() {
        return inboundMapper;
    }

    @Autowired
    private InboundMapper inboundMapper;

    @Autowired
    private InboundConvert inboundConvert;

    @Autowired
    private InboundItemMapper inboundItemMapper;

    @Autowired
    private InboundItemConvert inboundItemConvert;

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private PurchaseItemMapper purchaseItemMapper;

    @Autowired
    private CommonService commonService;

    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private AuthUtils authUtils;

    /**
     * @desc : 条件查询
     * @date : 2024/3/7 14:12
     * @author : 寇珊珊
     */
    @Pagination
    public ResponseResultVO<PageList<InboundResponse>> selectByCond(InboundQuery inboundQuery) {
        return super.mergeListWithCount(inboundQuery, inboundMapper.selectByCond(inboundQuery),
                inboundMapper.countByCond(inboundQuery));
    }

    /**
     * @desc : 查询明细
     * @date : 2024/3/9 15:43
     * @author : 寇珊珊
     */
    @Pagination
    public ResponseResultVO<Map<String, Object>> selectPurchaseInboundItemInfoById(String id) {
        Map<String, Object> result = new HashMap<>();
        // 商品明细
        List<InboundItemResponse> inboundItemResponses = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(id));
        result.put("itemList", inboundItemResponses);
        // 收款

        // 附件
        return ResponseResultUtil.success(result);
    }

    /**
     * @desc : 查询入库价
     * @date : 2024/7/1 11:29
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public InboundItemResponse selectPriceInto(InboundItemVO inboundItemVO) {
        List<InboundItemResponse> inboundItemResponses = inboundItemMapper.selectPriceInto(new InboundItemQuery().setSkuId(inboundItemVO.getSkuId())
                .setNonStdCode(inboundItemVO.getNonStdCode())
                .setWhId(inboundItemVO.getWhId()));
        if (inboundItemResponses != null && inboundItemResponses.size() > 0) {
            return inboundItemResponses.get(0);
        }
        return null;
    }

    /**
     * @desc : 采购入库新建
     * @date : 2024/3/7 14:13
     * 入库中数量/金额  已入库数量/金额  由调用方传入
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> purchaseInboundInsert(InboundVO inboundVO) {

        //region 校验明细
        if (inboundVO.getItemList().size() == 0) {
            throw new BaseBusinessException(ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getCode(),
                    ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage());
        }
        //endregion

        //region  查询当前入库明细中是否存在未空或者0的入库价，如果存在去库存流水差最近一条有价格的数据赋值到当前明细
        Boolean priceIntoFlag = false;
        for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
            if (inboundItemVO.getCostPrice() == null || inboundItemVO.getCostPrice().compareTo(BigDecimal.ZERO) == 0) {
                priceIntoFlag = true;
                //查询库存批次最近一条入库价
                InboundItemResponse inboundItemResponse = this.selectPriceInto(inboundItemVO);
                //todo 2024年7月5日15:57:01   priceOut和outingAmt 前台传 z确认修改
                //todo 2024年7月9日08:52:55  入库价和入库价总和后台计算  z确认修改
                inboundItemVO.setCostPrice(inboundItemResponse != null ? inboundItemResponse.getCostPrice() : BigDecimal.ZERO);
                inboundItemVO.setCostAmt(inboundItemResponse != null ? inboundItemResponse.getCostPrice().multiply(inboundItemVO.getIntoingQty()).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
                inboundItemVO.setPriceInto(inboundItemResponse != null ? inboundItemResponse.getCostPrice() : BigDecimal.ZERO);
                inboundItemVO.setIntoingAmt(inboundItemResponse != null ? inboundItemResponse.getPriceInto().multiply(inboundItemVO.getIntoingQty()).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
            }
        }
        if (priceIntoFlag) {
            BigDecimal intoingAmt = inboundVO.getItemList().stream().map(InboundItemVO::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
            inboundVO.setIntoingAmt(intoingAmt);
        }
        //endregion

        //region  查询当前公司的系统参数  自动办理信息  并赋值
        Map<String, Object> map = new HashMap<>();
        map.put("cpId", inboundVO.getCpId());
        map.put("code", Constant.SystemConstant.IVT_001.getValue());
        //自动办理标识 返回的值为true和false,
//        String flgHandleSetting = commonMapper.getSettingValue(map);
        //自动办理标识为1 自动办理入库
//        if (Constant.FlgAutoHandleStringType.ONE.getValue().equals(flgHandleSetting)) {
//            inboundVO.setFlgAutoHandle(Constant.FlgAutoHandle.TRUE.getValue());
//        }
        //自动办理标识参数，返回的值为true和false
        Boolean flgAutoHandle = Boolean.parseBoolean(commonMapper.getSettingValue(map));
        inboundVO.setFlgAutoHandle(flgAutoHandle);
        //endregion

        //region  总单
        //获取 id/单号
        Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.PURCASEINBOUND.getName(), false);
        inboundVO.setIntoId(codeMap.get("outId").toString()).
                setIntoNo(codeMap.get("outNote").toString());
        //入库类型
        inboundVO.setIntoType(Constant.IntoType.SALE.getName());
        //自动入库标识
//        if (inboundVO.getFlgAutoHandle()) {
//            //已入库
//            inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName());
//            //入库日期
//            inboundVO.setIntoDate(inboundVO.getIntoDate() == null ? LocalDate.now() : inboundVO.getIntoDate());
//        } else {
//            //入库中
//            inboundVO.setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName());
//        }
        if (inboundVO.getFlgAutoHandle()) {
            if (inboundVO.getInboundProcessingFlag()) {
                //已入库
                inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName());
                //入库日期
                inboundVO.setIntoDate(inboundVO.getIntoDate() == null ? LocalDate.now() : inboundVO.getIntoDate());
            } else {
                //入库中
                inboundVO.setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName());
            }
        } else {
            //已入库
            inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName());
            //入库日期
            inboundVO.setIntoDate(inboundVO.getIntoDate() == null ? LocalDate.now() : inboundVO.getIntoDate());
        }
        //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额
        if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) {
            inboundVO.setIntoQty(inboundVO.getIntoingQty())
                    .setIntoAmt(inboundVO.getIntoingAmt())
                    .setIntoingQty(BigDecimal.ZERO)
                    .setIntoingAmt(BigDecimal.ZERO)
            ;
        } else {
            inboundVO.setIntoQty(BigDecimal.ZERO)
                    .setIntoAmt(BigDecimal.ZERO)
            ;
        }
        //实体转换
        Inbound inbound = inboundConvert.convertToPo(inboundVO);
        inboundMapper.insert(inbound);
        //endregion

        if (inboundVO.getFromId() != null) {
            //region 采购总单
            this.insertOrderMessageByEdit(inboundVO, inboundVO.getItemList());
        }

        //endregion

        //region 明细
        for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {

            //region 将库存需要的参数赋值
            inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
            inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
            inboundItemVO.setAddOrEditFlag(true);
            //endregion

            //总单id
            inboundItemVO.setIntoId(inboundVO.getIntoId());
            //入库类型
            inboundItemVO.setIntoType(inboundVO.getIntoType());
            //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额
            if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) {
                inboundItemVO
                        .setIntoQty(inboundItemVO.getIntoingQty())
                        .setIntoAmt(inboundItemVO.getIntoingAmt())
                        .setIntoingQty(BigDecimal.ZERO)
                        .setIntoingAmt(BigDecimal.ZERO)
                ;
            } else {
                inboundItemVO
                        .setIntoQty(BigDecimal.ZERO)
                        .setIntoAmt(BigDecimal.ZERO);
            }
            //入库状态
            inboundItemVO.setIntoStatus(inboundVO.getIntoStatus());
            //实体转换
            InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
            inboundItemMapper.insert(inboundItem);
            inboundItemVO.setItemId(inboundItem.getItemId());
            //endregion

            //region 采购明细
            if (inboundItemVO.getFromItemId() != null) {
                //赋值（这里重写了更新方法，数量在更新方法中有数据库院士数量+本次数量）
                PurchaseItem purchaseItem = new PurchaseItem();
                purchaseItem.setItemId(inboundItemVO.getFromItemId());
                //根据id查询
                PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getFromItemId());
                //已入库
                if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) {
                    purchaseItem.setIntoingQty(BigDecimal.ZERO);
                    purchaseItem.setIntoingAmt(BigDecimal.ZERO);
                    purchaseItem.setIntoQty(inboundItemVO.getIntoQty());
                    purchaseItem.setIntoAmt(inboundItemVO.getIntoAmt());
                    //region  免费版本有这个功能  标识为true 可修改明细入库价和采购金额
                    String gradeCode = authUtils.getStaff().getGradeCode();
                    if (Constant.GradeCode.STD.getName().equals(gradeCode)) {
                        if (inboundVO.getUpdatePurchaseOrderFlag() != null && inboundVO.getUpdatePurchaseOrderFlag()) {
                            purchaseItem.setPricePur(inboundItemVO.getPriceInto());
                            purchaseItem.setItemAmt(inboundItemVO.getIntoAmt());
                        }
                    }
                    //endregion
                }
                //入库中
                else {
                    purchaseItem.setIntoingQty(inboundItemVO.getIntoingQty());
                    purchaseItem.setIntoingAmt(inboundItemVO.getIntoingAmt());
                    purchaseItem.setIntoQty(BigDecimal.ZERO);
                    purchaseItem.setIntoAmt(BigDecimal.ZERO);
                    //region  免费版本有这个功能 标识为true 可修改明细入库价和采购金额
                    String gradeCode = authUtils.getStaff().getGradeCode();
                    if (Constant.GradeCode.STD.getName().equals(gradeCode)) {
                        if (inboundVO.getUpdatePurchaseOrderFlag() != null && inboundVO.getUpdatePurchaseOrderFlag()) {
                            purchaseItem.setPricePur(inboundItemVO.getPriceInto());
                            purchaseItem.setItemAmt(inboundItemVO.getIntoingAmt());
                        }
                    }
                    //endregion
                }

                //入库状态
                String intoStatus = this.setIntoStatus(purchaseItemResponse.getIntoingQty().add(purchaseItem.getIntoingQty()),
                        purchaseItemResponse.getIntoQty().add(purchaseItem.getIntoQty()), purchaseItemResponse.getItemQty());
                purchaseItem.setIntoStatus(intoStatus);
                int countRow = purchaseItemMapper.updateById(purchaseItem);
                //数量超出
                if (countRow == 0) {
                    throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                }
            }
            //endregion
        }
        //endregion

        //region 入账
        if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) {
            accountService.accPayable(inboundVO.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
        }
        //endregion

        //region 库存
        if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) {
            Map<String, Object> invMap = new HashMap<>();
            invMap.put("intoDetail", inboundVO.getItemList());
            inventoryService.operatingInventoryInformation(invMap);
        }
        //endregion

        return ResponseResultUtil.success(inboundVO);
    }

    /**
     * @desc : 新建采购入库修改采购订单信息
     * @date : 2024/7/6 15:15
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public void insertOrderMessageByEdit(InboundVO inboundVO, List<InboundItemVO> itemList) {
        //入库明细汇总
        Map<String, List<InboundItemVO>> purchaseIntoItemVOListMap = itemList.stream().collect(Collectors.groupingBy(InboundItemVO::getFromId));
        //入库明细汇总循环
        for (String fromId : purchaseIntoItemVOListMap.keySet()) {

            //region  提取新建分组后的明细
            List<InboundItemVO> purchaseIntoItemVOListGroup = purchaseIntoItemVOListMap.get(fromId);
            //分组后的明细 入库数量和金额总和
            BigDecimal purchaseSumQty = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal purchaseSumAmt = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
            //endregion

            //region  反写订单数据
            //根据id查询
            PurchaseResponse purchaseResponse = purchaseMapper.selectById(fromId);
            Purchase purchase = new Purchase();
            purchase.setPurId(fromId);
            //已入库
            if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) {
                purchase.setIntoingQty(BigDecimal.ZERO);
                purchase.setIntoingAmt(BigDecimal.ZERO);
                purchase.setIntoQty(purchaseSumQty);
                purchase.setIntoAmt(purchaseSumAmt);
            }
            //出库中
            else {
                purchase.setIntoingQty(purchaseSumQty);
                purchase.setIntoingAmt(purchaseSumAmt);
                purchase.setIntoQty(BigDecimal.ZERO);
                purchase.setIntoAmt(BigDecimal.ZERO);
            }
            //免费版本有这个功能    标识为true 可修改总单合计金额
            String gradeCode = authUtils.getStaff().getGradeCode();
            if (Constant.GradeCode.STD.getName().equals(gradeCode)) {
                if (inboundVO.getUpdatePurchaseOrderFlag() != null && inboundVO.getUpdatePurchaseOrderFlag()) {
                    purchase.setSumAmount(purchaseSumAmt);
                }
            }

            //入库状态
            String intoStatus = this.setIntoStatus(purchaseResponse.getIntoingQty().add(purchase.getIntoingQty()),
                    purchaseResponse.getIntoQty().add(purchase.getIntoQty()), purchaseResponse.getSumQuantity());
            purchase.setIntoStatus(intoStatus);
            //修改
            int countRow = purchaseMapper.updateById(purchase);
            //数量超出
            if (countRow == 0) {
                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
            }
            //endregion
        }
    }

    /**
     * @desc : 采购入库编辑
     * @date : 2024/3/25 16:25
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> purchaseInboundUpdate(InboundVO inboundVO) {
        String gradeCode = authUtils.getStaff().getGradeCode();

        //region 小编辑
        if (!inboundVO.getLimitEdit()) {
            Inbound inbound = new Inbound();
            inbound.setIntoId(inboundVO.getIntoId());
            inbound.setRemarks(inboundVO.getRemarks());
            inbound.setAnnexPaths(inboundVO.getAnnexPaths());
            inboundMapper.update(inbound,
                    new UpdateWrapper<Inbound>().lambda()
                            .eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
            );
            for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
                InboundItem inboundItem = new InboundItem();
                inboundItem.setItemId(inboundItemVO.getItemId());
                if (inboundItemVO.getRemarks() != null && inboundItemVO.getRemarks() != "") {
                    inboundItem.setRemarks(inboundItemVO.getRemarks());
                    inboundItemMapper.update(inboundItem,
                            new UpdateWrapper<InboundItem>().lambda()
                                    .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                    );
                }
            }
        }
        //endregion

        //region 大编辑
        else {
            //region 根据id查询 此条入库单的数据还未更改前的数据
            InboundResponse inboundResponse = inboundMapper.selectById(inboundVO.getIntoId());
            // 需要ReceivableId 周兴 2024-7-12 增加
            inboundVO.setReceivableId(inboundResponse.getReceivableId());
            BigDecimal sumQty = inboundVO.getItemList().stream().map(InboundItemVO::getIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal sumAmt = inboundVO.getItemList().stream().map(InboundItemVO::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
            //endregion

            //region   已入库编辑
            if (Constant.IntoStatus.YIRUKU.getName().equals(inboundResponse.getIntoStatus())) {

                //region 退账
                if (inboundVO.getReceivableId() != null) {
                    accountService.reversePayable(inboundVO.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
                }
                //endregion

                //region 修改明细
                for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
                    //根据id查询
                    InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(inboundItemVO.getItemId());

                    //region 编辑明细
                    if (inboundItemVO.getItemId() != null) {

                        //region 反写订单总单 数据需要  把查出来的已入库数量用别的变量存起来
                        inboundItemVO.setUpdatePurchaseIntoQty(inboundItemResponse.getIntoQty());
                        inboundItemVO.setUpdatePurchaseIntoAmt(inboundItemResponse.getIntoAmt());
                        //endregion

                        //region 将库存需要的参数赋值
                        inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                        inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                        //编辑之前的数
                        inboundItemVO.setQtyBeforeUpdate(inboundItemResponse.getIntoQty().subtract(inboundItemResponse.getReturnQty()));
                        inboundItemVO.setAmtBeforeUpdate(inboundItemResponse.getIntoAmt().subtract(inboundItemResponse.getReturnAmt()));
                        //编辑之后的数
                        inboundItemVO.setIntoQty(inboundItemVO.getIntoingQty());
                        inboundItemVO.setIntoAmt(inboundItemVO.getIntoingAmt());
                        inboundItemVO.setAddOrEditFlag(false);
                        //endregion

                        InboundItem inboundItem = new InboundItem();
                        inboundItem.setItemId(inboundItemVO.getItemId());
                        inboundItem.setIntoQty(inboundItemVO.getIntoingQty());
                        inboundItem.setIntoAmt(inboundItemVO.getIntoingAmt());
                        inboundItem.setPriceInto(inboundItemVO.getPriceInto());
                        inboundItem.setWhId(inboundItemVO.getWhId());
                        inboundItem.setNonStdCode(inboundItemVO.getNonStdCode());
                        //修改
                        inboundItemMapper.update(inboundItem,
                                new UpdateWrapper<InboundItem>().lambda()
                                        .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                        );

                        //反算上游单据时小将这两个值清空
                        inboundItemVO.setIntoingQty(BigDecimal.ZERO);
                        inboundItemVO.setIntoingAmt(BigDecimal.ZERO);
                    }
                    //endregion

                    //region 新建明细
                    else {
                        inboundItemVO
                                .setIntoQty(inboundItemVO.getIntoingQty())
                                .setIntoAmt(inboundItemVO.getIntoingAmt())
                                .setIntoingQty(BigDecimal.ZERO)
                                .setIntoingAmt(BigDecimal.ZERO)
                        ;
                        inboundItemVO.setIntoId(inboundVO.getIntoId());
                        //入库状态
                        inboundItemVO.setIntoStatus(inboundVO.getIntoStatus());
                        inboundItemVO.setIntoType(Constant.IntoType.SALE.getName());
                        //实体转换
                        InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
                        inboundItemMapper.insert(inboundItem);
                        inboundItemVO.setItemId(inboundItem.getItemId());

                        //region 将库存需要的参数赋值
                        inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                        inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                        inboundItemVO.setIntoQty(inboundItemVO.getIntoQty());
                        inboundItemVO.setIntoAmt(inboundItemVO.getIntoAmt());
                        inboundItemVO.setAddOrEditFlag(true);
                        //endregion
                    }
                    //endregion

                    //region  采购明细
                    if (inboundItemVO.getFromItemId() != null) {
                        //根据id查询
                        PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getFromItemId());
                        PurchaseItem purchaseItem = new PurchaseItem();
                        purchaseItem.setItemId(inboundItemVO.getFromItemId());
                        purchaseItem.setIntoQty((purchaseItemResponse != null ? purchaseItemResponse.getIntoQty().negate() : BigDecimal.ZERO).add(inboundItemVO.getIntoQty()));
                        purchaseItem.setIntoAmt((purchaseItemResponse != null ? purchaseItemResponse.getIntoAmt().negate() : BigDecimal.ZERO).add(inboundItemVO.getIntoAmt()));
                        purchaseItem.setIntoingQty((purchaseItemResponse != null && purchaseItemResponse.getIntoingQty().compareTo(BigDecimal.ZERO) > 0) ? inboundItemVO.getIntoQty().negate() : BigDecimal.ZERO);
                        purchaseItem.setIntoingAmt((purchaseItemResponse != null && purchaseItemResponse.getIntoingAmt().compareTo(BigDecimal.ZERO) > 0) ? inboundItemVO.getIntoAmt().negate() : BigDecimal.ZERO);
                        purchaseItem.setUpdateIntoMessageFlag(true);
                        //入库状态
                        String intoStatus = this.setIntoStatus(purchaseItemResponse.getIntoingQty(),
                                purchaseItemResponse.getIntoQty().add(purchaseItem.getIntoQty()), purchaseItemResponse.getItemQty());
                        purchaseItem.setIntoStatus(intoStatus);
                        int countRow = purchaseItemMapper.updateById(purchaseItem);
                        //数量超出
                        if (countRow == 0) {
                            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                        }
                    }
                    //endregion
                }
                //endregion

                //region 删除明细
                if (inboundVO.getDeleteItemList() != null && inboundVO.getDeleteItemList().size() > 0) {
                    for (InboundItemVO inboundItemVO : inboundVO.getDeleteItemList()) {
                        if (inboundItemVO.getItemId() != null) {

                            //region 将库存需要的参数赋值
                            inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                            inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                            inboundItemVO.setIntoQty(inboundItemVO.getIntoQty());
                            inboundItemVO.setIntoAmt(inboundItemVO.getIntoAmt());
                            //endregion

                            InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
                            inboundItem.setFlgValid(false);
                            //修改
                            inboundItemMapper.update(inboundItem,
                                    new UpdateWrapper<InboundItem>().lambda()
                                            .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                            );
                        }

                        //region  采购明细
                        if (!Constant.GradeCode.STD.getName().equals(gradeCode)) {
                            if (inboundItemVO.getFromItemId() != null) {
                                //根据id查询
                                PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getFromItemId());
                                PurchaseItem purchaseItem = new PurchaseItem();
                                purchaseItem.setItemId(inboundItemVO.getFromItemId());
                                purchaseItem.setIntoQty(inboundItemVO.getIntoQty().negate());
                                purchaseItem.setIntoAmt(inboundItemVO.getIntoAmt().negate());
                                //入库状态
                                String intoStatus = this.setIntoStatus(purchaseItemResponse.getIntoingQty(),
                                        purchaseItemResponse.getIntoQty().add(purchaseItem.getIntoQty()), purchaseItemResponse.getItemQty());
                                purchaseItem.setIntoStatus(intoStatus);
                                int countRow = purchaseItemMapper.updateById(purchaseItem);
                                //数量超出
                                if (countRow == 0) {
                                    throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                                }
                            }
                        }
                        //endregion
                    }
                }
                //endregion

                //region  修改入库总单
                Inbound inbound = new Inbound();
                inbound.setIntoId(inboundVO.getIntoId());
                inbound.setIntoQty(inboundVO.getIntoingQty());
                inbound.setIntoAmt(inboundVO.getIntoingAmt());
                //修改
                inboundMapper.update(inbound,
                        new UpdateWrapper<Inbound>().lambda().eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
                );
                //endregion

                //region 修改采购订单总单
                this.updateOrderMessageByEdit(inboundResponse, inboundVO.getItemList(), inboundVO.getDeleteItemList());
                //endregion

                //region  入账
                if (inboundVO.getReceivableId() != null) {
                    accountService.accPayable(inboundVO.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
                }
                //endregion

                //region  调用库存
                Map<String, Object> map = new HashMap<>();
                map.put("intoDetail", inboundVO.getItemList());
                map.put("delIntoDetail", inboundVO.getDeleteItemList());
                inventoryService.operatingInventoryInformation(map);
                //endregion
            }
            //endregion

            //region 入库中编辑
            if (Constant.IntoStatus.RUKUZHONG.getName().equals(inboundResponse.getIntoStatus()) ||
                    Constant.IntoStatus.DAIRUKU.getName().equals(inboundResponse.getIntoStatus())) {

                //region 修改明细
                for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {

                    //根据id查询 获取到还未进行修改的数据
                    InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(inboundItemVO.getItemId());

                    //region  编辑明细
                    if (inboundItemVO.getItemId() != null) {

                        //region 反写订单总单 数据需要  把查出来的入库中数量用别的变量存起来
                        inboundItemVO.setUpdatePurchaseIntoingQty(inboundItemResponse.getIntoingQty());
                        inboundItemVO.setUpdatePurchaseIntoingAmt(inboundItemResponse.getIntoingAmt());
                        //endregion

                        InboundItem inboundItem = new InboundItem();
                        inboundItem.setItemId(inboundItemVO.getItemId());
                        inboundItem.setIntoingQty(inboundItemVO.getIntoingQty());
                        inboundItem.setIntoingAmt(inboundItemVO.getIntoingAmt());
                        //修改
                        inboundItemMapper.update(inboundItem,
                                new UpdateWrapper<InboundItem>().lambda()
                                        .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                        );
                    }
                    //endregion

                    //region 新建明细
                    else {
                        inboundItemVO
                                .setIntoQty(BigDecimal.ZERO)
                                .setIntoAmt(BigDecimal.ZERO)
                        ;
                        inboundItemVO.setIntoId(inboundVO.getIntoId());
                        //入库状态
                        inboundItemVO.setIntoStatus(inboundVO.getIntoStatus());
                        inboundItemVO.setIntoType(Constant.IntoType.SALE.getName());
                        //实体转换
                        InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
                        inboundItemMapper.insert(inboundItem);
                        inboundItemVO.setItemId(inboundItem.getItemId());
                    }
                    //endregion

                    //region  采购明细
                    if (inboundItemVO.getFromItemId() != null) {
                        //根据id查询
                        PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getFromItemId());
                        PurchaseItem purchaseItem = new PurchaseItem();
                        purchaseItem.setItemId(inboundItemVO.getFromItemId());
                        purchaseItem.setIntoingQty((purchaseItemResponse != null ? purchaseItemResponse.getIntoingQty().negate() : BigDecimal.ZERO).add(inboundItemVO.getIntoingQty()));
                        purchaseItem.setIntoingQty((purchaseItemResponse != null ? purchaseItemResponse.getIntoingAmt().negate() : BigDecimal.ZERO).add(inboundItemVO.getIntoingAmt()));
                        purchaseItem.setUpdateIntoingMessageFlag(true);
                        //入库状态
                        String intoStatus = this.setIntoStatus(purchaseItemResponse.getIntoingQty().add(purchaseItem.getIntoingQty()),
                                purchaseItemResponse.getIntoQty().add(purchaseItem.getIntoQty()), purchaseItemResponse.getItemQty());
                        purchaseItem.setIntoStatus(intoStatus);
                        int countRow = purchaseItemMapper.updateById(purchaseItem);
                        //数量超出
                        if (countRow == 0) {
                            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                        }
                    }
                    //endregion
                }
                //endregion

                //region 删除明细
                if (inboundVO.getDeleteItemList() != null && inboundVO.getDeleteItemList().size() > 0) {
                    for (InboundItemVO inboundItemVO : inboundVO.getDeleteItemList()) {
                        //region 明细
                        if (inboundItemVO.getItemId() != null) {
                            InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
                            inboundItem.setFlgValid(false);
                            //修改
                            inboundItemMapper.update(inboundItem,
                                    new UpdateWrapper<InboundItem>().lambda()
                                            .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                            );
                        }
                        //endregion

                        //region  采购明细
                        if (inboundItemVO.getFromItemId() != null) {
                            //根据id查询
                            PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getFromItemId());
                            PurchaseItem purchaseItem = new PurchaseItem();
                            purchaseItem.setItemId(inboundItemVO.getFromItemId());
                            purchaseItem.setIntoingQty(inboundItemVO.getIntoingQty().negate());
                            purchaseItem.setIntoingAmt(inboundItemVO.getIntoingAmt().negate());
                            //入库状态
                            String intoStatus = this.setIntoStatus(purchaseItemResponse.getIntoingQty().add(purchaseItem.getIntoingQty()),
                                    purchaseItemResponse.getIntoQty(), purchaseItemResponse.getItemQty());
                            purchaseItem.setIntoStatus(intoStatus);
                            int countRow = purchaseItemMapper.updateById(purchaseItem);
                            //数量超出
                            if (countRow == 0) {
                                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                            }
                        }
                        //endregion
                    }
                }
                //endregion

                //region  修改入库总单
                Inbound inbound = new Inbound();
                inbound.setIntoId(inboundVO.getIntoId());
                inbound.setIntoingQty(inboundVO.getIntoingQty());
                inbound.setIntoingAmt(inboundVO.getIntoingAmt());
                //修改
                inboundMapper.update(inbound,
                        new UpdateWrapper<Inbound>().lambda().eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
                );
                //endregion

                //region 修改采购订单总单
                this.updateOrderMessageByEdit(inboundResponse, inboundVO.getItemList(), inboundVO.getDeleteItemList());
                //endregion
            }
            //endregion

        }
        //endregion

        return ResponseResultUtil.success(inboundVO);
    }

    /**
     * @desc : 采购入库编辑修改订单信息
     * @date : 2024/7/8 9:12
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public void updateOrderMessageByEdit(InboundResponse inboundResponse, List<InboundItemVO> itemList, List<InboundItemVO> deleteItemList) {
        //入库明细汇总
        Map<String, List<InboundItemVO>> purchaseIntoItemVOListMap = itemList.stream().collect(Collectors.groupingBy(InboundItemVO::getFromId));
        //入库明细删除汇总
        Map<String, List<InboundItemVO>> delPurchaseIntoItemVOListMap = deleteItemList.stream().collect(Collectors.groupingBy(InboundItemVO::getFromId));
        //人库明细汇总循环
        for (String fromId : purchaseIntoItemVOListMap.keySet()) {
            //有需要删除的修改数据
            if (delPurchaseIntoItemVOListMap != null && delPurchaseIntoItemVOListMap.keySet().size() > 0) {
                //入库明细删除汇总循环
                for (String delFromId : delPurchaseIntoItemVOListMap.keySet()) {
                    //新建入库明细来源Id = 删除入库明细来源Id
                    if (fromId.equals(delFromId)) {

                        //region  提取新建分组后的明细
                        List<InboundItemVO> purchaseIntoItemVOListGroup = purchaseIntoItemVOListMap.get(fromId);
                        //分组后的明细 出库数量和金额总和
                        BigDecimal purchaseSumQtying = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                        BigDecimal purchaseSumAmting = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                        BigDecimal purchaseSumQty = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                        BigDecimal purchaseSumAmt = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                        //分组后的明细 原出库数量和金额总和
                        BigDecimal updatePurchaseIntoQty = BigDecimal.ZERO;
                        BigDecimal updatePurchaseIntoAmt = BigDecimal.ZERO;
                        BigDecimal updatePurchaseIntoingQty = BigDecimal.ZERO;
                        BigDecimal updatePurchaseIntoingAmt = BigDecimal.ZERO;
                        //已入库
                        if (Constant.IntoStatus.YIRUKU.getName().equals(inboundResponse.getIntoStatus())) {
                            updatePurchaseIntoQty = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoQty() != null).map(InboundItemVO::getUpdatePurchaseIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                            updatePurchaseIntoAmt = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoAmt() != null).map(InboundItemVO::getUpdatePurchaseIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                        }
                        //入库中
                        else {
                            updatePurchaseIntoingQty = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoingQty() != null).map(InboundItemVO::getUpdatePurchaseIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                            updatePurchaseIntoingAmt = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoingAmt() != null).map(InboundItemVO::getUpdatePurchaseIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                        }
                        //endregion

                        //region 提取删除分组后的明细
                        List<InboundItemVO> delPurchaseIntoItemVOListGroup = delPurchaseIntoItemVOListMap.get(delFromId);
                        BigDecimal purchaseDelOutQty = BigDecimal.ZERO;
                        BigDecimal purchaseDelOutAmt = BigDecimal.ZERO;
                        //已出库
                        if (Constant.IntoStatus.YIRUKU.getName().equals(inboundResponse.getIntoStatus())) {
                            purchaseDelOutQty = delPurchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                            purchaseDelOutAmt = delPurchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                        }
                        //出库中
                        else if (Constant.IntoStatus.RUKUZHONG.getName().equals(inboundResponse.getIntoStatus())) {
                            purchaseDelOutQty = delPurchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                            purchaseDelOutAmt = delPurchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                        }
                        //endregion

                        //region  反写订单数据
                        //根据id查询
                        PurchaseResponse purchaseResponse = purchaseMapper.selectById(fromId);
                        Purchase purchase = new Purchase();
                        purchase.setPurId(fromId);
                        //已入库
                        if (Constant.IntoStatus.YIRUKU.getName().equals(inboundResponse.getIntoStatus())) {
                            purchase.setIntoQty(purchaseSumQty.subtract(updatePurchaseIntoQty).subtract(purchaseDelOutQty));
                            purchase.setIntoAmt(purchaseSumAmt.subtract(updatePurchaseIntoAmt).subtract(purchaseDelOutAmt));
//                            purchase.setIntoingQty(purchaseDelOutQty);
//                            purchase.setIntoingAmt(purchaseDelOutAmt);
                            purchase.setUpdateIntoMessageFlag(true);
                            //入库状态
                            String intoStatus = this.setIntoStatus(purchaseResponse.getIntoingQty(),
                                    purchaseResponse.getIntoQty().add(purchase.getIntoQty()), purchaseResponse.getSumQuantity());
                            purchase.setIntoStatus(intoStatus);
                        }
                        //入库中
                        else if (Constant.IntoStatus.RUKUZHONG.getName().equals(inboundResponse.getIntoStatus())) {
                            purchase.setIntoingQty(purchaseSumQtying.subtract(updatePurchaseIntoingQty).subtract(purchaseDelOutQty));
                            purchase.setIntoingAmt(purchaseSumAmting.subtract(updatePurchaseIntoingAmt).subtract(purchaseDelOutAmt));
                            purchase.setUpdateIntoingMessageFlag(true);
                            //入库状态
                            String intoStatus = this.setIntoStatus(purchaseResponse.getIntoingQty().add(purchase.getIntoingQty()),
                                    purchaseResponse.getIntoQty(), purchaseResponse.getSumQuantity());
                            purchase.setIntoStatus(intoStatus);
                        }
                        //修改
                        int countRow = purchaseMapper.updateById(purchase);
                        //数量超出
                        if (countRow == 0) {
                            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                        }
                        //endregion
                    }
                }
            }
            //没有需要删除的修改数据
            else {

                //region  提取新建分组后的明细
                List<InboundItemVO> purchaseIntoItemVOListGroup = purchaseIntoItemVOListMap.get(fromId);
                //分组后的明细 出库数量和金额总和
                BigDecimal purchaseSumQty = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                BigDecimal purchaseSumAmt = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                //分组后的明细 原出库数量和金额总和
                BigDecimal updatePurchaseIntoQty = BigDecimal.ZERO;
                BigDecimal updatePurchaseIntoAmt = BigDecimal.ZERO;
                BigDecimal updatePurchaseIntoingQty = BigDecimal.ZERO;
                BigDecimal updatePurchaseIntoingAmt = BigDecimal.ZERO;
                //已入库
                if (Constant.IntoStatus.YIRUKU.getName().equals(inboundResponse.getIntoStatus())) {
                    updatePurchaseIntoQty = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoQty() != null).map(InboundItemVO::getUpdatePurchaseIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                    updatePurchaseIntoAmt = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoAmt() != null).map(InboundItemVO::getUpdatePurchaseIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                }
                //入库中
                else {
                    updatePurchaseIntoingQty = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoingQty() != null).map(InboundItemVO::getUpdatePurchaseIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                    updatePurchaseIntoingAmt = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoingAmt() != null).map(InboundItemVO::getUpdatePurchaseIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                }
                //endregion

                //region  反写订单数据
                //根据id查询
                PurchaseResponse purchaseResponse = purchaseMapper.selectById(fromId);
                Purchase purchase = new Purchase();
                purchase.setPurId(fromId);
                //已出库
                if (Constant.IntoStatus.YIRUKU.getName().equals(inboundResponse.getIntoStatus())) {
                    purchase.setIntoQty(purchaseSumQty.subtract(updatePurchaseIntoQty));
                    purchase.setIntoAmt(purchaseSumAmt.subtract(updatePurchaseIntoAmt));
                    purchase.setUpdateIntoMessageFlag(true);
                    //入库状态
                    String intoStatus = this.setIntoStatus(purchaseResponse.getIntoingQty(),
                            purchaseResponse.getIntoQty().add(purchase.getIntoQty()), purchaseResponse.getSumQuantity());
                    purchase.setIntoStatus(intoStatus);
                }
                //入库中
                else if (Constant.IntoStatus.RUKUZHONG.getName().equals(inboundResponse.getIntoStatus())) {
                    purchase.setIntoingQty(updatePurchaseIntoingQty.negate());
                    purchase.setIntoingAmt(updatePurchaseIntoingAmt.negate());
                    purchase.setUpdateIntoingMessageFlag(true);
                    //入库状态
                    String intoStatus = this.setIntoStatus(purchaseResponse.getIntoingQty().add(purchase.getIntoingQty()),
                            purchaseResponse.getIntoQty(), purchaseResponse.getSumQuantity());
                    purchase.setIntoStatus(intoStatus);
                }
                //修改
                int countRow = purchaseMapper.updateById(purchase);
                //数量超出
                if (countRow == 0) {
                    throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                }
                //endregion
            }
        }
    }

    /**
     * @desc : 采购入库作废
     * @date : 2024/3/26 9:24
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> purchaseInboundRepeal(String intoId) {

        //region  查询总单  查询明细
        //根据id查询 此条入库单的数据还未更改前的数据
        InboundResponse inboundResponse = inboundMapper.selectById(intoId);
        //根据总单id查询
        List<InboundItemResponse> inboundItemResponseList = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(inboundResponse.getIntoId()));
        //endregion

        //region  已入库作废
        if (Constant.IntoStatus.YIRUKU.getName().equals(inboundResponse.getIntoStatus())) {

            //region 退账
            if (inboundResponse.getReceivableId() != null) {
                accountService.reversePayable(inboundResponse.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
            }
            //endregion

            //region 修改明细
            for (InboundItemResponse inboundItemResponse : inboundItemResponseList) {

                //region 将库存需要的参数赋值
                inboundItemResponse.setInventoryType(Constant.InventoryType.INBOUND.getName());
                inboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                inboundItemResponse.setIntoQty(inboundItemResponse.getIntoQty().negate());
                inboundItemResponse.setIntoAmt(inboundItemResponse.getIntoAmt().negate());
                //endregion

                //region  编辑明细  赋值 赋值明细  防止作废的单据查不到明细 故注掉下面代码
//                InboundItem inboundItem = new InboundItem();
//                inboundItem.setItemId(inboundItemResponse.getItemId());
//                inboundItem.setFlgValid(false);
//                //修改
//                inboundItemMapper.update(inboundItem,
//                        new UpdateWrapper<InboundItem>().lambda()
//                                .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
//                );
                //endregion

                //region  采购明细
                if (inboundItemResponse.getFromItemId() != null) {
                    //根据id查询
                    PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemResponse.getFromItemId());
                    PurchaseItem purchaseItem = new PurchaseItem();
                    purchaseItem.setItemId(inboundItemResponse.getFromItemId());
                    purchaseItem.setIntoQty(inboundItemResponse.getIntoQty().negate());
                    purchaseItem.setIntoAmt(inboundItemResponse.getIntoAmt().negate());
                    //入库状态
                    String intoStatus = this.setIntoStatus(purchaseItemResponse.getIntoingQty(),
                            purchaseItemResponse.getIntoQty().add(purchaseItem.getIntoQty()), purchaseItemResponse.getItemQty());
                    purchaseItem.setIntoStatus(intoStatus);
                    int countRow = purchaseItemMapper.updateById(purchaseItem);
                    //数量超出
                    if (countRow == 0) {
                        throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                    }
                }
                //endregion
            }
            //endregion

            //region  修改入库总单
            Inbound inbound = new Inbound();
            inbound.setIntoId(inboundResponse.getIntoId());
            inbound.setFlgValid(false);
            //修改
            inboundMapper.update(inbound,
                    new UpdateWrapper<Inbound>().lambda().eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
            );
            //endregion

            //region 修改采购总单
            this.updateOrderMessageByRepeal(inboundResponse, inboundItemResponseList);
            //endregion

            //region  入账
            if (inboundResponse.getReceivableId() != null) {
                accountService.accPayable(inboundResponse.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
            }
            //endregion

            //region   调用库存
            Map<String, Object> map = new HashMap<>();
            map.put("delIntoDetail", inboundItemResponseList);
            inventoryService.operatingInventoryInformation(map);
            //endregion
        }
        //endregion

        //region 入库中、待入库作废
        if (Constant.IntoStatus.RUKUZHONG.getName().equals(inboundResponse.getIntoStatus()) ||
                Constant.IntoStatus.DAIRUKU.getName().equals(inboundResponse.getIntoStatus())) {
            //region 修改明细
            for (InboundItemResponse inboundItemResponse : inboundItemResponseList) {
                //region 编辑明细 赋值
                InboundItem inboundItem = new InboundItem();
                inboundItem.setItemId(inboundItemResponse.getItemId());
                inboundItem.setIntoingQty(BigDecimal.ZERO);
                inboundItem.setIntoingAmt(BigDecimal.ZERO);
//                inboundItem.setFlgValid(false);
                //修改
                inboundItemMapper.update(inboundItem,
                        new UpdateWrapper<InboundItem>().lambda()
                                .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                );
                //endregion

                //region  采购明细
                if (inboundItemResponse.getFromItemId() != null) {
                    //根据id查询
                    PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemResponse.getFromItemId());
                    PurchaseItem purchaseItem = new PurchaseItem();
                    purchaseItem.setItemId(inboundItemResponse.getFromItemId());
                    purchaseItem.setIntoingQty(inboundItemResponse.getIntoingQty().negate());
                    purchaseItem.setIntoingAmt(inboundItemResponse.getIntoingAmt().negate());
                    //入库状态
                    String intoStatus = this.setIntoStatus(purchaseItemResponse.getIntoingQty().add(purchaseItem.getIntoingQty()),
                            purchaseItemResponse.getIntoQty(), purchaseItemResponse.getItemQty());
                    purchaseItem.setIntoStatus(intoStatus);
                    int countRow = purchaseItemMapper.updateById(purchaseItem);
                    //数量超出
                    if (countRow == 0) {
                        throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                    }
                }
                //endregion
            }
            //endregion

            //region  修改入库总单
            Inbound inbound = new Inbound();
            inbound.setIntoId(inboundResponse.getIntoId());
            inbound.setIntoingQty(BigDecimal.ZERO);
            inbound.setIntoAmt(BigDecimal.ZERO);
            inbound.setFlgValid(false);
            //修改
            inboundMapper.update(inbound,
                    new UpdateWrapper<Inbound>().lambda().eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
            );
            //endregion

            //region 修改采购总单
            this.updateOrderMessageByRepeal(inboundResponse, inboundItemResponseList);
            //endregion
        }
        //endregion

        return ResponseResultUtil.success();
    }

    /**
     * @desc : 采购入库作废修改采购订单信息
     * @date : 2024/7/6 15:27
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public void updateOrderMessageByRepeal(InboundResponse inboundResponse, List<InboundItemResponse> itemList) {
        //出库明细汇总
        Map<String, List<InboundItemResponse>> purchaseIntoItemListMap = itemList.stream().collect(Collectors.groupingBy(InboundItemResponse::getFromId));
        //出库明细汇总循环
        for (String fromId : purchaseIntoItemListMap.keySet()) {

            //region  提取新建分组后的明细
            //分组后的明细 原入库数量和金额总和
            List<InboundItemResponse> purchaseIntoItemListGroup = purchaseIntoItemListMap.get(fromId);
            BigDecimal updatePurchaseIntoQty = BigDecimal.ZERO;
            BigDecimal updatePurchaseIntoAmt = BigDecimal.ZERO;
            BigDecimal updatePurchaseIntoingQty = BigDecimal.ZERO;
            BigDecimal updatePurchaseIntoingAmt = BigDecimal.ZERO;
            //已入库
            if (Constant.IntoStatus.YIRUKU.getName().equals(inboundResponse.getIntoStatus())) {
                updatePurchaseIntoQty = purchaseIntoItemListGroup.stream().map(InboundItemResponse::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                updatePurchaseIntoAmt = purchaseIntoItemListGroup.stream().map(InboundItemResponse::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
            }
            //入库中
            else {
                updatePurchaseIntoingQty = purchaseIntoItemListGroup.stream().map(InboundItemResponse::getIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                updatePurchaseIntoingAmt = purchaseIntoItemListGroup.stream().map(InboundItemResponse::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
            }
            //endregion

            //region  反写订单数据
            //根据id查询
            PurchaseResponse purchaseResponse = purchaseMapper.selectById(fromId);
            Purchase purchase = new Purchase();
            purchase.setPurId(fromId);
            //已入库
            if (Constant.IntoStatus.YIRUKU.getName().equals(purchaseResponse.getIntoStatus())) {
                purchase.setIntoQty(updatePurchaseIntoQty.negate());
                purchase.setIntoAmt(updatePurchaseIntoAmt.negate());
                //入库转态
                String intoStatus = this.setIntoStatus(purchaseResponse.getIntoingQty(),
                        purchaseResponse.getIntoQty().add(purchase.getIntoQty()), purchaseResponse.getSumQuantity());
                purchase.setIntoStatus(intoStatus);
            }
            //入库中
            else if (Constant.IntoStatus.RUKUZHONG.getName().equals(purchaseResponse.getIntoStatus())) {
                purchase.setIntoingQty(updatePurchaseIntoingQty.negate());
                purchase.setIntoingAmt(updatePurchaseIntoingAmt.negate());
                //入库状态
                String intoStatus = this.setIntoStatus(purchaseResponse.getIntoingQty().add(purchase.getIntoingQty()),
                        purchaseResponse.getIntoQty(), purchaseResponse.getSumQuantity());
                purchase.setIntoStatus(intoStatus);
            }
            //修改
            int countRow = purchaseMapper.updateById(purchase);
            //数量超出
            if (countRow == 0) {
                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
            }
            //endregion
        }
    }

    /**
     * @desc : 采购入库办理
     * @date : 2024/3/7 15:47
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> purchaseHandleInbound(InboundVO inboundVO) {

        //region 根据id查询 此条入库单的数据还未更改前的数据
        InboundResponse inboundResponse = inboundMapper.selectById(inboundVO.getIntoId());
        //endregion

        //region 退账
        if (inboundResponse.getReceivableId() != null) {
            accountService.reversePayable(inboundResponse.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
        }
        //endregion

        //region 编辑明细
        //校验明细
        if (inboundVO.getItemList().size() == 0) {
            throw new BaseBusinessException(ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getCode(),
                    ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage());
        }
        for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {

            //region 编辑明细
            if (inboundItemVO.getItemId() != null) {
                //入库明细根据id查询
                InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(inboundItemVO.getItemId());

                //region 校验数量是否超出
                if (inboundItemVO.getIntoingQty().compareTo(inboundItemResponse.getIntoingQty()) > 0) {
                    throw new BaseBusinessException(ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITY_IN_THE_OUTBOUND_SHIPMENT.getCode(),
                            ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITY_IN_THE_OUTBOUND_SHIPMENT.getMessage());
                }
                //endregion

                //region 反写订单总单 数据需要  把查出来的出库中数量用别的变量存起来
                inboundItemVO.setUpdatePurchaseIntoingQty(inboundItemResponse.getIntoingQty());
                inboundItemVO.setUpdatePurchaseIntoingAmt(inboundItemResponse.getIntoingAmt());
                //endregion

                inboundItemVO
                        .setIntoQty(inboundItemResponse.getIntoQty().add(inboundItemVO.getIntoingQty()))
                        .setIntoAmt(inboundItemResponse.getIntoAmt().add(inboundItemVO.getIntoingAmt()))
                        .setIntoingQty(BigDecimal.ZERO)
                        .setIntoingAmt(BigDecimal.ZERO)
                ;
                //入库状态
                String intoStatus = this.setIntoStatus(inboundItemVO.getIntoingQty(), inboundItemVO.getIntoQty());
                inboundItemVO.setIntoStatus(intoStatus);
                //实体转换
                InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
                //修改
                inboundItemMapper.update(inboundItem,
                        new UpdateWrapper<InboundItem>().lambda()
                                .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                );

                //region 将库存需要的参数赋值
                inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                //编辑之前的数
                if (inboundItemResponse.getIntoQty().compareTo(BigDecimal.ZERO) > 0) {
                    inboundItemVO.setQtyBeforeUpdate(inboundItemResponse.getIntoQty().subtract(inboundItemResponse.getReturnQty()));
                    inboundItemVO.setAmtBeforeUpdate(inboundItemResponse.getIntoAmt().subtract(inboundItemResponse.getReturnAmt()));
                }
                inboundItemVO.setAddOrEditFlag(true);
                //endregion
            }
            //endregion

            //region 新建明细
            else {
                inboundItemVO
                        .setIntoQty(inboundItemVO.getIntoingQty())
                        .setIntoAmt(inboundItemVO.getIntoingAmt())
                        .setIntoId(inboundVO.getIntoId())
                        .setIntoType(Constant.IntoType.SALE.getName())
                        .setIntoingQty(BigDecimal.ZERO)
                        .setIntoingAmt(BigDecimal.ZERO)
                ;
                //入库状态
                String intoStatus = this.setIntoStatus(inboundItemVO.getIntoingQty(), inboundItemVO.getIntoQty());
                inboundItemVO.setIntoStatus(intoStatus);
                //实体转换
                InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
                //新建
                inboundItemMapper.insert(inboundItem);
                inboundItemVO.setItemId(inboundItem.getItemId());

                //region 将库存需要的参数赋值
                inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                inboundItemVO.setAddOrEditFlag(true);
                //endregion
            }
            //endregion

            //region 采购明细
            if (inboundItemVO.getFromItemId() != null) {
                //根据id查询
                PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getFromItemId());
                //赋值（这里重写了更新方法，数量在更新方法中有数据库院士数量+本次数量）
                PurchaseItem purchaseItem = new PurchaseItem();
                purchaseItem.setItemId(inboundItemVO.getFromItemId());
                purchaseItem.setIntoingQty(inboundItemVO.getIntoQty().negate());
                purchaseItem.setIntoingAmt(inboundItemVO.getIntoAmt().negate());
                purchaseItem.setIntoQty(inboundItemVO.getIntoQty());
                purchaseItem.setIntoAmt(inboundItemVO.getIntoAmt());
                //入库状态
                String purItemIntoStatus = this.setIntoStatus(purchaseItemResponse.getIntoingQty().add(purchaseItem.getIntoingQty()),
                        purchaseItemResponse.getIntoQty().add(purchaseItem.getIntoQty()), purchaseItemResponse.getItemQty());
                purchaseItem.setIntoStatus(purItemIntoStatus);
                int countRow = purchaseItemMapper.updateById(purchaseItem);
                //数量超出
                if (countRow == 0) {
                    throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                }
            }
            //endregion
        }
        //endregion

        //region 删除明细
        BigDecimal delIntoingQty = BigDecimal.ZERO;
        BigDecimal delIntoingAmt = BigDecimal.ZERO;
        BigDecimal delIntoQty = BigDecimal.ZERO;
        BigDecimal delIntoAmt = BigDecimal.ZERO;
        if (inboundVO.getDeleteItemList() != null && inboundVO.getDeleteItemList().size() > 0) {
            delIntoingQty = inboundVO.getDeleteItemList().stream().map(InboundItemVO::getIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
            delIntoingAmt = inboundVO.getDeleteItemList().stream().map(InboundItemVO::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
            delIntoQty = inboundVO.getDeleteItemList().stream().map(InboundItemVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
            delIntoAmt = inboundVO.getDeleteItemList().stream().map(InboundItemVO::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
            for (InboundItemVO inboundItemVO : inboundVO.getDeleteItemList()) {
                if (inboundItemVO.getItemId() != null) {
                    //region 将库存需要的参数赋值
                    inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                    inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                    //endregion
                    InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
                    inboundItem.setFlgValid(false);
                    //修改
                    inboundItemMapper.update(inboundItem,
                            new UpdateWrapper<InboundItem>().lambda()
                                    .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                    );
                }

                //region  采购
                if (inboundItemVO.getFromItemId() != null) {
                    //region 采购订单明细
                    //赋值（这里重写了更新方法，数量在更新方法中有数据库院士数量+本次数量）
                    PurchaseItem purchaseItem = new PurchaseItem();
                    purchaseItem.setItemId(inboundItemVO.getFromItemId());
                    purchaseItem.setIntoingQty(inboundItemVO.getIntoingQty().negate());
                    purchaseItem.setIntoingAmt(inboundItemVO.getIntoingAmt().negate());
                    purchaseItem.setIntoQty(inboundItemVO.getIntoQty().negate());
                    purchaseItem.setIntoAmt(inboundItemVO.getIntoAmt().negate());
                    //根据id查询
                    PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getFromItemId());
                    //入库状态
                    String intoStatus = this.setIntoStatus(purchaseItemResponse.getIntoingQty().add(purchaseItem.getIntoingQty()),
                            purchaseItemResponse.getIntoQty().add(purchaseItem.getIntoQty()), purchaseItemResponse.getItemQty());
                    purchaseItem.setIntoStatus(intoStatus);
                    int countRow = purchaseItemMapper.updateById(purchaseItem);
                    //数量超出
                    if (countRow == 0) {
                        throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                    }
                    //endregion
                }
                //endregion

            }
        }
        //endregion

        //region 编辑总单
        inboundVO.setIntoQty(inboundVO.getIntoingQty());
        inboundVO.setIntoAmt(inboundVO.getIntoingAmt());
        inboundVO.setIntoingQty(BigDecimal.ZERO);
        inboundVO.setIntoingAmt(BigDecimal.ZERO);
        //入库状态
        String intoStatus = this.setIntoStatus(inboundVO.getIntoingQty(), inboundVO.getIntoQty());
        inboundVO.setIntoStatus(intoStatus);
        //实体转换
        Inbound inbound = inboundConvert.convertToPo(inboundVO);
        //修改
        inboundMapper.update(inbound,
                new UpdateWrapper<Inbound>().lambda().eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
        );
        //endregion

        //region  修改采购订单
        this.updateOrderMessageByHandle(inboundVO.getItemList(), inboundVO.getDeleteItemList());
        //endregion

        //region  入账
        accountService.accPayable(inboundVO.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
        //endregion

        //region  调用库存
        Map<String, Object> map = new HashMap<>();
        map.put("intoDetail", inboundVO.getItemList());
        map.put("delIntoDetail", inboundVO.getDeleteItemList());
        inventoryService.operatingInventoryInformation(map);
        //endregion

        return ResponseResultUtil.success(inboundVO);
    }

    /**
     * @desc : 采购入库办理修改采购订单信息
     * @date : 2024/7/8 14:03
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public void updateOrderMessageByHandle(List<InboundItemVO> itemList, List<InboundItemVO> deleteItemList) {
        //出库明细汇总
        Map<String, List<InboundItemVO>> purchaseIntoItemVOListMap = itemList.stream().collect(Collectors.groupingBy(InboundItemVO::getFromId));
        //出库明细删除汇总
        Map<String, List<InboundItemVO>> delPurchaseIntoItemVOListMap = deleteItemList.stream().collect(Collectors.groupingBy(InboundItemVO::getFromId));
        //出库明细汇总循环g
        for (String fromId : purchaseIntoItemVOListMap.keySet()) {
            //有需要删除的办理数据
            if (delPurchaseIntoItemVOListMap != null && delPurchaseIntoItemVOListMap.keySet().size() > 0) {
                //入库明细删除汇总循环
                for (String delFromId : delPurchaseIntoItemVOListMap.keySet()) {
                    //新建入库明细来源Id = 删除入库明细来源Id
                    if (fromId.equals(delFromId)) {
                        //region  提取新建分组后的明细
                        List<InboundItemVO> purchaseIntoItemVOListGroup = purchaseIntoItemVOListMap.get(fromId);
                        //分组后的明细 入库数量和金额总和
                        BigDecimal purchaseSumQty = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                        BigDecimal purchaseSumAmt = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                        //分组后的明细 原入库数量和金额总和
                        BigDecimal updatePurchaseIntoingQty = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoingQty() != null).map(InboundItemVO::getUpdatePurchaseIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                        BigDecimal updatePurchaseIntoingAmt = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoingAmt() != null).map(InboundItemVO::getUpdatePurchaseIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                        //endregion

                        //region 提取删除分组后的明细
                        List<InboundItemVO> delPurchaseIntoItemVOListGroup = delPurchaseIntoItemVOListMap.get(delFromId);
                        BigDecimal purchaseDelIntoingQty = delPurchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                        BigDecimal purchaseDelIntoingAmt = delPurchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                        BigDecimal purchaseDelIntoQty = delPurchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                        BigDecimal purchaseDelIntoAmt = delPurchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                        //endregion

                        //region 反写订单数据
                        //根据id查询
                        PurchaseResponse purchaseResponse = purchaseMapper.selectById(fromId);
                        Purchase purchase = new Purchase();
                        purchase.setPurId(fromId);
                        purchase.setIntoQty(purchaseSumQty.subtract(purchaseDelIntoQty));
                        purchase.setIntoAmt(purchaseSumAmt.subtract(purchaseDelIntoAmt));
                        purchase.setIntoingQty((updatePurchaseIntoingQty.add(purchaseDelIntoingQty)).negate());
                        purchase.setIntoingAmt((updatePurchaseIntoingAmt.add(purchaseDelIntoingAmt)).negate());
                        //入库状态
                        String purIntoStatus = this.setIntoStatus(purchaseResponse.getIntoingQty().add(purchase.getIntoingQty()),
                                purchaseResponse.getIntoQty().add(purchase.getIntoQty()), purchaseResponse.getSumQuantity());
                        purchase.setIntoStatus(purIntoStatus);
                        //修改
                        int countRow = purchaseMapper.updateById(purchase);
                        //数量超出
                        if (countRow == 0) {
                            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                        }
                        //endregion
                    }
                }
            }
            //没有需要删除的办理数据
            else {
                //region  提取新建分组后的明细
                List<InboundItemVO> purchaseIntoItemVOListGroup = purchaseIntoItemVOListMap.get(fromId);
                //分组后的明细 入库数量和金额总和
                BigDecimal purchaseSumQty = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                BigDecimal purchaseSumAmt = purchaseIntoItemVOListGroup.stream().map(InboundItemVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                //分组后的明细 原入库数量和金额总和
                BigDecimal updatePurchaseIntoingQty = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoingQty() != null).map(InboundItemVO::getUpdatePurchaseIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                BigDecimal updatePurchaseIntoingAmt = purchaseIntoItemVOListGroup.stream().filter(it -> it.getUpdatePurchaseIntoingAmt() != null).map(InboundItemVO::getUpdatePurchaseIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                //endregion

                //region 反写订单数据
                //根据id查询
                PurchaseResponse purchaseResponse = purchaseMapper.selectById(fromId);
                Purchase purchase = new Purchase();
                purchase.setPurId(fromId);
                purchase.setIntoQty(purchaseSumQty);
                purchase.setIntoAmt(purchaseSumAmt);
                purchase.setIntoingAmt(updatePurchaseIntoingAmt.negate());
                purchase.setIntoingQty(updatePurchaseIntoingQty.negate());
                //入库状态
                String purIntoStatus = this.setIntoStatus(purchaseResponse.getIntoingQty().add(purchase.getIntoingQty()),
                        purchaseResponse.getIntoQty().add(purchase.getIntoQty()), purchaseResponse.getSumQuantity());
                purchase.setIntoStatus(purIntoStatus);
                //修改
                int countRow = purchaseMapper.updateById(purchase);
                //数量超出
                if (countRow == 0) {
                    throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                }
                //endregion
            }
        }
    }

    /**
     * @desc : 采购入库撤销
     * @date : 2024/3/7 17:06
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> purchaseInboundCancel(InboundVO inboundVO) {
        //region 查询入库总单数据信息
        InboundResponse inboundResponse = inboundMapper.selectById(inboundVO.getIntoId());
        //endregion

        //region 根据总单id查明细
        List<InboundItemResponse> inboundItemResponseList = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(inboundResponse.getIntoId()));
        //endregion

        //region 退账
        if (inboundResponse.getReceivableId() != null) {
            accountService.reversePayable(inboundVO.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
        }
        //endregion

        //region 修改订单数据信息
        this.updateOrderMessageByCancel(inboundItemResponseList);
        //endregion

        //region 修改总单数据信息
        Inbound inbound = new Inbound();
        inbound.setIntoId(inboundVO.getIntoId());
        inbound.setIntoDate(null);
        inbound.setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName());
        inbound.setIntoingQty(inboundResponse.getIntoingQty().add(inboundResponse.getIntoQty()));
        inbound.setIntoingAmt(inboundResponse.getIntoingAmt().add(inboundResponse.getIntoAmt()));
        inbound.setIntoQty(BigDecimal.ZERO);
        inbound.setIntoAmt(BigDecimal.ZERO);
        //修改
        inboundMapper.update(inbound,
                new UpdateWrapper<Inbound>().lambda()
                        .eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
        );
        //endregion

        //region 明细数据
        for (InboundItemResponse inboundItemResponse : inboundItemResponseList) {

            //region 修改采购明细数据信息
            if (inboundItemResponse.getFromItemId() != null) {
                //根据id查询
                PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemResponse.getFromItemId());
                //赋值  （这里重写了更新方法，数量在更新方法中有数据库院士数量+本次数量）
                PurchaseItem purchaseItem = new PurchaseItem();
                purchaseItem.setItemId(inboundItemResponse.getFromItemId());
                purchaseItem.setIntoQty(inboundItemResponse.getIntoQty().negate());
                purchaseItem.setIntoAmt(inboundItemResponse.getIntoAmt().negate());
                purchaseItem.setIntoingQty(inboundItemResponse.getIntoQty());
                purchaseItem.setIntoingAmt(inboundItemResponse.getIntoAmt());
                //入库状态
                String intoStatus = this.setIntoStatus(purchaseItemResponse.getIntoingQty().add(purchaseItem.getIntoingQty()),
                        purchaseItemResponse.getIntoQty().add(purchaseItem.getIntoQty()), purchaseItemResponse.getItemQty());
                purchaseItem.setIntoStatus(intoStatus);
                //修改
                int countRow = purchaseItemMapper.updateById(purchaseItem);
                //数量超出
                if (countRow == 0) {
                    throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
                }
            }
            //endregion

            //region修改入库明细信息
            InboundItem inboundItem = new InboundItem();

            //region 将库存需要的参数赋值
            inboundItemResponse.setInventoryType(Constant.InventoryType.INBOUND.getName());
            inboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
            inboundItemResponse.setIntoQty(inboundItemResponse.getIntoQty().negate());
            inboundItemResponse.setIntoAmt(inboundItemResponse.getIntoAmt().negate());
            //endregion

            inboundItem
                    .setIntoId(inbound.getIntoId())
                    .setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName())
                    .setIntoingQty(inboundItemResponse.getIntoingQty().add(inboundItemResponse.getIntoQty()))
                    .setIntoingAmt(inboundItemResponse.getIntoingAmt().add(inboundItemResponse.getIntoAmt()))
                    .setIntoQty(BigDecimal.ZERO)
                    .setIntoAmt(BigDecimal.ZERO)
                    .setCostPrice(BigDecimal.ZERO)
                    .setCostAmt(BigDecimal.ZERO)
                    .setItemId(inboundItemResponse.getItemId());
            //入库状态
            String intoStatus = this.setIntoStatus(inboundItem.getIntoingQty(), inboundItem.getIntoQty());
            inboundItem.setIntoStatus(intoStatus);
            //修改
            inboundItemMapper.update(inboundItem,
                    new UpdateWrapper<InboundItem>().lambda()
                            .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
            );
            //endregion
        }
        //endregion

        //region   调用库存
        Map<String, Object> map = new HashMap<>();
        map.put("delIntoDetail", inboundItemResponseList);
        inventoryService.operatingInventoryInformation(map);
        //endregion

        return ResponseResultUtil.success();
    }

    /**
     * @desc : 采购入库撤销修改采购订单信息
     * @date : 2024/7/8 16:50
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public void updateOrderMessageByCancel(List<InboundItemResponse> itemList) {
        //出库明细汇总
        Map<String, List<InboundItemResponse>> purchaseIntoItemListMap = itemList.stream().collect(Collectors.groupingBy(InboundItemResponse::getFromId));
        //出库明细汇总循环
        for (String fromId : purchaseIntoItemListMap.keySet()) {

            //region  提取新建分组后的明细
            List<InboundItemResponse> purchaseIntoItemListGroup = purchaseIntoItemListMap.get(fromId);
            //分组后的明细 入库数量和金额总和
            BigDecimal purchaseSumQty = purchaseIntoItemListGroup.stream().map(InboundItemResponse::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal purchaseSumAmt = purchaseIntoItemListGroup.stream().map(InboundItemResponse::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
            //endregion

            //region  反写订单数据
            //根据id查询
            //根据id查询
            PurchaseResponse purchaseResponse = purchaseMapper.selectById(fromId);
            Purchase purchase = new Purchase();
            purchase.setPurId(fromId);
            purchase.setIntoingQty(purchaseSumQty);
            purchase.setIntoingAmt(purchaseSumAmt);
            purchase.setIntoQty(purchaseSumQty.negate());
            purchase.setIntoAmt(purchaseSumAmt.negate());
            //入库状态
            String intoStatus = this.setIntoStatus(purchaseResponse.getIntoingQty().add(purchase.getIntoingQty()),
                    purchaseResponse.getIntoQty().add(purchase.getIntoQty()), purchaseResponse.getSumQuantity());
            purchase.setIntoStatus(intoStatus);
            int countRow = purchaseMapper.updateById(purchase);
            //数量超出
            if (countRow == 0) {
                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
            }
            //endregion
        }
    }

    /**
     * @desc : 校验库存是否存在和库存量是否足够本次退货作废
     * @date : 2024/7/6 10:52
     * @author : 寇珊珊
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public void checkInventoryExist(List<InboundItemResponse> inboundItemResponseList) {
        for (InboundItemResponse inboundItemResponse : inboundItemResponseList) {
            //存在标识
            //根据sku，仓库，非标号查询
            InventoryResponse inventoryResponse = null;
            if (inboundItemResponse.getSkuId() != null) {
                inventoryResponse = inventoryMapper.selectByOther(new InventoryQuery()
                        .setSkuId(inboundItemResponse.getSkuId())
                        .setWhId(inboundItemResponse.getWhId())
                        .setNonStdCode(inboundItemResponse.getNonStdCode()));
            }
            //库存不存在
            if (inventoryResponse == null) {
                //当前出库商品中在库存中不存在
                throw new BaseBusinessException(ErrorCodeEnum.SKU_IS_NOT_IN_INVENTORY.getCode(),
                        ErrorCodeEnum.SKU_IS_NOT_IN_INVENTORY.getMessage());
            }
            //判断库存中的数量是否足够本次采购退货作废（采购退货作废等于出库）
            if (inventoryResponse.getInvQty().compareTo(inboundItemResponse.getIntoQty()) < 0) {
                //当前库存数量不足,不允许采购退货作废操作
                throw new BaseBusinessException(ErrorCodeEnum.INSUFFICIENT_INVENTORY_NOT_ALLOW_PURCHASE_RETURN.getCode(),
                        ErrorCodeEnum.INSUFFICIENT_INVENTORY_NOT_ALLOW_PURCHASE_RETURN.getMessage());
            }
        }
    }

    /**
     * @desc : 一键作废
     * @date : 2024/7/6 11:03
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> oneKeyPurchaseInboundRepeal(String orderId) {
        List<InboundItemResponse> InboundItemResponses = inboundItemMapper.selectMessageByOtherCondition(new InboundItemQuery().setFromId(orderId));
        if (InboundItemResponses != null && InboundItemResponses.size() > 0) {
            Map<String, List<InboundItemResponse>> inboundItemMap = InboundItemResponses.stream().collect(Collectors.groupingBy(InboundItemResponse::getIntoId));
            for (String str : inboundItemMap.keySet()) {
                //region  查询总单  查询明细
                //根据id查询 此条入库单的数据还未更改前的数据
                InboundResponse inboundResponse = inboundMapper.selectById(str);
                //根据总单id查询
                List<InboundItemResponse> inboundItemResponseList = inboundItemMapper.selectMessageByOtherCondition(new InboundItemQuery().setIntoId(inboundResponse.getIntoId()));
                //endregion

                //region 已入库状态作废
                if (Constant.IntoStatus.YIRUKU.getName().equals(inboundResponse.getIntoStatus())) {

                    //region  查询当前订单下的入库单的库存是否足够允许作废
                    this.checkInventoryExist(inboundItemResponseList);
                    //endregion

                    //region 退账 不需要判断，判断反而不对，周兴 202407020
//                    if (inboundResponse.getReceivableId() != null) {
                    accountService.reversePayable(inboundResponse.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
//                    }
                    //endregion

                    //region 修改明细
                    for (InboundItemResponse inboundItemResponse : inboundItemResponseList) {

                        //region 将库存需要的参数赋值
                        inboundItemResponse.setInventoryType(Constant.InventoryType.INBOUND.getName());
                        inboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                        inboundItemResponse.setIntoQty(inboundItemResponse.getIntoQty());
                        inboundItemResponse.setIntoAmt(inboundItemResponse.getIntoAmt());
                        //endregion

                        //region  编辑明细  赋值 赋值明细  防止作废的单据查不到明细 故注掉下面代码
//                InboundItem inboundItem = new InboundItem();
//                inboundItem.setItemId(inboundItemResponse.getItemId());
//                inboundItem.setFlgValid(false);
//                //修改
//                inboundItemMapper.update(inboundItem,
//                        new UpdateWrapper<InboundItem>().lambda()
//                                .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
//                );
                        //endregion

                    }
                    //endregion

                    //region  修改入库总单
                    Inbound inbound = new Inbound();
                    inbound.setIntoId(inboundResponse.getIntoId());
                    inbound.setFlgValid(false);
                    //修改
                    inboundMapper.update(inbound,
                            new UpdateWrapper<Inbound>().lambda().eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
                    );
                    //endregion

                    //region  入账 作废把账作废就行，不要再入账了 周兴 202407020
//                    if (inboundResponse.getReceivableId() != null) {
//                         accountService.accPayable(inboundResponse.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
//                    }
                    //endregion

                    //region   调用库存
                    Map<String, Object> map = new HashMap<>();
                    map.put("delIntoDetail", inboundItemResponseList);
                    inventoryService.operatingInventoryInformation(map);
                    //endregion
                }
                //endregion

                //region 出库中、待出库状态作废
                if (Constant.IntoStatus.DAIRUKU.getName().equals(inboundResponse.getIntoStatus()) ||
                        Constant.IntoStatus.RUKUZHONG.getName().equals(inboundResponse.getIntoStatus())) {

                    //region 修改明细
                    for (InboundItemResponse inboundItemResponse : inboundItemResponseList) {

                        //region 编辑明细 赋值
                        InboundItem inboundItem = new InboundItem();
                        inboundItem.setItemId(inboundItemResponse.getItemId());
                        inboundItem.setIntoingQty(BigDecimal.ZERO);
                        inboundItem.setIntoingAmt(BigDecimal.ZERO);
//                inboundItem.setFlgValid(false);
                        //修改
                        inboundItemMapper.update(inboundItem,
                                new UpdateWrapper<InboundItem>().lambda()
                                        .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                        );
                        //endregion
                    }
                    //endregion

                    //region  修改入库总单
                    Inbound inbound = new Inbound();
                    inbound.setIntoId(inboundResponse.getIntoId());
                    inbound.setIntoingQty(BigDecimal.ZERO);
                    inbound.setIntoAmt(BigDecimal.ZERO);
                    inbound.setFlgValid(false);
                    //修改
                    inboundMapper.update(inbound,
                            new UpdateWrapper<Inbound>().lambda().eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
                    );
                    //endregion
                }
                //endregion
            }
        }
        return ResponseResultUtil.success();
    }

    /**
     * @desc : 入库状态通用（目前本页面）
     * @date : 2024/3/9 8:59
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public String setIntoStatus(BigDecimal intoingQty, BigDecimal intoQty) {
        //入库状态
        String intoStatus = null;
        //已入库数量>0 入库中数量>0
        if (intoQty.compareTo(BigDecimal.ZERO) >= 0 && intoingQty.compareTo(BigDecimal.ZERO) > 0) {
            //入库中
            intoStatus = Constant.IntoStatus.RUKUZHONG.getName();
        }
        //已入库数量=0 入库中数量=0
        else if (intoQty.compareTo(BigDecimal.ZERO) == 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
            //待入库
            intoStatus = Constant.IntoStatus.DAIRUKU.getName();
        }
        //已入库数量>0 入库中数量=0
        else if (intoQty.compareTo(BigDecimal.ZERO) > 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
            //已入库
            intoStatus = Constant.IntoStatus.YIRUKU.getName();
        }
        return intoStatus;
    }

    /**
     * @desc : 上游单据入库状态通用（目前本页面）
     * @date : 2024/4/1 17:14
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public String setIntoStatus(BigDecimal intoingQty, BigDecimal intoQty, BigDecimal sumQty) {
        //入库状态
        String intoStatus = null;
        //已入库数量>=0 入库中数量>0
        if (intoQty.compareTo(BigDecimal.ZERO) >= 0 && intoingQty.compareTo(BigDecimal.ZERO) > 0) {
            //入库中
            intoStatus = Constant.IntoStatus.RUKUZHONG.getName();
        }
        //已入库数量=0 入库中数量=0
        else if (intoQty.compareTo(BigDecimal.ZERO) == 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
            //待入库
            intoStatus = Constant.IntoStatus.DAIRUKU.getName();
        }
        //已入库 数量>0 入库中数量=0
        else if (intoQty.compareTo(BigDecimal.ZERO) > 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
            if (sumQty.compareTo(intoQty.add(intoingQty)) == 0) {
                //已入库
                intoStatus = Constant.IntoStatus.YIRUKU.getName();
            } else {
                //入库中
                intoStatus = Constant.IntoStatus.RUKUZHONG.getName();
            }

        }
        return intoStatus;
    }

}
