-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
174 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ /* | ||
~ * Copyright (c) 2019-2020, 冷冷 ([email protected]). | ||
~ * <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> |
30 changes: 30 additions & 0 deletions
30
...pig-common-excel/src/main/java/com/pig4cloud/pig/common/excel/ExcelAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...n-excel/src/main/java/com/pig4cloud/pig/common/excel/provider/RemoteDictDataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.pig4cloud.pig.common.excel.ExcelAutoConfiguration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters