| | |
| | | package cn.iocoder.yudao.module.crm.service.credit; |
| | | |
| | | 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.crm.controller.admin.credit.vo.CrmCreditRatingImportExcelVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.credit.vo.CrmCreditRatingImportReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.credit.vo.CrmCreditRatingImportRespVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.credit.vo.CrmCreditRatingPageReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.credit.vo.CrmCreditRatingRespVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.credit.vo.CrmCreditRatingSaveReqVO; |
| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | |
| | | 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.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | | import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*; |
| | | |
| | | @Service |
| | | @Validated |
| | |
| | | return entity == null ? null : BeanUtils.toBean(entity, CrmCreditRatingRespVO.class); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public CrmCreditRatingImportRespVO importCreditRatingList(List<CrmCreditRatingImportExcelVO> importCreditRatings, |
| | | CrmCreditRatingImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importCreditRatings = filterList(importCreditRatings, item -> StrUtil.isNotBlank(item.getCustomerName())); |
| | | if (CollUtil.isEmpty(importCreditRatings)) { |
| | | throw exception(CRM_CREDIT_RATING_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 操作人:为空则默认当前操作人 |
| | | Long operatorId = importReqVO.getOperatorId() != null ? importReqVO.getOperatorId() : getLoginUserId(); |
| | | |
| | | // 3. 逐条处理 |
| | | CrmCreditRatingImportRespVO respVO = CrmCreditRatingImportRespVO.builder() |
| | | .createCreditRatingNames(new ArrayList<>()) |
| | | .updateCreditRatingNames(new ArrayList<>()) |
| | | .failureCreditRatingNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importCreditRatings.forEach(importCreditRating -> { |
| | | try { |
| | | // 3.1 解析客户 |
| | | CrmCustomerDO customer = customerMapper.selectByCustomerName(importCreditRating.getCustomerName()); |
| | | if (customer == null) { |
| | | throw exception(CRM_CREDIT_RATING_IMPORT_CUSTOMER_NOT_EXISTS, importCreditRating.getCustomerName()); |
| | | } |
| | | // 3.2 判断该客户是否已存在评级 |
| | | CrmCreditRatingDO existRating = creditRatingMapper.selectLatestByCustomerId(customer.getId()); |
| | | if (existRating == null) { |
| | | // 3.2.1 新增 |
| | | if (StrUtil.isBlank(importCreditRating.getOperatorName())) { |
| | | importCreditRating.setOperatorName(customer.getName()); |
| | | } |
| | | if (importCreditRating.getOperateTime() == null) { |
| | | importCreditRating.setOperateTime(LocalDateTime.now()); |
| | | } |
| | | CrmCreditRatingDO entity = BeanUtils.toBean(importCreditRating, CrmCreditRatingDO.class, |
| | | vo -> vo.setCustomerId(customer.getId()).setCustomerName(customer.getName()) |
| | | .setOperatorId(operatorId)); |
| | | creditRatingMapper.insert(entity); |
| | | customer.setCreditLevel(entity.getLevel()); |
| | | customer.setCreditScore(entity.getScore()); |
| | | customerMapper.updateById(customer); |
| | | respVO.getCreateCreditRatingNames().add(importCreditRating.getCustomerName()); |
| | | return; |
| | | } |
| | | // 3.2.2 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureCreditRatingNames().put(importCreditRating.getCustomerName(), |
| | | StrUtil.format(CRM_CREDIT_RATING_NAME_EXISTS.getMsg(), importCreditRating.getCustomerName())); |
| | | return; |
| | | } |
| | | CrmCreditRatingDO entity = BeanUtils.toBean(importCreditRating, CrmCreditRatingDO.class, |
| | | vo -> vo.setId(existRating.getId()).setCustomerId(customer.getId()) |
| | | .setCustomerName(customer.getName()).setOperatorId(operatorId)); |
| | | creditRatingMapper.updateById(entity); |
| | | CrmCreditRatingDO latest = creditRatingMapper.selectLatestByCustomerId(customer.getId()); |
| | | if (latest != null && latest.getId().equals(entity.getId())) { |
| | | customer.setCreditLevel(latest.getLevel()); |
| | | customer.setCreditScore(latest.getScore()); |
| | | customerMapper.updateById(customer); |
| | | } |
| | | respVO.getUpdateCreditRatingNames().add(importCreditRating.getCustomerName()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureCreditRatingNames().put(importCreditRating.getCustomerName(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |