| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | 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.enums.task.BpmTaskStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanImportExcelVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanImportReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanImportRespVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanPageReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanRespVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanSaveReqVO; |
| | |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchasePlanItemDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseRequestDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseRequestItemDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpSupplierDO; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchasePlanItemMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchasePlanMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseRequestItemMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseRequestMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpSupplierMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.redis.no.ErpNoRedisDAO; |
| | | import cn.iocoder.yudao.module.erp.enums.ErpPurchasePlanStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.enums.ErpPurchaseRequestStatusEnum; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | import cn.iocoder.yudao.module.mdm.api.unit.MdmUnitMeasureApi; |
| | | import cn.iocoder.yudao.module.mdm.api.unit.dto.MdmUnitMeasureRespDTO; |
| | | import cn.iocoder.yudao.module.mes.api.wms.MesWmStockApi; |
| | | import cn.iocoder.yudao.module.system.api.approval.ApprovalConfigApi; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | |
| | | |
| | | 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.common.util.collection.CollectionUtils.filterList; |
| | | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | | import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*; |
| | | |
| | |
| | | * 通用审批配置的业务类型编码(对应 system_approval_config.biz_type) |
| | | */ |
| | | private static final String PURCHASE_PLAN_APPROVE_BIZ_TYPE = "purchase_plan_approve"; |
| | | |
| | | /** |
| | | * 采购计划审批的 BPM 流程定义 Key |
| | | */ |
| | | private static final String PURCHASE_PLAN_PROCESS_KEY = "purchase_plan_approve"; |
| | | |
| | | /** |
| | | * 需求来源:库存预警 |
| | |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi bpmProcessInstanceApi; |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | | |
| | | @Resource |
| | | private MdmItemApi mdmItemApi; |
| | | @Resource |
| | | private MdmUnitMeasureApi mdmUnitMeasureApi; |
| | | @Resource |
| | | private ErpSupplierMapper supplierMapper; |
| | | @Resource |
| | | private MesWmStockApi mesWmStockApi; |
| | | |
| | |
| | | // 3. 校验审批配置已启用且配置了有效审批人(未配置时抛出带明确提示的业务异常) |
| | | approvalConfigApi.validateApprovalEnabledAndGetApprovers(PURCHASE_PLAN_APPROVE_BIZ_TYPE); |
| | | // 4. 状态置为审批中,并清空上一轮审核结果,避免残留「审核不通过」的原因 |
| | | purchasePlanMapper.submitAndResetAuditInfo(id, ErpPurchasePlanStatusEnum.PROCESS.getStatus()); |
| | | if (purchasePlanMapper.submitAndResetAuditInfo(id, ErpPurchasePlanStatusEnum.PROCESS.getStatus()) != 1) { |
| | | throw exception(PURCHASE_PLAN_SUBMIT_FAIL_NOT_ALLOWED); |
| | | } |
| | | // 5. 创建 BPM 流程实例,使审批出现在「协同办公 - 我的流程」 |
| | | bpmProcessInstanceApi.createProcessInstance(getLoginUserId(), |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(PURCHASE_PLAN_PROCESS_KEY) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (StrUtil.isBlank(reviewRemark)) { |
| | | throw exception(PURCHASE_PLAN_AUDIT_REJECT_REASON_REQUIRED); |
| | | } |
| | | purchasePlanMapper.auditPurchasePlan(id, ErpPurchasePlanStatusEnum.REJECT.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | if (purchasePlanMapper.auditPurchasePlan(id, ErpPurchasePlanStatusEnum.REJECT.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark) != 1) { |
| | | throw exception(PURCHASE_PLAN_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | return; |
| | | } |
| | | // 5. 审核通过:记录审核人与审核意见,状态置为审核通过 |
| | | purchasePlanMapper.auditPurchasePlan(id, ErpPurchasePlanStatusEnum.APPROVE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | if (purchasePlanMapper.auditPurchasePlan(id, ErpPurchasePlanStatusEnum.APPROVE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark) != 1) { |
| | | throw exception(PURCHASE_PLAN_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | // 6. 审核通过且未生成申请,自动生成采购申请 |
| | | if (!Boolean.TRUE.equals(plan.getGeneratedFlag())) { |
| | | try { |
| | |
| | | } catch (Exception e) { |
| | | log.error("[updatePurchasePlanAuditStatus] 采购计划({}) 自动生成采购申请失败", id, e); |
| | | // 不抛出异常,避免影响状态更新 |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updatePurchasePlanAuditStatus(Long id, Integer bpmStatus, String reason) { |
| | | // 1. BPM 审批结果映射为采购计划状态:通过=20 / 不通过=30 / 取消=40 |
| | | Integer newStatus = convertBpmResultToStatus(bpmStatus); |
| | | if (newStatus == null) { |
| | | return; |
| | | } |
| | | // 2. 仅当仍处于「审批中」时回写,避免重复覆盖 |
| | | if (purchasePlanMapper.updateAuditStatusByBpm(id, newStatus, reason) != 1) { |
| | | log.warn("[updatePurchasePlanAuditStatus] 采购计划({}) 不处于审批中状态,忽略 BPM 结果({})", id, bpmStatus); |
| | | return; |
| | | } |
| | | // 3. 审批通过且未生成申请,自动生成采购申请 |
| | | if (ErpPurchasePlanStatusEnum.APPROVE.getStatus().equals(newStatus)) { |
| | | ErpPurchasePlanDO plan = purchasePlanMapper.selectById(id); |
| | | if (plan != null && !Boolean.TRUE.equals(plan.getGeneratedFlag())) { |
| | | try { |
| | | List<Long> requestIds = doGeneratePurchaseRequests(plan); |
| | | log.info("[updatePurchasePlanAuditStatus] 采购计划({}) 自动生成采购申请({})", id, requestIds); |
| | | } catch (Exception e) { |
| | | log.error("[updatePurchasePlanAuditStatus] 采购计划({}) 自动生成采购申请失败", id, e); |
| | | // 不抛出异常,避免影响状态更新 |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | return List.of(request.getId()); |
| | | } |
| | | |
| | | @Override |
| | | public ErpPurchasePlanImportRespVO importPurchasePlanList(List<ErpPurchasePlanImportExcelVO> importPlans, |
| | | ErpPurchasePlanImportReqVO importReqVO) { |
| | | // 1. 校验非空(过滤完全空白的行) |
| | | importPlans = filterList(importPlans, item -> StrUtil.isNotBlank(item.getProductCode())); |
| | | if (CollUtil.isEmpty(importPlans)) { |
| | | throw exception(PURCHASE_PLAN_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 按「单号」分组:相同单号的多行合并成一张采购计划;单号为空时每行各自成一张计划 |
| | | List<List<ErpPurchasePlanImportExcelVO>> groups = new ArrayList<>(); |
| | | Map<String, Integer> groupIndexMap = new LinkedHashMap<>(); |
| | | int rowIndex = 0; |
| | | for (ErpPurchasePlanImportExcelVO row : importPlans) { |
| | | rowIndex++; |
| | | String groupKey = StrUtil.isNotBlank(row.getNo()) ? row.getNo() : ("__row_" + rowIndex); |
| | | Integer index = groupIndexMap.get(groupKey); |
| | | if (index == null) { |
| | | index = groups.size(); |
| | | groupIndexMap.put(groupKey, index); |
| | | groups.add(new ArrayList<>()); |
| | | } |
| | | groups.get(index).add(row); |
| | | } |
| | | // 3. 逐组合并创建采购计划 |
| | | ErpPurchasePlanImportRespVO respVO = ErpPurchasePlanImportRespVO.builder() |
| | | .createPurchasePlanNos(new ArrayList<>()) |
| | | .failurePurchasePlanNos(new LinkedHashMap<>()) |
| | | .build(); |
| | | for (List<ErpPurchasePlanImportExcelVO> group : groups) { |
| | | String groupKey = StrUtil.isNotBlank(group.get(0).getNo()) ? group.get(0).getNo() |
| | | : "第" + (importPlans.indexOf(group.get(0)) + 1) + "行"; |
| | | try { |
| | | ErpPurchasePlanSaveReqVO createReqVO = buildPurchasePlanImportSaveReqVO(group); |
| | | Long planId = createPurchasePlan(createReqVO); |
| | | respVO.getCreatePurchasePlanNos().add(purchasePlanMapper.selectById(planId).getNo()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailurePurchasePlanNos().put(groupKey, ex.getMessage()); |
| | | } |
| | | } |
| | | return respVO; |
| | | } |
| | | |
| | | /** |
| | | * 将同一张采购计划的多行明细转换成创建 Request VO |
| | | */ |
| | | private ErpPurchasePlanSaveReqVO buildPurchasePlanImportSaveReqVO(List<ErpPurchasePlanImportExcelVO> group) { |
| | | ErpPurchasePlanImportExcelVO first = group.get(0); |
| | | // 1. 校验计划名称与计划日期 |
| | | if (StrUtil.isBlank(first.getName())) { |
| | | throw exception(PURCHASE_PLAN_IMPORT_NAME_REQUIRED); |
| | | } |
| | | if (first.getPlanDate() == null) { |
| | | throw exception(PURCHASE_PLAN_IMPORT_PLAN_DATE_REQUIRED); |
| | | } |
| | | // 2. 解析供应商(可选) |
| | | Long supplierId = null; |
| | | if (StrUtil.isNotBlank(first.getSupplierName())) { |
| | | ErpSupplierDO supplier = supplierMapper.selectByName(first.getSupplierName()); |
| | | if (supplier == null) { |
| | | throw exception(PURCHASE_PLAN_IMPORT_SUPPLIER_NOT_EXISTS, first.getSupplierName()); |
| | | } |
| | | supplierId = supplier.getId(); |
| | | } |
| | | // 3. 组装明细 |
| | | List<ErpPurchasePlanSaveReqVO.Item> items = new ArrayList<>(); |
| | | for (ErpPurchasePlanImportExcelVO row : group) { |
| | | MdmItemRespDTO product = mdmItemApi.getItemByCode(row.getProductCode()).getCheckedData(); |
| | | if (product == null) { |
| | | throw exception(PURCHASE_PLAN_IMPORT_PRODUCT_NOT_EXISTS, row.getProductCode()); |
| | | } |
| | | MdmUnitMeasureRespDTO unit = mdmUnitMeasureApi.getUnitMeasureByCode(row.getProductUnitCode()).getCheckedData(); |
| | | if (unit == null) { |
| | | throw exception(PURCHASE_PLAN_IMPORT_UNIT_NOT_EXISTS, row.getProductUnitCode()); |
| | | } |
| | | ErpPurchasePlanSaveReqVO.Item item = new ErpPurchasePlanSaveReqVO.Item() |
| | | .setProductId(product.getId()) |
| | | .setProductUnitId(unit.getId()) |
| | | .setCount(row.getCount()) |
| | | .setDemandTime(row.getDemandTime()) |
| | | .setRemark(row.getItemRemark()); |
| | | items.add(item); |
| | | } |
| | | // 4. 组装主表 |
| | | return new ErpPurchasePlanSaveReqVO() |
| | | .setName(first.getName()) |
| | | .setDemandSource(first.getDemandSource()) |
| | | .setPlanDate(first.getPlanDate()) |
| | | .setSupplierId(supplierId) |
| | | .setRemark(first.getRemark()) |
| | | .setItems(items); |
| | | } |
| | | |
| | | private Integer convertBpmResultToStatus(Integer bpmResult) { |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | return ErpPurchasePlanStatusEnum.APPROVE.getStatus(); |
| | |
| | | |
| | | private ErpPurchasePlanDO validatePurchasePlanCanModify(Long id) { |
| | | ErpPurchasePlanDO plan = validatePurchasePlanExists(id); |
| | | if (!ErpPurchasePlanStatusEnum.DRAFT.getStatus().equals(plan.getStatus())) { |
| | | if (!ErpPurchasePlanStatusEnum.DRAFT.getStatus().equals(plan.getStatus()) |
| | | && !ErpPurchasePlanStatusEnum.REJECT.getStatus().equals(plan.getStatus())) { |
| | | throw exception(PURCHASE_PLAN_UPDATE_FAIL_NOT_DRAFT, plan.getNo()); |
| | | } |
| | | return plan; |