mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-08 12:57:13 +08:00
feat: 新增 Excel 字典数据转换器
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package top.continew.admin.common.config.excel;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 字典字段注解
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2025/4/9 20:25
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
public @interface DictExcelProperty {
|
||||
|
||||
/**
|
||||
* 字典编码
|
||||
*
|
||||
* @return 字典编码
|
||||
*/
|
||||
String value();
|
||||
}
|
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package top.continew.admin.common.config.excel;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||
import com.alibaba.excel.metadata.data.ReadCellData;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||
import top.continew.admin.common.service.CommonDictItemService;
|
||||
import top.continew.starter.core.constant.StringConstants;
|
||||
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Easy Excel 字典转换器
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2025/4/9 20:22
|
||||
*/
|
||||
public class ExcelDictConverter implements Converter<Object> {
|
||||
|
||||
@Override
|
||||
public Object convertToJavaData(ReadCellData<?> cellData,
|
||||
ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
// 获取字典项数据
|
||||
List<LabelValueResp> dictItemList = this.getDictCode(contentProperty);
|
||||
// 转换字典标签为字典值
|
||||
String value = dictItemList.stream()
|
||||
.filter(item -> Objects.equals(cellData.getStringValue(), item.getValue()))
|
||||
.findFirst()
|
||||
.map(LabelValueResp::getLabel)
|
||||
.orElse(null);
|
||||
// 转换字典值为对应类型
|
||||
return Convert.convert(contentProperty.getField().getType(), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WriteCellData<String> convertToExcelData(Object data,
|
||||
ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
if (null == data) {
|
||||
return new WriteCellData<>(StringConstants.EMPTY);
|
||||
}
|
||||
// 获取字典项数据
|
||||
List<LabelValueResp> dictItemList = this.getDictCode(contentProperty);
|
||||
if (CollUtil.isEmpty(dictItemList)) {
|
||||
return new WriteCellData<>(StringConstants.EMPTY);
|
||||
}
|
||||
// 转换字典值为字典标签
|
||||
return new WriteCellData<>(dictItemList.stream()
|
||||
.filter(item -> Objects.equals(data, item.getValue()))
|
||||
.findFirst()
|
||||
.map(LabelValueResp::getLabel)
|
||||
.orElse(StringConstants.EMPTY));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典项数据
|
||||
*
|
||||
* @param contentProperty Excel 内容属性
|
||||
* @return 字典项数据
|
||||
*/
|
||||
private List<LabelValueResp> getDictCode(ExcelContentProperty contentProperty) {
|
||||
DictExcelProperty dictExcelProperty = contentProperty.getField().getAnnotation(DictExcelProperty.class);
|
||||
if (null == dictExcelProperty) {
|
||||
throw new IllegalArgumentException("Excel 字典转换器异常:请为字段添加 @DictExcelProperty 注解");
|
||||
}
|
||||
CommonDictItemService dictItemService = SpringUtil.getBean(CommonDictItemService.class);
|
||||
return dictItemService.listByDictCode(dictExcelProperty.value());
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package top.continew.admin.common.service;
|
||||
|
||||
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 公共字典项业务接口
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2025/4/9 20:17
|
||||
*/
|
||||
public interface CommonDictItemService {
|
||||
|
||||
/**
|
||||
* 根据字典编码查询
|
||||
*
|
||||
* @param dictCode 字典编码
|
||||
* @return 字典项列表
|
||||
*/
|
||||
List<LabelValueResp> listByDictCode(String dictCode);
|
||||
}
|
@@ -16,10 +16,16 @@
|
||||
|
||||
package top.continew.admin.system.model.resp;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import top.continew.admin.common.model.resp.BaseDetailResp;
|
||||
import top.continew.admin.common.config.excel.DictExcelProperty;
|
||||
import top.continew.admin.common.config.excel.ExcelDictConverter;
|
||||
import top.continew.admin.common.enums.DisEnableStatusEnum;
|
||||
import top.continew.admin.common.model.resp.BaseDetailResp;
|
||||
import top.continew.starter.file.excel.converter.ExcelBaseEnumConverter;
|
||||
import top.continew.starter.file.excel.converter.ExcelListConverter;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.util.List;
|
||||
@@ -32,6 +38,7 @@ import java.util.List;
|
||||
* @since 2024/12/03 16:04
|
||||
*/
|
||||
@Data
|
||||
@ExcelIgnoreUnannotated
|
||||
@Schema(description = "终端信息")
|
||||
public class ClientResp extends BaseDetailResp {
|
||||
|
||||
@@ -42,12 +49,14 @@ public class ClientResp extends BaseDetailResp {
|
||||
* 终端 ID
|
||||
*/
|
||||
@Schema(description = "终端 ID", example = "ef51c9a3e9046c4f2ea45142c8a8344a")
|
||||
@ExcelProperty(value = "终端 ID", order = 2)
|
||||
private String clientId;
|
||||
|
||||
/**
|
||||
* 终端 Key
|
||||
*/
|
||||
@Schema(description = "终端 Key", example = "PC")
|
||||
@ExcelProperty(value = "终端 Key", order = 3)
|
||||
private String clientKey;
|
||||
|
||||
/**
|
||||
@@ -60,29 +69,35 @@ public class ClientResp extends BaseDetailResp {
|
||||
* 认证类型
|
||||
*/
|
||||
@Schema(description = "认证类型", example = "ACCOUNT")
|
||||
@ExcelProperty(value = "认证类型", converter = ExcelListConverter.class, order = 4)
|
||||
private List<String> authType;
|
||||
|
||||
/**
|
||||
* 终端类型
|
||||
* 终端类型(取值于字典 client_type
|
||||
*/
|
||||
@Schema(description = "终端类型", example = "PC")
|
||||
@Schema(description = "终端类型(取值于字典 client_type)", example = "PC")
|
||||
@ExcelProperty(value = "终端类型", converter = ExcelDictConverter.class, order = 5)
|
||||
@DictExcelProperty("client_type")
|
||||
private String clientType;
|
||||
|
||||
/**
|
||||
* Token 最低活跃频率(单位:秒,-1:不限制,永不冻结)
|
||||
*/
|
||||
@Schema(description = "Token 最低活跃频率(单位:秒,-1:不限制,永不冻结)", example = "1800")
|
||||
@ExcelProperty(value = "Token 最低活跃频率", order = 6)
|
||||
private Long activeTimeout;
|
||||
|
||||
/**
|
||||
* Token 有效期(单位:秒,-1:永不过期)
|
||||
*/
|
||||
@Schema(description = "Token 有效期(单位:秒,-1:永不过期)", example = "86400")
|
||||
@ExcelProperty(value = "Token 有效期", order = 7)
|
||||
private Long timeout;
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
@Schema(description = "状态", example = "1")
|
||||
@ExcelProperty(value = "状态", converter = ExcelBaseEnumConverter.class, order = 8)
|
||||
private DisEnableStatusEnum status;
|
||||
}
|
@@ -20,8 +20,12 @@ import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import top.continew.admin.common.config.excel.DictExcelProperty;
|
||||
import top.continew.admin.common.config.excel.ExcelDictConverter;
|
||||
import top.continew.admin.common.model.resp.BaseDetailResp;
|
||||
import top.continew.admin.system.enums.NoticeScopeEnum;
|
||||
import top.continew.admin.system.enums.NoticeStatusEnum;
|
||||
import top.continew.starter.file.excel.converter.ExcelBaseEnumConverter;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -45,35 +49,42 @@ public class NoticeDetailResp extends BaseDetailResp {
|
||||
* 标题
|
||||
*/
|
||||
@Schema(description = "标题", example = "这是公告标题")
|
||||
@ExcelProperty(value = "标题")
|
||||
@ExcelProperty(value = "标题", order = 2)
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
@Schema(description = "内容", example = "这是公告内容")
|
||||
@ExcelProperty(value = "内容")
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 类型(取值于字典 notice_type)
|
||||
*/
|
||||
@Schema(description = "类型(取值于字典 notice_type)", example = "1")
|
||||
@ExcelProperty(value = "类型")
|
||||
@ExcelProperty(value = "类型", converter = ExcelDictConverter.class, order = 3)
|
||||
@DictExcelProperty("notice_type")
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
@Schema(description = "状态", example = "1")
|
||||
@ExcelProperty(value = "状态", converter = ExcelBaseEnumConverter.class, order = 4)
|
||||
private NoticeStatusEnum status;
|
||||
|
||||
/**
|
||||
* 生效时间
|
||||
*/
|
||||
@Schema(description = "生效时间", example = "2023-08-08 00:00:00", type = "string")
|
||||
@ExcelProperty(value = "生效时间")
|
||||
@ExcelProperty(value = "生效时间", order = 5)
|
||||
private LocalDateTime effectiveTime;
|
||||
|
||||
/**
|
||||
* 终止时间
|
||||
*/
|
||||
@Schema(description = "终止时间", example = "2023-08-08 23:59:59", type = "string")
|
||||
@ExcelProperty(value = "终止时间")
|
||||
@ExcelProperty(value = "终止时间", order = 6)
|
||||
private LocalDateTime terminateTime;
|
||||
|
||||
/**
|
||||
@@ -87,4 +98,8 @@ public class NoticeDetailResp extends BaseDetailResp {
|
||||
*/
|
||||
@Schema(description = "指定用户", example = "[1,2,3]")
|
||||
private List<String> noticeUsers;
|
||||
|
||||
public NoticeStatusEnum getStatus() {
|
||||
return NoticeStatusEnum.getStatus(effectiveTime, terminateTime);
|
||||
}
|
||||
}
|
@@ -16,12 +16,12 @@
|
||||
|
||||
package top.continew.admin.system.service;
|
||||
|
||||
import top.continew.admin.common.service.CommonDictItemService;
|
||||
import top.continew.admin.system.model.entity.DictItemDO;
|
||||
import top.continew.admin.system.model.query.DictItemQuery;
|
||||
import top.continew.admin.system.model.req.DictItemReq;
|
||||
import top.continew.admin.system.model.resp.DictItemResp;
|
||||
import top.continew.starter.data.mp.service.IService;
|
||||
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
|
||||
import top.continew.starter.extension.crud.service.BaseService;
|
||||
|
||||
import java.util.List;
|
||||
@@ -32,15 +32,7 @@ import java.util.List;
|
||||
* @author Charles7c
|
||||
* @since 2023/9/11 21:29
|
||||
*/
|
||||
public interface DictItemService extends BaseService<DictItemResp, DictItemResp, DictItemQuery, DictItemReq>, IService<DictItemDO> {
|
||||
|
||||
/**
|
||||
* 根据字典编码查询
|
||||
*
|
||||
* @param dictCode 字典编码
|
||||
* @return 字典项列表
|
||||
*/
|
||||
List<LabelValueResp> listByDictCode(String dictCode);
|
||||
public interface DictItemService extends BaseService<DictItemResp, DictItemResp, DictItemQuery, DictItemReq>, IService<DictItemDO>, CommonDictItemService {
|
||||
|
||||
/**
|
||||
* 根据字典 ID 列表删除
|
||||
|
Reference in New Issue
Block a user