package cn.iocoder.yudao.module.srm.service.apply; 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.framework.common.exception.util.ServiceExceptionUtil; import cn.iocoder.yudao.module.srm.controller.admin.apply.vo.SrmSupplierApplyPageReqVO; import cn.iocoder.yudao.module.srm.controller.admin.apply.vo.SrmSupplierApplySaveReqVO; import cn.iocoder.yudao.module.srm.dal.dataobject.SrmSupplierApplyDO; import cn.iocoder.yudao.module.srm.dal.dataobject.SrmSupplierDO; import cn.iocoder.yudao.module.srm.dal.mysql.SrmSupplierApplyMapper; import cn.iocoder.yudao.module.srm.dal.mysql.SrmSupplierMapper; import cn.iocoder.yudao.module.srm.enums.ErrorCodeConstants; import cn.iocoder.yudao.module.srm.enums.SupplierApplyStatusEnum; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import java.time.LocalDateTime; /** * SRM 供应商准入申请 Service 实现类 */ @Service @Validated public class SrmSupplierApplyServiceImpl implements SrmSupplierApplyService { @Resource private SrmSupplierApplyMapper supplierApplyMapper; @Resource private SrmSupplierMapper supplierMapper; @Override public Long createApply(SrmSupplierApplySaveReqVO createReqVO) { // 校验:必须选择 SRM 供应商或手动填写供应商名称 if (createReqVO.getSupplierId() != null) { SrmSupplierDO supplier = supplierMapper.selectById(createReqVO.getSupplierId()); if (supplier == null) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.SUPPLIER_NOT_EXISTS); } } else if (StrUtil.isBlank(createReqVO.getSupplierName())) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.APPLY_SUPPLIER_NAME_REQUIRED); } SrmSupplierApplyDO apply = BeanUtils.toBean(createReqVO, SrmSupplierApplyDO.class); apply.setApplyNo(generateApplyNo()); apply.setApplyStatus(SupplierApplyStatusEnum.DRAFT.getCode()); apply.setApplyTime(LocalDateTime.now()); supplierApplyMapper.insert(apply); return apply.getId(); } @Override public void updateApply(SrmSupplierApplySaveReqVO updateReqVO) { SrmSupplierApplyDO apply = validateApplyExists(updateReqVO.getId()); if (!SupplierApplyStatusEnum.DRAFT.getCode().equals(apply.getApplyStatus())) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.APPLY_STATUS_NOT_DRAFT); } // 校验:必须选择 SRM 供应商或手动填写供应商名称 if (updateReqVO.getSupplierId() != null) { SrmSupplierDO supplier = supplierMapper.selectById(updateReqVO.getSupplierId()); if (supplier == null) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.SUPPLIER_NOT_EXISTS); } } else if (StrUtil.isBlank(updateReqVO.getSupplierName())) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.APPLY_SUPPLIER_NAME_REQUIRED); } SrmSupplierApplyDO updateObj = BeanUtils.toBean(updateReqVO, SrmSupplierApplyDO.class); supplierApplyMapper.updateById(updateObj); } @Override public void deleteApply(Long id) { validateApplyExists(id); supplierApplyMapper.deleteById(id); } @Override public SrmSupplierApplyDO getApply(Long id) { return supplierApplyMapper.selectById(id); } @Override public PageResult getApplyPage(SrmSupplierApplyPageReqVO pageReqVO) { return supplierApplyMapper.selectPage(pageReqVO); } @Override @Transactional(rollbackFor = Exception.class) public void submitApply(Long id) { SrmSupplierApplyDO apply = validateApplyExists(id); if (!SupplierApplyStatusEnum.DRAFT.getCode().equals(apply.getApplyStatus())) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.APPLY_STATUS_NOT_DRAFT); } apply.setApplyStatus(SupplierApplyStatusEnum.PENDING_PURCHASE.getCode()); apply.setApplyTime(LocalDateTime.now()); supplierApplyMapper.updateById(apply); } @Override @Transactional(rollbackFor = Exception.class) public void reviewPurchase(Long id, String opinion, Boolean passed) { SrmSupplierApplyDO apply = validateApplyExists(id); if (!SupplierApplyStatusEnum.PENDING_PURCHASE.getCode().equals(apply.getApplyStatus())) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.APPLY_STATUS_NOT_PENDING); } apply.setPurchaseReviewOpinion(opinion); apply.setPurchaseReviewTime(LocalDateTime.now()); apply.setApplyStatus(Boolean.TRUE.equals(passed) ? SupplierApplyStatusEnum.PENDING_QUALITY.getCode() : SupplierApplyStatusEnum.REJECTED.getCode()); supplierApplyMapper.updateById(apply); } @Override @Transactional(rollbackFor = Exception.class) public void reviewQuality(Long id, String opinion, Boolean passed) { SrmSupplierApplyDO apply = validateApplyExists(id); if (!SupplierApplyStatusEnum.PENDING_QUALITY.getCode().equals(apply.getApplyStatus())) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.APPLY_STATUS_NOT_PENDING); } apply.setQualityReviewOpinion(opinion); apply.setQualityReviewTime(LocalDateTime.now()); apply.setApplyStatus(Boolean.TRUE.equals(passed) ? SupplierApplyStatusEnum.PENDING_FINANCE.getCode() : SupplierApplyStatusEnum.REJECTED.getCode()); supplierApplyMapper.updateById(apply); } @Override @Transactional(rollbackFor = Exception.class) public void reviewFinance(Long id, String opinion, Boolean passed) { SrmSupplierApplyDO apply = validateApplyExists(id); if (!SupplierApplyStatusEnum.PENDING_FINANCE.getCode().equals(apply.getApplyStatus())) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.APPLY_STATUS_NOT_PENDING); } apply.setFinanceReviewOpinion(opinion); apply.setFinanceReviewTime(LocalDateTime.now()); apply.setApplyStatus(Boolean.TRUE.equals(passed) ? SupplierApplyStatusEnum.APPROVED.getCode() : SupplierApplyStatusEnum.REJECTED.getCode()); supplierApplyMapper.updateById(apply); } @Override @Transactional(rollbackFor = Exception.class) public void approve(Long id) { SrmSupplierApplyDO apply = validateApplyExists(id); if (!SupplierApplyStatusEnum.APPROVED.getCode().equals(apply.getApplyStatus())) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.APPLY_STATUS_NOT_PENDING); } // 如果已关联 SRM 供应商,不需要重复创建 if (apply.getSupplierId() == null) { SrmSupplierDO supplier = new SrmSupplierDO(); supplier.setName(apply.getSupplierName()); supplier.setCompanyNature(apply.getCompanyNature()); supplier.setRegisteredCapital(apply.getRegisteredCapital()); supplier.setBusinessLicenseNo(apply.getBusinessLicenseNo()); supplier.setTaxNo(apply.getTaxNo()); supplier.setAddress(apply.getAddress()); supplier.setContactName(apply.getContactName()); supplier.setContactMobile(apply.getContactMobile()); supplier.setContactEmail(apply.getContactEmail()); supplier.setStatus(0); supplierMapper.insert(supplier); apply.setSupplierId(supplier.getId()); } supplierApplyMapper.updateById(apply); } // ========== 校验方法 ========== private SrmSupplierApplyDO validateApplyExists(Long id) { SrmSupplierApplyDO apply = supplierApplyMapper.selectById(id); if (apply == null) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.APPLY_NOT_EXISTS); } return apply; } /** * 生成申请编号 */ private String generateApplyNo() { String prefix = "AP"; String date = java.time.LocalDate.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyyMM")); int seq = (int) (Math.random() * 9000) + 1000; return prefix + date + String.format("%04d", seq); } }