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

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dk.common.exception.BaseBusinessException;
import com.dk.common.infrastructure.annotaiton.Pagination;
import com.dk.common.infrastructure.constant.Constant;
import com.dk.common.infrastructure.enums.ErrorCodeEnum;
import com.dk.common.mapper.BaseMapper;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseCodeEnum;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.common.service.BaseService;
import com.dk.mdm.infrastructure.convert.ivt.InboundConvert;
import com.dk.mdm.infrastructure.convert.ivt.InboundItemConvert;
import com.dk.mdm.mapper.common.CommonMapper;
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.ivt.IntoReturnMapper;
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.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.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.vo.ivt.InboundItemVO;
import com.dk.mdm.model.vo.ivt.InboundVO;
import com.dk.mdm.model.vo.ivt.OutboundItemVO;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


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

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

    @Autowired
    private InboundMapper inboundMapper;

    @Autowired
    private InboundConvert inboundConvert;

    @Autowired
    private InboundItemMapper inboundItemMapper;

    @Autowired
    private InboundItemConvert inboundItemConvert;

    @Autowired
    private CommonService commonService;

    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private AccountService accountService;


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

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

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

    /**
     * @desc : 采购退货入库新建
     * @date : 2024/3/7 14:13
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> purchaseReturnInboundInsert(InboundVO inboundVO) {
        //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());
        }
        //endregion

        //region  总单
        //获取 id/单号
        Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.SALERETURN.getName(), false);
        inboundVO.setIntoId(codeMap.get("outId").toString()).
                setIntoNo(codeMap.get("outNote").toString());
        //入库类型
        inboundVO.setIntoType(Constant.IntoType.PURRETURN_RETURN.getName());
        //自动入库标识
        if (inboundVO.getFlgAutoHandle()) {
            //已入库
            inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName());
        } else {
            //入库中
            inboundVO.setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName());
        }
        //退货数量
        inboundVO.setReturnQty(inboundVO.getIntoingQty());
        //退货金额
        inboundVO.setReturnAmt(inboundVO.getIntoingAmt());
        //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额
        if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) {
            inboundVO
                    .setIntoQty(inboundVO.getIntoingQty())
                    .setIntoAmt(inboundVO.getIntoingAmt())
                    .setIntoingQty(BigDecimal.ZERO)
                    .setIntoingAmt(BigDecimal.ZERO)
            ;
        } else {
            inboundVO
                    .setIntoQty(BigDecimal.ZERO)
                    .setIntoAmt(BigDecimal.ZERO)
            ;
        }

        //实体转换
        Inbound inbound = inboundConvert.convertToPo(inboundVO);
        inboundMapper.insert(inbound);
        //endregion

        //region 明细
        //校验明细
        if (inboundVO.getItemList().size() == 0) {
            throw new BaseBusinessException(ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getCode(),
                    ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage());
        }
        for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
            //region 将库存需要的参数赋值
            inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
            inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_RETURN.getValue());
            inboundItemVO.setAddOrEditFlag(true);
            //endregion

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

        }
        //endregion

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

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

        return ResponseResultUtil.success(inboundVO);
    }

    /**
     * @desc : 采购退货入库编辑
     * @date : 2024/3/25 16:25
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> purchaseReturnInboundUpdate(InboundVO inboundVO) {
        //region 小编辑
        if (!inboundVO.getLimitEdit()) {
            Inbound inbound = new Inbound();
            inbound.setIntoId(inboundVO.getIntoId());
            inbound.setRemarks(inboundVO.getRemarks());
            inbound.setAnnexPaths(inboundVO.getAnnexPaths());
            inboundMapper.update(inbound,
                    new UpdateWrapper<Inbound>().lambda()
                            .eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
            );
            for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
                InboundItem inboundItem = new InboundItem();
                inboundItem.setItemId(inboundItemVO.getItemId());
                if (inboundItemVO.getRemarks() != null || inboundItemVO.getRemarks() != " ") {
                    inboundItem.setRemarks(inboundItemVO.getRemarks());
                    inboundItemMapper.update(inboundItem,
                            new UpdateWrapper<InboundItem>().lambda()
                                    .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                    );
                }
            }
        }
        //endregion
        //region 大编辑
        else {
            //region 根据id查询 此条入库单的数据还未更改前的数据
            InboundResponse inboundResponse = inboundMapper.selectById(inboundVO.getIntoId());
            BigDecimal sumQty = inboundVO.getItemList().stream().map(InboundItemVO::getIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
            BigDecimal sumAmt = inboundVO.getItemList().stream().map(InboundItemVO::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
            //endregion

            //region  自动办理参数为true  已入库编辑
            //自动办理参数为true
            if (Constant.FlgHandleSetting.TRUE.getValue().equals(inboundVO.getFlgHandleSetting())) {
                //region 退账
                if (inboundVO.getReceivableId() != null) {
                    accountService.reversePayable(inboundVO.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
                }
                //endregion

                //region 修改明细
                List<InboundItemVO> itemList = inboundVO.getItemList();
                for (InboundItemVO inboundItemVO : itemList) {

                    //region  不能新建
//                    if (inboundItemVO.getSPurItemId() == null) {
//                        throw new BaseBusinessException(ErrorCodeEnum.ITEMS_THAT_ARE_NOT_AVAILABLE_HANDLE.getCode(),
//                                ErrorCodeEnum.ITEMS_THAT_ARE_NOT_AVAILABLE_HANDLE.getMessage());
//                    }
                    //endregion

                    //根据id查询
                    InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(inboundItemVO.getItemId());

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

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

                        InboundItem inboundItem = new InboundItem();
                        inboundItem.setItemId(inboundItemVO.getItemId());
                        inboundItem.setIntoQty(inboundItemVO.getIntoingQty());
                        inboundItem.setIntoAmt(inboundItemVO.getIntoingAmt());
                        inboundItem.setCostPrice(inboundItemVO.getPriceInto());
                        inboundItem.setCostAmt(inboundItemVO.getIntoQty().multiply(inboundItemVO.getPriceInto()).setScale(6, BigDecimal.ROUND_HALF_UP));
                        //修改
                        inboundItemMapper.update(inboundItem,
                                new UpdateWrapper<InboundItem>().lambda()
                                        .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                        );
                    }
                    //endregion

                    //region  新建
                    else {
                        inboundItemVO
                                .setIntoQty(inboundItemVO.getIntoingQty())
                                .setIntoAmt(inboundItemVO.getIntoingAmt())
                                .setIntoingQty(BigDecimal.ZERO)
                                .setIntoingAmt(BigDecimal.ZERO)
                                .setCostPrice(inboundItemVO.getPriceInto())
                                .setCostAmt(inboundItemVO.getOutQty().multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP))
                        ;
                        inboundItemVO.setIntoId(inboundVO.getIntoId());
                        //入库状态
                        inboundItemVO.setIntoStatus(inboundVO.getIntoStatus());
                        inboundItemVO.setIntoType(Constant.IntoType.PURRETURN_RETURN.getName());
                        //实体转换
                        InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
                        inboundItemMapper.insert(inboundItem);
                        inboundItemVO.setItemId(inboundItem.getItemId());

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

                }
                //endregion

                //region 删除明细
                if (inboundVO.getDeleteItemList() != null && inboundVO.getDeleteItemList().size() > 0) {
                    for (InboundItemVO inboundItemVO : inboundVO.getDeleteItemList()) {
                        if (inboundItemVO.getItemId() != null) {
                            //region 将库存需要的参数赋值
                            inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                            inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_RETURN.getValue());
                            //endregion

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

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

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

                //region  调用库存
                Map<String, Object> map = new HashMap<>();
                map.put("intoDetail",inboundVO.getItemList());
                map.put("delIntoDetail",inboundVO.getDeleteItemList());
                inventoryService.operatingInventoryInformation(map);
                //endregion
            }
            else{
                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.FLG_HANDLE_SETTING_NOT_ENABLED_INTO.getMessage());
            }
            //endregion

            //region 自动办理参数false  入库中编辑
            //自动办理标识为false 并且 自动办理参数为false  入库中
            if (!inboundVO.getFlgAutoHandle() && Constant.FlgHandleSetting.FALSE.getValue().equals(inboundVO.getFlgHandleSetting())) {
                //region 修改明细
                List<InboundItemVO> itemList = inboundVO.getItemList();
                for (InboundItemVO inboundItemVO : itemList) {
                    //region  不能新建
//                    if (inboundItemVO.getSPurItemId() == null) {
//                        throw new BaseBusinessException(ErrorCodeEnum.ITEMS_THAT_ARE_NOT_AVAILABLE_HANDLE.getCode(),
//                                ErrorCodeEnum.ITEMS_THAT_ARE_NOT_AVAILABLE_HANDLE.getMessage());
//                    }
                    //endregion

                    //region 编辑明细
                    if (inboundItemVO.getItemId() != null) {
                        InboundItem inboundItem = new InboundItem();
                        inboundItem.setItemId(inboundItemVO.getItemId());
                        inboundItem.setIntoingQty(inboundItemVO.getIntoingQty());
                        inboundItem.setIntoingAmt(inboundItemVO.getIntoingAmt());
                        //修改
                        inboundItemMapper.update(inboundItem,
                                new UpdateWrapper<InboundItem>().lambda()
                                        .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                        );
                    }
                    //endregion

                    //region  新建
                    else {
                        inboundItemVO
                                .setIntoQty(BigDecimal.ZERO)
                                .setIntoAmt(BigDecimal.ZERO)
                                .setCostPrice(inboundItemVO.getPriceInto())
                                .setCostAmt(inboundItemVO.getIntoingQty().multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP))
                        ;
                        inboundItemVO.setIntoId(inboundVO.getIntoId());
                        //入库状态
                        inboundItemVO.setIntoStatus(inboundVO.getIntoStatus());
                        inboundItemVO.setIntoType(Constant.IntoType.PURRETURN_RETURN.getName());
                        //实体转换
                        InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
                        inboundItemMapper.insert(inboundItem);
                        inboundItemVO.setItemId(inboundItem.getItemId());
                    }
                    //endregion

                }
                //endregion

                //region 删除明细
                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()))
                            );
                        }
                    }
                }
                //endregion

                //region  修改入库总单
                Inbound inbound = new Inbound();
                inbound.setIntoId(inboundVO.getIntoId());
                inbound.setIntoingQty(sumQty);
                inbound.setIntoingAmt(sumAmt);
                //修改
                inboundMapper.update(inbound,
                        new UpdateWrapper<Inbound>().lambda().eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
                );
                //endregion
            }
            else{
                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.FLG_HANDLE_SETTING_NOT_ENABLED_INTO.getMessage());
            }
            //endregion
        }
        //endregion
        return ResponseResultUtil.success(inboundVO);
    }

    /**
     * @desc : 采购退货入库作废
     * @date : 2024/3/26 9:24
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> purchaseReturnInboundRepeal(String intoId) {
        //region  查询总单  查询明细
        //根据id查询 此条入库单的数据还未更改前的数据
        InboundResponse inboundResponse = inboundMapper.selectById(intoId);
        //根据总单id查询
        List<InboundItemResponse> inboundItemResponseList = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(inboundResponse.getIntoId()));
        //endregion

        //region  自动办理参数为true  已入库编辑
        //自动办理参数为true
        if (Constant.FlgHandleSetting.TRUE.getValue().equals(inboundResponse.getFlgHandleSetting())) {
            //region 退账
            if (inboundResponse.getReceivableId() != null) {
                accountService.reversePayable(inboundResponse.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
            }
            //endregion

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

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

                //赋值
                InboundItem inboundItem = new InboundItem();
                inboundItem.setItemId(inboundItemResponse.getItemId());
                inboundItem.setFlgValid(false);
                //修改
                inboundItemMapper.update(inboundItem,
                        new UpdateWrapper<InboundItem>().lambda()
                                .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                );
            }
            //endregion

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

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

            //region   调用库存
            Map<String, Object> map = new HashMap<>();
            map.put("delIntoDetail", inboundItemResponseList);
            inventoryService.operatingInventoryInformation(map);
            //endregion
        }
        else{
            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.FLG_HANDLE_SETTING_NOT_ENABLED_INTO.getMessage());
        }
        //endregion

        //region 自动办理参数false  入库中编辑
        //自动办理标识为false 并且 自动办理参数为false  入库中
        if (!inboundResponse.getFlgAutoHandle() && Constant.FlgHandleSetting.FALSE.getValue().equals(inboundResponse.getFlgHandleSetting())) {
            //region 修改明细
            for (InboundItemResponse inboundItemResponse : inboundItemResponseList) {
                //赋值
                InboundItem inboundItem = new InboundItem();
                inboundItem.setItemId(inboundItemResponse.getItemId());
                inboundItem.setIntoingQty(BigDecimal.ZERO);
                inboundItem.setIntoingAmt(BigDecimal.ZERO);
                inboundItem.setFlgValid(false);
                //修改
                inboundItemMapper.update(inboundItem,
                        new UpdateWrapper<InboundItem>().lambda()
                                .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                );
            }
            //endregion

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

        }
        else{
            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.FLG_HANDLE_SETTING_NOT_ENABLED_INTO.getMessage());
        }
        //endregion

        return ResponseResultUtil.success();
    }

    /**
     * @desc : 采购退货入库办理
     * @date : 2024/3/7 15:47
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> saleReturnHandleInbound(InboundVO inboundVO) {
        //region 校验明细
        if (inboundVO.getItemList().size() == 0) {
            throw new BaseBusinessException(ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getCode(),
                    ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage());
        }
        //endregion

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

        //region 编辑明细
        for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
            //查询原单
            InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(inboundItemVO.getItemId());
            //region 校验数量是否超出
            if (inboundItemVO.getIntoingQty().compareTo(inboundItemResponse.getIntoingQty()) > 0) {
                throw new BaseBusinessException(ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITY_IN_THE_OUTBOUND_SHIPMENT.getCode(),
                        ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITY_IN_THE_OUTBOUND_SHIPMENT.getMessage());
            }
            //endregion

            //region 编辑明细
            if (inboundItemVO.getItemId() != null) {
                inboundItemVO
                        .setIntoQty(inboundItemResponse.getIntoingQty().add(inboundItemVO.getIntoingQty()))
                        .setIntoAmt(inboundItemResponse.getIntoAmt().add(inboundItemVO.getIntoingAmt()))
                        .setIntoingQty(BigDecimal.ZERO)
                        .setIntoAmt(BigDecimal.ZERO)
                        .setCostPrice(inboundItemVO.getPriceInto())
                        .setCostAmt(inboundItemVO.getIntoQty().multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP));
                //入库状态
                String intoStatus = this.setIntoStatus(inboundItemVO.getIntoingQty(), inboundItemVO.getIntoQty());
                inboundItemVO.setIntoStatus(intoStatus);
                //实体转换
                InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
                //修改
                inboundItemMapper.update(inboundItem,
                        new UpdateWrapper<InboundItem>().lambda()
                                .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                );
                //region 将库存需要的参数赋值
                inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_RETURN.getValue());
                //编辑之前的数
                if(inboundItemResponse.getIntoQty().compareTo(BigDecimal.ZERO)>0){
                    inboundItemVO.setQtyBeforeUpdate(inboundItemResponse.getIntoQty());
                    inboundItemVO.setAmtBeforeUpdate(inboundItemResponse.getIntoAmt());
                }
                inboundItemVO.setAddOrEditFlag(true);
                //endregion
            }
            //endregion

            //region  新建
            else{
                inboundItemVO
                        .setIntoQty(inboundItemVO.getIntoingQty())
                        .setIntoAmt(inboundItemVO.getIntoingAmt())
                        .setIntoId(inboundVO.getIntoId())
                        .setCostPrice(inboundItemVO.getPriceInto())
                        .setCostAmt(inboundItemVO.getIntoQty().multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP))
                        .setIntoType(Constant.IntoType.PURRETURN_RETURN.getName())
                        .setIntoingQty(BigDecimal.ZERO)
                        .setIntoingAmt(BigDecimal.ZERO)
                ;
                //入库状态
                String intoStatus = this.setIntoStatus(inboundItemVO.getIntoingQty(), inboundItemVO.getIntoQty());
                inboundItemVO.setIntoStatus(intoStatus);
                //实体转换
                InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
                //新建
                inboundItemMapper.insert(inboundItem);
                inboundItemVO.setItemId(inboundItem.getItemId());
                //region 将库存需要的参数赋值
                inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_RETURN.getValue());
                inboundItemVO.setAddOrEditFlag(true);
                //endregion
            }
            //endregion
        }
        //endregion

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

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

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

        //region 编辑总单
        BigDecimal sumIntoQty = inboundVO.getItemList().stream().map(InboundItemVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
        BigDecimal sumIntoAmt = inboundVO.getItemList().stream().map(InboundItemVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
        inboundVO.setIntoQty(sumIntoQty);
        inboundVO.setIntoAmt(sumIntoAmt);
        inboundVO.setIntoingQty(BigDecimal.ZERO);
        inboundVO.setIntoingAmt(BigDecimal.ZERO);
        //入库状态
        String intoStatus = this.setIntoStatus(inboundVO.getIntoingQty(), inboundVO.getIntoQty());
        inboundVO.setIntoStatus(intoStatus);
        //实体转换
        Inbound inbound = inboundConvert.convertToPo(inboundVO);
        //修改
        inboundMapper.update(inbound,
                new UpdateWrapper<Inbound>().lambda()
                        .eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
        );
        //endregion

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

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

        return ResponseResultUtil.success(inboundVO);
    }

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

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

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

        //region 明细数据
        //根据总单id查明细
        List<InboundItemResponse> inboundItemResponseList = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(inbound.getIntoId()));
        for (InboundItemResponse inboundItemResponse : inboundItemResponseList) {
            //region 将库存需要的参数赋值
            inboundItemResponse.setInventoryType(Constant.InventoryType.INBOUND.getName());
            inboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_RETURN.getValue());
            inboundItemResponse.setIntoQty(inboundItemResponse.getIntoQty().negate());
            inboundItemResponse.setIntoAmt(inboundItemResponse.getIntoAmt().negate());
            //endregion

            //region修改入库明细信息
            InboundItem inboundItem = new InboundItem();
            inboundItem
                    .setIntoId(inbound.getIntoId())
                    .setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName())
                    .setIntoQty(inboundItemResponse.getIntoQty())
                    .setIntoAmt(inboundItemResponse.getIntoAmt())
                    .setIntoQty(BigDecimal.ZERO)
                    .setIntoAmt(BigDecimal.ZERO)
                    .setItemId(inboundItemResponse.getItemId())
                    .setCostPrice(BigDecimal.ZERO)
                    .setCostAmt(BigDecimal.ZERO)
            ;
            //修改
            inboundItemMapper.update(inboundItem,
                    new UpdateWrapper<InboundItem>().lambda()
                            .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
            );
            //endregion
        }
        //endregion


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

        return ResponseResultUtil.success();
    }

    /**
     * @desc : 入库状态通用（目前本页面）
     * @date : 2024/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/16 16:28
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public ResponseResultVO<?> selectByUpdate(String id) {
        Map<String, Object> dataInfo = new HashMap<>();
        //总单
        InboundResponse inboundResponse = inboundMapper.selectMessageByOtherQuery(new InboundQuery().setIntoId(id).setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName()));
        //单据不存在
        if (inboundResponse == null) {
            throw new BaseBusinessException(ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_INBOUND_QUANTITY.getCode(),
                    ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_INBOUND_QUANTITY.getMessage());
        }
        dataInfo.put("data", inboundResponse);
        // 明细
        List<InboundItemResponse> inboundItemResponses = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(inboundResponse.getIntoId()));
        dataInfo.put("dataItem", inboundItemResponses);
        return ResponseResultUtil.success(dataInfo);
    }

}
