| | |
| | | package cn.iocoder.yudao.module.srm.service.supplier; |
| | | |
| | | 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.framework.mybatis.core.query.LambdaQueryWrapperX; |
| | | import cn.iocoder.yudao.module.srm.controller.admin.supplier.vo.SrmSupplierImportExcelVO; |
| | | import cn.iocoder.yudao.module.srm.controller.admin.supplier.vo.SrmSupplierImportReqVO; |
| | | import cn.iocoder.yudao.module.srm.controller.admin.supplier.vo.SrmSupplierImportRespVO; |
| | | import cn.iocoder.yudao.module.srm.controller.admin.supplier.vo.SrmSupplierPageReqVO; |
| | | import cn.iocoder.yudao.module.srm.controller.admin.supplier.vo.SrmSupplierSaveReqVO; |
| | | import cn.iocoder.yudao.module.srm.dal.dataobject.*; |
| | |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | |
| | | @Service |
| | | @Validated |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<SrmSupplierDO> getSupplierAllList() { |
| | | return supplierMapper.selectList(new LambdaQueryWrapperX<SrmSupplierDO>() |
| | | .orderByAsc(SrmSupplierDO::getId)); |
| | | } |
| | | |
| | | @Override |
| | | public List<SrmSupplierDO> getSupplierList(Collection<Long> ids) { |
| | | if (CollUtil.isEmpty(ids)) { |
| | | return Collections.emptyList(); |
| | |
| | | return supplierMapper.selectBatchIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public SrmSupplierImportRespVO importSupplierList(List<SrmSupplierImportExcelVO> importSuppliers, |
| | | SrmSupplierImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importSuppliers = CollectionUtils.filterList(importSuppliers, |
| | | item -> StrUtil.isNotBlank(item.getName())); |
| | | if (CollUtil.isEmpty(importSuppliers)) { |
| | | throw ServiceExceptionUtil.exception(ErrorCodeConstants.SUPPLIER_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | |
| | | // 2. 逐条处理 |
| | | SrmSupplierImportRespVO respVO = SrmSupplierImportRespVO.builder() |
| | | .createSupplierNames(new ArrayList<>()) |
| | | .updateSupplierNames(new ArrayList<>()) |
| | | .failureSupplierNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importSuppliers.forEach(importSupplier -> { |
| | | try { |
| | | // 2.1 校验编码 |
| | | if (StrUtil.isBlank(importSupplier.getCode())) { |
| | | throw ServiceExceptionUtil.exception(ErrorCodeConstants.SUPPLIER_IMPORT_CODE_REQUIRED); |
| | | } |
| | | // 2.2 判断是否存在 |
| | | SrmSupplierDO existSupplier = supplierMapper.selectByCode(importSupplier.getCode()); |
| | | if (existSupplier == null) { |
| | | // 2.2.1 新增 |
| | | SrmSupplierDO supplier = BeanUtils.toBean(importSupplier, SrmSupplierDO.class); |
| | | if (supplier.getStatus() == null) { |
| | | supplier.setStatus(0); |
| | | } |
| | | supplierMapper.insert(supplier); |
| | | respVO.getCreateSupplierNames().add(importSupplier.getName()); |
| | | return; |
| | | } |
| | | // 2.2.2 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureSupplierNames().put(importSupplier.getName(), |
| | | StrUtil.format(ErrorCodeConstants.SUPPLIER_IMPORT_CODE_EXISTS.getMsg(), importSupplier.getCode())); |
| | | return; |
| | | } |
| | | SrmSupplierDO updateSupplier = BeanUtils.toBean(importSupplier, SrmSupplierDO.class) |
| | | .setId(existSupplier.getId()); |
| | | if (updateSupplier.getStatus() == null) { |
| | | updateSupplier.setStatus(existSupplier.getStatus()); |
| | | } |
| | | supplierMapper.updateById(updateSupplier); |
| | | respVO.getUpdateSupplierNames().add(importSupplier.getName()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureSupplierNames().put(importSupplier.getName(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | // ========== 校验方法 ========== |
| | | |
| | | private void validateSupplierExists(Long id) { |