| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi; |
| | | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; |
| | | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; |
| | | import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.equipmentreceipt.vo.MesWmEquipmentReceiptPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.equipmentreceipt.vo.MesWmEquipmentReceiptSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.equipmentreceipt.MesWmEquipmentReceiptDO; |
| | |
| | | import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseAreaService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseLocationService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseService; |
| | | 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 jakarta.annotation.Resource; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.flowable.engine.repository.ProcessDefinition; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | |
| | | 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 MesWmEquipmentReceiptServiceImpl implements MesWmEquipmentReceiptService { |
| | | |
| | | /** |
| | | * 设备入库单审批的业务类型编码 |
| | | * |
| | | * 与 system_approval_config.biz_type 对应,审核人在「系统管理 - 审批配置」中维护。 |
| | | */ |
| | | private static final String EQUIPMENT_RECEIPT_APPROVE_BIZ_TYPE = "equipment_receipt_approve"; |
| | | |
| | | @Resource |
| | | private MesWmEquipmentReceiptMapper equipmentReceiptMapper; |
| | |
| | | @Resource |
| | | private MesWmWarehouseAreaService wmAreaService; |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | private AdminUserApi adminUserApi; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | |
| | | @Override |
| | | public Long createEquipmentReceipt(MesWmEquipmentReceiptSaveReqVO createReqVO) { |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitEquipmentReceiptApproval(Long id, String processDefinitionKey) { |
| | | // 校验存在 + 草稿状态 |
| | | public void submitEquipmentReceiptApproval(Long id) { |
| | | // 1. 校验存在 + 草稿状态 |
| | | validateEquipmentReceiptExistsAndPrepare(id); |
| | | // 校验至少有一条行 |
| | | // 2. 校验至少有一条行 |
| | | List<MesWmEquipmentReceiptLineDO> lines = equipmentReceiptLineService.getEquipmentReceiptLineListByReceiptId(id); |
| | | if (CollUtil.isEmpty(lines)) { |
| | | throw exception(WM_EQUIP_RECEIPT_NO_LINE); |
| | | } |
| | | // 创建 BPM 流程实例 |
| | | String processInstanceId = processInstanceApi.createProcessInstance(getCurrentUserId(), |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | // 提交审批(草稿 → 审批中) |
| | | equipmentReceiptMapper.updateById(new MesWmEquipmentReceiptDO() |
| | | .setId(id) |
| | | .setStatus(MesWmEquipmentReceiptStatusEnum.APPROVING.getStatus()) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | // 3. 校验审批配置已启用且配置了有效审核人(未配置时抛出带明确提示的业务异常) |
| | | approvalConfigApi.validateApprovalEnabledAndGetApprovers(EQUIPMENT_RECEIPT_APPROVE_BIZ_TYPE); |
| | | // 4. 状态置为审批中,并清空上一轮审核结果 |
| | | equipmentReceiptMapper.submitAndResetAuditInfo(id, MesWmEquipmentReceiptStatusEnum.APPROVING.getStatus()); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getEquipmentReceiptApprovalProcessDefinitionList() { |
| | | return getProcessDefinitionListByCategory(APPROVE_CATEGORY_CODE); |
| | | public Set<Long> getEquipmentReceiptApproverUserIds() { |
| | | return approvalConfigApi.getApproverUserIds(EQUIPMENT_RECEIPT_APPROVE_BIZ_TYPE); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateEquipmentReceiptAuditStatus(Long id, Integer bpmResult) { |
| | | public void auditEquipmentReceipt(Long id, Boolean pass, String reviewRemark) { |
| | | // 1. 校验存在 |
| | | MesWmEquipmentReceiptDO receipt = validateEquipmentReceiptExists(id); |
| | | // 2. 校验处于审批中状态 |
| | | if (!MesWmEquipmentReceiptStatusEnum.APPROVING.getStatus().equals(receipt.getStatus())) { |
| | | log.warn("[updateEquipmentReceiptAuditStatus] 设备入库单({}) 不处于审批中状态", id); |
| | | throw exception(WM_EQUIP_RECEIPT_NOT_APPROVING); |
| | | } |
| | | // 3. 校验当前登录用户是该业务类型的审核人(或签:任一人均可审核) |
| | | Long userId = getLoginUserId(); |
| | | approvalConfigApi.validateApprover(EQUIPMENT_RECEIPT_APPROVE_BIZ_TYPE, userId); |
| | | // 4. 审核不通过:必须填写原因,退回草稿可修改后重新提交 |
| | | if (!Boolean.TRUE.equals(pass)) { |
| | | if (StrUtil.isBlank(reviewRemark)) { |
| | | throw exception(WM_EQUIP_RECEIPT_REJECT_REASON_REQUIRED); |
| | | } |
| | | equipmentReceiptMapper.auditEquipmentReceipt(id, MesWmEquipmentReceiptStatusEnum.PREPARE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | return; |
| | | } |
| | | // 2. 根据审批结果更新状态 |
| | | Integer newStatus = convertBpmResultToStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | equipmentReceiptMapper.updateById(new MesWmEquipmentReceiptDO().setId(id).setStatus(newStatus)); |
| | | } |
| | | // 5. 审核通过:记录审核人与审核意见,状态置为已审批(可执行入库) |
| | | equipmentReceiptMapper.auditEquipmentReceipt(id, MesWmEquipmentReceiptStatusEnum.APPROVED.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | } |
| | | |
| | | private Integer convertBpmResultToStatus(Integer bpmResult) { |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | return MesWmEquipmentReceiptStatusEnum.APPROVED.getStatus(); |
| | | } |
| | | if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmResult)) { |
| | | return MesWmEquipmentReceiptStatusEnum.PREPARE.getStatus(); |
| | | } |
| | | if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmResult)) { |
| | | return MesWmEquipmentReceiptStatusEnum.CANCELED.getStatus(); |
| | | } |
| | | /** |
| | | * 获取用户昵称 |
| | | * |
| | | * @param userId 用户编号 |
| | | * @return 昵称,用户不存在时返回 null |
| | | */ |
| | | private String getUserNickname(Long userId) { |
| | | if (userId == null) { |
| | | return null; |
| | | } |
| | | |
| | | private List<Map<String, Object>> getProcessDefinitionListByCategory(String categoryCode) { |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(categoryCode); |
| | | if (CollUtil.isEmpty(definitionInfoList)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | // 过滤激活状态,保留最新版本 |
| | | Map<String, ProcessDefinition> latestVersionMap = new HashMap<>(); |
| | | for (BpmProcessDefinitionInfoDO info : definitionInfoList) { |
| | | ProcessDefinition pd = bpmProcessDefinitionService.getProcessDefinition(info.getProcessDefinitionId()); |
| | | if (pd == null || pd.isSuspended()) { |
| | | continue; |
| | | } |
| | | ProcessDefinition existing = latestVersionMap.get(pd.getKey()); |
| | | if (existing == null || pd.getVersion() > existing.getVersion()) { |
| | | latestVersionMap.put(pd.getKey(), pd); |
| | | } |
| | | } |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | for (ProcessDefinition pd : latestVersionMap.values()) { |
| | | Map<String, Object> item = new HashMap<>(); |
| | | item.put("id", pd.getId()); |
| | | item.put("key", pd.getKey()); |
| | | item.put("name", pd.getName()); |
| | | result.add(item); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private Long getCurrentUserId() { |
| | | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication != null && authentication.getPrincipal() instanceof Long) { |
| | | return (Long) authentication.getPrincipal(); |
| | | } |
| | | return 1L; // 默认用户 |
| | | AdminUserRespDTO user = adminUserApi.getUser(userId); |
| | | return user == null ? null : user.getNickname(); |
| | | } |
| | | |
| | | @Override |