Commit 1f35ef59d11a1c1d96a102aa770d5f88d79ddbfc

Authored by YunaiV
1 parent fc773431

CRM:code review excel 导出的重构

yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/handler/SelectSheetWriteHandler.java
... ... @@ -3,6 +3,7 @@ package cn.iocoder.yudao.framework.excel.core.handler;
3 3 import cn.hutool.core.collection.CollUtil;
4 4 import cn.hutool.core.map.MapUtil;
5 5 import cn.hutool.extra.spring.SpringUtil;
  6 +import cn.hutool.poi.excel.ExcelUtil;
6 7 import cn.iocoder.yudao.framework.common.core.KeyValue;
7 8 import cn.iocoder.yudao.framework.excel.core.annotations.ExcelColumnSelect;
8 9 import cn.iocoder.yudao.framework.excel.core.service.ExcelColumnSelectDataService;
... ... @@ -27,7 +28,9 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.
27 28 */
28 29 public class SelectSheetWriteHandler implements SheetWriteHandler {
29 30  
  31 + // TODO @puhui999:注释哈;
30 32 private static final List<String> ALPHABET = getExcelColumnNameList();
  33 +
31 34 private static final List<ExcelColumnSelectDataService> EXCEL_COLUMN_SELECT_DATA_SERVICES = new ArrayList<>();
32 35  
33 36 /**
... ... @@ -43,6 +46,7 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
43 46  
44 47 private static final String DICT_SHEET_NAME = "字典sheet";
45 48  
  49 + // TODO @puhui999:这个 selectMap 可以改成 Map<Integer, List<String>>,然后在 afterSheetCreate 里面进行排序。这样整体的理解成本会更简单
46 50 private final List<KeyValue<Integer, List<String>>> selectMap = new ArrayList<>(); // 使用 List + KeyValue 组合方便排序
47 51  
48 52 public SelectSheetWriteHandler(Class<?> head) {
... ... @@ -51,11 +55,14 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
51 55 if (MapUtil.isEmpty(beansMap)) {
52 56 return;
53 57 }
  58 + // TODO @puhui999:static 是共享。如果并发情况下,会不会存在问题? 其实 EXCEL_COLUMN_SELECT_DATA_SERVICES 不用全局声明,直接 按照 ExcelColumnSelect 去拿下就好了;
54 59 if (CollUtil.isEmpty(EXCEL_COLUMN_SELECT_DATA_SERVICES) || EXCEL_COLUMN_SELECT_DATA_SERVICES.size() != beansMap.values().size()) {
55 60 EXCEL_COLUMN_SELECT_DATA_SERVICES.clear();
56 61 EXCEL_COLUMN_SELECT_DATA_SERVICES.addAll(convertList(beansMap.values(), b -> b));
57 62 }
  63 +
58 64 // 解析下拉数据
  65 + // TODO @puhui999:感觉可以 head 循环 field,如果有 ExcelColumnSelect 则进行处理;而 ExcelProperty 可能是非必须的
59 66 Map<String, Field> excelPropertyFields = getFieldsWithAnnotation(head, ExcelProperty.class);
60 67 Map<String, Field> excelColumnSelectFields = getFieldsWithAnnotation(head, ExcelColumnSelect.class);
61 68 int colIndex = 0;
... ... @@ -157,6 +164,7 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
157 164  
158 165  
159 166 private static List<String> getExcelColumnNameList() {
  167 + // TODO @puhui999:是不是可以使用 ExcelUtil.indexToColName() 替代
160 168 ArrayList<String> strings = new ArrayList<>();
161 169 for (int i = 1; i <= 52; i++) { // 生成 52 列名称,需要更多请重写此方法
162 170 if (i <= 26) {
... ...
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/service/ExcelColumnSelectDataService.java
... ... @@ -9,11 +9,14 @@ import java.util.List;
9 9 * Excel 列下拉数据源获取接口
10 10 *
11 11 * 为什么不直接解析字典还搞个接口?考虑到有的下拉数据不是从字典中获取的所有需要做一个兼容
  12 + * TODO @puhui999:是不是 @ExcelColumnSelect 可以搞两个属性,一个 dictType,一个 functionName;如果 dictType,则默认走字典,否则走 functionName
  13 + * 这样的话,ExcelColumnSelectDataService 改成 ExcelColumnSelectFunction 用于获取数据。
12 14 *
13 15 * @author HUIHUI
14 16 */
15 17 public interface ExcelColumnSelectDataService {
16 18  
  19 + // TODO @puhui999:可以考虑改成 getName
17 20 /**
18 21 * 获得方法名称
19 22 *
... ... @@ -21,6 +24,7 @@ public interface ExcelColumnSelectDataService {
21 24 */
22 25 String getFunctionName();
23 26  
  27 + // TODO @puhui999:可以考虑改成 getOptions;因为 select 下面是 option 哈,和前端 html 类似的标签;
24 28 /**
25 29 * 获得列下拉数据源
26 30 *
... ... @@ -28,6 +32,7 @@ public interface ExcelColumnSelectDataService {
28 32 */
29 33 List<String> getSelectDataList();
30 34  
  35 + // TODO @puhui999:这个建议放到 SelectSheetWriteHandler 里
31 36 default List<String> handle(String funcName) {
32 37 if (StrUtil.isEmptyIfStr(funcName) || !StrUtil.equals(getFunctionName(), funcName)) {
33 38 return Collections.emptyList();
... ...
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/framework/excel/package-info.java
  1 +// TODO @puhui999:在 framework 目录,保持 config 和 core 目录的风格哈;
1 2 package cn.iocoder.yudao.module.crm.framework.excel;
2 3 \ No newline at end of file
... ...
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/framework/excel/service/AreaExcelColumnSelectDataServiceImpl.java
... ... @@ -3,8 +3,6 @@ package cn.iocoder.yudao.module.crm.framework.excel.service;
3 3 import cn.iocoder.yudao.framework.excel.core.service.ExcelColumnSelectDataService;
4 4 import cn.iocoder.yudao.framework.ip.core.Area;
5 5 import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
6   -import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
7   -import jakarta.annotation.Resource;
8 6 import org.springframework.stereotype.Service;
9 7  
10 8 import java.util.List;
... ... @@ -19,9 +17,6 @@ public class AreaExcelColumnSelectDataServiceImpl implements ExcelColumnSelectDa
19 17  
20 18 public static final String FUNCTION_NAME = "getCrmAreaNameList"; // 防止和别的模块重名
21 19  
22   - @Resource
23   - private DictDataApi dictDataApi;
24   -
25 20 @Override
26 21 public String getFunctionName() {
27 22 return FUNCTION_NAME;
... ... @@ -31,7 +26,7 @@ public class AreaExcelColumnSelectDataServiceImpl implements ExcelColumnSelectDa
31 26 public List<String> getSelectDataList() {
32 27 // 获取地区下拉数据
33 28 // TODO @puhui999:嘿嘿,这里改成省份、城市、区域,三个选项,难度大么?
34   - Area area = AreaUtils.parseArea(Area.ID_CHINA);
  29 + Area area = AreaUtils.getArea(Area.ID_CHINA);
35 30 return AreaUtils.getAreaNodePathList(area.getChildren());
36 31 }
37 32  
... ...
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/contract/CrmContractServiceImpl.java
... ... @@ -226,18 +226,19 @@ public class CrmContractServiceImpl implements CrmContractService {
226 226 success = CRM_CONTRACT_DELETE_SUCCESS)
227 227 @CrmPermission(bizType = CrmBizTypeEnum.CRM_CONTRACT, bizId = "#id", level = CrmPermissionLevelEnum.OWNER)
228 228 public void deleteContract(Long id) {
229   - // 校验存在
  229 + // 1.1 校验存在
230 230 CrmContractDO contract = validateContractExists(id);
231   - // 如果被 CrmReceivableDO 所使用,则不允许删除
  231 + // 1.2 如果被 CrmReceivableDO 所使用,则不允许删除
232 232 if (CollUtil.isNotEmpty(receivableService.getReceivableByContractId(contract.getId()))) {
233 233 throw exception(CONTRACT_DELETE_FAIL);
234 234 }
235   - // 删除
  235 +
  236 + // 2.1 删除合同
236 237 contractMapper.deleteById(id);
237   - // 删除数据权限
  238 + // 2.2 删除数据权限
238 239 crmPermissionService.deletePermission(CrmBizTypeEnum.CRM_CONTRACT.getType(), id);
239 240  
240   - // 记录操作日志上下文
  241 + // 3. 记录操作日志上下文
241 242 LogRecordContext.putVariable("contractName", contract.getName());
242 243 }
243 244  
... ...
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/receivable/CrmReceivableService.java
... ... @@ -122,6 +122,7 @@ public interface CrmReceivableService {
122 122 */
123 123 Map<Long, BigDecimal> getReceivablePriceMapByContractId(Collection<Long> contractIds);
124 124  
  125 + // TODO @puhui999:这个搞成根据数量判断,会更好一点哈;
125 126 /**
126 127 * 更具合同编号查询回款列表
127 128 *
... ...
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/receivable/CrmReceivableServiceImpl.java
... ... @@ -224,12 +224,13 @@ public class CrmReceivableServiceImpl implements CrmReceivableService {
224 224 if (ObjUtil.equal(receivable.getAuditStatus(), CrmAuditStatusEnum.APPROVE.getStatus())) {
225 225 throw exception(RECEIVABLE_DELETE_FAIL_IS_APPROVE);
226 226 }
227   - // 2. 删除
  227 +
  228 + // 2.1 删除回款
228 229 receivableMapper.deleteById(id);
229   - // 3. 删除数据权限
  230 + // 2.2 删除数据权限
230 231 permissionService.deletePermission(CrmBizTypeEnum.CRM_RECEIVABLE.getType(), id);
231 232  
232   - // 4. 记录操作日志上下文
  233 + // 3. 记录操作日志上下文
233 234 LogRecordContext.putVariable("receivable", receivable);
234 235 LogRecordContext.putVariable("period", getReceivablePeriod(receivable.getPlanId()));
235 236 }
... ...