| | |
| | | |
| | | 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.salesTarget.vo.CrmSalesTargetChangeStatusReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.salesTarget.vo.CrmSalesTargetImportExcelVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.salesTarget.vo.CrmSalesTargetImportReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.salesTarget.vo.CrmSalesTargetImportRespVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.salesTarget.vo.CrmSalesTargetPageReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.salesTarget.vo.CrmSalesTargetRespVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.salesTarget.vo.CrmSalesTargetSaveReqVO; |
| | |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.HashSet; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | |
| | | 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.convertSet; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; |
| | | import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | |
| | | return convertListToRespVO(list); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public CrmSalesTargetImportRespVO importSalesTargetList(List<CrmSalesTargetImportExcelVO> importSalesTargets, |
| | | CrmSalesTargetImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importSalesTargets = filterList(importSalesTargets, item -> StrUtil.isNotBlank(item.getName())); |
| | | if (CollUtil.isEmpty(importSalesTargets)) { |
| | | throw exception(CRM_SALES_TARGET_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | |
| | | // 2. 逐条处理 |
| | | CrmSalesTargetImportRespVO respVO = CrmSalesTargetImportRespVO.builder() |
| | | .createSalesTargetNames(new ArrayList<>()) |
| | | .updateSalesTargetNames(new ArrayList<>()) |
| | | .failureSalesTargetNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importSalesTargets.forEach(importSalesTarget -> { |
| | | try { |
| | | // 2.1 解析目标维度对象(个人/部门/产品) |
| | | Long targetUserId = null; |
| | | Long targetDeptId = null; |
| | | Long targetItemId = null; |
| | | if (Objects.equals(importSalesTarget.getTargetDimension(), CrmSalesTargetDimensionEnum.USER.getType())) { |
| | | if (StrUtil.isBlank(importSalesTarget.getTargetUserName())) { |
| | | throw exception(CRM_SALES_TARGET_DIMENSION_TARGET_MISSING, "用户"); |
| | | } |
| | | List<AdminUserRespDTO> users = adminUserApi.getUserListByNickname(importSalesTarget.getTargetUserName()); |
| | | if (CollUtil.isEmpty(users)) { |
| | | throw exception(CRM_SALES_TARGET_IMPORT_DIMENSION_TARGET_NOT_FOUND, "用户", importSalesTarget.getTargetUserName()); |
| | | } |
| | | targetUserId = users.get(0).getId(); |
| | | } else if (Objects.equals(importSalesTarget.getTargetDimension(), CrmSalesTargetDimensionEnum.DEPT.getType())) { |
| | | if (StrUtil.isBlank(importSalesTarget.getTargetDeptName())) { |
| | | throw exception(CRM_SALES_TARGET_DIMENSION_TARGET_MISSING, "部门"); |
| | | } |
| | | DeptRespDTO dept = deptApi.getDeptList().stream() |
| | | .filter(d -> d.getName().equals(importSalesTarget.getTargetDeptName())) |
| | | .findFirst().orElse(null); |
| | | if (dept == null) { |
| | | throw exception(CRM_SALES_TARGET_IMPORT_DIMENSION_TARGET_NOT_FOUND, "部门", importSalesTarget.getTargetDeptName()); |
| | | } |
| | | targetDeptId = dept.getId(); |
| | | } else if (Objects.equals(importSalesTarget.getTargetDimension(), CrmSalesTargetDimensionEnum.ITEM.getType())) { |
| | | if (StrUtil.isBlank(importSalesTarget.getTargetItemCode())) { |
| | | throw exception(CRM_SALES_TARGET_DIMENSION_TARGET_MISSING, "物料"); |
| | | } |
| | | MdmItemRespDTO item = mdmItemApi.getItemByCode(importSalesTarget.getTargetItemCode()).getCheckedData(); |
| | | if (item == null) { |
| | | throw exception(CRM_SALES_TARGET_IMPORT_DIMENSION_TARGET_NOT_FOUND, "物料", importSalesTarget.getTargetItemCode()); |
| | | } |
| | | targetItemId = item.getId(); |
| | | } |
| | | |
| | | // 2.2 计算周期起止与状态 |
| | | LocalDateTime[] period = calculatePeriod(importSalesTarget.getPeriodType(), importSalesTarget.getPeriodValue()); |
| | | Integer status = calcStatusByPeriod(period[0], period[1]); |
| | | |
| | | // 2.3 判断是否已存在同名目标 |
| | | CrmSalesTargetDO existTarget = salesTargetMapper.selectByName(importSalesTarget.getName()); |
| | | if (existTarget == null) { |
| | | // 2.3.1 新增 |
| | | CrmSalesTargetDO target = BeanUtils.toBean(importSalesTarget, CrmSalesTargetDO.class) |
| | | .setTargetUserId(targetUserId).setTargetDeptId(targetDeptId).setTargetItemId(targetItemId) |
| | | .setStartTime(period[0]).setEndTime(period[1]).setStatus(status); |
| | | salesTargetMapper.insert(target); |
| | | respVO.getCreateSalesTargetNames().add(importSalesTarget.getName()); |
| | | return; |
| | | } |
| | | // 2.3.2 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureSalesTargetNames().put(importSalesTarget.getName(), |
| | | StrUtil.format(CRM_SALES_TARGET_NAME_EXISTS.getMsg(), importSalesTarget.getName())); |
| | | return; |
| | | } |
| | | CrmSalesTargetDO updateDO = BeanUtils.toBean(importSalesTarget, CrmSalesTargetDO.class) |
| | | .setId(existTarget.getId()) |
| | | .setTargetUserId(targetUserId).setTargetDeptId(targetDeptId).setTargetItemId(targetItemId) |
| | | .setStartTime(period[0]).setEndTime(period[1]); |
| | | // 手动作废的状态不因导入覆盖 |
| | | updateDO.setStatus(Objects.equals(existTarget.getStatus(), CrmSalesTargetStatusEnum.INVALID.getStatus()) |
| | | ? CrmSalesTargetStatusEnum.INVALID.getStatus() : status); |
| | | salesTargetMapper.updateById(updateDO); |
| | | respVO.getUpdateSalesTargetNames().add(importSalesTarget.getName()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureSalesTargetNames().put(importSalesTarget.getName(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | // ========== 私有方法 ========== |
| | | |
| | | private CrmSalesTargetDO validateSalesTargetExists(Long id) { |