CRM:完善客户导入

This commit is contained in:
puhui999 2024-02-22 11:07:34 +08:00
parent b26eb6266f
commit 41b8575b8d
6 changed files with 108 additions and 28 deletions

View File

@ -4,7 +4,7 @@ import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.csv.CsvRow; import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil; import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.ip.core.Area; import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum; import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum;
@ -18,6 +18,7 @@ import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.findFirst;
/** /**
* 区域工具类 * 区域工具类
@ -75,6 +76,26 @@ public class AreaUtils {
return areas.get(id); return areas.get(id);
} }
/**
* 获得指定区域对应的编号
*
* @param path 区域编号
* @return 编号
*/
public static Area getArea(String path) {
String[] paths = path.split("/");
Area area = null;
for (int i = 0; i < paths.length; i++) {
final int finalI = i;
if (area == null) {
area = findFirst(convertList(areas.values(), a -> a), item -> ObjUtil.equal(paths[finalI], item.getName()));
continue;
}
area = findFirst(area.getChildren(), item -> ObjUtil.equal(paths[finalI], item.getName()));
}
return area;
}
/** /**
* 获取所有节点的全路径名称如河南省/石家庄市/新华区 * 获取所有节点的全路径名称如河南省/石家庄市/新华区
* *
@ -82,23 +103,29 @@ public class AreaUtils {
* @return 所有节点的全路径名称 * @return 所有节点的全路径名称
*/ */
public static List<String> getAllAreaNodePaths(List<Area> areas) { public static List<String> getAllAreaNodePaths(List<Area> areas) {
return convertList(areas, AreaUtils::buildTreePath); List<String> paths = new ArrayList<>();
areas.forEach(area -> traverse(area, "", paths));
return paths;
} }
// TODO @puhui999: 展开树再构建全路径 /**
private static String buildTreePath(Area node) { * 构建一棵树的所有节点的全路径名称并将其存储为 "祖先/父级/子级" 的形式
if (node.getParent() == null || Area.ID_CHINA.equals(node.getParent().getId())) { // 忽略中国 *
// 已经是根节点直接返回节点名称 * @param node 父节点
return node.getName(); * @param path 全路径名称
} else { * @param paths 全路径名称列表
// 递归拼接上级节点的名称 */
Area parent = getArea(node.getParent().getId()); private static void traverse(Area node, String path, List<String> paths) {
if (parent != null) { if (node == null) {
String parentPath = buildTreePath(parent); return;
return parentPath + "/" + node.getName(); }
} // 构建当前节点的路径
String currentPath = path.isEmpty() ? node.getName() : path + "/" + node.getName();
paths.add(currentPath);
// 递归遍历子节点
for (Area child : node.getChildren()) {
traverse(child, currentPath, paths);
} }
return StrUtil.EMPTY;
} }
/** /**

View File

@ -46,6 +46,11 @@
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
</dependency> </dependency>
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-spring-boot-starter-biz-ip</artifactId>
<scope>provided</scope> <!-- 设置为 provided只有 ExcelUtils 使用 -->
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,46 @@
package cn.iocoder.yudao.framework.excel.core.convert;
import cn.hutool.core.convert.Convert;
import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import lombok.extern.slf4j.Slf4j;
/**
* Excel 数据地区转换器
*
* @author HUIHUI
*/
@Slf4j
public class AreaConvert implements Converter<Object> {
@Override
public Class<?> supportJavaTypeKey() {
throw new UnsupportedOperationException("暂不支持,也不需要");
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
throw new UnsupportedOperationException("暂不支持,也不需要");
}
@Override
public Object convertToJavaData(ReadCellData readCellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
// 解析地区编号
String label = readCellData.getStringValue();
Area area = AreaUtils.getArea(label);
if (area == null) {
log.error("[convertToJavaData][label({}) 解析不掉]", label);
return null;
}
// value 转换成对应的属性
Class<?> fieldClazz = contentProperty.getField().getType();
return Convert.convert(fieldClazz, area.getId());
}
}

View File

@ -9,6 +9,7 @@ import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellRangeAddressList;
import java.util.Comparator;
import java.util.List; import java.util.List;
/** /**
@ -18,12 +19,13 @@ import java.util.List;
*/ */
public class SelectSheetWriteHandler implements SheetWriteHandler { public class SelectSheetWriteHandler implements SheetWriteHandler {
private List<KeyValue<Integer, List<String>>> selectMap; private final List<KeyValue<Integer, List<String>>> selectMap;
private final char[] alphabet = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', private static final char[] ALPHABET = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
public SelectSheetWriteHandler(List<KeyValue<Integer, List<String>>> selectMap) { public SelectSheetWriteHandler(List<KeyValue<Integer, List<String>>> selectMap) {
selectMap.sort(Comparator.comparing(item -> item.getValue().size())); // 升序不然创建下拉会报错
this.selectMap = selectMap; this.selectMap = selectMap;
} }
@ -86,17 +88,17 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
*/ */
private String getExcelColumn(int num) { private String getExcelColumn(int num) {
String column = ""; String column = "";
int len = alphabet.length - 1; int len = ALPHABET.length - 1;
int first = num / len; int first = num / len;
int second = num % len; int second = num % len;
if (num <= len) { if (num <= len) {
column = alphabet[num] + ""; column = ALPHABET[num] + "";
} else { } else {
column = alphabet[first - 1] + ""; column = ALPHABET[first - 1] + "";
if (second == 0) { if (second == 0) {
column = column + alphabet[len] + ""; column = column + ALPHABET[len] + "";
} else { } else {
column = column + alphabet[second - 1] + ""; column = column + ALPHABET[second - 1] + "";
} }
} }
return column; return column;

View File

@ -37,7 +37,10 @@ import org.springframework.web.bind.annotation.*;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@ -332,8 +335,6 @@ public class CrmCustomerController {
// 获取客户来源 // 获取客户来源
List<String> customerSources = dictDataApi.getDictDataLabelList(CRM_CUSTOMER_SOURCE); List<String> customerSources = dictDataApi.getDictDataLabelList(CRM_CUSTOMER_SOURCE);
selectMap.add(new KeyValue<>(10, customerSources)); selectMap.add(new KeyValue<>(10, customerSources));
// 升序不然创建下拉会报错
selectMap.sort(Comparator.comparing(item -> item.getValue().size()));
return selectMap; return selectMap;
} }

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.crm.controller.admin.customer.vo.customer; package cn.iocoder.yudao.module.crm.controller.admin.customer.vo.customer;
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat; import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
import cn.iocoder.yudao.framework.excel.core.convert.AreaConvert;
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert; import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -39,14 +40,12 @@ public class CrmCustomerImportExcelVO {
@ExcelProperty("邮箱") @ExcelProperty("邮箱")
private String email; private String email;
// TODO @puhui999需要选择省市区需要研究下怎么搞合理点 @ExcelProperty(value = "地区", converter = AreaConvert.class)
@ExcelProperty("地区编号")
private Integer areaId; private Integer areaId;
@ExcelProperty("详细地址") @ExcelProperty("详细地址")
private String detailAddress; private String detailAddress;
// TODO @puhui999industryIdlevelsource 字段可以研究下怎么搞下拉框
@ExcelProperty(value = "所属行业", converter = DictConvert.class) @ExcelProperty(value = "所属行业", converter = DictConvert.class)
@DictFormat(CRM_CUSTOMER_INDUSTRY) @DictFormat(CRM_CUSTOMER_INDUSTRY)
private Integer industryId; private Integer industryId;