package com.dk.mdm.service.ivt;

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.model.pojo.PageList;
import com.dk.common.response.ResponseCodeEnum;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.ivt.IntoReturnConvert;
import com.dk.mdm.infrastructure.convert.ivt.IntoReturnItemConvert;
import com.dk.mdm.infrastructure.util.AuthUtils;
import com.dk.mdm.mapper.ivt.InboundItemMapper;
import com.dk.mdm.mapper.ivt.InboundMapper;
import com.dk.mdm.mapper.ivt.IntoReturnItemMapper;
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.mapper.ivt.IntoReturnMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.pojo.ivt.IntoReturnItem;
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.*;
import com.dk.mdm.service.common.CommonService;
import com.dk.mdm.service.ivt.inbound.InboundItemService;
import com.dk.mdm.service.ivt.inbound.InboundPurchaseReturnService;
import com.dk.mdm.service.ivt.inbound.InboundPurchaseService;
import com.dk.mdm.service.ivt.outbound.OutboundService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class IntoReturnService extends BaseService<IntoReturn> {

    @Override
    public BaseMapper<IntoReturn> getRepository() {
        return intoReturnMapper;
    }

    @Autowired
    private IntoReturnMapper intoReturnMapper;

    @Autowired
    private IntoReturnConvert intoReturnConvert;

    @Autowired
    private IntoReturnItemConvert intoReturnItemConvert;

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private PurchaseItemMapper purchaseItemMapper;

    @Autowired
    private IntoReturnItemService intoReturnItemService;


    @Autowired
    private IntoReturnItemMapper intoReturnItemMapper;


    @Autowired
    private CommonService commonService;

    @Autowired
    private AuthUtils authUtils;

    @Autowired
    private OutboundService outboundService;

    @Autowired
    private InboundMapper inboundMapper;

    @Autowired
    private InboundItemMapper inboundItemMapper;

    @Autowired
    private InboundItemService inboundItemService;

    @Autowired
    private InboundPurchaseReturnService inboundPurchaseReturnService;

    /**
     * @desc : 重写主键
     * @author : 于继渤
     * @date : 2024/2/29 20:29
     */
    @Override
    public String getPrimaryKey() {
        return "return_id";
    }

    /**
     * @desc : 查询
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Pagination
    public ResponseResultVO<PageList<IntoReturnResponse>> selectByCond(IntoReturnQuery intoReturnQuery) {
        return super.mergeListWithCount(intoReturnQuery, intoReturnMapper.selectByCond(intoReturnQuery), intoReturnMapper.countByCond(intoReturnQuery));
    }

    /**
     * @desc : 新建采购退货
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> insert(IntoReturnVO intoReturnVO) {
        //校验明细长度
        List<IntoReturnItemVO> intoReturnItemVOList = intoReturnVO.getIntoReturnItemVOList();
        //明细不存在
        if (intoReturnItemVOList == null || intoReturnItemVOList.size() == 0) {
            //返回 采购退货明细不可为空，请重新操作
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.PURCHASE_RETURN_DETAIL_NOT_EXIST.getMessage());
        }
        //查询采购订单
        PurchaseResponse purchaseResponse = purchaseMapper.selectById(intoReturnVO.getPurId());
        intoReturnVO.setSumQuantity(intoReturnVO.getSumQuantity().abs());
        intoReturnVO.setSumAmount(intoReturnVO.getSumAmount().abs());
        //设置总单
        intoReturnVO = this.insertTotal(intoReturnVO, purchaseResponse);
        //设置明细
        intoReturnItemVOList = this.insertDetail(intoReturnVO, intoReturnItemVOList);


        //反写采购订单已入库退货数量、金额
        purchaseMapper.updateAmount(new Purchase().setPurId(intoReturnVO.getPurId())
                .setReturnQty(purchaseResponse.getReturnQty().add(intoReturnVO.getSumQuantity()))
                .setReturnAmt(purchaseResponse.getReturnAmt().add(intoReturnVO.getSumAmount()))
        );
        BigDecimal sumReturnQty = BigDecimal.ZERO;
        BigDecimal sumReturnAmt = BigDecimal.ZERO;
        //反写入库订单明细、反写订单明细
        for (IntoReturnItemVO intoReturnItemVO : intoReturnItemVOList) {

            //根据退货明细中入库明细id查询入库明细数据
            InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(intoReturnItemVO.getIntoItemId());
            BigDecimal returnQty = inboundItemResponse.getReturnQty().add(intoReturnItemVO.getItemQty().setScale(6, BigDecimal.ROUND_HALF_UP));
            BigDecimal returnAmt = inboundItemResponse.getReturnAmt().add(intoReturnItemVO.getItemAmt().setScale(6, BigDecimal.ROUND_HALF_UP));
            sumReturnQty = sumReturnQty.add(returnQty);
            sumReturnAmt = sumReturnAmt.add(returnAmt);
            //反写入库单退货数量金额 todo
            InboundResponse inboundResponse = inboundMapper.selectById(intoReturnItemVO.getIntoId());
            inboundMapper.updateAmount(new Inbound().setIntoId(intoReturnItemVO.getIntoId())
                    .setReturnQty(inboundResponse.getReturnQty().add(sumReturnQty))
                    .setReturnAmt(inboundResponse.getReturnAmt().add(sumReturnAmt)));
            inboundItemMapper.updateAmount(
                    new InboundItem()
                            .setItemId(inboundItemResponse.getItemId())
                            .setReturnQty(returnQty)
                            .setReturnAmt(returnAmt)
            );
            //TODO 反写订单出库明细 退货数量和退货金额
            //查询采购订单明细数据
            PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(intoReturnItemVO.getPurItemId());
            //反写订单明细退货数量和退货金额
            purchaseItemMapper.updateAmount(
                    new PurchaseItem()
                            .setItemId(intoReturnItemVO.getPurItemId())
                            .setReturnQty(purchaseItemResponse.getReturnQty().add(intoReturnItemVO.getItemQty().setScale(6, BigDecimal.ROUND_HALF_UP)))
                            .setReturnAmt(purchaseItemResponse.getReturnAmt().add(intoReturnItemVO.getItemAmt().setScale(6, BigDecimal.ROUND_HALF_UP)))

            );
        }
        //生成逆向  负数的 入库单


        InboundVO inboundVO = new InboundVO();
        //自动标识
        inboundVO.setAutomaticFlg(intoReturnVO.getAutomaticFlg());

        inboundVO.setFromId(intoReturnVO.getReturnId());
        inboundVO.setFromNo(intoReturnVO.getReturnNo());
        inboundVO.setStaffId(intoReturnVO.getStaffId());
        inboundVO.setOrgId(intoReturnVO.getOrgId());
        inboundVO.setMakeStaff(authUtils.getStaff().getStaffId());
        inboundVO.setMakeTime(LocalDateTime.now());
        inboundVO.setIntoingQty(intoReturnVO.getSumQuantity().multiply(new BigDecimal(-1)));
        inboundVO.setIntoingAmt(intoReturnVO.getSumAmount().multiply(new BigDecimal(-1)));
        List<InboundItemVO> list = new ArrayList<>();
        for (IntoReturnItemVO intoReturnItemVO : intoReturnItemVOList) {
            System.out.println("'id==='" + intoReturnItemVO.getItemId());
            InboundItemVO inboundItemVO = new InboundItemVO();
            inboundItemVO.setIntoingQty(intoReturnItemVO.getItemQty().multiply(new BigDecimal(-1)));
            inboundItemVO.setIntoingAmt(intoReturnItemVO.getItemAmt().multiply(new BigDecimal(-1)));
            inboundItemVO.setPriceInto(intoReturnItemVO.getPriceReturn());
            inboundItemVO.setFromItemId(intoReturnItemVO.getItemId());
            inboundItemVO.setSkuId(intoReturnItemVO.getSkuId());
            inboundItemVO.setItemIndex(intoReturnItemVO.getItemIndex());
            inboundItemVO.setNonStdCode(intoReturnItemVO.getNonStdCode());
            inboundItemVO.setReturnQty(intoReturnItemVO.getItemQty());
            inboundItemVO.setReturnAmt(intoReturnItemVO.getItemAmt());
            list.add(inboundItemVO);
        }

        inboundVO.setItemList(list);

        inboundPurchaseReturnService.purchaseReturnInboundInsert(inboundVO);


        return ResponseResultUtil.success();
    }

    /**
     * @desc : 设置总单信息
     * @date : 2023/9/28 16:20
     * @author : 于继渤
     */

    public IntoReturnVO insertTotal(IntoReturnVO intoReturnVO, PurchaseResponse purchaseResponse) {
        // 获取单号
        Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.PURCHASEOUT.getName(), false);
        intoReturnVO.setReturnId(codeMap.get("outId").toString()).setReturnNo(codeMap.get("outNote").toString())
                .setReturnType(Constant.returnType.PURRETURN.getName());

        //部门Id
        intoReturnVO.setOrgId(purchaseResponse.getOrgId());
        //退货状态 通过
        intoReturnVO.setReturnStatus(Constant.returnStatus.OUT_REJECT_STATUS_ADOPT.getName());
        //出库状态
        intoReturnVO.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
        //制单员
        intoReturnVO.setMakeStaff(authUtils.getStaff().getStaffId());
        //业务员
        intoReturnVO.setStaffId(purchaseResponse.getStaffId());
        //供应商
        intoReturnVO.setSupId(purchaseResponse.getSupId());
        //联系人
        intoReturnVO.setContactName(purchaseResponse.getContactName());
        //联系电话
        intoReturnVO.setContactPhone(purchaseResponse.getContactPhone());
        //送货日期//取货日期
        intoReturnVO.setDeliveryDate(purchaseResponse.getPickupDate());
        //新建总单
        IntoReturn intoReturn = intoReturnConvert.convertToPo(intoReturnVO);
        intoReturnMapper.insert(intoReturn);
        return intoReturnVO;
    }


    /**
     * @desc : 设置明细信息
     * @date : 2023/9/28 16:20
     * @author : 于继渤
     */

    public List<IntoReturnItemVO> insertDetail(IntoReturnVO intoReturnVO, List<IntoReturnItemVO> intoReturnItemVOList) {
        for (IntoReturnItemVO intoReturnItemVO : intoReturnItemVOList) {
            //总单Id
            intoReturnItemVO.setReturnId(intoReturnVO.getReturnId());
            //公司Id
            intoReturnItemVO.setCpId(intoReturnVO.getCpId());
            intoReturnItemVO.setPriceReturn(intoReturnItemVO.getPriceReturn().abs());
            intoReturnItemVO.setItemQty(intoReturnItemVO.getItemQty().abs());
            //根据分销订单明细Id查询数据
            //查询明细数据
            PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(intoReturnItemVO.getFromItemId());
            //采购订单id
            intoReturnItemVO.setPurId(purchaseItemResponse.getPurId());
            intoReturnItemVO.setPurItemId(purchaseItemResponse.getItemId());
            intoReturnItemVO.setIntoItemId(intoReturnItemVO.getIntoItemId());
            Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.PURCHASEOUT.getName(), false);
            intoReturnItemVO.setItemId(codeMap.get("outId").toString());
            //标价
            intoReturnItemVO.setPriceStd(purchaseItemResponse.getPriceStd());
            //标价金额 (商品数量*标价)
            intoReturnItemVO.setAmtStd(intoReturnItemVO.getItemQty().multiply(intoReturnItemVO.getPriceStd()).setScale(2, BigDecimal.ROUND_HALF_UP));
            //退货价格
            intoReturnItemVO.setPriceReturn(purchaseItemResponse.getPricePur());
            //退货金额 (商品数量*退货价)
            intoReturnItemVO.setItemAmt(intoReturnItemVO.getItemQty().multiply(intoReturnItemVO.getPriceReturn()).setScale(2, BigDecimal.ROUND_HALF_UP));
            //自动]出库赋值  已入库数量、已入库金额
            intoReturnItemVO.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
             //新建明细
            IntoReturnItem intoReturnItem = intoReturnItemConvert.convertToPo(intoReturnItemVO);
            intoReturnItemMapper.insert(intoReturnItem);

        }
        return intoReturnItemVOList;
    }


    /**
     * @desc : 作废
     * @date : 2022/6/28 16:58
     * @author : 于继渤
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<String> cancel(IntoReturnVO intoReturnVO) {
        //根据入库退货单id查询到退货单

        List<IntoReturnResponse> intoReturnResponses = intoReturnMapper.selectByCond(new IntoReturnQuery().setReturnId(intoReturnVO.getReturnId()));


        if (intoReturnResponses != null && intoReturnResponses.size() > 0) {
            IntoReturnResponse intoReturnResponse = intoReturnResponses.get(0);
            if (intoReturnResponse.getReturnStatus().equals(Constant.returnStatus.OUT_REJECT_STATUS_RIGHTING_NOT.getName())) {
                //当前单据已经作废，不能重复操
                return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
            }
            //非出库中单据不能作废
            if (intoReturnResponse.getOutStatus().equals(Constant.OutStatus.YICHUKU.getName())) {
                return ResponseResultUtil.error(ErrorCodeEnum.NO_WAIT_INTO_RETURN_CANCEL.getCode(),
                        ErrorCodeEnum.NO_WAIT_INTO_RETURN_CANCEL.getMessage());
            }

            //查询采购单
            PurchaseResponse purchaseResponse = purchaseMapper.selectById(intoReturnResponse.getPurId());
            if (purchaseResponse == null) {
                //返回 单据所对应的原始单据不存在，请重新操作
                return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ORIGINAL_DOCUMENT_ABSENT.getMessage());
            }
            //修改采购单 已入库数量/金额
            purchaseMapper.updateAmount(
                    new Purchase().setReturnQty(purchaseResponse.getReturnQty().subtract(intoReturnResponse.getSumQuantity()))
                            .setReturnAmt(purchaseResponse.getReturnAmt().subtract(intoReturnResponse.getSumAmount())).setPurId(purchaseResponse.getPurId())
            );

            //查询入库退货单明细
            List<IntoReturnItemResponse> intoReturnItemList = intoReturnItemService.selectByCond(new IntoReturnItemQuery().setReturnId(intoReturnResponse.getReturnId())).getData().getList();
            //明细不存在
            if (intoReturnItemList == null || intoReturnItemList.size() == 0) {
                //返回 采购退货明细不可为空，请重新操作
                return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.PURCHASE_RETURN_DETAIL_NOT_EXIST.getMessage());
            }

            for (IntoReturnItemResponse intoReturnItemResponse : intoReturnItemList) {
                //查询入库单明细
                InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(intoReturnItemResponse.getIntoItemId());
                if (inboundItemResponse == null) {
                    //返回 单据所对应的原始单据不存在，请重新操作
                    return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ORIGINAL_DOCUMENT_ABSENT.getMessage());
                }
                //修改入库单明细 已入库退货数量/金额
                inboundItemMapper.updateAmount(new InboundItem()
                        .setItemId(intoReturnItemResponse.getIntoItemId())
                        .setReturnQty(inboundItemResponse.getReturnQty().subtract(intoReturnItemResponse.getItemQty()))
                        .setReturnAmt(inboundItemResponse.getReturnAmt().subtract(intoReturnItemResponse.getItemAmt()))
                );
                //查询入库单 修改 已入库退货数量/金额
                InboundResponse inboundResponse = inboundMapper.selectById(intoReturnItemResponse.getIntoId());
                if (inboundResponse == null) {
                    //返回 单据所对应的原始单据不存在，请重新操作
                    return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ORIGINAL_DOCUMENT_ABSENT.getMessage());
                }
                inboundMapper.updateAmount(new Inbound().setIntoId(inboundResponse.getIntoId())
                        .setReturnQty(inboundResponse.getReturnQty().subtract(intoReturnItemResponse.getItemQty()))
                        .setReturnAmt(inboundResponse.getReturnAmt().subtract(intoReturnItemResponse.getItemAmt())));

                //查询采购单明细
                PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(intoReturnItemResponse.getPurItemId());
                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(intoReturnItemResponse.getItemQty()))
                                .setReturnAmt(purchaseItemResponse.getReturnAmt().subtract(intoReturnItemResponse.getItemAmt()))

                );
            }

            //修改 负向入库单 状态
            inboundMapper.updateIntoStatus(new Inbound()
                    .setIntoId(intoReturnResponse.getIntoId())
                    .setIntoStatus(Constant.IntoStatus.ZUOFEI.getName()));

            //修改 负向入库单明细 状态

            List<InboundItemResponse> inboundItemResponses = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(intoReturnResponse.getIntoId()));
            if (inboundItemResponses != null && inboundItemResponses.size() > 0) {
                for (InboundItemResponse inboundItemRespons : inboundItemResponses) {
                    inboundItemMapper.updateIntoStatus(new InboundItem().setItemId(inboundItemRespons.getItemId()).setIntoStatus(Constant.IntoStatus.ZUOFEI.getName()));
                }
            }


            //修改入库退货单明细
            super.updateByUuid(
                    new IntoReturn()
                            .setReturnId(intoReturnVO.getReturnId())
                            //修改退货状态 作废
                            .setReturnStatus(Constant.returnStatus.OUT_REJECT_STATUS_RIGHTING_NOT.getName())
            );
        }


        return ResponseResultUtil.success();
    }


    /**
     * @desc : 获取单据信息（编辑用）
     * @date : 2024/3/16 16:28
     * @author : 于继渤
     */
    public ResponseResultVO<?> selectByUpdate(String id) {
        Map<String, Object> dataInfo = new HashMap<>();
        //总单
        IntoReturnResponse intoReturnResponse = intoReturnMapper.selectByIntoReturnQuery(new IntoReturnQuery().setReturnId(id));
        //单据不存在
        if (intoReturnResponse == null) {
            return ResponseResultUtil.error(ErrorCodeEnum.ORIGINAL_DOCUMENT_ABSENT.getCode(),
                    ErrorCodeEnum.ORIGINAL_DOCUMENT_ABSENT.getMessage());
        }
        dataInfo.put("data", intoReturnResponse);
        // 明细
        List<IntoReturnItemResponse> intoReturnItemResponses = intoReturnItemMapper.selectByCond(new IntoReturnItemQuery().setReturnId(id));
        dataInfo.put("dataItem", intoReturnItemResponses);
        return ResponseResultUtil.success(dataInfo);
    }

    /**
     * @desc :出库中 编辑
     * @date : 2024/3/16 16:28
     * @author : 于继渤
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> update(IntoReturnVO intoReturnVO) {
        //校验单据
        if (intoReturnVO.getReturnId() == null) {
            return ResponseResultUtil.error(ErrorCodeEnum.ORIGINAL_DOCUMENT_ABSENT.getCode(),
                    ErrorCodeEnum.ORIGINAL_DOCUMENT_ABSENT.getMessage());
        }
//        //非出库中单据不能编辑
//        if (intoReturnVO.getOutStatus().equals(Constant.OutStatus.YICHUKU.getName())) {
//            return ResponseResultUtil.error(ErrorCodeEnum.NO_WAIT_INTO_RETURN.getCode(),
//                    ErrorCodeEnum.NO_WAIT_INTO_RETURN.getMessage());
//        }
        IntoReturn intoReturn = intoReturnConvert.convertToPo(intoReturnVO);
//        //查询原始退货总单
//        IntoReturnResponse intoReturnResponse = intoReturnMapper.selectById(intoReturnVO.getReturnId());
//        //负数转正数
//        intoReturnVO.setSumQuantity(intoReturnVO.getSumQuantity().abs());
//        intoReturnVO.setSumAmount(intoReturnVO.getSumAmount().abs());
//        //查询采购订单
//        PurchaseResponse purchaseResponse = purchaseMapper.selectById(intoReturnVO.getPurId());
//        //采购订单原始退货数量 - 原始退货单退货数量
//        BigDecimal purchaseReturnQty = purchaseResponse.getReturnQty().subtract(intoReturnResponse.getSumQuantity());
//        BigDecimal purchaseReturnAmt = purchaseResponse.getReturnAmt().subtract(intoReturnResponse.getSumAmount());
//        //反写采购订单已入库退货数量、金额
//        purchaseMapper.updateAmount(new Purchase().setPurId(intoReturnVO.getPurId())
//                .setReturnQty(purchaseReturnQty.add(intoReturnVO.getSumQuantity()))
//                .setReturnAmt(purchaseReturnAmt.add(intoReturnVO.getSumAmount()))
//        );
//        //校验明细
//        if (intoReturnVO.getIntoReturnItemVOList() != null && intoReturnVO.getIntoReturnItemVOList().size() > 0) {
//
//            for (IntoReturnItemVO intoReturnItemVO : intoReturnVO.getIntoReturnItemVOList()) {
//                //负数转正数
//                intoReturnItemVO.setPriceReturn(intoReturnItemVO.getPriceReturn().abs());
//                intoReturnItemVO.setItemQty(intoReturnItemVO.getItemQty().abs());
//                intoReturnItemVO.setItemAmt(intoReturnItemVO.getItemAmt().abs());
//                //查询原始采购退货明细
//                IntoReturnItemResponse intoReturnItemResponse = intoReturnItemMapper.selectById(intoReturnItemVO.getItemId());
//                //查询采购订单明细数据
//                PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(intoReturnItemVO.getPurItemId());
//                BigDecimal purchaseItemReturnQty = purchaseItemResponse.getReturnQty().subtract(intoReturnItemResponse.getItemQty());
//                BigDecimal purchaseItemReturnAmt = purchaseItemResponse.getReturnAmt().subtract(intoReturnItemResponse.getItemAmt());
//                //反写采购订单明细退货数量和退货金额
//                purchaseItemMapper.updateAmount(
//                        new PurchaseItem()
//                                .setItemId(intoReturnItemVO.getPurItemId())
//                                .setReturnQty(purchaseItemReturnQty.add(intoReturnItemVO.getItemQty().setScale(6, BigDecimal.ROUND_HALF_UP)))
//                                .setReturnAmt(purchaseItemReturnAmt.add(intoReturnItemVO.getItemAmt().setScale(6, BigDecimal.ROUND_HALF_UP)))
//
//                );
//                //根据退货明细中入库明细id查询入库明细数据
//                InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(intoReturnItemVO.getIntoItemId());
//                BigDecimal inboundItemReturnQty = inboundItemResponse.getReturnQty().subtract(intoReturnItemResponse.getItemQty());
//                BigDecimal inboundItemReturnAmt = inboundItemResponse.getReturnAmt().subtract(intoReturnItemResponse.getItemAmt());
//
//
//                BigDecimal ReturnQty = inboundItemReturnQty.add(intoReturnItemVO.getItemQty().setScale(6, BigDecimal.ROUND_HALF_UP));
//                BigDecimal ReturnAm = inboundItemReturnAmt.add(intoReturnItemVO.getItemAmt().setScale(6, BigDecimal.ROUND_HALF_UP));
//                //反写原始入库单明细 不是负向的入库单
//                inboundItemMapper.updateAmount(
//                        new InboundItem()
//                                .setItemId(inboundItemResponse.getItemId())
//                                .setReturnQty(ReturnQty)
//                                .setReturnAmt(ReturnAm)
//                );
//                //反写原始入库单
//                InboundResponse inboundResponse = inboundMapper.selectById(intoReturnItemVO.getIntoId());
//                BigDecimal inboundReturnQty = inboundResponse.getReturnQty().subtract(intoReturnItemResponse.getItemQty());
//                BigDecimal inboundReturnAmt = inboundResponse.getReturnAmt().subtract(intoReturnItemResponse.getItemAmt());
//                inboundMapper.updateAmount(new Inbound().setIntoId(intoReturnItemVO.getIntoId())
//                        .setReturnQty(inboundReturnQty.add(intoReturnItemVO.getItemQty()))
//                        .setReturnAmt(inboundReturnAmt.add(intoReturnItemVO.getItemAmt())));
//                IntoReturnItem intoReturnItem = intoReturnItemConvert.convertToPo(intoReturnItemVO);
//                //新建编辑操作
//                if (intoReturnItemVO.getItemId() != null) {
//                    //编辑明细
//                    intoReturnItemMapper.updateById(intoReturnItem);
//                } else {
//                    Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.PURCHASEOUT.getName(), false);
//                    intoReturnItemResponse.setItemId(codeMap.get("outId").toString());
//                    intoReturnItemMapper.insert(intoReturnItem);
//                }
//            }
//        }
        intoReturnMapper.updateByUuId(intoReturn);
//        //生成逆向  负数的 入库单
//        InboundVO inboundVO = new InboundVO();
//        //自动标识
//        inboundVO.setAutomaticFlg(intoReturnVO.getAutomaticFlg());
//        inboundVO.setFromId(intoReturnVO.getReturnId());
//        inboundVO.setFromNo(intoReturnVO.getReturnNo());
//        inboundVO.setStaffId(intoReturnVO.getStaffId());
//        inboundVO.setOrgId(intoReturnVO.getOrgId());
//        inboundVO.setMakeStaff(authUtils.getStaff().getStaffId());
//        inboundVO.setMakeTime(LocalDateTime.now());
//        inboundVO.setIntoingQty(intoReturnVO.getSumQuantity().multiply(new BigDecimal(-1)));
//        inboundVO.setIntoingAmt(intoReturnVO.getSumAmount().multiply(new BigDecimal(-1)));
//        List<InboundItemVO> list = new ArrayList<>();
//        for (IntoReturnItemVO intoReturnItemVO : intoReturnVO.getIntoReturnItemVOList()) {
//            //删除原始负数入库单
//            if (intoReturnItemVO.getFromItemId() != null) {
//                inboundItemMapper.deleteByFromId(new InboundItem().setFromItemId(intoReturnItemVO.getItemId()));
//            }
//            InboundItemVO inboundItemVO = new InboundItemVO();
//            inboundItemVO.setIntoingQty(intoReturnItemVO.getItemQty().multiply(new BigDecimal(-1)));
//            inboundItemVO.setIntoingAmt(intoReturnItemVO.getItemAmt().multiply(new BigDecimal(-1)));
//            inboundItemVO.setPriceInto(intoReturnItemVO.getPriceReturn());
//            inboundItemVO.setFromItemId(intoReturnItemVO.getItemId());
//            inboundItemVO.setSkuId(intoReturnItemVO.getSkuId());
//            inboundItemVO.setItemIndex(intoReturnItemVO.getItemIndex());
//            inboundItemVO.setNonStdCode(intoReturnItemVO.getNonStdCode());
//            inboundItemVO.setReturnQty(intoReturnItemVO.getItemQty());
//            inboundItemVO.setReturnAmt(intoReturnItemVO.getItemAmt());
//            list.add(inboundItemVO);
//        }
//        inboundVO.setItemList(list);
//        inboundPurchaseReturnService.purchaseReturnInboundInsert(inboundVO);
        return ResponseResultUtil.success();
    }
}
