| | |
| | | import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmReturnVendorStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmTransactionTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.service.md.vendor.MesMdVendorService; |
| | | import cn.iocoder.yudao.module.srm.api.supplier.SrmSupplierApi; |
| | | 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.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; |
| | | import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; |
| | | import jakarta.annotation.Resource; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.flowable.engine.repository.ProcessDefinition; |
| | | 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.List; |
| | | import java.util.*; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| | |
| | | */ |
| | | @Service |
| | | @Validated |
| | | @Slf4j |
| | | public class MesWmReturnVendorServiceImpl implements MesWmReturnVendorService { |
| | | |
| | | @Resource |
| | |
| | | @Resource |
| | | private MesWmReturnVendorDetailService returnVendorDetailService; |
| | | @Resource |
| | | private MesMdVendorService vendorService; |
| | | private SrmSupplierApi srmSupplierApi; |
| | | @Resource |
| | | private MesWmTransactionService wmTransactionService; |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitReturnVendor(Long id) { |
| | | public void submitReturnVendorApproval(Long id, String processDefinitionKey) { |
| | | // 校验存在 + 草稿状态 |
| | | validateReturnVendorExistsAndPrepare(id); |
| | | // 校验至少有一条行 |
| | |
| | | if (CollUtil.isEmpty(lines)) { |
| | | throw exception(WM_RETURN_VENDOR_NO_LINE); |
| | | } |
| | | |
| | | // 提交(草稿 -> 待拣货) |
| | | // 创建 BPM 流程实例 |
| | | String processInstanceId = processInstanceApi.createProcessInstance(getCurrentUserId(), |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | // 提交审批(草稿 → 审批中) |
| | | returnVendorMapper.updateById(new MesWmReturnVendorDO() |
| | | .setId(id).setStatus(MesWmReturnVendorStatusEnum.APPROVING.getStatus())); |
| | | .setId(id) |
| | | .setStatus(MesWmReturnVendorStatusEnum.APPROVING.getStatus()) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getReturnVendorApprovalProcessDefinitionList() { |
| | | return getProcessDefinitionListByCategory(APPROVE_CATEGORY_CODE); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateReturnVendorAuditStatus(Long id, Integer bpmResult) { |
| | | // 1. 校验存在 |
| | | MesWmReturnVendorDO returnVendor = validateReturnVendorExists(id); |
| | | if (!MesWmReturnVendorStatusEnum.APPROVING.getStatus().equals(returnVendor.getStatus())) { |
| | | log.warn("[updateReturnVendorAuditStatus] 供应商退货单({}) 不处于审批中状态", id); |
| | | return; |
| | | } |
| | | // 2. 根据审批结果更新状态 |
| | | Integer newStatus = convertBpmResultToStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | returnVendorMapper.updateById(new MesWmReturnVendorDO().setId(id).setStatus(newStatus)); |
| | | } |
| | | } |
| | | |
| | | private Integer convertBpmResultToStatus(Integer bpmResult) { |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | return MesWmReturnVendorStatusEnum.APPROVED.getStatus(); |
| | | } |
| | | if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmResult)) { |
| | | return MesWmReturnVendorStatusEnum.PREPARE.getStatus(); |
| | | } |
| | | if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmResult)) { |
| | | return MesWmReturnVendorStatusEnum.CANCELED.getStatus(); |
| | | } |
| | | 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; // 默认用户 |
| | | } |
| | | |
| | | @Override |
| | |
| | | validateReturnVendorExistsAndPrepare(reqVO.getId()); |
| | | } |
| | | // 校验供应商存在 |
| | | vendorService.validateVendorExistsAndEnable(reqVO.getVendorId()); |
| | | srmSupplierApi.getSupplier(reqVO.getVendorId()); |
| | | // 校验编码唯一 |
| | | validateCodeUnique(reqVO.getId(), reqVO.getCode()); |
| | | } |