Skip to content

Commit b0ff258

Browse files
committed
refactor(common-excel): 支持字典自动转换
1 parent 27b2df3 commit b0ff258

File tree

9 files changed

+174
-4
lines changed

9 files changed

+174
-4
lines changed

pig-common/pig-common-bom/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<mysql.version>9.0.0</mysql.version>
2929
<dynamic-ds.version>4.3.1</dynamic-ds.version>
3030
<seata.version>1.7.0</seata.version>
31-
<excel.version>3.3.0</excel.version>
31+
<excel.version>3.3.1-SNAPSHOT</excel.version>
3232
<asm.version>7.1</asm.version>
3333
<sms.version>3.0.0</sms.version>
3434
<jaxb.version>2.3.5</jaxb.version>
@@ -88,6 +88,11 @@
8888
<artifactId>pig-common-xss</artifactId>
8989
<version>${revision}</version>
9090
</dependency>
91+
<dependency>
92+
<groupId>com.pig4cloud</groupId>
93+
<artifactId>pig-common-excel</artifactId>
94+
<version>${revision}</version>
95+
</dependency>
9196
<dependency>
9297
<groupId>com.pig4cloud</groupId>
9398
<artifactId>pig-upms-api</artifactId>

pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/util/R.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.pig4cloud.pig.common.core.constant.CommonConstants;
2020
import lombok.*;
2121
import lombok.experimental.Accessors;
22+
import lombok.experimental.FieldNameConstants;
2223

2324
import java.io.Serializable;
2425

@@ -32,6 +33,7 @@
3233
@NoArgsConstructor
3334
@AllArgsConstructor
3435
@Accessors(chain = true)
36+
@FieldNameConstants
3537
public class R<T> implements Serializable {
3638

3739
private static final long serialVersionUID = 1L;

pig-common/pig-common-excel/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ /*
4+
~ * Copyright (c) 2019-2020, 冷冷 ([email protected]).
5+
~ * <p>
6+
~ * Licensed under the GNU Lesser General Public License 3.0 (the "License");
7+
~ * you may not use this file except in compliance with the License.
8+
~ * You may obtain a copy of the License at
9+
~ * <p>
10+
~ * https://www.gnu.org/licenses/lgpl.html
11+
~ * <p>
12+
~ * Unless required by applicable law or agreed to in writing, software
13+
~ * distributed under the License is distributed on an "AS IS" BASIS,
14+
~ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ * See the License for the specific language governing permissions and
16+
~ * limitations under the License.
17+
~ */
18+
-->
19+
20+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>com.pig4cloud</groupId>
25+
<artifactId>pig-common</artifactId>
26+
<version>${revision}</version>
27+
</parent>
28+
29+
<artifactId>pig-common-excel</artifactId>
30+
<packaging>jar</packaging>
31+
32+
<description>excel 导入导出处理模块</description>
33+
34+
<dependencies>
35+
<!--核心依赖,提供字典查询能力-->
36+
<dependency>
37+
<groupId>com.pig4cloud</groupId>
38+
<artifactId>pig-common-core</artifactId>
39+
</dependency>
40+
<!-- excel 导入导出工具类:https://github.com/pig-mesh/excel-spring-boot-starter-->
41+
<dependency>
42+
<groupId>com.pig4cloud.excel</groupId>
43+
<artifactId>excel-spring-boot-starter</artifactId>
44+
</dependency>
45+
</dependencies>
46+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.pig4cloud.pig.common.excel;
2+
3+
import com.pig4cloud.pig.common.excel.provider.RemoteDictDataProvider;
4+
import com.pig4cloud.plugin.excel.handler.DictDataProvider;
5+
import org.springframework.boot.autoconfigure.AutoConfiguration;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.web.client.RestTemplate;
9+
10+
/**
11+
* excel 自动装配类
12+
*
13+
* @author lengleng
14+
* @date 2024/9/1
15+
*/
16+
@AutoConfiguration
17+
public class ExcelAutoConfiguration {
18+
19+
/**
20+
* dict 数据提供程序
21+
* @param restTemplate REST 模板
22+
* @return {@link DictDataProvider }
23+
*/
24+
@Bean
25+
@ConditionalOnMissingBean
26+
public DictDataProvider dictDataProvider(RestTemplate restTemplate) {
27+
return new RemoteDictDataProvider(restTemplate);
28+
}
29+
30+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.pig4cloud.pig.common.excel.provider;
2+
3+
import cn.hutool.core.collection.CollUtil;
4+
import cn.hutool.core.map.MapUtil;
5+
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
6+
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
7+
import com.pig4cloud.pig.common.core.util.R;
8+
import com.pig4cloud.pig.common.core.util.SpringContextHolder;
9+
import com.pig4cloud.plugin.excel.handler.DictDataProvider;
10+
import com.pig4cloud.plugin.excel.vo.DictEnum;
11+
import lombok.RequiredArgsConstructor;
12+
import org.springframework.http.HttpEntity;
13+
import org.springframework.http.HttpHeaders;
14+
import org.springframework.http.HttpMethod;
15+
import org.springframework.http.ResponseEntity;
16+
import org.springframework.web.client.RestTemplate;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
/**
23+
* 远程 dict 数据提供程序
24+
*
25+
* @author lengleng
26+
* @date 2024/09/01
27+
*/
28+
@RequiredArgsConstructor
29+
public class RemoteDictDataProvider implements DictDataProvider {
30+
31+
private final RestTemplate restTemplate;
32+
33+
/**
34+
* 获取 dict
35+
* @param type 类型
36+
* @return {@link DictEnum[] }
37+
*/
38+
@Override
39+
public DictEnum[] getDict(String type) {
40+
// 获取服务URL
41+
String serviceUrl = getServiceUrl(type);
42+
// 创建请求实体
43+
HttpHeaders headers = new HttpHeaders();
44+
headers.add(SecurityConstants.FROM, SecurityConstants.FROM_IN);
45+
HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
46+
// 发送HTTP请求并获取响应
47+
ResponseEntity<Map> response = restTemplate.exchange(serviceUrl, HttpMethod.GET, requestEntity, Map.class,
48+
type);
49+
50+
// 解析响应数据
51+
List<Map<String, Object>> dictDataList = MapUtil.get(response.getBody(), R.Fields.data, ArrayList.class);
52+
if (CollUtil.isEmpty(dictDataList)) {
53+
return new DictEnum[0];
54+
}
55+
56+
// 构建 DictEnum 数组
57+
DictEnum.Builder dictEnumBuilder = DictEnum.builder();
58+
for (Map<String, Object> dictData : dictDataList) {
59+
String value = MapUtil.getStr(dictData, "value");
60+
String label = MapUtil.getStr(dictData, "label");
61+
dictEnumBuilder.add(value, label);
62+
}
63+
64+
return dictEnumBuilder.build();
65+
}
66+
67+
/**
68+
* 获取服务 URL
69+
* @param param 参数
70+
* @return {@link String }
71+
*/
72+
private String getServiceUrl(String param) {
73+
// 根据当前架构模式,组装URL
74+
if (SpringContextHolder.isMicro()) {
75+
return String.format("http://%s/dict/remote/type/%s", ServiceNameConstants.UPMS_SERVICE, param);
76+
}
77+
else {
78+
return String.format("http://%s/dict/remote/type/%s", SpringContextHolder.getEnvironment()
79+
.resolvePlaceholders("127.0.0.1:${server.port}${server.servlet.context-path}"), param);
80+
}
81+
}
82+
83+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.pig4cloud.pig.common.excel.ExcelAutoConfiguration

pig-common/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@
4141
<module>pig-common-feign</module>
4242
<module>pig-common-swagger</module>
4343
<module>pig-common-xss</module>
44+
<module>pig-common-excel</module>
4445
</modules>
4546
</project>

pig-upms/pig-upms-api/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
<groupId>com.pig4cloud</groupId>
4747
<artifactId>pig-common-mybatis</artifactId>
4848
</dependency>
49-
<!-- excel 导入导出 https://github.com/pig-mesh/excel-spring-boot-starter -->
49+
<!-- excel 导入导出 -->
5050
<dependency>
51-
<groupId>com.pig4cloud.excel</groupId>
52-
<artifactId>excel-spring-boot-starter</artifactId>
51+
<groupId>com.pig4cloud</groupId>
52+
<artifactId>pig-common-excel</artifactId>
5353
</dependency>
5454
</dependencies>
5555
</project>

pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/vo/UserExcelVO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.alibaba.excel.annotation.ExcelIgnore;
44
import com.alibaba.excel.annotation.ExcelProperty;
55
import com.alibaba.excel.annotation.write.style.ColumnWidth;
6+
import com.pig4cloud.plugin.excel.annotation.DictTypeProperty;
67
import com.pig4cloud.plugin.excel.annotation.ExcelLine;
78
import jakarta.validation.constraints.NotBlank;
89
import lombok.Data;
@@ -95,6 +96,7 @@ public class UserExcelVO implements Serializable {
9596
* 锁定标记
9697
*/
9798
@ExcelProperty("锁定标记,0:正常,9:已锁定")
99+
@DictTypeProperty("lock_flagX")
98100
private String lockFlag;
99101

100102
/**

0 commit comments

Comments
 (0)