package com.dk.mdm.service.ivt;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.mapper.common.CommonMapper;
import com.dk.mdm.mapper.ivt.InboundItemMapper;
import com.dk.mdm.mapper.ivt.InboundMapper;
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.ivt.IntoReturn;
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.IntoReturnItemQuery;
import com.dk.mdm.model.query.ivt.IntoReturnQuery;
import com.dk.mdm.model.response.ivt.InboundItemResponse;
import com.dk.mdm.model.response.ivt.InboundResponse;
import com.dk.mdm.model.response.ivt.IntoReturnItemResponse;
import com.dk.mdm.model.response.ivt.IntoReturnResponse;
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.model.vo.ivt.IntoReturnVO;
import com.dk.mdm.service.common.CommonService;
import com.dk.mdm.service.ivt.inbound.InboundPurchaseReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service("inboundReturn")
@Transactional
public class InboundReturnService extends BaseService<Inbound> {

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

    @Resource
    private InboundMapper inboundMapper;

    @Autowired
    private InboundItemMapper inboundItemMapper;

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private PurchaseItemMapper purchaseItemMapper;

    @Autowired
    private CommonService commonService;

    @Autowired
    private InboundConvert inboundConvert;

    @Autowired
    private InboundItemConvert inboundItemConvert;
    @Autowired
    private InboundPurchaseReturnService inboundPurchaseReturnService;

    @Autowired
    private CommonMapper commonMapper;

    @Pagination
    public ResponseResultVO<PageList<InboundResponse>> selectByCond(InboundQuery inboundQuery) {
        return super.mergeListWithCount(inboundQuery, inboundMapper.selectByInboundReturnCond(inboundQuery), inboundMapper.countByInboundReturnCond(inboundQuery));
    }

    /**
     * @desc : 查看来源单据,总单加明细  采购退货用
     * @author : 于继渤
     * @date : 2024/3/6 10:36
     */
    public ResponseResultVO<?> selectInboundReturnById(String intoId) {
        //根据id查询
        InboundResponse inboundResponse = inboundMapper.selectInboundReturnById(intoId);
        //不存在
        if (inboundResponse == null) {
            return ResponseResultUtil.error(ErrorCodeEnum.INBOUND_CANNOT_BE_EMPTY.getCode(),
                    ErrorCodeEnum.INBOUND_CANNOT_BE_EMPTY.getMessage());
        }
        //查询明细
        List<InboundItemResponse> inboundItemResponselist = inboundItemMapper.selectByInboundReturnCond(new InboundItemQuery().setIntoId(intoId).setFlgValid(true));
        inboundResponse.setGoodsList(inboundItemResponselist);
        return ResponseResultUtil.success(inboundResponse);
    }

    /**
     * @desc : 新建采购退货
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> insert(InboundVO inboundVO) {
        //校验明细长度
        List<InboundItemVO> itemList = inboundVO.getItemList();
        //明细不存在
        if (itemList == null || itemList.size() == 0) {
            //返回 采购退货明细不可为空，请重新操作
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.PURCHASE_RETURN_DETAIL_NOT_EXIST.getMessage());
        }
        inboundVO.setReturnQty(inboundVO.getReturnQty().abs());
        inboundVO.setReturnAmt(inboundVO.getReturnAmt().abs());
        if (inboundVO.getFromId() != null) {
            //查询采购订单
            PurchaseResponse purchaseResponse = purchaseMapper.selectById(inboundVO.getFromId()); //采购订单id


            //反写采购订单已入库退货数量、金额
            purchaseMapper.updateAmount(new Purchase().setPurId(inboundVO.getFromId())
                    .setReturnQty(purchaseResponse.getReturnQty().add(inboundVO.getReturnQty()))
                    .setReturnAmt(purchaseResponse.getReturnAmt().add(inboundVO.getReturnAmt()))
            );
        }


        //反写入库订单明细、反写订单明细
        for (InboundItemVO inboundItemVO : itemList) {

            inboundItemVO.setReturnQty(inboundItemVO.getReturnQty().abs());
            inboundItemVO.setReturnAmt(inboundItemVO.getReturnAmt().abs());
            if (inboundItemVO.getItemId() != null) {
                //根据退货明细中入库明细id查询入库明细数据
                InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(inboundItemVO.getItemId()); //原入库单明细id
                BigDecimal returnQty = inboundItemResponse.getReturnQty().add(inboundItemVO.getReturnQty().setScale(6, BigDecimal.ROUND_HALF_UP));
                BigDecimal returnAmt = inboundItemResponse.getReturnAmt().add(inboundItemVO.getReturnAmt().setScale(6, BigDecimal.ROUND_HALF_UP));

                //反写入库单退货数量金额
                InboundResponse inboundResponse = inboundMapper.selectById(inboundItemVO.getIntoId()); //原入库单id
                inboundMapper.updateAmount(new Inbound().setIntoId(inboundItemVO.getIntoId())
                        .setReturnQty(inboundResponse.getReturnQty().add(returnQty))
                        .setReturnAmt(inboundResponse.getReturnAmt().add(returnAmt)));
                inboundItemMapper.updateAmount(
                        new InboundItem()
                                .setItemId(inboundItemResponse.getItemId())
                                .setReturnQty(returnQty)
                                .setReturnAmt(returnAmt)
                );
            }
            if (inboundItemVO.getFromItemId() != null) {
                //反写订单出库明细 退货数量和退货金额
                //查询采购订单明细数据
                PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getFromItemId());
                //反写订单明细退货数量和退货金额
                purchaseItemMapper.updateAmount(
                        new PurchaseItem()
                                .setItemId(inboundItemVO.getFromItemId())
                                .setReturnQty(purchaseItemResponse.getReturnQty().add(inboundItemVO.getReturnQty().setScale(6, BigDecimal.ROUND_HALF_UP)))
                                .setReturnAmt(purchaseItemResponse.getReturnAmt().add(inboundItemVO.getReturnAmt().setScale(6, BigDecimal.ROUND_HALF_UP)))
                );
            }

        }
        inboundVO.setIntoingQty(inboundVO.getReturnQty().multiply(new BigDecimal(-1)));
        inboundVO.setIntoingAmt(inboundVO.getReturnAmt().multiply(new BigDecimal(-1)));
        List<InboundItemVO> list = new ArrayList<>();
        for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
            //源入库单ID（采购退货时记录）
            inboundItemVO.setSIntoId(inboundItemVO.getIntoId());
            //源入库单明细ID（采购退货时记录）
            inboundItemVO.setSIntoItemId(inboundItemVO.getItemId());
            //源采购订单ID（采购退货时记录）
            inboundItemVO.setSPurId(inboundItemVO.getFromId());
            //源采购明细ID（采购退货时记录）
            inboundItemVO.setSPurItemId(inboundItemVO.getFromItemId());
            inboundItemVO.setIntoingQty(inboundItemVO.getReturnQty());
            inboundItemVO.setIntoingAmt(inboundItemVO.getReturnAmt());
            inboundItemVO.setPriceInto(inboundItemVO.getPriceInto().abs());
            inboundItemVO.setFromItemId(inboundItemVO.getItemId());
            inboundItemVO.setSkuId(inboundItemVO.getSkuId());
            inboundItemVO.setItemIndex(inboundItemVO.getItemIndex());
            inboundItemVO.setNonStdCode(inboundItemVO.getNonStdCode());
            inboundItemVO.setReturnQty(inboundItemVO.getReturnQty().multiply(new BigDecimal(-1)));
            inboundItemVO.setReturnAmt(inboundItemVO.getReturnAmt().multiply(new BigDecimal(-1)));

            inboundItemVO.setItemId(null);
            list.add(inboundItemVO);
        }

        inboundVO.setItemList(list);
        inboundPurchaseReturnService.purchaseReturnInboundInsert(inboundVO);
        return ResponseResultUtil.success();
    }

    /**
     * @desc : 编辑
     * @date : 2022/6/28 16:58
     * @author : 于继渤
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<String> update(InboundVO inboundVO) {
        inboundVO.setReturnQty(inboundVO.getReturnQty().abs());
        inboundVO.setReturnAmt(inboundVO.getReturnAmt().abs());
        //查询编辑之前的采退单
        InboundResponse inboundReturnResponse = inboundMapper.selectById(inboundVO.getIntoId());
        inboundReturnResponse.setReturnQty(inboundReturnResponse.getReturnQty().abs());
        inboundReturnResponse.setReturnAmt(inboundReturnResponse.getReturnAmt().abs());

        inboundVO.setReturnAmt(inboundVO.getReturnAmt().abs());
        if(inboundVO.getFromId() != null ){
            //查询采购订单
            PurchaseResponse purchaseResponse = purchaseMapper.selectById(inboundVO.getFromId());
            //采购订单原始退货数量 - 原始退货单退货数量
            BigDecimal purchaseReturnQty = purchaseResponse.getReturnQty().subtract(inboundReturnResponse.getReturnQty());
            BigDecimal purchaseReturnAmt = purchaseResponse.getReturnAmt().subtract(inboundReturnResponse.getReturnAmt());
            //反写采购订单已入库退货数量、金额
            purchaseMapper.updateAmount(new Purchase().setPurId(inboundVO.getFromId())
                    .setReturnQty(purchaseReturnQty.add(inboundVO.getReturnAmt()))
                    .setReturnAmt(purchaseReturnAmt.add(inboundVO.getReturnQty()))
            );
        }

        //校验明细
        if (inboundVO.getItemList() != null && inboundVO.getItemList().size() > 0) {
            for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
                inboundItemVO.setReturnQty(inboundItemVO.getReturnQty().abs());
                inboundItemVO.setReturnAmt(inboundItemVO.getReturnAmt().abs());
                if(inboundItemVO.getSIntoItemId() != null ){
                    InboundItemResponse inboundItemReturnResponse = inboundItemMapper.selectById(inboundItemVO.getSIntoItemId());
                    inboundItemReturnResponse.setReturnQty(inboundItemReturnResponse.getReturnQty().abs());
                    inboundItemReturnResponse.setReturnAmt(inboundItemReturnResponse.getReturnAmt().abs());


                    //查询采购订单明细数据
                    PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getSPurItemId());
                    BigDecimal purchaseItemReturnQty = purchaseItemResponse.getReturnQty().subtract(inboundItemReturnResponse.getReturnQty());
                    BigDecimal purchaseItemReturnAmt = purchaseItemResponse.getReturnAmt().subtract(inboundItemReturnResponse.getReturnAmt());
                    //反写采购订单明细退货数量和退货金额
                    purchaseItemMapper.updateAmount(
                            new PurchaseItem()
                                    .setItemId(inboundItemVO.getSPurItemId())
                                    .setReturnQty(purchaseItemReturnQty.add(inboundItemVO.getReturnQty().setScale(6, BigDecimal.ROUND_HALF_UP)))
                                    .setReturnAmt(purchaseItemReturnAmt.add(inboundItemVO.getReturnAmt().setScale(6, BigDecimal.ROUND_HALF_UP)))
                    );
                    //根据退货明细中入库明细id查询入库明细数据
                    InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(inboundItemVO.getSIntoItemId());
                    BigDecimal inboundItemReturnQty = inboundItemResponse.getReturnQty().subtract(inboundItemReturnResponse.getReturnQty());
                    BigDecimal inboundItemReturnAmt = inboundItemResponse.getReturnAmt().subtract(inboundItemReturnResponse.getReturnAmt());


                    BigDecimal ReturnQty = inboundItemReturnQty.add(inboundItemVO.getReturnQty().setScale(6, BigDecimal.ROUND_HALF_UP));
                    BigDecimal ReturnAm = inboundItemReturnAmt.add(inboundItemVO.getReturnAmt().setScale(6, BigDecimal.ROUND_HALF_UP));
                    //反写原始入库单明细 不是负向的入库单
                    inboundItemMapper.updateAmount(
                            new InboundItem()
                                    .setItemId(inboundItemResponse.getItemId())
                                    .setReturnQty(ReturnQty)
                                    .setReturnAmt(ReturnAm)
                    );
                    //反写原始入库单
                    InboundResponse inboundResponse = inboundMapper.selectById(inboundItemVO.getSIntoId());
                    BigDecimal inboundReturnQty = inboundResponse.getReturnQty().subtract(inboundItemReturnResponse.getReturnQty());
                    BigDecimal inboundReturnAmt = inboundResponse.getReturnAmt().subtract(inboundItemReturnResponse.getReturnAmt());
                    inboundMapper.updateAmount(new Inbound().setIntoId(inboundItemVO.getSIntoId())
                            .setReturnQty(inboundReturnQty.add(inboundItemVO.getReturnQty()))
                            .setReturnAmt(inboundReturnAmt.add(inboundItemVO.getReturnAmt())));
                }




                //修改采退明细
                InboundItem inboundItem = new InboundItem();
                inboundItem.setItemId(inboundItemVO.getItemId());
                inboundItem.setReturnQty(inboundItemVO.getReturnQty().multiply(new BigDecimal(-1)));
                inboundItem.setReturnAmt(inboundItemVO.getReturnAmt().multiply(new BigDecimal(-1)));
                //修改
                inboundItemMapper.update(inboundItem,
                        new UpdateWrapper<InboundItem>().lambda()
                                .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                );
            }
        }

        //删除 采退入库明细
        if (inboundVO.getDeleteItemList() != null && inboundVO.getDeleteItemList().size() > 0) {
            for (InboundItemVO inboundItemVO : inboundVO.getDeleteItemList()) {
                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()))
                    );
                }
            }
        }

        //修改采退总单
        Inbound inbound = new Inbound();
        inbound.setIntoId(inboundVO.getIntoId());
        inbound.setReturnQty(inboundVO.getReturnQty().multiply(new BigDecimal(-1)));
        inbound.setReturnAmt(inboundVO.getReturnAmt().multiply(new BigDecimal(-1)));
        //修改
        inboundMapper.update(inbound,
                new UpdateWrapper<Inbound>().lambda().eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
        );


        inboundPurchaseReturnService.purchaseReturnInboundUpdate(inboundVO);
        return ResponseResultUtil.success();
    }

    /**
     * @desc : 作废
     * @date : 2022/6/28 16:58
     * @author : 于继渤
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<String> cancel(InboundVO inboundVO) {
        //查询当前 采退入库单 负
        InboundResponse inboundResponse = inboundMapper.selectInboundReturnById(inboundVO.getIntoId());
        if (inboundResponse.getIntoStatus().equals(Constant.IntoStatus.ZUOFEI.getName())) {
            //当前单据已经作废，不能重复操
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
        }
        //校验单据是否可以作废


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

        //TODO  //系统参数自动出入库办理 已出库（未退款）可以作废,已出库（已退款）不可以作废
        if (inboundResponse.getFlgHandleSetting() && inboundResponse.getIntoStatus().equals(Constant.IntoStatus.YIRUKU.getName())) {
            return ResponseResultUtil.error(ErrorCodeEnum.NO_WAIT_INTO_RETURN_CANCEL.getCode(),
                    ErrorCodeEnum.NO_WAIT_INTO_RETURN_CANCEL.getMessage());
        }
        //系统参数手动出入库办理 出库中，可以作废,已出库（未收款、已收款），不可以作废
        if (!inboundResponse.getFlgHandleSetting() && inboundResponse.getIntoStatus().equals(Constant.IntoStatus.YIRUKU.getName())) {
            return ResponseResultUtil.error(ErrorCodeEnum.NO_WAIT_INTO_RETURN_CANCEL.getCode(),
                    ErrorCodeEnum.NO_WAIT_INTO_RETURN_CANCEL.getMessage());
        }
        if(inboundResponse.getFromId()!=null){
            //查询采购单
            PurchaseResponse purchaseResponse = purchaseMapper.selectById(inboundResponse.getFromId());
            if (purchaseResponse == null) {
                //返回 单据所对应的原始单据不存在，请重新操作
                return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ORIGINAL_DOCUMENT_ABSENT.getMessage());
            }
            //修改采购单 已入库数量/金额
            //TODO 如果 退货存的 退货数量是负数的话
            System.out.println(purchaseResponse.getReturnQty().subtract(inboundResponse.getReturnQty()));

            purchaseMapper.updateAmount(
                    new Purchase().setReturnQty(purchaseResponse.getReturnQty().subtract(inboundResponse.getReturnQty().abs()))
                            .setReturnAmt(purchaseResponse.getReturnAmt().subtract(inboundResponse.getReturnAmt().abs())).setPurId(purchaseResponse.getPurId())
            );
        }

        //查询退货明细
        List<InboundItemResponse> inboundItemResponses = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(inboundResponse.getIntoId()));
        for (InboundItemResponse inboundItemRespons : inboundItemResponses) {
            if(inboundItemRespons.getSIntoItemId() != null){
                //查询源入库单明细
                InboundItemResponse inboundItem = inboundItemMapper.selectById(inboundItemRespons.getSIntoItemId());
                if (inboundItem == null) {
                    //返回 单据所对应的原始单据不存在，请重新操作
                    return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ORIGINAL_DOCUMENT_ABSENT.getMessage());
                }
                //修改入库单明细 已入库退货数量/金额
                inboundItemMapper.updateAmount(new InboundItem()
                        .setItemId(inboundItem.getItemId())
                        .setReturnQty(inboundItem.getReturnQty().subtract(inboundItemRespons.getReturnQty().abs()))
                        .setReturnAmt(inboundItem.getReturnAmt().subtract(inboundItemRespons.getReturnAmt().abs()))
                );
            }

            if(inboundItemRespons.getSIntoId() != null){
                //查询源入库单 修改 已入库退货数量/金额
                InboundResponse inboundRes = inboundMapper.selectById(inboundItemRespons.getSIntoId());
                if (inboundRes == null) {
                    //返回 单据所对应的原始单据不存在，请重新操作
                    return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ORIGINAL_DOCUMENT_ABSENT.getMessage());
                }
                inboundMapper.updateAmount(new Inbound().setIntoId(inboundRes.getIntoId())
                        .setReturnQty(inboundRes.getReturnQty().subtract(inboundItemRespons.getReturnQty().abs()))
                        .setReturnAmt(inboundRes.getReturnAmt().subtract(inboundItemRespons.getReturnAmt().abs())));
            }


            if(inboundItemRespons.getSPurItemId()!=null){
                //查询采购单明细
                PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemRespons.getSPurItemId());
                if (purchaseItemResponse == null) {
                    //返回 单据所对应的原始单据不存在，请重新操作
                    return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ORIGINAL_DOCUMENT_ABSENT.getMessage());
                }
                //修改采购单明细 已入库退货数量/金额
                purchaseItemMapper.updateAmount(
                        new PurchaseItem()
                                .setItemId(purchaseItemResponse.getItemId())
                                .setReturnQty(purchaseItemResponse.getReturnQty().subtract(inboundItemRespons.getReturnQty().abs()))
                                .setReturnAmt(purchaseItemResponse.getReturnAmt().subtract(inboundItemRespons.getReturnAmt().abs()))
                );
            }


        }
        //修改 负向入库单 状态为作废
        inboundMapper.updateIntoStatus(new Inbound()
                .setIntoId(inboundVO.getIntoId())
                .setIntoStatus(Constant.IntoStatus.ZUOFEI.getName()));
        //修改 负向入库单明细 状态为作废
        if (inboundItemResponses != null && inboundItemResponses.size() > 0) {
            for (InboundItemResponse inboundItemRespons : inboundItemResponses) {
                inboundItemMapper.updateIntoStatus(new InboundItem().setItemId(inboundItemRespons.getItemId()).setIntoStatus(Constant.IntoStatus.ZUOFEI.getName()));
            }
        }
        inboundPurchaseReturnService.purchaseReturnInboundRepeal(inboundVO.getIntoId());
        return ResponseResultUtil.success();
    }


    /**
     * @desc : 办理
     * @date : 2022/6/28 16:58
     * @author : 于继渤
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<String> returnHandle(InboundVO inboundVO) {
        //校验明细长度
        List<InboundItemVO> itemList = inboundVO.getItemList();
        //明细不存在
        if (itemList == null || itemList.size() == 0) {
            //返回 采购退货明细不可为空，请重新操作
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.PURCHASE_RETURN_DETAIL_NOT_EXIST.getMessage());
        }
        inboundVO.setReturnQty(inboundVO.getReturnQty().abs());
        inboundVO.setReturnAmt(inboundVO.getReturnAmt().abs());
        if(inboundVO.getFromId() != null){
            //查询采购订单
            PurchaseResponse purchaseResponse = purchaseMapper.selectById(inboundVO.getFromId()); //采购订单id

            //反写采购订单已入库退货数量、金额
            purchaseMapper.updateAmount(new Purchase().setPurId(inboundVO.getFromId())
                    .setReturnQty(purchaseResponse.getReturnQty().add(inboundVO.getReturnQty()))
                    .setReturnAmt(purchaseResponse.getReturnAmt().add(inboundVO.getReturnAmt()))
            );
        }


        //反写入库订单明细、反写订单明细
        for (InboundItemVO inboundItemVO : itemList) {
            inboundItemVO.setReturnQty(inboundItemVO.getReturnQty().abs());
            inboundItemVO.setReturnAmt(inboundItemVO.getReturnAmt().abs());
            //根据退货明细中入库明细id查询入库明细数据
            if(inboundItemVO.getSIntoItemId() != null){
                InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(inboundItemVO.getSIntoItemId()); //原入库单明细id
                BigDecimal returnQty = inboundItemResponse.getReturnQty().add(inboundItemVO.getReturnQty().setScale(6, BigDecimal.ROUND_HALF_UP));
                BigDecimal returnAmt = inboundItemResponse.getReturnAmt().add(inboundItemVO.getReturnAmt().setScale(6, BigDecimal.ROUND_HALF_UP));

                //反写入库单退货数量金额
                InboundResponse inboundResponse = inboundMapper.selectById(inboundItemVO.getSIntoId()); //原入库单id
                inboundMapper.updateAmount(new Inbound().setIntoId(inboundItemVO.getSIntoId())
                        .setReturnQty(inboundResponse.getReturnQty().add(returnQty))
                        .setReturnAmt(inboundResponse.getReturnAmt().add(returnAmt)));
                inboundItemMapper.updateAmount(
                        new InboundItem()
                                .setItemId(inboundItemResponse.getSIntoItemId())
                                .setReturnQty(returnQty)
                                .setReturnAmt(returnAmt)
                );
            }
            if(inboundItemVO.getSPurItemId() != null){
                //反写订单出库明细 退货数量和退货金额
                //查询采购订单明细数据
                PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getSPurItemId());
                //反写订单明细退货数量和退货金额
                purchaseItemMapper.updateAmount(
                        new PurchaseItem()
                                .setItemId(inboundItemVO.getSPurItemId())
                                .setReturnQty(purchaseItemResponse.getReturnQty().add(inboundItemVO.getReturnQty().setScale(6, BigDecimal.ROUND_HALF_UP)))
                                .setReturnAmt(purchaseItemResponse.getReturnAmt().add(inboundItemVO.getReturnAmt().setScale(6, BigDecimal.ROUND_HALF_UP)))
                );
            }


            inboundItemVO.setIntoingQty(inboundItemVO.getReturnQty().abs());
            inboundItemVO.setIntoingAmt(inboundItemVO.getReturnAmt().abs());
            inboundItemVO.setReturnQty(inboundItemVO.getReturnQty().multiply(new BigDecimal(-1)));
            inboundItemVO.setReturnAmt(inboundItemVO.getReturnAmt().multiply(new BigDecimal(-1)));
        }

        inboundVO.setReturnQty(inboundVO.getReturnQty().multiply(new BigDecimal(-1)));
        inboundVO.setReturnAmt(inboundVO.getReturnAmt().multiply(new BigDecimal(-1)));
        //调用办理
        inboundPurchaseReturnService.saleReturnHandleInbound(inboundVO);
        return ResponseResultUtil.success();
    }

    /**
     * @desc : 采购退货入库撤销
     * @date : 2022/6/28 16:58
     * @author : 于继渤
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<String> inboundReturnCancel(InboundVO inboundVO) {
        inboundPurchaseReturnService.saleReturnInboundCancel(inboundVO);
        return ResponseResultUtil.success();
    }

    public static void main(String[] args) {
        BigDecimal a = new BigDecimal(-1000);
        BigDecimal b = new BigDecimal(-990);
        System.out.println(a.subtract(b));
    }

}
