From 8ad99e5f72085d14fd56e8faa7aa36701823c8b5 Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期四, 05 十一月 2026 20:16:23 +0800
Subject: [PATCH] feat(crm): 统一客户团队数据权限并放开开票金额上限
---
yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/contract/CrmContractServiceImpl.java | 197 ++++++++++++++++++++++++++++++-------------------
1 files changed, 121 insertions(+), 76 deletions(-)
diff --git a/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/contract/CrmContractServiceImpl.java b/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/contract/CrmContractServiceImpl.java
index 77d52cc..3052329 100644
--- a/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/contract/CrmContractServiceImpl.java
+++ b/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/contract/CrmContractServiceImpl.java
@@ -33,7 +33,6 @@
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;
@@ -115,8 +114,6 @@
@Resource
private CrmCustomerService customerService;
@Resource
- private CrmCustomerApi customerApi;
- @Resource
private CrmBusinessService businessService;
@Resource
private CrmContactService contactService;
@@ -170,6 +167,9 @@
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);
@@ -481,10 +481,10 @@
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
@@ -545,7 +545,8 @@
@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);
}
@@ -623,81 +624,125 @@
}
@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 瑙f瀽瀹㈡埛
- 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;
+ }
+
+
/**
* 璁板綍瀵煎叆鍚堝悓鏃剁殑鎿嶄綔鏃ュ織
*
--
Gitblit v1.9.3