| | |
| | | package cn.iocoder.yudao.module.erp.service.purchase; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | | 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.erp.controller.admin.purchase.vo.supplier.ErpSupplierImportExcelVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.supplier.ErpSupplierImportReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.supplier.ErpSupplierImportRespVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.supplier.ErpSupplierPageReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.supplier.ErpSupplierSaveReqVO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpSupplierDO; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpSupplierMapper; |
| | | import cn.iocoder.yudao.module.srm.api.supplier.SrmSupplierApi; |
| | | import cn.iocoder.yudao.module.srm.api.supplier.dto.SrmSupplierRespDTO; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; |
| | | import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * ERP 供应商 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | |
| | | |
| | | @Resource |
| | | private ErpSupplierMapper supplierMapper; |
| | | |
| | | @Resource |
| | | private SrmSupplierApi srmSupplierApi; |
| | | |
| | | @Override |
| | | public Long createSupplier(ErpSupplierSaveReqVO createReqVO) { |
| | |
| | | public void deleteSupplier(Long id) { |
| | | // 校验存在 |
| | | validateSupplierExists(id); |
| | | // 校验供应商是否被业务引用 |
| | | validateSupplierNoReference(id); |
| | | // 删除 |
| | | supplierMapper.deleteById(id); |
| | | } |
| | | |
| | | /** |
| | | * 校验供应商未被业务引用(采购、质检、生产、库存等) |
| | | * |
| | | * @param id 供应商编号 |
| | | */ |
| | | private void validateSupplierNoReference(Long id) { |
| | | List<Map<String, Object>> references = supplierMapper.selectSupplierReferenceCount(id); |
| | | if (CollUtil.isNotEmpty(references)) { |
| | | String refDesc = references.stream() |
| | | .map(r -> r.get("source") + "(" + r.get("cnt") + ")") |
| | | .collect(Collectors.joining("、")); |
| | | throw exception(SUPPLIER_EXISTS_REFERENCE, refDesc); |
| | | } |
| | | } |
| | | |
| | | private void validateSupplierExists(Long id) { |
| | |
| | | |
| | | @Override |
| | | public ErpSupplierDO validateSupplier(Long id) { |
| | | ErpSupplierDO supplier = supplierMapper.selectById(id); |
| | | SrmSupplierRespDTO supplier = srmSupplierApi.getSupplier(id).getCheckedData(); |
| | | if (supplier == null) { |
| | | throw exception(SUPPLIER_NOT_EXISTS); |
| | | } |
| | | if (CommonStatusEnum.isDisable(supplier.getStatus())) { |
| | | throw exception(SUPPLIER_NOT_ENABLE, supplier.getName()); |
| | | } |
| | | return supplier; |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | |
| | | return supplierMapper.selectListByStatus(status); |
| | | } |
| | | |
| | | @Override |
| | | public ErpSupplierImportRespVO importSupplierList(List<ErpSupplierImportExcelVO> importSuppliers, |
| | | ErpSupplierImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importSuppliers = filterList(importSuppliers, item -> StrUtil.isNotBlank(item.getName())); |
| | | if (CollUtil.isEmpty(importSuppliers)) { |
| | | throw exception(SUPPLIER_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 逐条处理 |
| | | ErpSupplierImportRespVO respVO = ErpSupplierImportRespVO.builder() |
| | | .createSupplierNames(new ArrayList<>()) |
| | | .updateSupplierNames(new ArrayList<>()) |
| | | .failureSupplierNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importSuppliers.forEach(importSupplier -> { |
| | | try { |
| | | ErpSupplierDO existSupplier = supplierMapper.selectByName(importSupplier.getName()); |
| | | if (existSupplier == null) { |
| | | // 新增 |
| | | ErpSupplierDO supplier = BeanUtils.toBean(importSupplier, ErpSupplierDO.class); |
| | | supplierMapper.insert(supplier); |
| | | respVO.getCreateSupplierNames().add(importSupplier.getName()); |
| | | return; |
| | | } |
| | | // 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureSupplierNames().put(importSupplier.getName(), |
| | | StrUtil.format(SUPPLIER_NAME_EXISTS.getMsg(), importSupplier.getName())); |
| | | return; |
| | | } |
| | | ErpSupplierDO updateSupplier = BeanUtils.toBean(importSupplier, ErpSupplierDO.class) |
| | | .setId(existSupplier.getId()); |
| | | supplierMapper.updateById(updateSupplier); |
| | | respVO.getUpdateSupplierNames().add(importSupplier.getName()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureSupplierNames().put(importSupplier.getName(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |