mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 00:15:06 +08:00
✨ CRM:code review 客户导入
This commit is contained in:
@ -257,11 +257,11 @@ public class CollectionUtils {
|
||||
return !CollectionUtil.isEmpty(from) ? from.get(0) : null;
|
||||
}
|
||||
|
||||
public static <T> T findFirst(List<T> from, Predicate<T> predicate) {
|
||||
public static <T> T findFirst(Collection<T> from, Predicate<T> predicate) {
|
||||
return findFirst(from, predicate, Function.identity());
|
||||
}
|
||||
|
||||
public static <T, U> U findFirst(List<T> from, Predicate<T> predicate, Function<T, U> func) {
|
||||
public static <T, U> U findFirst(Collection<T> from, Predicate<T> predicate, Function<T, U> func) {
|
||||
if (CollUtil.isEmpty(from)) {
|
||||
return null;
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.text.csv.CsvRow;
|
||||
import cn.hutool.core.text.csv.CsvUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.ip.core.Area;
|
||||
import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum;
|
||||
@ -72,26 +71,25 @@ public class AreaUtils {
|
||||
* @param id 区域编号
|
||||
* @return 区域
|
||||
*/
|
||||
public static Area getArea(Integer id) {
|
||||
public static Area parseArea(Integer id) {
|
||||
return areas.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定区域对应的编号
|
||||
*
|
||||
* @param path 区域编号
|
||||
* @return 编号
|
||||
* @param pathStr 区域路径,例如说:河南省/石家庄市/新华区
|
||||
* @return 区域
|
||||
*/
|
||||
public static Area getArea(String path) {
|
||||
String[] paths = path.split("/");
|
||||
public static Area parseArea(String pathStr) {
|
||||
String[] paths = pathStr.split("/");
|
||||
Area area = null;
|
||||
for (int i = 0; i < paths.length; i++) {
|
||||
final int finalI = i;
|
||||
for (String path : paths) {
|
||||
if (area == null) {
|
||||
area = findFirst(convertList(areas.values(), a -> a), item -> ObjUtil.equal(paths[finalI], item.getName()));
|
||||
continue;
|
||||
area = findFirst(areas.values(), item -> item.getName().equals(path));
|
||||
} else {
|
||||
area = findFirst(area.getChildren(), item -> item.getName().equals(path));
|
||||
}
|
||||
area = findFirst(area.getChildren(), item -> ObjUtil.equal(paths[finalI], item.getName()));
|
||||
}
|
||||
return area;
|
||||
}
|
||||
@ -102,9 +100,9 @@ public class AreaUtils {
|
||||
* @param areas 地区树
|
||||
* @return 所有节点的全路径名称
|
||||
*/
|
||||
public static List<String> getAllAreaNodePaths(List<Area> areas) {
|
||||
public static List<String> getAreaNodePathList(List<Area> areas) {
|
||||
List<String> paths = new ArrayList<>();
|
||||
areas.forEach(area -> traverse(area, "", paths));
|
||||
areas.forEach(area -> getAreaNodePathList(area, "", paths));
|
||||
return paths;
|
||||
}
|
||||
|
||||
@ -113,9 +111,9 @@ public class AreaUtils {
|
||||
*
|
||||
* @param node 父节点
|
||||
* @param path 全路径名称
|
||||
* @param paths 全路径名称列表
|
||||
* @param paths 全路径名称列表,省份/城市/地区
|
||||
*/
|
||||
private static void traverse(Area node, String path, List<String> paths) {
|
||||
private static void getAreaNodePathList(Area node, String path, List<String> paths) {
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
@ -124,7 +122,7 @@ public class AreaUtils {
|
||||
paths.add(currentPath);
|
||||
// 递归遍历子节点
|
||||
for (Area child : node.getChildren()) {
|
||||
traverse(child, currentPath, paths);
|
||||
getAreaNodePathList(child, currentPath, paths);
|
||||
}
|
||||
}
|
||||
|
||||
@ -195,7 +193,7 @@ public class AreaUtils {
|
||||
*/
|
||||
public static Integer getParentIdByType(Integer id, @NonNull AreaTypeEnum type) {
|
||||
for (int i = 0; i < Byte.MAX_VALUE; i++) {
|
||||
Area area = AreaUtils.getArea(id);
|
||||
Area area = AreaUtils.parseArea(id);
|
||||
if (area == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ public class IPUtils {
|
||||
* @return 地区
|
||||
*/
|
||||
public static Area getArea(String ip) {
|
||||
return AreaUtils.getArea(getAreaId(ip));
|
||||
return AreaUtils.parseArea(getAreaId(ip));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,6 +82,6 @@ public class IPUtils {
|
||||
* @return 地区
|
||||
*/
|
||||
public static Area getArea(long ip) {
|
||||
return AreaUtils.getArea(getAreaId(ip));
|
||||
return AreaUtils.parseArea(getAreaId(ip));
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ public class AreaUtilsTest {
|
||||
@Test
|
||||
public void testGetArea() {
|
||||
// 调用:北京
|
||||
Area area = AreaUtils.getArea(110100);
|
||||
Area area = AreaUtils.parseArea(110100);
|
||||
// 断言
|
||||
assertEquals(area.getId(), 110100);
|
||||
assertEquals(area.getName(), "北京市");
|
||||
|
@ -49,7 +49,7 @@
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-biz-ip</artifactId>
|
||||
<scope>provided</scope> <!-- 设置为 provided,只有 ExcelUtils 使用 -->
|
||||
<optional>true</optional> <!-- 设置为 optional,只有在 AreaConvert 的时候使用 -->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -33,7 +33,7 @@ public class AreaConvert implements Converter<Object> {
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
// 解析地区编号
|
||||
String label = readCellData.getStringValue();
|
||||
Area area = AreaUtils.getArea(label);
|
||||
Area area = AreaUtils.parseArea(label);
|
||||
if (area == null) {
|
||||
log.error("[convertToJavaData][label({}) 解析不掉]", label);
|
||||
return null;
|
||||
|
@ -19,33 +19,40 @@ import java.util.List;
|
||||
*/
|
||||
public class SelectSheetWriteHandler implements SheetWriteHandler {
|
||||
|
||||
private static final String DICT_SHEET_NAME = "字典sheet";
|
||||
|
||||
// TODO @puhui999:key 不使用 int 值么?感觉不是很优雅哈。
|
||||
private final List<KeyValue<Integer, List<String>>> selectMap;
|
||||
|
||||
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'};
|
||||
|
||||
public SelectSheetWriteHandler(List<KeyValue<Integer, List<String>>> selectMap) {
|
||||
if (CollUtil.isEmpty(selectMap)) {
|
||||
this.selectMap = null;
|
||||
return;
|
||||
}
|
||||
selectMap.sort(Comparator.comparing(item -> item.getValue().size())); // 升序不然创建下拉会报错
|
||||
this.selectMap = selectMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
|
||||
if (selectMap == null || CollUtil.isEmpty(selectMap)) {
|
||||
if (CollUtil.isEmpty(selectMap)) {
|
||||
return;
|
||||
}
|
||||
// 需要设置下拉框的sheet页
|
||||
Sheet curSheet = writeSheetHolder.getSheet();
|
||||
DataValidationHelper helper = curSheet.getDataValidationHelper();
|
||||
String dictSheetName = "字典sheet";
|
||||
// 需要设置下拉框的 sheet 页
|
||||
Sheet currentSheet = writeSheetHolder.getSheet();
|
||||
DataValidationHelper helper = currentSheet.getDataValidationHelper();
|
||||
Workbook workbook = writeWorkbookHolder.getWorkbook();
|
||||
// 数据字典的sheet页
|
||||
Sheet dictSheet = workbook.createSheet(dictSheetName);
|
||||
|
||||
// 数据字典的 sheet 页
|
||||
Sheet dictSheet = workbook.createSheet(DICT_SHEET_NAME);
|
||||
for (KeyValue<Integer, List<String>> keyValue : selectMap) {
|
||||
// 设置下拉单元格的首行、末行、首列、末列
|
||||
CellRangeAddressList rangeAddressList = new CellRangeAddressList(1, 65533, keyValue.getKey(), keyValue.getKey());
|
||||
int rowLen = keyValue.getValue().size();
|
||||
// 设置字典sheet页的值 每一列一个字典项
|
||||
// 设置字典 sheet 页的值 每一列一个字典项
|
||||
for (int i = 0; i < rowLen; i++) {
|
||||
Row row = dictSheet.getRow(i);
|
||||
if (row == null) {
|
||||
@ -53,18 +60,18 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
|
||||
}
|
||||
row.createCell(keyValue.getKey()).setCellValue(keyValue.getValue().get(i));
|
||||
}
|
||||
String excelColumn = getExcelColumn(keyValue.getKey());
|
||||
// 下拉框数据来源 eg:字典sheet!$B1:$B2
|
||||
String refers = dictSheetName + "!$" + excelColumn + "$1:$" + excelColumn + "$" + rowLen;
|
||||
// 创建可被其他单元格引用的名称
|
||||
|
||||
// TODO @puhui999:下面 1. 2.1 2.2 2.3 我是按照已经理解的,调整了下格式;这样可读性更好;在 52 到 62 行,你可以看看,是不是也弄下序号;
|
||||
// 1. 创建可被其他单元格引用的名称
|
||||
Name name = workbook.createName();
|
||||
// 设置名称的名字
|
||||
name.setNameName("dict" + keyValue.getKey());
|
||||
// 设置公式
|
||||
name.setRefersToFormula(refers);
|
||||
// 设置引用约束
|
||||
DataValidationConstraint constraint = helper.createFormulaListConstraint("dict" + keyValue.getKey());
|
||||
// 设置约束
|
||||
// TODO @puhui999:下面的 excelColumn 和 refers 两行,是不是可以封装成一个方法,替代 getExcelColumn;
|
||||
String excelColumn = getExcelColumn(keyValue.getKey());
|
||||
String refers = DICT_SHEET_NAME + "!$" + excelColumn + "$1:$" + excelColumn + "$" + rowLen; // 下拉框数据来源 eg:字典sheet!$B1:$B2
|
||||
name.setNameName("dict" + keyValue.getKey()); // 设置名称的名字
|
||||
name.setRefersToFormula(refers); // 设置公式
|
||||
|
||||
// 2.1 设置约束
|
||||
DataValidationConstraint constraint = helper.createFormulaListConstraint("dict" + keyValue.getKey()); // 设置引用约束
|
||||
DataValidation validation = helper.createValidation(constraint, rangeAddressList);
|
||||
if (validation instanceof HSSFDataValidation) {
|
||||
validation.setSuppressDropDownArrow(false);
|
||||
@ -72,10 +79,10 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
|
||||
validation.setSuppressDropDownArrow(true);
|
||||
validation.setShowErrorBox(true);
|
||||
}
|
||||
// 阻止输入非下拉框的值
|
||||
// 2.2 阻止输入非下拉框的值
|
||||
validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
|
||||
validation.createErrorBox("提示", "此值不存在于下拉选择中!");
|
||||
// 添加下拉框约束
|
||||
// 2.3 添加下拉框约束
|
||||
writeSheetHolder.getSheet().addValidationData(validation);
|
||||
}
|
||||
}
|
||||
@ -86,8 +93,9 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
|
||||
* @param num 数字
|
||||
* @return 字母
|
||||
*/
|
||||
// TODO @puhui999:这个是必须字母列哇?还是数字其实也可以哈?主要想看看,怎么能把这个逻辑,进一步简化
|
||||
private String getExcelColumn(int num) {
|
||||
String column = "";
|
||||
String column;
|
||||
int len = ALPHABET.length - 1;
|
||||
int first = num / len;
|
||||
int second = num % len;
|
||||
@ -96,9 +104,9 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
|
||||
} else {
|
||||
column = ALPHABET[first - 1] + "";
|
||||
if (second == 0) {
|
||||
column = column + ALPHABET[len] + "";
|
||||
column = column + ALPHABET[len];
|
||||
} else {
|
||||
column = column + ALPHABET[second - 1] + "";
|
||||
column = column + ALPHABET[second - 1];
|
||||
}
|
||||
}
|
||||
return column;
|
||||
|
@ -33,15 +33,7 @@ public class ExcelUtils {
|
||||
*/
|
||||
public static <T> void write(HttpServletResponse response, String filename, String sheetName,
|
||||
Class<T> head, List<T> data) throws IOException {
|
||||
// 输出 Excel
|
||||
EasyExcel.write(response.getOutputStream(), head)
|
||||
.autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
|
||||
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度
|
||||
.registerConverter(new LongStringConverter()) // 避免 Long 类型丢失精度
|
||||
.sheet(sheetName).doWrite(data);
|
||||
// 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了
|
||||
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
|
||||
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
||||
write(response, filename, sheetName, head, data, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user