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

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.OutboundConvert;
import com.dk.mdm.infrastructure.convert.ivt.OutboundItemConvert;
import com.dk.mdm.mapper.common.CommonMapper;
import com.dk.mdm.mapper.ivt.OutboundItemMapper;
import com.dk.mdm.mapper.ivt.OutboundMapper;
import com.dk.mdm.mapper.sale.OrderItemMapper;
import com.dk.mdm.mapper.sale.OrderMapper;
import com.dk.mdm.model.pojo.ivt.InboundItem;
import com.dk.mdm.model.pojo.ivt.Outbound;
import com.dk.mdm.model.pojo.ivt.OutboundItem;
import com.dk.mdm.model.pojo.sale.Order;
import com.dk.mdm.model.pojo.sale.OrderItem;
import com.dk.mdm.model.query.ivt.OutboundItemQuery;
import com.dk.mdm.model.query.ivt.OutboundQuery;
import com.dk.mdm.model.response.ivt.OutboundItemResponse;
import com.dk.mdm.model.response.ivt.OutboundResponse;
import com.dk.mdm.model.response.sale.OrderItemResponse;
import com.dk.mdm.model.response.sale.OrderResponse;
import com.dk.mdm.model.vo.ivt.InboundItemVO;
import com.dk.mdm.model.vo.ivt.OutboundItemVO;
import com.dk.mdm.model.vo.ivt.OutboundVO;
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.util.*;
import java.util.stream.Collectors;


/**
 * @author : 寇珊珊
 * @desc : 销售退货出库业务层
 * @date : 2024/3/18 15:33
 */
@Service
public class OutboundSaleReturnService extends BaseService<Outbound> {

    @Override
    public BaseMapper<Outbound> getRepository() {
        return outboundMapper;
    }

    @Autowired
    private CommonService commonService;

    @Autowired
    private OutboundMapper outboundMapper;

    @Autowired
    private OutboundConvert outboundConvert;

    @Autowired
    private OutboundItemMapper outboundItemMapper;

    @Autowired
    private OutboundItemConvert outboundItemConvert;

    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OutCommon outCommon;


    /**
     * @desc : 修改原总单数据（订单，出库单）
     * posNegFlag :正负标识  true:正数，false:负数
     * 默认传值过来的是负数
     * @date : 2024/3/29 10:43
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public void updatePrimaryTotal(OutboundVO outboundVO, Boolean posNegFlag) {
        if (outboundVO.getFromId() != null) {
            //region 原入库单
            Map<String, List<OutboundItemVO>> outboundItemVOListOutPri = outboundVO.getItemList().stream().collect(Collectors.groupingBy(OutboundItemVO::getSOutId));
            for (String str : outboundItemVOListOutPri.keySet()) {
                List<OutboundItemVO> outboundItemList = outboundItemVOListOutPri.get(str);
                //region 汇总退货数量 金额
                BigDecimal returnSumQty = outboundItemList.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                BigDecimal returnSumAmt = outboundItemList.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal returnIngSumQty = outboundItemList.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                BigDecimal returnIngSumAmt = outboundItemList.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                //endregion
                //查询原单
                OutboundResponse outboundResponse = outboundMapper.selectById(str);
                //校验数据
                if (returnSumQty.add(returnIngSumQty).compareTo(outboundResponse.getOutQty()) > 0) {
                    throw new BaseBusinessException(ErrorCodeEnum.OUTBOUND_ITEM_RETURN_QTY_ERROR.getCode(),
                            ErrorCodeEnum.OUTBOUND_ITEM_RETURN_QTY_ERROR.getMessage());
                }
                Outbound outboundPri = new Outbound();
                outboundPri.setOutId(str);
                if (posNegFlag) {
                    outboundPri.setReturnQty((returnSumQty.add(returnIngSumQty)).negate());
                    outboundPri.setReturnAmt((returnSumAmt.add(returnIngSumAmt)).negate());
                } else {
                    outboundPri.setReturnQty(returnSumQty.add(returnIngSumQty));
                    outboundPri.setReturnAmt(returnSumAmt.add(returnIngSumAmt));
                }
                outboundMapper.updateReturnMessage(outboundPri);
            }
            //endregion

            //region 原销售订单
            Map<String, List<OutboundItemVO>> outboundItemVOListOrderPri = outboundVO.getItemList().stream().collect(Collectors.groupingBy(OutboundItemVO::getSOrderId));
            for (String str : outboundItemVOListOrderPri.keySet()) {
                List<OutboundItemVO> outboundItemList = outboundItemVOListOrderPri.get(str);
                //region 汇总退货数量 金额
                BigDecimal returnSumQty = outboundItemList.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                BigDecimal returnSumAmt = outboundItemList.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal returnIngSumQty = outboundItemList.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                BigDecimal returnIngSumAmt = outboundItemList.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);

                //endregion
                //查询原单
                OrderResponse orderResponse = orderMapper.selectById(str);
                //校验数据
                if (returnSumQty.add(returnIngSumQty).compareTo(orderResponse.getOutQty()) > 0) {
                    throw new BaseBusinessException(ErrorCodeEnum.OUTBOUND_ITEM_RETURN_QTY_ERROR.getCode(),
                            ErrorCodeEnum.OUTBOUND_ITEM_RETURN_QTY_ERROR.getMessage());
                }
                Order orderPri = new Order();
                orderPri.setOrderId(str);
                if (posNegFlag) {
                    orderPri.setReturnQty((returnSumQty.add(returnIngSumQty)).negate());
                    orderPri.setReturnAmt((returnSumAmt.add(returnIngSumAmt)).negate());
                } else {
                    orderPri.setReturnQty(returnSumQty.add(returnIngSumQty));
                    orderPri.setReturnAmt(returnSumAmt.add(returnIngSumAmt));
                }
                orderMapper.updateReturnMessage(orderPri);
            }
            //endregion
        }
    }

    /**
     * @desc : 修改原明细（订单，出库）
     * posNegFlag :正负标识  true:正数，false:负数
     * isOutEdit：取入库中还是已入库数量  true 取已入库  false 取 入库中
     * 默认传值过来的是负数
     * @date : 2024/3/29 10:46
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public void updatePrimaryDetail(OutboundItemVO outboundItemVO, Boolean posNegFlag, Boolean isOutEdit) {
        if (outboundItemVO.getFromItemId() != null) {

            //region  反写出库明细
            if (outboundItemVO.getSOutItemId() != null) {
                OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getSOutItemId());
                //region  校验可退数量
                if (outboundItemResponse.getOutQty().compareTo(outboundItemVO.getOutQty()) < 0) {
                    throw new BaseBusinessException(ErrorCodeEnum.OUTBOUND_ITEM_RETURN_QTY_ERROR.getCode(),
                            ErrorCodeEnum.OUTBOUND_ITEM_RETURN_QTY_ERROR.getMessage());
                }
                //endregion
                //region  赋值修改
                OutboundItem outboundItemPri = new OutboundItem();
                outboundItemPri.setItemId(outboundItemVO.getSOutItemId());
                if (posNegFlag) {
                    outboundItemPri.setReturnQty(isOutEdit ? outboundItemVO.getOutQty().negate() : outboundItemVO.getOutingQty().negate());
                    outboundItemPri.setReturnAmt(isOutEdit ? outboundItemVO.getOutAmt().negate() : outboundItemVO.getOutingAmt().negate());
                } else {
                    outboundItemPri.setReturnQty(isOutEdit ? outboundItemVO.getOutQty() : outboundItemVO.getOutingQty());
                    outboundItemPri.setReturnAmt(isOutEdit ? outboundItemVO.getOutAmt() : outboundItemVO.getOutingAmt());
                }
                outboundItemMapper.updateReturnMessage(outboundItemPri);
                //endregion
            }
            //endregion

            //region  反写订单明细
            if (outboundItemVO.getSOrderItemId() != null) {
                OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getSOrderItemId());
                //region  校验可退数量
                if (orderItemResponse.getOutQty().compareTo(outboundItemVO.getOutQty()) < 0) {
                    throw new BaseBusinessException(ErrorCodeEnum.OUTBOUND_ITEM_RETURN_QTY_ERROR.getCode(),
                            ErrorCodeEnum.OUTBOUND_ITEM_RETURN_QTY_ERROR.getMessage());
                }
                //endregion
                //region  赋值修改
                OrderItem orderItem = new OrderItem();
                orderItem.setItemId(outboundItemVO.getSOutItemId());
                if (posNegFlag) {
                    orderItem.setReturnQty(outboundItemVO.getOutingQty().negate());
                    orderItem.setReturnAmt(outboundItemVO.getOutingAmt().negate());
                } else {
                    orderItem.setReturnQty(outboundItemVO.getOutingQty());
                    orderItem.setReturnAmt(outboundItemVO.getOutingAmt());
                }
                orderItemMapper.updateReturnMessage(orderItem);
                //endregion
            }
            //endregion
        }
    }

    /**
     * @desc : 回滚原总单和明细信息（订单，出库单）
     * @date : 2024/4/3 17:01
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public void rollBackTotalAndDetail(List<OutboundItemVO> deleteItemList) {
        if (deleteItemList.size() > 0 &&  deleteItemList.get(0).getFromItemId() != null) {
            //出库单
            Map<String, List<OutboundItemVO>> collect = deleteItemList.stream().collect(Collectors.groupingBy(OutboundItemVO::getSOutId));
            for (String outId : collect.keySet()) {
                List<OutboundItemVO> outboundItemVOList = collect.get(outId);
                BigDecimal delOutQty = outboundItemVOList.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                BigDecimal delOutAmt = outboundItemVOList.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal delOutingQty = outboundItemVOList.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                BigDecimal delOutingAmt = outboundItemVOList.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                //总单
                Outbound outboundPri = new Outbound();
                outboundPri.setOutId(outId);
                outboundPri.setReturnQty((delOutQty.add(delOutingQty)).negate());
                outboundPri.setReturnAmt(delOutAmt.add(delOutingAmt).negate());
                outboundMapper.updateReturnMessage(outboundPri);
                //明细
                for (OutboundItemVO outboundItemVO : outboundItemVOList) {
                    OutboundItem outboundItemPri = new OutboundItem();
                    outboundItemPri.setItemId(outboundItemVO.getItemId());
                    outboundItemPri.setReturnQty(outboundItemVO.getOutingQty());
                    outboundItemPri.setReturnAmt(outboundItemVO.getOutingAmt());
                    outboundItemMapper.updateReturnMessage(outboundItemPri);
                }
            }
            //订单
            Map<String, List<OutboundItemVO>> orderCollect = deleteItemList.stream().collect(Collectors.groupingBy(OutboundItemVO::getSOrderId));
            for (String orderId : orderCollect.keySet()) {
                List<OutboundItemVO> outboundItemVOList = orderCollect.get(orderId);
                BigDecimal delOutQty = outboundItemVOList.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                BigDecimal delOutAmt = outboundItemVOList.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal delOutingQty = outboundItemVOList.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
                BigDecimal delOutingAmt = outboundItemVOList.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                //总单
                Order orderPri = new Order();
                orderPri.setOrderId(orderId);
                orderPri.setReturnQty((delOutQty.add(delOutingQty)).negate());
                orderPri.setReturnAmt(delOutAmt.add(delOutingAmt).negate());
                orderMapper.updateReturnMessage(orderPri);
                //明细
                for (OutboundItemVO outboundItemVO : outboundItemVOList) {
                    OrderItem orderItemPri = new OrderItem();
                    orderItemPri.setItemId(outboundItemVO.getItemId());
                    orderItemPri.setReturnQty(outboundItemVO.getOutingQty());
                    orderItemPri.setReturnAmt(outboundItemVO.getOutingAmt());
                    orderItemMapper.updateReturnMessage(orderItemPri);
                }
            }
        }
    }

    /**
     * @desc : 销售退货出库新建
     * @date : 2024/3/7 14:13
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> saleReturnOutboundInsert(OutboundVO outboundVO) {

        //region 如果没有客户id，要新建
        if (outboundVO.getCusId() == null) {
            outboundVO = outCommon.insertCustomer(outboundVO);
        }
        //endregion

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

        //region 原入库单  原销售订单
        this.updatePrimaryTotal(outboundVO, true);
        //endregion

        //region  总单
        //获取 id/单号
        Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.PURCASERETURNINBOUND.getName(), false);
        outboundVO.setOutId(codeMap.get("outId").toString()).
                setOutNo(codeMap.get("outNote").toString());
        //出库类型
        outboundVO.setOutType(Constant.OutType.SALE_RETURN.getName());
        //自动入库标识
        if (outboundVO.getFlgAutoHandle()) {
            //已出库
            outboundVO.setOutStatus(Constant.OutStatus.YICHUKU.getName());
        } else {
            //出库中
            outboundVO.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
        }
        //出库状态等于已出库 更新合计出库数量/金额 = 出库中数量/出库中金额
        if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
            outboundVO
                    .setOutQty(outboundVO.getOutingQty())
                    .setOutAmt(outboundVO.getOutingAmt())
                    .setOutingQty(BigDecimal.ZERO)
                    .setOutingAmt(BigDecimal.ZERO)
            ;
        } else {
            outboundVO
                    .setOutQty(BigDecimal.ZERO)
                    .setOutAmt(BigDecimal.ZERO)
            ;
        }
        //实体转换
        Outbound outbound = outboundConvert.convertToPo(outboundVO);
        outboundMapper.insert(outbound);
        //endregion

        //region  新建多业务归属
        outCommon.insertMultiOwner(outboundVO);
        //endregion

        //region 明细
        //校验明细
        if (outboundVO.getItemList().size() == 0) {
            throw new BaseBusinessException(ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getCode(),
                    ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getMessage());
        }
        for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {

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

            //region  新建明细
            //总单id
            outboundItemVO.setOutId(outboundVO.getOutId());
            //出库类型
            outboundItemVO.setOutType(outboundVO.getOutType());
            //出库状态等于已出库  更新合计出库数量/金额 = 出库中数量/出库中金额
            if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
                outboundItemVO
                        .setOutQty(outboundItemVO.getOutingQty())
                        .setOutAmt(outboundItemVO.getOutingAmt())
                        .setOutingQty(BigDecimal.ZERO)
                        .setOutingAmt(BigDecimal.ZERO)
                        .setCostPrice(outboundItemVO.getPriceOut())
                        .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
                ;

                //region  反写出库明细 反写订单明细
                this.updatePrimaryDetail(outboundItemVO, true, true);
                //endregion

            } else {
                outboundItemVO
                        .setOutQty(BigDecimal.ZERO)
                        .setOutAmt(BigDecimal.ZERO);

                //region  反写出库明细 反写订单明细
                this.updatePrimaryDetail(outboundItemVO, true, false);
                //endregion

            }
            //入库状态
            outboundItemVO.setOutStatus(outboundVO.getOutStatus());
            //实体转换
            OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
            outboundItemMapper.insert(outboundItem);
            outboundItemVO.setItemId(outboundItem.getItemId());
            //endregion
        }
        //endregion

        //region  账款
        if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
            accountService.accReceivable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
        }
        //endregion

        //region 修改库存
        if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
            Map<String, Object> invMap = new HashMap<>();
            invMap.put("outDetail", outboundVO.getItemList());
            inventoryService.operatingInventoryInformation(invMap);
        }
        //endregion

        return ResponseResultUtil.success(outboundVO);
    }

    /**
     * @desc : 销售出库退货编辑
     * @date : 2024/3/25 16:25
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> saleReturnOutboundUpdate(OutboundVO outboundVO) {
        //region 小编辑
        if (!outboundVO.getLimitEdit()) {
            Outbound outbound = new Outbound();
            outbound.setOutId(outboundVO.getOutId());
            outbound.setRemarks(outboundVO.getRemarks());
            outbound.setAnnexPaths(outboundVO.getAnnexPaths());
            outboundMapper.update(outbound,
                    new UpdateWrapper<Outbound>().lambda()
                            .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
            );
            for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
                OutboundItem outboundItem = new OutboundItem();
                outboundItem.setItemId(outboundItemVO.getItemId());
                if (outboundItemVO.getRemarks() != null || outboundItemVO.getRemarks() != " ") {
                    outboundItem.setRemarks(outboundItemVO.getRemarks());
                    outboundItemMapper.update(outboundItem,
                            new UpdateWrapper<OutboundItem>().lambda()
                                    .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
                    );
                }
            }
        }
        //endregion

        //region 大编辑
        else {
            //region 明细数量金额  求和
            BigDecimal sumQty = outboundVO.getItemList().stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal sumAmt = outboundVO.getItemList().stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
            //endregion

            //region  自动办理参数为true  已出库编辑
            //自动办理参数为true
            if (Constant.FlgHandleSetting.TRUE.getValue().equals(outboundVO.getFlgHandleSetting())) {
                //region 退账
                if (outboundVO.getReceivableId() != null) {
                    accountService.reverseReceivable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
                }
                //endregion

                //region  反写出库总单和明细 反写订单总单和明细
                this.rollBackTotalAndDetail(outboundVO.getItemList());
                //endregion

                //region 修改明细
                List<OutboundItemVO> itemList = outboundVO.getItemList();
                for (OutboundItemVO outboundItemVO : itemList) {

                    //根据id查询
                    OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());

                    //region 编辑明细赋值
                    if (outboundItemVO.getItemId() != null) {

                        //region 将库存需要的参数赋值
                        outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
                        outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_RETURN.getValue());
                        //编辑之前的数
                        outboundItemVO.setQtyBeforeUpdate(outboundItemResponse.getOutQty());
                        outboundItemVO.setAmtBeforeUpdate(outboundItemResponse.getOutAmt());
                        //编辑之后的数
                        outboundItemVO.setOutQty(outboundItemVO.getOutingQty());
                        outboundItemVO.setOutAmt(outboundItemVO.getOutingAmt());
                        outboundItemVO.setAddOrEditFlag(false);
                        //endregion

                        OutboundItem outboundItem = new OutboundItem();
                        outboundItem.setItemId(outboundItemVO.getItemId());
                        outboundItem.setOutQty(outboundItemVO.getOutingQty());
                        outboundItem.setOutAmt(outboundItemVO.getOutingAmt());
                        outboundItem.setCostPrice(outboundItemVO.getPriceOut());
                        outboundItem.setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(6, BigDecimal.ROUND_HALF_UP));
                        //修改
                        outboundItemMapper.update(outboundItem,
                                new UpdateWrapper<OutboundItem>().lambda()
                                        .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
                        );
                    }
                    //endregion

                    //region 新建明细
                    else {
                        outboundItemVO
                                .setOutId(outboundItemVO.getOutId())
                                .setOutQty(outboundItemVO.getOutingQty())
                                .setOutAmt(outboundItemVO.getOutingAmt())
                                .setOutingQty(BigDecimal.ZERO)
                                .setOutingAmt(BigDecimal.ZERO)
                                .setCostPrice(outboundItemVO.getPriceOut())
                                .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
                                .setOutType(Constant.OutType.SALE_RETURN.getName())
                        ;
                        //出库状态
                        String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
                        outboundItemVO.setOutStatus(outStatus);
                        //实体转换
                        OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
                        //新建
                        outboundItemMapper.insert(outboundItem);
                        outboundItemVO.setItemId(outboundItem.getItemId());

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

                    //region  反写出库明细 反写订单明细
                    this.updatePrimaryDetail(outboundItemVO, true, true);
                    //endregion
                }
                //endregion

                //region 删除明细
                if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
                    for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
                        if (outboundItemVO.getItemId() != null) {

                            //region 将库存需要的参数赋值
                            outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
                            outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_RETURN.getValue());
                            //endregion

                            OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
                            outboundItem.setFlgValid(false);
                            //修改
                            outboundItemMapper.update(outboundItem,
                                    new UpdateWrapper<OutboundItem>().lambda()
                                            .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
                            );
                        }
                    }
                    //region  反写出库总单和明细 反写订单总单和明细
                    this.rollBackTotalAndDetail(outboundVO.getDeleteItemList());
                    //endregion
                }
                //endregion

                //region  修改出库总单
                Outbound outbound = new Outbound();
                outbound.setOutId(outboundVO.getOutId());
                outbound.setOutQty(sumQty);
                outbound.setOutAmt(sumAmt);
                //修改
                outboundMapper.update(outbound,
                        new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
                );
                //endregion

                //region  反写出库总单 反写订单总单
                this.updatePrimaryTotal(outboundVO, true);
                //endregion

                //region  入账
                if (outboundVO.getReceivableId() != null) {
                    accountService.accReceivable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
                }
                //endregion

                //region 修改库存
                Map<String, Object> invMap = new HashMap<>();
                invMap.put("outDetail", outboundVO.getItemList());
                invMap.put("delOutDetail", outboundVO.getDeleteItemList());
                inventoryService.operatingInventoryInformation(invMap);
                //endregion
            } else {
                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.FLG_HANDLE_SETTING_NOT_ENABLED_OUT.getMessage());
            }
            //endregion

            //region 自动办理参数false  出库中编辑
            //自动办理标识为false 并且 自动办理参数为false  出库中
            if (!outboundVO.getFlgAutoHandle() && Constant.FlgHandleSetting.FALSE.getValue().equals(outboundVO.getFlgHandleSetting())) {

                //region  反写出库总单和明细 反写订单总单和明细
                this.rollBackTotalAndDetail(outboundVO.getItemList());
                //endregion

                //region 修改明细
                List<OutboundItemVO> itemList = outboundVO.getItemList();
                for (OutboundItemVO outboundItemVO : itemList) {

                    //region 编辑明细
                    if (outboundItemVO.getItemId() != null) {
                        OutboundItem outboundItem = new OutboundItem();
                        outboundItem.setItemId(outboundItemVO.getItemId());
                        outboundItem.setOutingQty(outboundItemVO.getOutingQty());
                        outboundItem.setOutingAmt(outboundItemVO.getOutingAmt());
                        //修改
                        outboundItemMapper.update(outboundItem,
                                new UpdateWrapper<OutboundItem>().lambda()
                                        .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
                        );
                    }
                    //endregion

                    //region 新建明细
                    else {
                        outboundItemVO
                                .setOutId(outboundItemVO.getOutId())
                                .setOutQty(BigDecimal.ZERO)
                                .setOutAmt(BigDecimal.ZERO)
                                .setCostPrice(outboundItemVO.getPriceOut())
                                .setCostAmt(outboundItemVO.getOutingQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
                                .setOutType(Constant.OutType.SALE_RETURN.getName())
                        ;
                        //出库状态
                        String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
                        outboundItemVO.setOutStatus(outStatus);
                        //实体转换
                        OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
                        //新建
                        outboundItemMapper.insert(outboundItem);
                        outboundItemVO.setItemId(outboundItem.getItemId());
                    }
                    //endregion

                    //region  反写出库明细 反写订单明细
                    this.updatePrimaryDetail(outboundItemVO, true, false);
                    //endregion
                }
                //endregion

                //region 删除明细
                if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
                    for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
                        if (outboundItemVO.getItemId() != null) {
                            OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
                            outboundItem.setFlgValid(false);
                            //修改
                            outboundItemMapper.update(outboundItem,
                                    new UpdateWrapper<OutboundItem>().lambda()
                                            .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
                            );
                        }
                    }

                    //region  反写出库总单和明细 反写订单总单和明细
                    this.rollBackTotalAndDetail(outboundVO.getItemList());
                    //endregion
                }
                //endregion

                //region  修改出库总单
                Outbound outbound = new Outbound();
                outbound.setOutId(outboundVO.getOutId());
                outbound.setOutingQty(sumQty);
                outbound.setOutingAmt(sumAmt);
                //修改
                outboundMapper.update(outbound,
                        new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
                );
                //endregion

                //region  反写出库总单 反写订单总单
                this.updatePrimaryTotal(outboundVO, true);
                //endregion
            } else {
                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.FLG_HANDLE_SETTING_NOT_ENABLED_OUT.getMessage());
            }
            //endregion
        }
        //endregion

        return ResponseResultUtil.success(outboundVO);
    }

    /**
     * @desc : 销售出库退货作废
     * @date : 2024/3/25 16:25
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> saleReturnOutboundRepeal(String outId) {
        //region  查询总单  查询明细
        //根据id查询 此条出库单的数据还未更改前的数据
        OutboundResponse outboundResponse = outboundMapper.selectById(outId);
        //根据总单id查询
        List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
        //endregion

        //region  自动办理参数为true  已出库编辑
        //自动办理参数为true
        if (Constant.FlgHandleSetting.TRUE.getValue().equals(outboundResponse.getFlgHandleSetting())) {
            //region 退账
            if (outboundResponse.getReceivableId() != null) {
                accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
            }
            //endregion

            //region  回滚原总单和明细信息（订单，出库单）
            List<OutboundItemVO> outboundItemVOList = outboundItemConvert.convertResListToVOList(outboundItemResponseList);
            this.rollBackTotalAndDetail(outboundItemVOList);
            //endregion

            //region 修改明细
            for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
                //region 将库存需要的参数赋值
                outboundItemResponse.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
                outboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.SALE_RETURN.getValue());
                outboundItemResponse.setOutQty(outboundItemResponse.getOutQty().negate());
                outboundItemResponse.setOutAmt(outboundItemResponse.getOutAmt().negate());
                //endregion

                //region 赋值明细
                OutboundItem outboundItem = new OutboundItem();
                outboundItem.setItemId(outboundItemResponse.getItemId());
                outboundItem.setFlgValid(false);
                //修改
                outboundItemMapper.update(outboundItem,
                        new UpdateWrapper<OutboundItem>().lambda()
                                .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
                );
                //endregion
            }
            //endregion

            //region  修改出库总单
            Outbound outbound = new Outbound();
            outbound.setOutId(outboundResponse.getOutId());
            outbound.setFlgValid(false);
            //修改
            outboundMapper.update(outbound,
                    new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
            );
            //endregion


            //region  修改库存
            Map<String, Object> map = new HashMap<>();
            map.put("delOutDetail", outboundItemResponseList);
            inventoryService.operatingInventoryInformation(map);
            //endregion
        } else {
            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.FLG_HANDLE_SETTING_NOT_ENABLED_OUT.getMessage());
        }
        //endregion

        //region 自动办理参数false  出库中编辑
        //自动办理标识为false 并且 自动办理参数为false  出库中
        if (!outboundResponse.getFlgAutoHandle() && Constant.FlgHandleSetting.FALSE.getValue().equals(outboundResponse.getFlgHandleSetting())) {

            //region  回滚原总单和明细信息（订单，出库单）
            List<OutboundItemVO> outboundItemVOList = outboundItemConvert.convertResListToVOList(outboundItemResponseList);
            this.rollBackTotalAndDetail(outboundItemVOList);
            //endregion

            //region 修改明细
            for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
                //region 赋值明细
                OutboundItem outboundItem = new OutboundItem();
                outboundItem.setItemId(outboundItemResponse.getItemId());
                outboundItem.setOutingQty(BigDecimal.ZERO);
                outboundItem.setOutingAmt(BigDecimal.ZERO);
                outboundItem.setFlgValid(false);
                //修改
                outboundItemMapper.update(outboundItem,
                        new UpdateWrapper<OutboundItem>().lambda()
                                .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
                );
                //endregion

            }
            //endregion

            //region  修改出库总单
            Outbound outbound = new Outbound();
            outbound.setOutId(outboundResponse.getOutId());
            outbound.setOutingQty(BigDecimal.ZERO);
            outbound.setOutingAmt(BigDecimal.ZERO);
            outbound.setFlgValid(false);
            //修改
            outboundMapper.update(outbound,
                    new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
            );
            //endregion
        } else {
            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.FLG_HANDLE_SETTING_NOT_ENABLED_OUT.getMessage());
        }
        //endregion

        return ResponseResultUtil.success();
    }

    /**
     * @desc : 销售退货出库办理
     * @date : 2024/3/7 15:47
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> saleReturnHandleOutbound(OutboundVO outboundVO) {
        //校验明细
        if (outboundVO.getItemList().size() == 0) {
            throw new BaseBusinessException(ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getCode(),
                    ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getMessage());
        }

        //region 退账
        if (outboundVO.getReceivableId() != null) {
            accountService.reverseReceivable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
        }
        //endregion

        //region 查询出库总单数据信息
        OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
        //endregion

        //region  回滚原总单和明细信息（订单，出库单）
        List<OutboundItemVO> outboundItemVOList = new ArrayList<>();
        for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
            if (outboundItemVO.getItemId() != null) {
                OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());
                OutboundItemVO outboundItemVO1 = outboundItemConvert.convertResToVO(outboundItemResponse);
                outboundItemVOList.add(outboundItemVO1);
            }
        }
        this.rollBackTotalAndDetail(outboundItemVOList);
        //endregion

        //region 编辑明细
        for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
            //endregion

            //查询原单
            OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());
            //region 校验数量是否超出
            if (outboundItemVO.getOutingQty().compareTo(outboundItemResponse.getOutingQty()) > 0) {
                throw new BaseBusinessException(ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITYIN_THE_WAREHOUSE.getCode(),
                        ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITYIN_THE_WAREHOUSE.getMessage());
            }
            //endregion

            //region 编辑明细
            if (outboundItemVO.getItemId() != null) {
                outboundItemVO
                        .setOutQty(outboundItemResponse.getOutQty().add(outboundItemVO.getOutingQty()))
                        .setOutAmt(outboundItemResponse.getOutAmt().add(outboundItemVO.getOutingAmt()))
                        .setOutingQty(outboundItemResponse.getOutingQty().subtract(outboundItemVO.getOutingQty()))
                        .setOutingAmt(outboundItemResponse.getOutingAmt().subtract(outboundItemVO.getOutingAmt()))
                        .setCostPrice(outboundItemVO.getPriceOut())
                        .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
                ;
                //出库状态
                String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
                outboundItemVO.setOutStatus(outStatus);
                //实体转换
                OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
                //修改
                outboundItemMapper.update(outboundItem,
                        new UpdateWrapper<OutboundItem>().lambda()
                                .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
                );
                //region 将库存需要的参数赋值
                outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
                outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_RETURN.getValue());
                //编辑之前的数
                if (outboundItemResponse.getOutQty().compareTo(BigDecimal.ZERO) > 0) {
                    outboundItemVO.setQtyBeforeUpdate(outboundItemResponse.getOutQty());
                    outboundItemVO.setAmtBeforeUpdate(outboundItemResponse.getOutAmt());
                }
                outboundItemVO.setAddOrEditFlag(true);
                //endregion
            }
            //endregion

            //region  新建明细
            else {
                outboundItemVO
                        .setOutId(outboundItemVO.getOutId())
                        .setOutQty(outboundItemVO.getOutingQty())
                        .setOutAmt(outboundItemVO.getOutingAmt())
                        .setCostPrice(outboundItemVO.getPriceOut())
                        .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
                        .setOutType(Constant.OutType.SALE_RETURN.getName())
                        .setOutingQty(BigDecimal.ZERO)
                        .setOutingAmt(BigDecimal.ZERO)
                ;
                //出库状态
                String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
                outboundItemVO.setOutStatus(outStatus);
                //实体转换
                OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
                //新建
                outboundItemMapper.insert(outboundItem);
                outboundItemVO.setItemId(outboundItem.getItemId());
                //region 将库存需要的参数赋值
                outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
                outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_RETURN.getValue());
                outboundItemVO.setAddOrEditFlag(true);
                //endregion
            }
            //endregion

            //region  反写出库明细 反写订单明细
            this.updatePrimaryDetail(outboundItemVO, true, true);
            //endregion
        }
        //endregion

        //region 删除明细
        if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
            for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
                if (outboundItemVO.getItemId() != null) {

                    //region 将库存需要的参数赋值
                    outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
                    outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_RETURN.getValue());
                    //endregion

                    OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
                    outboundItem.setFlgValid(false);
                    //修改
                    outboundItemMapper.update(outboundItem,
                            new UpdateWrapper<OutboundItem>().lambda()
                                    .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
                    );
                }
            }
            //region  回滚原总单和明细信息（订单，出库单）
            this.rollBackTotalAndDetail(outboundVO.getDeleteItemList());
            //endregion
        }
        //endregion

        //region 编辑总单
        BigDecimal sumOutQty = outboundVO.getItemList().stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
        BigDecimal sumOutAmt = outboundVO.getItemList().stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
        outboundVO.setOutQty(sumOutQty);
        outboundVO.setOutAmt(sumOutAmt);
        outboundVO.setOutingQty(BigDecimal.ZERO);
        outboundVO.setOutingAmt(BigDecimal.ZERO);
        //出库状态
        String outStatus = this.setOutStatus(outboundVO.getOutingQty(), outboundVO.getOutQty());
        outboundVO.setOutStatus(outStatus);
        //实体转换
        Outbound outbound = outboundConvert.convertToPo(outboundVO);
        //修改
        outboundMapper.update(outbound,
                new UpdateWrapper<Outbound>().lambda()
                        .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
        );
        //endregion

        //region  反写出库总单 反写订单总单
        this.updatePrimaryTotal(outboundVO, true);
        //endregion

        //region 入账
        accountService.accReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
        //endregion

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

        return ResponseResultUtil.success(outboundVO);
    }

    /**
     * @desc : 销售退货出库撤销
     * @date : 2024/3/7 17:06
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> saleReturnOutboundCancel(OutboundVO outboundVO) {
        //region 查询出库总单数据信息
        OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
        //endregion

        //region    退账
        if (outboundResponse.getReceivableId() != null) {
            accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
        }

        //region 修改总单数据信息
        Outbound outbound = new Outbound();
        outbound.setOutId(outboundVO.getOutId());
        outbound.setOutDate(null);
        outbound.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
        outbound.setOutQty(BigDecimal.ZERO);
        outbound.setOutAmt(BigDecimal.ZERO);
        outbound.setOutingQty(outboundResponse.getOutQty());
        outbound.setOutingAmt(outboundResponse.getOutAmt());
        //修改
        outboundMapper.update(outbound,
                new UpdateWrapper<Outbound>().lambda()
                        .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
        );
        //endregion

        //region 明细数据
        //根据总单id查明细
        List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundVO.getOutId()));
        for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
            //region 将库存需要的参数赋值
            outboundItemResponse.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
            outboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.SALE_RETURN.getValue());
            outboundItemResponse.setOutQty(outboundItemResponse.getOutQty().negate());
            outboundItemResponse.setOutAmt(outboundItemResponse.getOutAmt().negate());
            //endregion

            //region修改出库明细信息
            OutboundItem outboundItem = new OutboundItem();
            outboundItem
                    .setOutId(outboundResponse.getOutId())
                    .setOutStatus(Constant.OutStatus.CHUKUZHONG.getName())
                    .setOutingQty(outboundItemResponse.getOutingQty())
                    .setOutAmt(outboundItemResponse.getOutingAmt())
                    .setOutQty(BigDecimal.ZERO)
                    .setOutAmt(BigDecimal.ZERO)
                    .setCostPrice(BigDecimal.ZERO)
                    .setCostAmt(BigDecimal.ZERO)
                    .setItemId(outboundItemResponse.getItemId());
            //修改
            outboundItemMapper.update(outboundItem,
                    new UpdateWrapper<OutboundItem>().lambda()
                            .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
            );
            //endregion
        }
        //endregion

        //region  回滚原总单和明细信息（订单，出库单）
        List<OutboundItemVO> outboundItemVOList = outboundItemConvert.convertResListToVOList(outboundItemResponseList);
        this.rollBackTotalAndDetail(outboundItemVOList);
        //endregion

        //region   调用库存
        Map<String, Object> map = new HashMap<>();
        map.put("outDetail", outboundItemResponseList);
        inventoryService.operatingInventoryInformation(map);
        //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/3/9 8:59
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public String setOutStatus(BigDecimal outingQty, BigDecimal outQty) {
        //出库状态
        String outStatus = null;
        //已出库数量<0 出库中数量<0
        if (outQty.compareTo(BigDecimal.ZERO) <= 0 && outingQty.compareTo(BigDecimal.ZERO) < 0) {
            //出库中
            outStatus = Constant.OutStatus.CHUKUZHONG.getName();
        }
        //出入库数量=0 出库中数量=0
        else if (outQty.compareTo(BigDecimal.ZERO) == 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
            //待出库
            outStatus = Constant.OutStatus.DAICHUKU.getName();
        }
        //已出库数量<0 出库中数量=0
        else if (outQty.compareTo(BigDecimal.ZERO) < 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
            //已出库
            outStatus = Constant.OutStatus.YICHUKU.getName();
        }
        return outStatus;
    }

    /********************************************** 销售退货查询相关方法begin *************************************/
    /**
     * @desc : 一览页销售退货
     * @author : 付斌
     * @date : 2023/1/9 10:40
     */
    @Pagination
    public ResponseResultVO<PageList<OutboundResponse>> selectByCond(OutboundQuery outboundQuery) {
        outboundQuery.setOutType(Constant.OutType.SALE_RETURN.getName());
        return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
                outboundMapper.countByCond(outboundQuery));
    }

    /**
     * @desc : 一览页销售退货明细（货物、附件）
     * @author : 付斌
     * @date : 2024-02-28 13:25
     */
    @Pagination
    public ResponseResultVO<Map<String, Object>> selectOutboundInfoById(String id) {
        Map<String, Object> result = new HashMap<>();
        // 商品明细
        List<OutboundItemResponse> outboundItem = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
        result.put("outboundItem", outboundItem);

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

    /**
     * @desc : 获取销售退货信息（编辑用）
     * @author : 付斌
     * @date : 2024-03-02 17:27
     */
    public ResponseResultVO<?> getOutReturnForUpdate(String id) {
        Map<String, Object> dataInfo = new HashMap<>();
        OutboundResponse outboundResponse = outboundMapper.selectById(id);
        dataInfo.put("data", outboundResponse);

        // 商品明细
        List<OutboundItemResponse> outboundItemResponse = outboundItemMapper.selectByCondForReturnEdit(id);
        dataInfo.put("dataItem", outboundItemResponse);
        return ResponseResultUtil.success(dataInfo);
    }
    /********************************************** 销售退货查询相关方法end *************************************/

//    /**
//     * @desc : 条件查询
//     * @date : 2024/3/18 11:20
//     * @author : 寇珊珊
//     */
//    @Pagination
//    public ResponseResultVO<PageList<InboundResponse>> selectByCond(OutboundQuery outboundQuery) {
//        return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
//                outboundMapper.countByCond(outboundQuery));
//    }
//
//    /**
//     * @desc : 查询明细
//     * @date : 2024/3/15 16:43
//     * @author : 寇珊珊
//     */
//    @Pagination
//    public ResponseResultVO<Map<String, Object>> selectOutBoundSaleReturnItemInfoById(String id) {
//        Map<String, Object> result = new HashMap<>();
//        // 商品明细
//        List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
//        result.put("itemList", outboundItemResponseList);
//        // 收款
//
//        // 附件
//        return ResponseResultUtil.success(result);
//    }
//
//    /**
//     * @desc : 获取单据信息（编辑用）
//     * @date : 2024/3/16 16:28
//     * @author : 寇珊珊
//     */
//    public ResponseResultVO<?> selectByUpdate(String id) {
//        Map<String, Object> dataInfo = new HashMap<>();
//        //总单
//        OutboundResponse outboundResponse = outboundMapper.selectMessageByOtherQuery(new OutboundQuery().setOutId(id).setOutStatus(Constant.OutStatus.CHUKUZHONG.getName()));
//        //单据不存在
//        if (outboundResponse == null) {
//            throw new BaseBusinessException(ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getCode(),
//                    ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getMessage());
//        }
//        dataInfo.put("data", outboundResponse);
//        // 明细
//        List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
//        dataInfo.put("dataItem", outboundItemResponseList);
//        return ResponseResultUtil.success(dataInfo);
//    }


}
