Commit 462a4ee8230b05b1fb4a9b15382ce84446e7cabf

Authored by puhui999
1 parent e5c3e69f

重构 excel 导入模版下拉生成逻辑使用字段注解的方式配置下拉选择

Showing 12 changed files with 291 additions and 92 deletions
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/annotations/ExcelColumnSelect.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.excel.core.annotations;
  2 +
  3 +import java.lang.annotation.*;
  4 +
  5 +/**
  6 + * 给列添加下拉选择数据
  7 + *
  8 + * @author HUIHUI
  9 + */
  10 +@Target({ElementType.FIELD})
  11 +@Retention(RetentionPolicy.RUNTIME)
  12 +@Inherited
  13 +public @interface ExcelColumnSelect {
  14 +
  15 + /**
  16 + * @return 获取下拉数据源的方法名称
  17 + */
  18 + String value();
  19 +
  20 +}
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/enums/ExcelColumn.java deleted
1 -package cn.iocoder.yudao.framework.excel.core.enums;  
2 -  
3 -import lombok.AllArgsConstructor;  
4 -import lombok.Getter;  
5 -  
6 -// TODO @puhui999:列表有办法通过 field name 么?主要考虑一个点,可能导入模版的顺序可能会变  
7 -/**  
8 - * Excel 列名枚举  
9 - * 默认枚举 26 列列名如果有需求更多的列名请自行补充  
10 - *  
11 - * @author HUIHUI  
12 - */  
13 -@Getter  
14 -@AllArgsConstructor  
15 -public enum ExcelColumn {  
16 -  
17 - A(0), B(1), C(2), D(3), E(4), F(5), G(6), H(7), I(8),  
18 - J(9), K(10), L(11), M(12), N(13), O(14), P(15), Q(16),  
19 - R(17), S(18), T(19), U(20), V(21), W(22), X(23), Y(24),  
20 - Z(25);  
21 -  
22 - /**  
23 - * 列索引  
24 - */  
25 - private final int colNum;  
26 -  
27 -}  
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/handler/SelectSheetWriteHandler.java
1 package cn.iocoder.yudao.framework.excel.core.handler; 1 package cn.iocoder.yudao.framework.excel.core.handler;
2 2
3 import cn.hutool.core.collection.CollUtil; 3 import cn.hutool.core.collection.CollUtil;
4 -import cn.hutool.core.lang.Assert; 4 +import cn.hutool.core.map.MapUtil;
  5 +import cn.hutool.extra.spring.SpringUtil;
5 import cn.iocoder.yudao.framework.common.core.KeyValue; 6 import cn.iocoder.yudao.framework.common.core.KeyValue;
6 -import cn.iocoder.yudao.framework.excel.core.enums.ExcelColumn; 7 +import cn.iocoder.yudao.framework.excel.core.annotations.ExcelColumnSelect;
  8 +import cn.iocoder.yudao.framework.excel.core.service.ExcelColumnSelectDataService;
  9 +import com.alibaba.excel.annotation.ExcelProperty;
7 import com.alibaba.excel.write.handler.SheetWriteHandler; 10 import com.alibaba.excel.write.handler.SheetWriteHandler;
8 import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; 11 import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
9 import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; 12 import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
@@ -11,10 +14,11 @@ import org.apache.poi.hssf.usermodel.HSSFDataValidation; @@ -11,10 +14,11 @@ import org.apache.poi.hssf.usermodel.HSSFDataValidation;
11 import org.apache.poi.ss.usermodel.*; 14 import org.apache.poi.ss.usermodel.*;
12 import org.apache.poi.ss.util.CellRangeAddressList; 15 import org.apache.poi.ss.util.CellRangeAddressList;
13 16
14 -import java.util.Comparator;  
15 -import java.util.List;  
16 -import java.util.Map;  
17 -import java.util.stream.Collectors; 17 +import java.lang.annotation.Annotation;
  18 +import java.lang.reflect.Field;
  19 +import java.util.*;
  20 +
  21 +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
18 22
19 /** 23 /**
20 * 基于固定 sheet 实现下拉框 24 * 基于固定 sheet 实现下拉框
@@ -23,6 +27,9 @@ import java.util.stream.Collectors; @@ -23,6 +27,9 @@ import java.util.stream.Collectors;
23 */ 27 */
24 public class SelectSheetWriteHandler implements SheetWriteHandler { 28 public class SelectSheetWriteHandler implements SheetWriteHandler {
25 29
  30 + private static final List<String> ALPHABET = getExcelColumnNameList();
  31 + private static final List<ExcelColumnSelectDataService> EXCEL_COLUMN_SELECT_DATA_SERVICES = new ArrayList<>();
  32 +
26 /** 33 /**
27 * 数据起始行从 0 开始 34 * 数据起始行从 0 开始
28 * 35 *
@@ -36,21 +43,46 @@ public class SelectSheetWriteHandler implements SheetWriteHandler { @@ -36,21 +43,46 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
36 43
37 private static final String DICT_SHEET_NAME = "字典sheet"; 44 private static final String DICT_SHEET_NAME = "字典sheet";
38 45
39 - // TODO @puhui999:Map<ExcelColumn, List<String>> 可以么?之前用 keyvalue 的原因,返回给前端,无法用 linkedhashmap,默认 key 会乱序  
40 - private final List<KeyValue<ExcelColumn, List<String>>> selectMap; 46 + private final List<KeyValue<Integer, List<String>>> selectMap = new ArrayList<>(); // 使用 List + KeyValue 组合方便排序
41 47
42 - public SelectSheetWriteHandler(List<KeyValue<ExcelColumn, List<String>>> selectMap) {  
43 - if (CollUtil.isEmpty(selectMap)) {  
44 - this.selectMap = null; 48 + public SelectSheetWriteHandler(Class<?> head) {
  49 + // 加载下拉数据获取接口
  50 + Map<String, ExcelColumnSelectDataService> beansMap = SpringUtil.getBeanFactory().getBeansOfType(ExcelColumnSelectDataService.class);
  51 + if (MapUtil.isEmpty(beansMap)) {
45 return; 52 return;
46 } 53 }
47 - // 校验一下 key 是否唯一  
48 - Map<String, Long> nameCounts = selectMap.stream()  
49 - .collect(Collectors.groupingBy(item -> item.getKey().name(), Collectors.counting()));  
50 - Assert.isFalse(nameCounts.entrySet().stream().allMatch(entry -> entry.getValue() > 1), "下拉数据 key 重复请排查!!!");  
51 - 54 + if (CollUtil.isEmpty(EXCEL_COLUMN_SELECT_DATA_SERVICES) || EXCEL_COLUMN_SELECT_DATA_SERVICES.size() != beansMap.values().size()) {
  55 + EXCEL_COLUMN_SELECT_DATA_SERVICES.clear();
  56 + EXCEL_COLUMN_SELECT_DATA_SERVICES.addAll(convertList(beansMap.values(), b -> b));
  57 + }
  58 + // 解析下拉数据
  59 + Map<String, Field> excelPropertyFields = getFieldsWithAnnotation(head, ExcelProperty.class);
  60 + Map<String, Field> excelColumnSelectFields = getFieldsWithAnnotation(head, ExcelColumnSelect.class);
  61 + int colIndex = 0;
  62 + for (String fieldName : excelPropertyFields.keySet()) {
  63 + Field field = excelColumnSelectFields.get(fieldName);
  64 + if (field != null) {
  65 + getSelectDataList(colIndex, field);
  66 + }
  67 + colIndex++;
  68 + }
52 selectMap.sort(Comparator.comparing(item -> item.getValue().size())); // 升序不然创建下拉会报错 69 selectMap.sort(Comparator.comparing(item -> item.getValue().size())); // 升序不然创建下拉会报错
53 - this.selectMap = selectMap; 70 + }
  71 +
  72 + /**
  73 + * 获得下拉数据
  74 + *
  75 + * @param colIndex 列索引
  76 + * @param field 字段
  77 + */
  78 + private void getSelectDataList(int colIndex, Field field) {
  79 + EXCEL_COLUMN_SELECT_DATA_SERVICES.forEach(selectDataService -> {
  80 + List<String> stringList = selectDataService.handle(field.getAnnotation(ExcelColumnSelect.class).value());
  81 + if (CollUtil.isEmpty(stringList)) {
  82 + return;
  83 + }
  84 + selectMap.add(new KeyValue<>(colIndex, stringList));
  85 + });
54 } 86 }
55 87
56 @Override 88 @Override
@@ -65,7 +97,7 @@ public class SelectSheetWriteHandler implements SheetWriteHandler { @@ -65,7 +97,7 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
65 97
66 // 2. 创建数据字典的 sheet 页 98 // 2. 创建数据字典的 sheet 页
67 Sheet dictSheet = workbook.createSheet(DICT_SHEET_NAME); 99 Sheet dictSheet = workbook.createSheet(DICT_SHEET_NAME);
68 - for (KeyValue<ExcelColumn, List<String>> keyValue : selectMap) { 100 + for (KeyValue<Integer, List<String>> keyValue : selectMap) {
69 int rowLength = keyValue.getValue().size(); 101 int rowLength = keyValue.getValue().size();
70 // 2.1 设置字典 sheet 页的值 每一列一个字典项 102 // 2.1 设置字典 sheet 页的值 每一列一个字典项
71 for (int i = 0; i < rowLength; i++) { 103 for (int i = 0; i < rowLength; i++) {
@@ -73,7 +105,7 @@ public class SelectSheetWriteHandler implements SheetWriteHandler { @@ -73,7 +105,7 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
73 if (row == null) { 105 if (row == null) {
74 row = dictSheet.createRow(i); 106 row = dictSheet.createRow(i);
75 } 107 }
76 - row.createCell(keyValue.getKey().getColNum()).setCellValue(keyValue.getValue().get(i)); 108 + row.createCell(keyValue.getKey()).setCellValue(keyValue.getValue().get(i));
77 } 109 }
78 // 2.2 设置单元格下拉选择 110 // 2.2 设置单元格下拉选择
79 setColumnSelect(writeSheetHolder, workbook, helper, keyValue); 111 setColumnSelect(writeSheetHolder, workbook, helper, keyValue);
@@ -84,10 +116,10 @@ public class SelectSheetWriteHandler implements SheetWriteHandler { @@ -84,10 +116,10 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
84 * 设置单元格下拉选择 116 * 设置单元格下拉选择
85 */ 117 */
86 private static void setColumnSelect(WriteSheetHolder writeSheetHolder, Workbook workbook, DataValidationHelper helper, 118 private static void setColumnSelect(WriteSheetHolder writeSheetHolder, Workbook workbook, DataValidationHelper helper,
87 - KeyValue<ExcelColumn, List<String>> keyValue) { 119 + KeyValue<Integer, List<String>> keyValue) {
88 // 1.1 创建可被其他单元格引用的名称 120 // 1.1 创建可被其他单元格引用的名称
89 Name name = workbook.createName(); 121 Name name = workbook.createName();
90 - String excelColumn = keyValue.getKey().name(); 122 + String excelColumn = ALPHABET.get(keyValue.getKey());
91 // 1.2 下拉框数据来源 eg:字典sheet!$B1:$B2 123 // 1.2 下拉框数据来源 eg:字典sheet!$B1:$B2
92 String refers = DICT_SHEET_NAME + "!$" + excelColumn + "$1:$" + excelColumn + "$" + keyValue.getValue().size(); 124 String refers = DICT_SHEET_NAME + "!$" + excelColumn + "$1:$" + excelColumn + "$" + keyValue.getValue().size();
93 name.setNameName("dict" + keyValue.getKey()); // 设置名称的名字 125 name.setNameName("dict" + keyValue.getKey()); // 设置名称的名字
@@ -97,7 +129,7 @@ public class SelectSheetWriteHandler implements SheetWriteHandler { @@ -97,7 +129,7 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
97 DataValidationConstraint constraint = helper.createFormulaListConstraint("dict" + keyValue.getKey()); // 设置引用约束 129 DataValidationConstraint constraint = helper.createFormulaListConstraint("dict" + keyValue.getKey()); // 设置引用约束
98 // 设置下拉单元格的首行、末行、首列、末列 130 // 设置下拉单元格的首行、末行、首列、末列
99 CellRangeAddressList rangeAddressList = new CellRangeAddressList(FIRST_ROW, LAST_ROW, 131 CellRangeAddressList rangeAddressList = new CellRangeAddressList(FIRST_ROW, LAST_ROW,
100 - keyValue.getKey().getColNum(), keyValue.getKey().getColNum()); 132 + keyValue.getKey(), keyValue.getKey());
101 DataValidation validation = helper.createValidation(constraint, rangeAddressList); 133 DataValidation validation = helper.createValidation(constraint, rangeAddressList);
102 if (validation instanceof HSSFDataValidation) { 134 if (validation instanceof HSSFDataValidation) {
103 validation.setSuppressDropDownArrow(false); 135 validation.setSuppressDropDownArrow(false);
@@ -112,4 +144,28 @@ public class SelectSheetWriteHandler implements SheetWriteHandler { @@ -112,4 +144,28 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
112 writeSheetHolder.getSheet().addValidationData(validation); 144 writeSheetHolder.getSheet().addValidationData(validation);
113 } 145 }
114 146
  147 + public static Map<String, Field> getFieldsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass) {
  148 + Map<String, Field> annotatedFields = new LinkedHashMap<>();
  149 + Field[] fields = clazz.getDeclaredFields();
  150 + for (Field field : fields) {
  151 + if (field.isAnnotationPresent(annotationClass)) {
  152 + annotatedFields.put(field.getName(), field);
  153 + }
  154 + }
  155 + return annotatedFields;
  156 + }
  157 +
  158 +
  159 + private static List<String> getExcelColumnNameList() {
  160 + ArrayList<String> strings = new ArrayList<>();
  161 + for (int i = 1; i <= 52; i++) { // 生成 52 列名称,需要更多请重写此方法
  162 + if (i <= 26) {
  163 + strings.add(String.valueOf((char) ('A' + i - 1))); // 使用 ASCII 码值转字母
  164 + } else {
  165 + strings.add(String.valueOf((char) ('A' + (i - 1) / 26 - 1)) + (char) ('A' + (i - 1) % 26));
  166 + }
  167 + }
  168 + return strings;
  169 + }
  170 +
115 } 171 }
116 \ No newline at end of file 172 \ No newline at end of file
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/service/ExcelColumnSelectDataService.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.excel.core.service;
  2 +
  3 +import cn.hutool.core.util.StrUtil;
  4 +
  5 +import java.util.Collections;
  6 +import java.util.List;
  7 +
  8 +/**
  9 + * Excel 列下拉数据源获取接口
  10 + *
  11 + * 为什么不直接解析字典还搞个接口?考虑到有的下拉数据不是从字典中获取的所有需要做一个兼容
  12 + *
  13 + * @author HUIHUI
  14 + */
  15 +public interface ExcelColumnSelectDataService {
  16 +
  17 + /**
  18 + * 获得方法名称
  19 + *
  20 + * @return 方法名称
  21 + */
  22 + String getFunctionName();
  23 +
  24 + /**
  25 + * 获得列下拉数据源
  26 + *
  27 + * @return 下拉数据源
  28 + */
  29 + List<String> getSelectDataList();
  30 +
  31 + default List<String> handle(String funcName) {
  32 + if (StrUtil.isEmptyIfStr(funcName) || !StrUtil.equals(getFunctionName(), funcName)) {
  33 + return Collections.emptyList();
  34 + }
  35 + return getSelectDataList();
  36 + }
  37 +
  38 +}
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/util/ExcelUtils.java
1 package cn.iocoder.yudao.framework.excel.core.util; 1 package cn.iocoder.yudao.framework.excel.core.util;
2 2
3 -import cn.iocoder.yudao.framework.common.core.KeyValue;  
4 -import cn.iocoder.yudao.framework.excel.core.enums.ExcelColumn;  
5 import cn.iocoder.yudao.framework.excel.core.handler.SelectSheetWriteHandler; 3 import cn.iocoder.yudao.framework.excel.core.handler.SelectSheetWriteHandler;
6 import com.alibaba.excel.EasyExcel; 4 import com.alibaba.excel.EasyExcel;
7 import com.alibaba.excel.converters.longconverter.LongStringConverter; 5 import com.alibaba.excel.converters.longconverter.LongStringConverter;
@@ -34,27 +32,11 @@ public class ExcelUtils { @@ -34,27 +32,11 @@ public class ExcelUtils {
34 */ 32 */
35 public static <T> void write(HttpServletResponse response, String filename, String sheetName, 33 public static <T> void write(HttpServletResponse response, String filename, String sheetName,
36 Class<T> head, List<T> data) throws IOException { 34 Class<T> head, List<T> data) throws IOException {
37 - write(response, filename, sheetName, head, data, null);  
38 - }  
39 -  
40 - /**  
41 - * 将列表以 Excel 响应给前端  
42 - *  
43 - * @param response 响应  
44 - * @param filename 文件名  
45 - * @param sheetName Excel sheet 名  
46 - * @param head Excel head 头  
47 - * @param data 数据列表哦  
48 - * @param selectMap 下拉选择数据 Map<下拉所对应的列表名,下拉数据>  
49 - * @throws IOException 写入失败的情况  
50 - */  
51 - public static <T> void write(HttpServletResponse response, String filename, String sheetName,  
52 - Class<T> head, List<T> data, List<KeyValue<ExcelColumn, List<String>>> selectMap) throws IOException {  
53 // 输出 Excel 35 // 输出 Excel
54 EasyExcel.write(response.getOutputStream(), head) 36 EasyExcel.write(response.getOutputStream(), head)
55 .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理 37 .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
56 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度 38 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度
57 - .registerWriteHandler(new SelectSheetWriteHandler(selectMap)) // 基于固定 sheet 实现下拉框 39 + .registerWriteHandler(new SelectSheetWriteHandler(head)) // 基于固定 sheet 实现下拉框
58 .registerConverter(new LongStringConverter()) // 避免 Long 类型丢失精度 40 .registerConverter(new LongStringConverter()) // 避免 Long 类型丢失精度
59 .sheet(sheetName).doWrite(data); 41 .sheet(sheetName).doWrite(data);
60 // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了 42 // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/CrmCustomerController.java
@@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.crm.controller.admin.customer; @@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.crm.controller.admin.customer;
2 2
3 import cn.hutool.core.collection.CollUtil; 3 import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.map.MapUtil; 4 import cn.hutool.core.map.MapUtil;
5 -import cn.iocoder.yudao.framework.common.core.KeyValue;  
6 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 5 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
7 import cn.iocoder.yudao.framework.common.pojo.PageResult; 6 import cn.iocoder.yudao.framework.common.pojo.PageResult;
8 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; 7 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
@@ -10,9 +9,7 @@ import cn.iocoder.yudao.framework.common.util.collection.MapUtils; @@ -10,9 +9,7 @@ import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
10 import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; 9 import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
11 import cn.iocoder.yudao.framework.common.util.number.NumberUtils; 10 import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
12 import cn.iocoder.yudao.framework.common.util.object.BeanUtils; 11 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
13 -import cn.iocoder.yudao.framework.excel.core.enums.ExcelColumn;  
14 import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; 12 import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
15 -import cn.iocoder.yudao.framework.ip.core.Area;  
16 import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils; 13 import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
17 import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; 14 import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
18 import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.customer.*; 15 import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.customer.*;
@@ -38,7 +35,6 @@ import org.springframework.web.bind.annotation.*; @@ -38,7 +35,6 @@ import org.springframework.web.bind.annotation.*;
38 35
39 import java.io.IOException; 36 import java.io.IOException;
40 import java.time.LocalDateTime; 37 import java.time.LocalDateTime;
41 -import java.util.ArrayList;  
42 import java.util.Arrays; 38 import java.util.Arrays;
43 import java.util.List; 39 import java.util.List;
44 import java.util.Map; 40 import java.util.Map;
@@ -49,7 +45,6 @@ import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE; @@ -49,7 +45,6 @@ import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE;
49 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; 45 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
50 import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT; 46 import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
51 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; 47 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
52 -import static cn.iocoder.yudao.module.crm.enums.DictTypeConstants.*;  
53 import static java.util.Collections.singletonList; 48 import static java.util.Collections.singletonList;
54 49
55 @Tag(name = "管理后台 - CRM 客户") 50 @Tag(name = "管理后台 - CRM 客户")
@@ -266,25 +261,7 @@ public class CrmCustomerController { @@ -266,25 +261,7 @@ public class CrmCustomerController {
266 .areaId(null).detailAddress("").remark("").build() 261 .areaId(null).detailAddress("").remark("").build()
267 ); 262 );
268 // 输出 263 // 输出
269 - ExcelUtils.write(response, "客户导入模板.xls", "客户列表", CrmCustomerImportExcelVO.class, list, builderSelectMap());  
270 - }  
271 -  
272 - private List<KeyValue<ExcelColumn, List<String>>> builderSelectMap() {  
273 - List<KeyValue<ExcelColumn, List<String>>> selectMap = new ArrayList<>();  
274 - // 获取地区下拉数据  
275 - // TODO @puhui999:嘿嘿,这里改成省份、城市、区域,三个选项,难度大么?  
276 - Area area = AreaUtils.parseArea(Area.ID_CHINA);  
277 - selectMap.add(new KeyValue<>(ExcelColumn.G, AreaUtils.getAreaNodePathList(area.getChildren())));  
278 - // 获取客户所属行业  
279 - List<String> customerIndustries = dictDataApi.getDictDataLabelList(CRM_CUSTOMER_INDUSTRY);  
280 - selectMap.add(new KeyValue<>(ExcelColumn.I, customerIndustries));  
281 - // 获取客户等级  
282 - List<String> customerLevels = dictDataApi.getDictDataLabelList(CRM_CUSTOMER_LEVEL);  
283 - selectMap.add(new KeyValue<>(ExcelColumn.J, customerLevels));  
284 - // 获取客户来源  
285 - List<String> customerSources = dictDataApi.getDictDataLabelList(CRM_CUSTOMER_SOURCE);  
286 - selectMap.add(new KeyValue<>(ExcelColumn.K, customerSources));  
287 - return selectMap; 264 + ExcelUtils.write(response, "客户导入模板.xls", "客户列表", CrmCustomerImportExcelVO.class, list);
288 } 265 }
289 266
290 @PostMapping("/import") 267 @PostMapping("/import")
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/vo/customer/CrmCustomerImportExcelVO.java
1 package cn.iocoder.yudao.module.crm.controller.admin.customer.vo.customer; 1 package cn.iocoder.yudao.module.crm.controller.admin.customer.vo.customer;
2 2
3 import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat; 3 import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
  4 +import cn.iocoder.yudao.framework.excel.core.annotations.ExcelColumnSelect;
4 import cn.iocoder.yudao.framework.excel.core.convert.AreaConvert; 5 import cn.iocoder.yudao.framework.excel.core.convert.AreaConvert;
5 import cn.iocoder.yudao.framework.excel.core.convert.DictConvert; 6 import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
  7 +import cn.iocoder.yudao.module.crm.framework.excel.service.AreaExcelColumnSelectDataServiceImpl;
  8 +import cn.iocoder.yudao.module.crm.framework.excel.service.CrmCustomerIndustryExcelColumnSelectDataServiceImpl;
  9 +import cn.iocoder.yudao.module.crm.framework.excel.service.CrmCustomerLevelExcelColumnSelectDataServiceImpl;
  10 +import cn.iocoder.yudao.module.crm.framework.excel.service.CrmCustomerSourceExcelColumnSelectDataServiceImpl;
6 import com.alibaba.excel.annotation.ExcelProperty; 11 import com.alibaba.excel.annotation.ExcelProperty;
7 import lombok.AllArgsConstructor; 12 import lombok.AllArgsConstructor;
8 import lombok.Builder; 13 import lombok.Builder;
@@ -41,6 +46,7 @@ public class CrmCustomerImportExcelVO { @@ -41,6 +46,7 @@ public class CrmCustomerImportExcelVO {
41 private String email; 46 private String email;
42 47
43 @ExcelProperty(value = "地区", converter = AreaConvert.class) 48 @ExcelProperty(value = "地区", converter = AreaConvert.class)
  49 + @ExcelColumnSelect(AreaExcelColumnSelectDataServiceImpl.FUNCTION_NAME)
44 private Integer areaId; 50 private Integer areaId;
45 51
46 @ExcelProperty("详细地址") 52 @ExcelProperty("详细地址")
@@ -48,14 +54,17 @@ public class CrmCustomerImportExcelVO { @@ -48,14 +54,17 @@ public class CrmCustomerImportExcelVO {
48 54
49 @ExcelProperty(value = "所属行业", converter = DictConvert.class) 55 @ExcelProperty(value = "所属行业", converter = DictConvert.class)
50 @DictFormat(CRM_CUSTOMER_INDUSTRY) 56 @DictFormat(CRM_CUSTOMER_INDUSTRY)
  57 + @ExcelColumnSelect(CrmCustomerIndustryExcelColumnSelectDataServiceImpl.FUNCTION_NAME)
51 private Integer industryId; 58 private Integer industryId;
52 59
53 @ExcelProperty(value = "客户等级", converter = DictConvert.class) 60 @ExcelProperty(value = "客户等级", converter = DictConvert.class)
54 @DictFormat(CRM_CUSTOMER_LEVEL) 61 @DictFormat(CRM_CUSTOMER_LEVEL)
  62 + @ExcelColumnSelect(CrmCustomerLevelExcelColumnSelectDataServiceImpl.FUNCTION_NAME)
55 private Integer level; 63 private Integer level;
56 64
57 @ExcelProperty(value = "客户来源", converter = DictConvert.class) 65 @ExcelProperty(value = "客户来源", converter = DictConvert.class)
58 @DictFormat(CRM_CUSTOMER_SOURCE) 66 @DictFormat(CRM_CUSTOMER_SOURCE)
  67 + @ExcelColumnSelect(CrmCustomerSourceExcelColumnSelectDataServiceImpl.FUNCTION_NAME)
59 private Integer source; 68 private Integer source;
60 69
61 @ExcelProperty("备注") 70 @ExcelProperty("备注")
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/framework/excel/package-info.java 0 → 100644
  1 +package cn.iocoder.yudao.module.crm.framework.excel;
0 \ No newline at end of file 2 \ 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 0 → 100644
  1 +package cn.iocoder.yudao.module.crm.framework.excel.service;
  2 +
  3 +import cn.iocoder.yudao.framework.excel.core.service.ExcelColumnSelectDataService;
  4 +import cn.iocoder.yudao.framework.ip.core.Area;
  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 +import org.springframework.stereotype.Service;
  9 +
  10 +import java.util.List;
  11 +
  12 +/**
  13 + * Excel 所属地区列下拉数据源获取接口实现类
  14 + *
  15 + * @author HUIHUI
  16 + */
  17 +@Service
  18 +public class AreaExcelColumnSelectDataServiceImpl implements ExcelColumnSelectDataService {
  19 +
  20 + public static final String FUNCTION_NAME = "getCrmAreaNameList"; // 防止和别的模块重名
  21 +
  22 + @Resource
  23 + private DictDataApi dictDataApi;
  24 +
  25 + @Override
  26 + public String getFunctionName() {
  27 + return FUNCTION_NAME;
  28 + }
  29 +
  30 + @Override
  31 + public List<String> getSelectDataList() {
  32 + // 获取地区下拉数据
  33 + // TODO @puhui999:嘿嘿,这里改成省份、城市、区域,三个选项,难度大么?
  34 + Area area = AreaUtils.parseArea(Area.ID_CHINA);
  35 + return AreaUtils.getAreaNodePathList(area.getChildren());
  36 + }
  37 +
  38 +}
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/framework/excel/service/CrmCustomerIndustryExcelColumnSelectDataServiceImpl.java 0 → 100644
  1 +package cn.iocoder.yudao.module.crm.framework.excel.service;
  2 +
  3 +import cn.iocoder.yudao.framework.excel.core.service.ExcelColumnSelectDataService;
  4 +import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
  5 +import jakarta.annotation.Resource;
  6 +import org.springframework.stereotype.Service;
  7 +
  8 +import java.util.List;
  9 +
  10 +import static cn.iocoder.yudao.module.crm.enums.DictTypeConstants.CRM_CUSTOMER_INDUSTRY;
  11 +
  12 +/**
  13 + * Excel 客户所属行业列下拉数据源获取接口实现类
  14 + *
  15 + * @author HUIHUI
  16 + */
  17 +@Service
  18 +public class CrmCustomerIndustryExcelColumnSelectDataServiceImpl implements ExcelColumnSelectDataService {
  19 +
  20 + public static final String FUNCTION_NAME = "getCrmCustomerIndustryList";
  21 +
  22 + @Resource
  23 + private DictDataApi dictDataApi;
  24 +
  25 + @Override
  26 + public String getFunctionName() {
  27 + return FUNCTION_NAME;
  28 + }
  29 +
  30 + @Override
  31 + public List<String> getSelectDataList() {
  32 + return dictDataApi.getDictDataLabelList(CRM_CUSTOMER_INDUSTRY);
  33 + }
  34 +
  35 +}
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/framework/excel/service/CrmCustomerLevelExcelColumnSelectDataServiceImpl.java 0 → 100644
  1 +package cn.iocoder.yudao.module.crm.framework.excel.service;
  2 +
  3 +import cn.iocoder.yudao.framework.excel.core.service.ExcelColumnSelectDataService;
  4 +import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
  5 +import jakarta.annotation.Resource;
  6 +import org.springframework.stereotype.Service;
  7 +
  8 +import java.util.List;
  9 +
  10 +import static cn.iocoder.yudao.module.crm.enums.DictTypeConstants.CRM_CUSTOMER_LEVEL;
  11 +
  12 +/**
  13 + * Excel 客户等级列下拉数据源获取接口实现类
  14 + *
  15 + * @author HUIHUI
  16 + */
  17 +@Service
  18 +public class CrmCustomerLevelExcelColumnSelectDataServiceImpl implements ExcelColumnSelectDataService {
  19 +
  20 + public static final String FUNCTION_NAME = "getCrmCustomerLevelList";
  21 +
  22 + @Resource
  23 + private DictDataApi dictDataApi;
  24 +
  25 + @Override
  26 + public String getFunctionName() {
  27 + return FUNCTION_NAME;
  28 + }
  29 +
  30 + @Override
  31 + public List<String> getSelectDataList() {
  32 + return dictDataApi.getDictDataLabelList(CRM_CUSTOMER_LEVEL);
  33 + }
  34 +
  35 +}
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/framework/excel/service/CrmCustomerSourceExcelColumnSelectDataServiceImpl.java 0 → 100644
  1 +package cn.iocoder.yudao.module.crm.framework.excel.service;
  2 +
  3 +import cn.iocoder.yudao.framework.excel.core.service.ExcelColumnSelectDataService;
  4 +import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
  5 +import jakarta.annotation.Resource;
  6 +import org.springframework.stereotype.Service;
  7 +
  8 +import java.util.List;
  9 +
  10 +import static cn.iocoder.yudao.module.crm.enums.DictTypeConstants.CRM_CUSTOMER_SOURCE;
  11 +
  12 +/**
  13 + * Excel 客户来源列下拉数据源获取接口实现类
  14 + *
  15 + * @author HUIHUI
  16 + */
  17 +@Service
  18 +public class CrmCustomerSourceExcelColumnSelectDataServiceImpl implements ExcelColumnSelectDataService {
  19 +
  20 + public static final String FUNCTION_NAME = "getCrmCustomerSourceList";
  21 +
  22 + @Resource
  23 + private DictDataApi dictDataApi;
  24 +
  25 + @Override
  26 + public String getFunctionName() {
  27 + return FUNCTION_NAME;
  28 + }
  29 +
  30 + @Override
  31 + public List<String> getSelectDataList() {
  32 + return dictDataApi.getDictDataLabelList(CRM_CUSTOMER_SOURCE);
  33 + }
  34 +
  35 +}