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.mapper.BaseMapper; import com.dk.common.model.pojo.PageList; 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.ivt.InboundItemMapper; import com.dk.mdm.mapper.ivt.InboundMapper; import com.dk.mdm.model.pojo.ivt.Inbound; import com.dk.mdm.model.pojo.ivt.InboundItem; import com.dk.mdm.model.query.ivt.InboundItemQuery; import com.dk.mdm.model.query.ivt.InboundQuery; import com.dk.mdm.model.query.sale.OutReturnItemQuery; import com.dk.mdm.model.response.ivt.InboundItemResponse; import com.dk.mdm.model.response.ivt.InboundResponse; import com.dk.mdm.model.response.sale.OutReturnItemResponse; import com.dk.mdm.model.vo.ivt.InboundItemVO; import com.dk.mdm.model.vo.ivt.InboundVO; import com.dk.mdm.service.common.CommonService; 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; /** * @desc : 其他入库业务层 * @date : 2024/3/7 14:11 * @author : 寇珊珊 */ @Service @Transactional public class InboundOtherService extends BaseService { @Override public BaseMapper getRepository() { return inboundMapper; } @Autowired private InboundMapper inboundMapper; @Autowired private InboundConvert inboundConvert; @Autowired private InboundItemMapper inboundItemMapper; @Autowired private InboundItemConvert inboundItemConvert; @Autowired private CommonService commonService; /** * @desc : 条件查询 * @date : 2024/3/7 14:12 * @author : 寇珊珊 */ @Pagination public ResponseResultVO> selectInboundAndItem(InboundQuery inboundQuery) { return super.mergeListWithCount(inboundQuery, inboundMapper.selectInboundAndItem(inboundQuery), inboundMapper.selectInboundAndItemCountByCond(inboundQuery)); } /** * @desc : 查询明细 * @date : 2024/3/9 15:43 * @author : 寇珊珊 */ @Pagination public ResponseResultVO> selectOtherInboundItemInfoById(String id) { Map result = new HashMap<>(); // 商品明细 List 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 otherInboundInsert(InboundVO inboundVO) { //region 总单 //获取 id/单号 Map codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.OTHERINBOUND.getName(),false); inboundVO.setIntoId(codeMap.get("outId").toString()). setIntoNo(codeMap.get("outNote").toString()); //入库类型 inboundVO.setIntoType(Constant.IntoType.OTHER.getName()); //自动入库标识 if (inboundVO.getAutomaticFlg()) { //已入库 inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName()); } else { //入库中 inboundVO.setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName()); } //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额 if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) { inboundVO.setIntoQty(inboundVO.getIntoingQty()) .setIntoAmt(inboundVO.getIntoingAmt()); } //实体转换 Inbound inbound = inboundConvert.convertToPo(inboundVO); inboundMapper.insert(inbound); //endregion //region 明细 //校验明细 if (inboundVO.getItemList().size() == 0) { return ResponseResultUtil.error(ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getCode(), ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage()); } for (InboundItemVO inboundItemVO : inboundVO.getItemList()) { Map map = commonService.getUniqueNoteCode(Constant.docNameConstant.OTHERINBOUNDITEM.getName(),true); //明细id inboundItemVO.setItemId(map.get("outId").toString()); //总单id inboundItemVO.setIntoId(inboundVO.getIntoId()); //入库类型 inboundItemVO.setIntoType(inboundVO.getIntoType()); //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额 if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) { inboundItemVO.setIntoQty(inboundItemVO.getIntoingQty()) .setIntoAmt(inboundItemVO.getIntoingAmt()); } //成本单价 inboundItemVO.setCostPrice(inboundItemVO.getCostPrice()); //成本金额 inboundItemVO.setCostAmt(inboundItemVO.getIntoingQty().multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP)); //库存数量 已入库-出库 inboundItemVO.setInvQty(inboundItemVO.getReturnQty().subtract(inboundItemVO.getOutQty())); //入库状态 inboundItemVO.setIntoStatus(inboundVO.getIntoStatus()); //实体转换 InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO); inboundItemMapper.insert(inboundItem); } //endregion //todo 如果是已入库 调用库存 后续写库存这里补上 //region 库存 //endregion return ResponseResultUtil.success(inboundVO); } /** * @desc : 其他入库办理 * @date : 2024/3/7 15:47 * @author : 寇珊珊 */ @Transactional(rollbackFor = {Exception.class}) public ResponseResultVO otherHandleInbound(InboundVO inboundVO) { //大编辑标识 if (inboundVO.getAllEdit() != null && inboundVO.getAllEdit()) { //region 编辑明细 //校验明细 if (inboundVO.getItemList().size() == 0) { return ResponseResultUtil.error(ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getCode(), ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage()); } for (InboundItemVO inboundItemVO : inboundVO.getItemList()) { if (inboundItemVO.getIntoId() != null) { //编辑明细 inboundItemVO.setCostPrice(inboundItemVO.getPriceInto()) .setCostAmt(inboundItemVO.getIntoingQty().multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP)) .setIntoQty(inboundItemVO.getIntoingQty()) .setIntoAmt(inboundItemVO.getIntoingAmt()); //入库状态 String intoStatus = this.setIntoStatus(inboundItemVO.getIntoingQty(), inboundItemVO.getIntoQty()); inboundItemVO.setIntoStatus(intoStatus); //实体转换 InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO); //修改 inboundItemMapper.updateById(inboundItem); } //endregion //region 新建明细 else { Map map = commonService.getUniqueNoteCode(Constant.docNameConstant.OTHERINBOUNDITEM.getName(),true); inboundItemVO .setItemId(map.get("outId").toString()) .setIntoQty(inboundItemVO.getIntoingQty()) .setIntoAmt(inboundItemVO.getIntoingAmt()) .setIntoId(inboundVO.getIntoId()) .setCostPrice(inboundItemVO.getPriceInto()) .setCostAmt(inboundItemVO.getIntoingQty().multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP)) .setIntoType(Constant.IntoType.OTHER.getName()); //入库状态 String intoStatus = this.setIntoStatus(inboundItemVO.getIntoingQty(), inboundItemVO.getIntoQty()); inboundItemVO.setIntoStatus(intoStatus); //实体转换 InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO); //新建 inboundItemMapper.insert(inboundItem); } } //endregion //region 删除明细 if (inboundVO.getDeleteItemList() != null) { for (InboundItemVO inboundItemVO : inboundVO.getDeleteItemList()) { if (inboundItemVO.getItemId() != null) { InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO); inboundItem.setFlgValid(false); inboundItemMapper.updateById(inboundItem); } } } //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); //已入库数量==入库中数量 入库完成 if(sumIntoQty.compareTo(inboundVO.getIntoQty())==0){ //已入库 inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName()); } else { //入库中 inboundVO.setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName()); } //实体转换 Inbound inbound = inboundConvert.convertToPo(inboundVO); //修改 super.update(inbound); //endregion //todo 如果是已入库 调用库存 后续写库存这里补上 //region 修改库存 //endregion } return ResponseResultUtil.success(inboundVO); } /** * @desc : 其它入库撤销 * @date : 2024/3/7 17:06 * @author : 寇珊珊 */ @Transactional(rollbackFor = {Exception.class}) public ResponseResultVO otherInboundCancel(InboundVO inboundVO) { //region 总单数据信息 InboundResponse inboundResponse = inboundMapper.selectById(inboundVO.getIntoId()); Inbound inbound = new Inbound(); inbound.setIntoId(inboundResponse.getIntoId()) .setIntoDate(null) .setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName()) .setIntoQty(BigDecimal.ZERO) .setIntoAmt(BigDecimal.ZERO); inboundMapper.updateById(inbound); //endregion //region 明细数据 InboundItemQuery inboundItemQuery = new InboundItemQuery().setIntoId(inbound.getIntoId()); //根据总单查明细 List orderEntryItemResponsesList = inboundItemMapper.selectByCond(inboundItemQuery); for (InboundItemResponse inboundItemResponse : orderEntryItemResponsesList) { InboundItem inboundItem = new InboundItem(); inboundItem.setIntoId(inbound.getIntoId()) .setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName()) .setIntoQty(BigDecimal.ZERO) .setIntoAmt(BigDecimal.ZERO) .setItemId(inboundItemResponse.getItemId()); inboundItemMapper.updateById(inboundItem); } //endregion //region 修改库存 //endregion return ResponseResultUtil.success(); } /** * @desc : 入库状态通用(目前本页面) * @date : 2024/3/9 8:59 * @author : 寇珊珊 */ 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.RUKUZHONG.getName(); } return intoStatus; } }