| | |
| | | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.itemreceipt.vo.MesWmItemReceiptImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.itemreceipt.vo.MesWmItemReceiptImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.itemreceipt.vo.MesWmItemReceiptPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.itemreceipt.vo.MesWmItemReceiptSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.arrivalnotice.MesWmArrivalNoticeDO; |
| | |
| | | import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmItemReceiptStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmTransactionTypeEnum; |
| | | import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.service.qc.iqc.MesQcIqcService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.arrivalnotice.MesWmArrivalNoticeService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.arrivalnotice.MesWmArrivalNoticeLineService; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.arrivalnotice.MesWmArrivalNoticeLineDO; |
| | | import cn.iocoder.yudao.module.mes.service.wm.transaction.MesWmTransactionService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.transaction.dto.MesWmTransactionSaveReqDTO; |
| | | import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi; |
| | | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; |
| | | import cn.iocoder.yudao.module.erp.api.purchase.PurchaseOrderApi; |
| | | import cn.iocoder.yudao.module.srm.api.supplier.SrmSupplierApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import cn.iocoder.yudao.module.system.api.approval.ApprovalConfigApi; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| | | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | |
| | | @Validated |
| | | @Slf4j |
| | | public class MesWmItemReceiptServiceImpl implements MesWmItemReceiptService { |
| | | |
| | | /** |
| | | * 采购入库单审批的业务类型编码 |
| | | * |
| | | * 与 system_approval_config.biz_type 对应,审核人在「系统管理 - 审批配置」中维护。 |
| | | */ |
| | | private static final String ITEM_RECEIPT_APPROVE_BIZ_TYPE = "item_receipt_approve"; |
| | | |
| | | @Resource |
| | | private MesWmItemReceiptMapper itemReceiptMapper; |
| | |
| | | private MesWmArrivalNoticeLineService arrivalNoticeLineService; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | private AdminUserApi adminUserApi; |
| | | |
| | | @Resource |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | |
| | | @Resource |
| | | private PurchaseOrderApi purchaseOrderApi; |
| | |
| | | // 校验物料批次管理:如果物料开启批次管理,则明细的批次号不能为空 |
| | | validateBatchManagement(lines); |
| | | |
| | | // 提交(草稿 → 待上架) |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id).setStatus(MesWmItemReceiptStatusEnum.APPROVING.getStatus())); |
| | | // 提交(草稿 → 待上架),并清空上一轮审核结果 |
| | | itemReceiptMapper.submitAndResetAuditInfo(id, MesWmItemReceiptStatusEnum.APPROVING.getStatus()); |
| | | } |
| | | |
| | | @Override |
| | | public Set<Long> getItemReceiptApproverUserIds() { |
| | | return approvalConfigApi.getApproverUserIds(ITEM_RECEIPT_APPROVE_BIZ_TYPE); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void auditItemReceipt(Long id, Boolean pass, String reviewRemark) { |
| | | // 1. 校验存在 |
| | | MesWmItemReceiptDO receipt = validateItemReceiptExists(id); |
| | | // 2. 校验处于待上架状态 |
| | | if (!MesWmItemReceiptStatusEnum.APPROVING.getStatus().equals(receipt.getStatus())) { |
| | | throw exception(WM_ITEM_RECEIPT_NOT_APPROVING); |
| | | } |
| | | // 3. 校验当前登录用户是该业务类型的审核人(或签:任一人均可审核) |
| | | Long userId = getLoginUserId(); |
| | | approvalConfigApi.validateApprover(ITEM_RECEIPT_APPROVE_BIZ_TYPE, userId); |
| | | // 4. 审核不通过:必须填写原因,退回草稿可修改后重新提交 |
| | | if (!Boolean.TRUE.equals(pass)) { |
| | | if (StrUtil.isBlank(reviewRemark)) { |
| | | throw exception(WM_ITEM_RECEIPT_REJECT_REASON_REQUIRED); |
| | | } |
| | | itemReceiptMapper.auditItemReceipt(id, MesWmItemReceiptStatusEnum.PREPARE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | return; |
| | | } |
| | | // 5. 审核通过:记录审核人与审核意见,状态置为待执行入库 |
| | | itemReceiptMapper.auditItemReceipt(id, MesWmItemReceiptStatusEnum.APPROVED.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户昵称 |
| | | * |
| | | * @param userId 用户编号 |
| | | * @return 昵称,用户不存在时返回 null |
| | | */ |
| | | private String getUserNickname(Long userId) { |
| | | if (userId == null) { |
| | | return null; |
| | | } |
| | | AdminUserRespDTO user = adminUserApi.getUser(userId); |
| | | return user == null ? null : user.getNickname(); |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void stockItemReceipt(Long id) { |
| | | // 校验存在 |
| | | // 校验存在 + 待上架 |
| | | MesWmItemReceiptDO receipt = validateItemReceiptExists(id); |
| | | if (ObjUtil.notEqual(MesWmItemReceiptStatusEnum.APPROVING.getStatus(), receipt.getStatus())) { |
| | | throw exception(WM_ITEM_RECEIPT_STATUS_ERROR); |
| | |
| | | throw exception(WM_ITEM_RECEIPT_DETAIL_QUANTITY_MISMATCH); |
| | | } |
| | | } |
| | | |
| | | // 执行上架(待上架 → 待入库) |
| | | // 上架完成(待上架 → 待执行入库) |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id).setStatus(MesWmItemReceiptStatusEnum.APPROVED.getStatus())); |
| | | } |
| | |
| | | |
| | | @Override |
| | | public MesWmItemReceiptDO validateItemReceiptEditable(Long id) { |
| | | // 草稿与待上架状态允许编辑上架明细:提交后仓库执行上架时录入库位/批次 |
| | | MesWmItemReceiptDO receipt = validateItemReceiptExists(id); |
| | | if (ObjUtil.notEqual(receipt.getStatus(), MesWmItemReceiptStatusEnum.PREPARE.getStatus()) |
| | | && ObjUtil.notEqual(receipt.getStatus(), MesWmItemReceiptStatusEnum.APPROVING.getStatus())) { |
| | | if (ObjUtil.notEqual(MesWmItemReceiptStatusEnum.PREPARE.getStatus(), receipt.getStatus()) |
| | | && ObjUtil.notEqual(MesWmItemReceiptStatusEnum.APPROVING.getStatus(), receipt.getStatus())) { |
| | | throw exception(WM_ITEM_RECEIPT_STATUS_NOT_PREPARE); |
| | | } |
| | | return receipt; |
| | | } |
| | | |
| | | @Override |
| | | public MesWmItemReceiptDO validateItemReceiptLineEditable(Long id) { |
| | | // 行数据仅草稿可改:提交后行数据锁定,避免上架阶段改动入库数量 |
| | | return validateItemReceiptExistsAndDraft(id); |
| | | } |
| | | |
| | | private MesWmItemReceiptDO validateItemReceiptExists(Long id) { |
| | |
| | | return receipt.getId(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitItemReceiptApproval(Long id, String processDefinitionKey) { |
| | | // 1. 校验存在 + 草稿状态 |
| | | validateItemReceiptExistsAndDraft(id); |
| | | |
| | | // 2. 创建 BPM 流程实例 |
| | | Long userId = getCurrentUserId(); |
| | | String processInstanceId = processInstanceApi.createProcessInstance(userId, |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 3. 更新入库单状态为审批中 |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id) |
| | | .setStatus(MesWmItemReceiptStatusEnum.APPROVING.getStatus()) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateItemReceiptAuditStatus(Long id, Integer bpmResult) { |
| | | // 1. 校验存在 |
| | | MesWmItemReceiptDO receipt = validateItemReceiptExists(id); |
| | | if (!MesWmItemReceiptStatusEnum.APPROVING.getStatus().equals(receipt.getStatus())) { |
| | | log.warn("[updateItemReceiptAuditStatus] 入库单({}) 不处于审批中状态", id); |
| | | return; |
| | | } |
| | | |
| | | // 2. 根据审批结果更新状态 |
| | | Integer newStatus = convertBpmResultToReceiptStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id).setStatus(newStatus)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换 BPM 审批结果为入库单状态 |
| | | */ |
| | | private Integer convertBpmResultToReceiptStatus(Integer bpmResult) { |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | return MesWmItemReceiptStatusEnum.APPROVED.getStatus(); |
| | | } else if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmResult)) { |
| | | return MesWmItemReceiptStatusEnum.PREPARE.getStatus(); |
| | | } else if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmResult)) { |
| | | return MesWmItemReceiptStatusEnum.CANCELED.getStatus(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前用户 ID |
| | | */ |
| | | private Long getCurrentUserId() { |
| | | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication != null && authentication.getPrincipal() instanceof Long) { |
| | | return (Long) authentication.getPrincipal(); |
| | | } |
| | | return 1L; // 默认用户 |
| | | } |
| | | |
| | | /** |
| | | * 生成入库单编码 |
| | | */ |
| | |
| | | return "IR" + System.currentTimeMillis(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesWmItemReceiptImportRespVO importItemReceiptList(List<MesWmItemReceiptImportExcelVO> importList, boolean updateSupport) { |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(WM_ITEM_RECEIPT_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | MesWmItemReceiptImportRespVO respVO = MesWmItemReceiptImportRespVO.builder() |
| | | .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>()) |
| | | .failureCodes(new LinkedHashMap<>()).build(); |
| | | AtomicInteger index = new AtomicInteger(1); |
| | | importList.forEach(importItem -> { |
| | | int currentIndex = index.getAndIncrement(); |
| | | String code = importItem.getCode(); |
| | | String key = StrUtil.blankToDefault(code, "第 " + currentIndex + " 行"); |
| | | if (StrUtil.isBlank(code)) { |
| | | respVO.getFailureCodes().put(key, "单号不能为空"); |
| | | return; |
| | | } |
| | | if (StrUtil.isBlank(importItem.getName())) { |
| | | respVO.getFailureCodes().put(key, "名称不能为空"); |
| | | return; |
| | | } |
| | | MesWmItemReceiptDO existing = itemReceiptMapper.selectByCode(code); |
| | | if (existing != null && updateSupport) { |
| | | if (!MesWmItemReceiptStatusEnum.PREPARE.getStatus().equals(existing.getStatus())) { |
| | | respVO.getFailureCodes().put(key, "单据非草稿状态,不允许覆盖更新"); |
| | | return; |
| | | } |
| | | MesWmItemReceiptDO updateObj = BeanUtils.toBean(importItem, MesWmItemReceiptDO.class); |
| | | updateObj.setId(existing.getId()); |
| | | updateObj.setStatus(existing.getStatus()); |
| | | itemReceiptMapper.updateById(updateObj); |
| | | respVO.getUpdateCodes().add(code); |
| | | } else if (existing != null) { |
| | | respVO.getFailureCodes().put(key, "单号已存在:" + code); |
| | | } else { |
| | | MesWmItemReceiptDO createObj = BeanUtils.toBean(importItem, MesWmItemReceiptDO.class); |
| | | createObj.setId(null); |
| | | createObj.setStatus(MesWmItemReceiptStatusEnum.PREPARE.getStatus()); |
| | | itemReceiptMapper.insert(createObj); |
| | | respVO.getCreateCodes().add(code); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |