diff --git a/pig-common/pig-common-bom/pom.xml b/pig-common/pig-common-bom/pom.xml index c27625ee..8636b2e5 100644 --- a/pig-common/pig-common-bom/pom.xml +++ b/pig-common/pig-common-bom/pom.xml @@ -28,7 +28,7 @@ 9.0.0 4.3.1 1.7.0 - 3.3.0 + 3.3.1-SNAPSHOT 7.1 3.0.0 2.3.5 @@ -88,6 +88,11 @@ pig-common-xss ${revision} + + com.pig4cloud + pig-common-excel + ${revision} + com.pig4cloud pig-upms-api diff --git a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/util/R.java b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/util/R.java index 7b79afa2..1800f16c 100755 --- a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/util/R.java +++ b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/util/R.java @@ -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 implements Serializable { private static final long serialVersionUID = 1L; diff --git a/pig-common/pig-common-excel/pom.xml b/pig-common/pig-common-excel/pom.xml new file mode 100755 index 00000000..556aa5d1 --- /dev/null +++ b/pig-common/pig-common-excel/pom.xml @@ -0,0 +1,46 @@ + + + + + 4.0.0 + + com.pig4cloud + pig-common + ${revision} + + + pig-common-excel + jar + + excel 导入导出处理模块 + + + + + com.pig4cloud + pig-common-core + + + + com.pig4cloud.excel + excel-spring-boot-starter + + + diff --git a/pig-common/pig-common-excel/src/main/java/com/pig4cloud/pig/common/excel/ExcelAutoConfiguration.java b/pig-common/pig-common-excel/src/main/java/com/pig4cloud/pig/common/excel/ExcelAutoConfiguration.java new file mode 100644 index 00000000..1cdac4bf --- /dev/null +++ b/pig-common/pig-common-excel/src/main/java/com/pig4cloud/pig/common/excel/ExcelAutoConfiguration.java @@ -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); + } + +} diff --git a/pig-common/pig-common-excel/src/main/java/com/pig4cloud/pig/common/excel/provider/RemoteDictDataProvider.java b/pig-common/pig-common-excel/src/main/java/com/pig4cloud/pig/common/excel/provider/RemoteDictDataProvider.java new file mode 100644 index 00000000..b66ece97 --- /dev/null +++ b/pig-common/pig-common-excel/src/main/java/com/pig4cloud/pig/common/excel/provider/RemoteDictDataProvider.java @@ -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 requestEntity = new HttpEntity<>(headers); + // 发送HTTP请求并获取响应 + ResponseEntity response = restTemplate.exchange(serviceUrl, HttpMethod.GET, requestEntity, Map.class, + type); + + // 解析响应数据 + List> 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 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); + } + } + +} diff --git a/pig-common/pig-common-excel/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/pig-common/pig-common-excel/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..7f0a4c19 --- /dev/null +++ b/pig-common/pig-common-excel/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ + com.pig4cloud.pig.common.excel.ExcelAutoConfiguration diff --git a/pig-common/pom.xml b/pig-common/pom.xml index 3e7fdf7b..b664552b 100755 --- a/pig-common/pom.xml +++ b/pig-common/pom.xml @@ -41,5 +41,6 @@ pig-common-feign pig-common-swagger pig-common-xss + pig-common-excel diff --git a/pig-upms/pig-upms-api/pom.xml b/pig-upms/pig-upms-api/pom.xml index d0853895..7f595225 100755 --- a/pig-upms/pig-upms-api/pom.xml +++ b/pig-upms/pig-upms-api/pom.xml @@ -46,10 +46,10 @@ com.pig4cloud pig-common-mybatis - + - com.pig4cloud.excel - excel-spring-boot-starter + com.pig4cloud + pig-common-excel diff --git a/pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/vo/UserExcelVO.java b/pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/vo/UserExcelVO.java index eac64877..d897a1b1 100644 --- a/pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/vo/UserExcelVO.java +++ b/pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/vo/UserExcelVO.java @@ -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; /**