| | |
| | | import cn.iocoder.yudao.module.crm.framework.permission.core.annotations.CrmPermission; |
| | | import cn.iocoder.yudao.module.crm.service.business.CrmBusinessService; |
| | | import cn.iocoder.yudao.module.crm.service.contact.CrmContactService; |
| | | import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; |
| | | import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService; |
| | | import cn.iocoder.yudao.module.crm.service.permission.CrmPermissionService; |
| | | import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionCreateReqBO; |
| | |
| | | @Resource |
| | | private CrmCustomerService customerService; |
| | | @Resource |
| | | private CrmCustomerApi customerApi; |
| | | @Resource |
| | | private CrmBusinessService businessService; |
| | | @Resource |
| | | private CrmContactService contactService; |
| | |
| | | crmPermissionService.createPermission(new CrmPermissionCreateReqBO().setUserId(contract.getOwnerUserId()) |
| | | .setBizType(CrmBizTypeEnum.CRM_CONTRACT.getType()).setBizId(contract.getId()) |
| | | .setLevel(CrmPermissionLevelEnum.OWNER.getLevel())); |
| | | // 3.1 客户负责人自动拥有该合同的读写权限 |
| | | crmPermissionService.createPermissionForCustomerOwner(CrmBizTypeEnum.CRM_CONTRACT.getType(), |
| | | contract.getId(), contract.getCustomerId(), contract.getOwnerUserId()); |
| | | |
| | | // 4. 记录操作日志上下文 |
| | | LogRecordContext.putVariable("contract", contract); |
| | |
| | | return PageResult.empty(); |
| | | } |
| | | } |
| | | // 2. 获取用户有权限的客户 ID 列表 |
| | | List<Long> permittedCustomerIds = customerApi.getPermittedCustomerIds(); |
| | | // 3. 查询分页 |
| | | return contractMapper.selectPage(pageReqVO, userId, config, permittedCustomerIds); |
| | | // 2. 查询分页 |
| | | // 合同可见性完全由 appendPermissionCondition 控制(合同团队 ∪ 客户团队), |
| | | // 不再额外按「有权限的客户」过滤,否则不在客户团队、但属于合同负责人/合同团队的用户会被错误过滤掉 |
| | | return contractMapper.selectPage(pageReqVO, userId, config); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @CrmPermission(bizType = CrmBizTypeEnum.CRM_CONTRACT, bizId = "#id", level = CrmPermissionLevelEnum.WRITE) |
| | | public Long generateSaleOrder(Long id, Long userId) { |
| | | // 1. 校验合同存在且已审批通过 |
| | | CrmContractDO contract = validateContractExists(id); |
| | | CrmContractDO contract = contractMapper.selectByIdForUpdate(id); |
| | | if (contract == null) throw exception(CONTRACT_NOT_EXISTS); |
| | | if (!CrmAuditStatusEnum.APPROVE.getStatus().equals(contract.getAuditStatus())) { |
| | | throw exception(CONTRACT_GENERATE_ORDER_FAIL_NOT_APPROVE); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public CrmContractImportRespVO importContractList(List<CrmContractImportExcelVO> importContracts, |
| | | CrmContractImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importContracts = filterList(importContracts, item -> StrUtil.isNotBlank(item.getName())); |
| | | if (CollUtil.isEmpty(importContracts)) { |
| | | throw exception(CONTRACT_IMPORT_LIST_IS_EMPTY); |
| | | public CrmContractImportRespVO importContractList(List<CrmContractImportExcelVO> rows, CrmContractImportReqVO req) { |
| | | if (CollUtil.isEmpty(rows)) throw exception(CONTRACT_IMPORT_LIST_IS_EMPTY); |
| | | Long ownerId = req.getOwnerUserId() != null ? req.getOwnerUserId() : getLoginUserId(); |
| | | adminUserApi.validateUser(ownerId); |
| | | CrmContractImportRespVO result = CrmContractImportRespVO.builder() |
| | | .createContractNames(new ArrayList<>()).updateContractNames(new ArrayList<>()) |
| | | .failureContractNames(new LinkedHashMap<>()).build(); |
| | | Map<String, List<CrmContractImportExcelVO>> groups = new LinkedHashMap<>(); |
| | | int rowNo = 0; |
| | | for (CrmContractImportExcelVO row : rows) { |
| | | rowNo++; |
| | | if (row == null) continue; |
| | | if (StrUtil.isBlank(row.getName()) && StrUtil.isBlank(row.getCustomerName()) && StrUtil.isBlank(row.getProductCode())) continue; |
| | | if (StrUtil.isBlank(row.getName()) || StrUtil.isBlank(row.getCustomerName())) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "第" + rowNo + "行合同名称和客户名称不能为空"); |
| | | } |
| | | String key = row.getCustomerName().trim() + " / " + row.getName().trim(); |
| | | groups.computeIfAbsent(key, ignored -> new ArrayList<>()).add(row); |
| | | } |
| | | // 2. 校验负责人(为空则默认当前操作人) |
| | | Long ownerUserId = importReqVO.getOwnerUserId() != null ? importReqVO.getOwnerUserId() : getLoginUserId(); |
| | | adminUserApi.validateUser(ownerUserId); |
| | | |
| | | // 3. 逐条处理 |
| | | CrmContractImportRespVO respVO = CrmContractImportRespVO.builder() |
| | | .createContractNames(new ArrayList<>()) |
| | | .updateContractNames(new ArrayList<>()) |
| | | .failureContractNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importContracts.forEach(importContract -> { |
| | | if (groups.isEmpty() && result.getFailureContractNames().isEmpty()) throw exception(CONTRACT_IMPORT_LIST_IS_EMPTY); |
| | | groups.forEach((key, group) -> { |
| | | try { |
| | | // 3.1 解析客户 |
| | | CrmCustomerDO customer = customerMapper.selectByCustomerName(importContract.getCustomerName()); |
| | | if (customer == null) { |
| | | throw exception(CONTRACT_IMPORT_CUSTOMER_NOT_EXISTS, importContract.getCustomerName()); |
| | | } |
| | | // 3.2 判断是否存在 |
| | | CrmContractDO existContract = contractMapper.selectByNameAndCustomerId( |
| | | importContract.getName(), customer.getId()); |
| | | if (existContract == null) { |
| | | // 3.2.1 新增 |
| | | String no = noRedisDAO.generate(CrmNoRedisDAO.CONTRACT_NO_PREFIX); |
| | | if (contractMapper.selectByNo(no) != null) { |
| | | throw exception(CONTRACT_NO_EXISTS); |
| | | } |
| | | CrmContractDO contract = BeanUtils.toBean(importContract, CrmContractDO.class) |
| | | .setCustomerId(customer.getId()).setOwnerUserId(ownerUserId) |
| | | .setNo(no).setAuditStatus(CrmAuditStatusEnum.DRAFT.getStatus()); |
| | | if (contract.getOrderDate() == null) { |
| | | contract.setOrderDate(LocalDateTime.now()); |
| | | } |
| | | if (contract.getDiscountPercent() == null) { |
| | | contract.setDiscountPercent(BigDecimal.ZERO); |
| | | } |
| | | if (contract.getTotalPrice() == null) { |
| | | calculateTotalPrice(contract, Collections.emptyList()); |
| | | } |
| | | contractMapper.insert(contract); |
| | | // 创建数据权限 |
| | | crmPermissionService.createPermission(new CrmPermissionCreateReqBO() |
| | | .setBizType(CrmBizTypeEnum.CRM_CONTRACT.getType()).setBizId(contract.getId()) |
| | | .setUserId(ownerUserId).setLevel(CrmPermissionLevelEnum.OWNER.getLevel())); |
| | | respVO.getCreateContractNames().add(importContract.getName()); |
| | | // 记录操作日志 |
| | | getSelf().importContractLog(contract, false); |
| | | return; |
| | | } |
| | | // 3.2.2 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureContractNames().put(importContract.getName(), |
| | | StrUtil.format(CONTRACT_NAME_EXISTS.getMsg(), importContract.getName())); |
| | | return; |
| | | } |
| | | CrmContractDO updateContract = BeanUtils.toBean(importContract, CrmContractDO.class) |
| | | .setId(existContract.getId()).setCustomerId(customer.getId()); |
| | | contractMapper.updateById(updateContract); |
| | | respVO.getUpdateContractNames().add(importContract.getName()); |
| | | // 记录操作日志 |
| | | getSelf().importContractLog(updateContract, true); |
| | | boolean created = getSelf().importContractGroup(group, ownerId, Boolean.TRUE.equals(req.getUpdateSupport())); |
| | | (created ? result.getCreateContractNames() : result.getUpdateContractNames()).add(key); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureContractNames().put(importContract.getName(), ex.getMessage()); |
| | | result.getFailureContractNames().put(key, ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | return result; |
| | | } |
| | | |
| | | /** 一张合同及全部明细在同一事务中保存。 */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean importContractGroup(List<CrmContractImportExcelVO> rows, Long ownerId, boolean updateSupport) { |
| | | CrmContractImportExcelVO first = rows.get(0); |
| | | CrmCustomerDO customer = customerMapper.selectByCustomerName(first.getCustomerName().trim()); |
| | | if (customer == null) throw exception(CONTRACT_IMPORT_CUSTOMER_NOT_EXISTS, first.getCustomerName()); |
| | | CrmContractDO existing = contractMapper.selectByNameAndCustomerId(first.getName().trim(), customer.getId()); |
| | | if (existing != null && !updateSupport) throw exception(CONTRACT_NAME_EXISTS, first.getName()); |
| | | if (existing != null && !ObjectUtils.equalsAny(existing.getAuditStatus(), CrmAuditStatusEnum.DRAFT.getStatus(), CrmAuditStatusEnum.REJECT.getStatus())) { |
| | | throw exception(CONTRACT_UPDATE_FAIL_NOT_DRAFT); |
| | | } |
| | | if (first.getDiscountPercent() != null && (first.getDiscountPercent().signum() < 0 || first.getDiscountPercent().compareTo(new BigDecimal("100")) > 0)) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "折扣必须在 0 到 100 之间"); |
| | | } |
| | | if (first.getStartTime() != null && first.getEndTime() != null && first.getEndTime().isBefore(first.getStartTime())) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "结束时间不能早于开始时间"); |
| | | } |
| | | List<CrmContractSaveReqVO.Product> products = new ArrayList<>(); |
| | | for (CrmContractImportExcelVO row : rows) { |
| | | if (row.getOrderDate() != null && !row.getOrderDate().equals(first.getOrderDate()) |
| | | || row.getDiscountPercent() != null && (first.getDiscountPercent() == null || row.getDiscountPercent().compareTo(first.getDiscountPercent()) != 0)) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "同一合同的下单日期和折扣必须一致"); |
| | | } |
| | | if (row.getStartTime() != null && !row.getStartTime().equals(first.getStartTime()) |
| | | || row.getEndTime() != null && !row.getEndTime().equals(first.getEndTime()) |
| | | || row.getTotalPrice() != null && (first.getTotalPrice() == null || row.getTotalPrice().compareTo(first.getTotalPrice()) != 0) |
| | | || row.getDepositPrice() != null && (first.getDepositPrice() == null || row.getDepositPrice().compareTo(first.getDepositPrice()) != 0) |
| | | || StrUtil.isNotBlank(row.getContractType()) && !StrUtil.equals(row.getContractType(), first.getContractType()) |
| | | || StrUtil.isNotBlank(row.getVoltageLevel()) && !StrUtil.equals(row.getVoltageLevel(), first.getVoltageLevel()) |
| | | || StrUtil.isNotBlank(row.getElectricityType()) && !StrUtil.equals(row.getElectricityType(), first.getElectricityType()) |
| | | || row.getContractCapacity() != null && (first.getContractCapacity() == null || row.getContractCapacity().compareTo(first.getContractCapacity()) != 0) |
| | | || StrUtil.isNotBlank(row.getTariffMode()) && !StrUtil.equals(row.getTariffMode(), first.getTariffMode()) |
| | | || StrUtil.isNotBlank(row.getMeterMode()) && !StrUtil.equals(row.getMeterMode(), first.getMeterMode()) |
| | | || row.getPayCycle() != null && !row.getPayCycle().equals(first.getPayCycle()) |
| | | || StrUtil.isNotBlank(row.getRemark()) && !StrUtil.equals(row.getRemark(), first.getRemark())) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "同一合同的主表信息必须一致"); |
| | | } |
| | | boolean hasDetail = StrUtil.isNotBlank(row.getProductCode()) || row.getProductPrice() != null || row.getProductCount() != null; |
| | | if (!hasDetail) continue; |
| | | if (StrUtil.isBlank(row.getProductCode()) || row.getProductPrice() == null || row.getProductPrice().signum() < 0 || row.getProductCount() == null || row.getProductCount() <= 0) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "物料编码、非负合同单价和大于零的整数数量必须同时填写"); |
| | | } |
| | | MdmItemRespDTO item = mdmItemApi.getItemByCode(row.getProductCode().trim()).getCheckedData(); |
| | | if (item == null) throw exception(CONTRACT_IMPORT_INVALID, "物料不存在:" + row.getProductCode()); |
| | | CrmContractSaveReqVO.Product product = new CrmContractSaveReqVO.Product(); |
| | | product.setItemId(item.getId()); |
| | | product.setItemPrice(item.getSalesPrice()); |
| | | product.setContractPrice(row.getProductPrice()); |
| | | product.setCount(row.getProductCount()); |
| | | products.add(product); |
| | | } |
| | | if (!products.isEmpty() && products.size() != rows.size()) throw exception(CONTRACT_IMPORT_INVALID, "同一合同的每一行都必须填写产品明细"); |
| | | CrmContractDO contract = BeanUtils.toBean(first, CrmContractDO.class).setCustomerId(customer.getId()); |
| | | contract.setName(first.getName().trim()); |
| | | if (existing == null) { |
| | | String no = noRedisDAO.generate(CrmNoRedisDAO.CONTRACT_NO_PREFIX); |
| | | if (contractMapper.selectByNo(no) != null) throw exception(CONTRACT_NO_EXISTS); |
| | | contract.setNo(no).setOwnerUserId(ownerId).setAuditStatus(CrmAuditStatusEnum.DRAFT.getStatus()); |
| | | if (contract.getOrderDate() == null) contract.setOrderDate(LocalDateTime.now()); |
| | | if (contract.getDiscountPercent() == null) contract.setDiscountPercent(BigDecimal.ZERO); |
| | | if (!products.isEmpty() || contract.getTotalPrice() == null) calculateTotalPrice(contract, validateContractProducts(products)); |
| | | contractMapper.insert(contract); |
| | | crmPermissionService.createPermission(new CrmPermissionCreateReqBO().setBizType(CrmBizTypeEnum.CRM_CONTRACT.getType()).setBizId(contract.getId()).setUserId(ownerId).setLevel(CrmPermissionLevelEnum.OWNER.getLevel())); |
| | | // 客户负责人自动拥有该合同的读写权限 |
| | | crmPermissionService.createPermissionForCustomerOwner(CrmBizTypeEnum.CRM_CONTRACT.getType(), |
| | | contract.getId(), customer.getId(), ownerId); |
| | | } else { |
| | | contract.setId(existing.getId()); |
| | | List<CrmContractProductDO> amountDetails = products.isEmpty() |
| | | ? contractProductMapper.selectListByContractId(existing.getId()) : validateContractProducts(products); |
| | | if (CollUtil.isNotEmpty(amountDetails)) { |
| | | if (contract.getDiscountPercent() == null) contract.setDiscountPercent( |
| | | existing.getDiscountPercent() == null ? BigDecimal.ZERO : existing.getDiscountPercent()); |
| | | calculateTotalPrice(contract, amountDetails); |
| | | } |
| | | contractMapper.updateById(contract); |
| | | } |
| | | if (!products.isEmpty()) { |
| | | List<CrmContractProductDO> details = validateContractProducts(products); |
| | | if (existing != null) contractProductMapper.delete(CrmContractProductDO::getContractId, existing.getId()); |
| | | details.forEach(detail -> detail.setContractId(contract.getId())); |
| | | contractProductMapper.insertBatch(details); |
| | | } |
| | | getSelf().importContractLog(contract, existing != null); |
| | | return existing == null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 记录导入合同时的操作日志 |
| | | * |