refactor(common-excel): 支持字典自动转换

This commit is contained in:
冷冷 2024-09-01 13:34:27 +08:00
parent 27b2df3069
commit b0ff258970
9 changed files with 174 additions and 4 deletions

View File

@ -28,7 +28,7 @@
<mysql.version>9.0.0</mysql.version>
<dynamic-ds.version>4.3.1</dynamic-ds.version>
<seata.version>1.7.0</seata.version>
<excel.version>3.3.0</excel.version>
<excel.version>3.3.1-SNAPSHOT</excel.version>
<asm.version>7.1</asm.version>
<sms.version>3.0.0</sms.version>
<jaxb.version>2.3.5</jaxb.version>
@ -88,6 +88,11 @@
<artifactId>pig-common-xss</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pig-common-excel</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pig-upms-api</artifactId>

View File

@ -19,6 +19,7 @@ package com.pig4cloud.pig.common.core.util;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldNameConstants;
import java.io.Serializable;
@ -32,6 +33,7 @@ import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@FieldNameConstants
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ /*
~ * Copyright (c) 2019-2020, 冷冷 (wangiegie@gmail.com).
~ * <p>
~ * Licensed under the GNU Lesser General Public License 3.0 (the "License");
~ * you may not use this file except in compliance with the License.
~ * You may obtain a copy of the License at
~ * <p>
~ * https://www.gnu.org/licenses/lgpl.html
~ * <p>
~ * Unless required by applicable law or agreed to in writing, software
~ * distributed under the License is distributed on an "AS IS" BASIS,
~ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ * See the License for the specific language governing permissions and
~ * limitations under the License.
~ */
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.pig4cloud</groupId>
<artifactId>pig-common</artifactId>
<version>${revision}</version>
</parent>
<artifactId>pig-common-excel</artifactId>
<packaging>jar</packaging>
<description>excel 导入导出处理模块</description>
<dependencies>
<!--核心依赖,提供字典查询能力-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pig-common-core</artifactId>
</dependency>
<!-- excel 导入导出工具类https://github.com/pig-mesh/excel-spring-boot-starter-->
<dependency>
<groupId>com.pig4cloud.excel</groupId>
<artifactId>excel-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,30 @@
package com.pig4cloud.pig.common.excel;
import com.pig4cloud.pig.common.excel.provider.RemoteDictDataProvider;
import com.pig4cloud.plugin.excel.handler.DictDataProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* excel 自动装配类
*
* @author lengleng
* @date 2024/9/1
*/
@AutoConfiguration
public class ExcelAutoConfiguration {
/**
* dict 数据提供程序
* @param restTemplate REST 模板
* @return {@link DictDataProvider }
*/
@Bean
@ConditionalOnMissingBean
public DictDataProvider dictDataProvider(RestTemplate restTemplate) {
return new RemoteDictDataProvider(restTemplate);
}
}

View File

@ -0,0 +1,83 @@
package com.pig4cloud.pig.common.excel.provider;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.core.util.SpringContextHolder;
import com.pig4cloud.plugin.excel.handler.DictDataProvider;
import com.pig4cloud.plugin.excel.vo.DictEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 远程 dict 数据提供程序
*
* @author lengleng
* @date 2024/09/01
*/
@RequiredArgsConstructor
public class RemoteDictDataProvider implements DictDataProvider {
private final RestTemplate restTemplate;
/**
* 获取 dict
* @param type 类型
* @return {@link DictEnum[] }
*/
@Override
public DictEnum[] getDict(String type) {
// 获取服务URL
String serviceUrl = getServiceUrl(type);
// 创建请求实体
HttpHeaders headers = new HttpHeaders();
headers.add(SecurityConstants.FROM, SecurityConstants.FROM_IN);
HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
// 发送HTTP请求并获取响应
ResponseEntity<Map> response = restTemplate.exchange(serviceUrl, HttpMethod.GET, requestEntity, Map.class,
type);
// 解析响应数据
List<Map<String, Object>> dictDataList = MapUtil.get(response.getBody(), R.Fields.data, ArrayList.class);
if (CollUtil.isEmpty(dictDataList)) {
return new DictEnum[0];
}
// 构建 DictEnum 数组
DictEnum.Builder dictEnumBuilder = DictEnum.builder();
for (Map<String, Object> dictData : dictDataList) {
String value = MapUtil.getStr(dictData, "value");
String label = MapUtil.getStr(dictData, "label");
dictEnumBuilder.add(value, label);
}
return dictEnumBuilder.build();
}
/**
* 获取服务 URL
* @param param 参数
* @return {@link String }
*/
private String getServiceUrl(String param) {
// 根据当前架构模式组装URL
if (SpringContextHolder.isMicro()) {
return String.format("http://%s/dict/remote/type/%s", ServiceNameConstants.UPMS_SERVICE, param);
}
else {
return String.format("http://%s/dict/remote/type/%s", SpringContextHolder.getEnvironment()
.resolvePlaceholders("127.0.0.1:${server.port}${server.servlet.context-path}"), param);
}
}
}

View File

@ -0,0 +1 @@
com.pig4cloud.pig.common.excel.ExcelAutoConfiguration

View File

@ -41,5 +41,6 @@
<module>pig-common-feign</module>
<module>pig-common-swagger</module>
<module>pig-common-xss</module>
<module>pig-common-excel</module>
</modules>
</project>

View File

@ -46,10 +46,10 @@
<groupId>com.pig4cloud</groupId>
<artifactId>pig-common-mybatis</artifactId>
</dependency>
<!-- excel 导入导出 https://github.com/pig-mesh/excel-spring-boot-starter -->
<!-- excel 导入导出 -->
<dependency>
<groupId>com.pig4cloud.excel</groupId>
<artifactId>excel-spring-boot-starter</artifactId>
<groupId>com.pig4cloud</groupId>
<artifactId>pig-common-excel</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -3,6 +3,7 @@ package com.pig4cloud.pig.admin.api.vo;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.pig4cloud.plugin.excel.annotation.DictTypeProperty;
import com.pig4cloud.plugin.excel.annotation.ExcelLine;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
@ -95,6 +96,7 @@ public class UserExcelVO implements Serializable {
* 锁定标记
*/
@ExcelProperty("锁定标记,0:正常,9:已锁定")
@DictTypeProperty("lock_flagX")
private String lockFlag;
/**