From 1abae7db187a4241dc8cf5442c97e5766f9e425b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=8A=9B?= Date: Tue, 31 May 2022 14:25:58 +0800 Subject: [PATCH] commit code --- .../modules/common/ArchivesListToMap.java | 67 +++++++ .../controller/ArchivesTypeController.java | 45 ++++- .../dictionary/domain/ArchivesDictionary.java | 4 + .../dictionary/domain/ArchivesType.java | 6 +- .../dictionary/domain/vo/NewTableVo.java | 32 +++ .../service/ArchivesTypeService.java | 8 +- .../service/dto/ArchivesTypeDTO.java | 31 ++- .../service/impl/ArchivesTypeServiceImpl.java | 60 ++++++ archives/src/test/java/ArchivesTypeTest.java | 9 + .../java/com/storeroom/utils/DateUtils.java | 189 ++++++++++++++++++ common/src/test/java/TestDate.java | 15 ++ .../system/controller/TestController.java | 1 - system/src/main/resources/application.yml | 7 +- 13 files changed, 456 insertions(+), 18 deletions(-) create mode 100644 archives/src/main/java/com/storeroom/modules/common/ArchivesListToMap.java create mode 100644 archives/src/main/java/com/storeroom/modules/dictionary/domain/vo/NewTableVo.java create mode 100644 archives/src/test/java/ArchivesTypeTest.java create mode 100644 common/src/main/java/com/storeroom/utils/DateUtils.java create mode 100644 common/src/test/java/TestDate.java diff --git a/archives/src/main/java/com/storeroom/modules/common/ArchivesListToMap.java b/archives/src/main/java/com/storeroom/modules/common/ArchivesListToMap.java new file mode 100644 index 0000000..bc62f2b --- /dev/null +++ b/archives/src/main/java/com/storeroom/modules/common/ArchivesListToMap.java @@ -0,0 +1,67 @@ +package com.storeroom.modules.common; + + +import com.storeroom.modules.dictionary.domain.vo.FieldVO; +import lombok.RequiredArgsConstructor; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +public class ArchivesListToMap { + + + public static String getMaps(List lists, String tableName) { + + StringBuilder stringBuilder = new StringBuilder("create table " + tableName + "(id varchar(100) primary key not null,"); + + List> maps = new ArrayList>(); + for (Object obj : lists) { + Class c = obj.getClass(); + Field[] f = c.getDeclaredFields(); + Map map = new HashMap(); + for (Field fie : f) { + try { + fie.setAccessible(true); + map.put(fie.getName(), fie.get(obj)); + } catch (Exception e) { + e.printStackTrace(); + } + + } + //获取父类的私有属性 + for (Field fie : c.getSuperclass().getDeclaredFields()) { + try { + fie.setAccessible(true);//取消语言访问检查 + map.put(fie.getName(), fie.get(obj));//获取私有变量值 + } catch (Exception e) { + e.printStackTrace(); + } + } + maps.add(map); + } + List> list = maps; + for (int i = 0; i < list.size(); i++) { + stringBuilder.append(list.get(i).get("fieldName")); + stringBuilder.append(" "); + stringBuilder.append(list.get(i).get("fieldType")); + stringBuilder.append("("); + stringBuilder.append(list.get(i).get("fieldExtent")); + stringBuilder.append(")"); + if (list.size() - 1 != i) { + stringBuilder.append(","); + } + if (list.size() - 1 == i) { + stringBuilder.append(")"); + } + + } + return stringBuilder.toString(); + } +} + + diff --git a/archives/src/main/java/com/storeroom/modules/dictionary/controller/ArchivesTypeController.java b/archives/src/main/java/com/storeroom/modules/dictionary/controller/ArchivesTypeController.java index 6474977..60485ac 100644 --- a/archives/src/main/java/com/storeroom/modules/dictionary/controller/ArchivesTypeController.java +++ b/archives/src/main/java/com/storeroom/modules/dictionary/controller/ArchivesTypeController.java @@ -2,11 +2,17 @@ package com.storeroom.modules.dictionary.controller; import com.storeroom.annotaion.rest.AnonymousGetMapping; +import com.storeroom.annotaion.rest.AnonymousPostMapping; +import com.storeroom.exception.BaseException; import com.storeroom.modules.common.ArchivesTypeEnum; +import com.storeroom.modules.dictionary.service.ArchivesTypeService; +import com.storeroom.modules.dictionary.service.dto.ArchivesTypeDTO; import com.storeroom.utils.ApiResponse; +import com.storeroom.utils.StringUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -19,17 +25,34 @@ import java.util.*; @RequestMapping("/api/archives-type/") public class ArchivesTypeController { + private final ArchivesTypeService archivesTypeService; - @ApiOperation("门类类型") - @AnonymousGetMapping("dropdown-list") - public ApiResponse getType(){ - Map list=new LinkedHashMap<>(); - list.put(ArchivesTypeEnum.folder.getCode(),ArchivesTypeEnum.folder.getType()); - list.put(ArchivesTypeEnum.project.getCode(),ArchivesTypeEnum.project.getType()); - list.put(ArchivesTypeEnum.archives.getCode(),ArchivesTypeEnum.archives.getType()); - list.put(ArchivesTypeEnum.inChive.getCode(),ArchivesTypeEnum.inChive.getType()); - list.put(ArchivesTypeEnum.files.getCode(),ArchivesTypeEnum.folder.getType()); - list.put(ArchivesTypeEnum.Template.getCode(),ArchivesTypeEnum.Template.getType()); - return ApiResponse.success(list); + @ApiOperation("门类类型") + @AnonymousGetMapping("dropdown-list") + public ApiResponse getType() { + Map list = new LinkedHashMap<>(); + list.put(ArchivesTypeEnum.folder.getCode(), ArchivesTypeEnum.folder.getType()); + list.put(ArchivesTypeEnum.project.getCode(), ArchivesTypeEnum.project.getType()); + list.put(ArchivesTypeEnum.archives.getCode(), ArchivesTypeEnum.archives.getType()); + list.put(ArchivesTypeEnum.inChive.getCode(), ArchivesTypeEnum.inChive.getType()); + list.put(ArchivesTypeEnum.files.getCode(), ArchivesTypeEnum.folder.getType()); + list.put(ArchivesTypeEnum.Template.getCode(), ArchivesTypeEnum.Template.getType()); + return ApiResponse.success(list); + } + + @ApiOperation("创建门类") + @AnonymousPostMapping("create") + public ApiResponse save(@RequestBody ArchivesTypeDTO archivesTypeDTO) { + if (!archivesTypeDTO.getId().isEmpty()) { + throw new BaseException("this is not empty Data"); + } + if (archivesTypeDTO.getIsType() == null) { + throw new BaseException("type cannot be Null"); + } + if (archivesTypeDTO.getCnName().isEmpty()) { + throw new BaseException("Name canot be null"); } + archivesTypeService.create(archivesTypeDTO); + return ApiResponse.success("保存成功"); + } } diff --git a/archives/src/main/java/com/storeroom/modules/dictionary/domain/ArchivesDictionary.java b/archives/src/main/java/com/storeroom/modules/dictionary/domain/ArchivesDictionary.java index 561e5c1..4b0a30d 100644 --- a/archives/src/main/java/com/storeroom/modules/dictionary/domain/ArchivesDictionary.java +++ b/archives/src/main/java/com/storeroom/modules/dictionary/domain/ArchivesDictionary.java @@ -43,6 +43,10 @@ public class ArchivesDictionary extends BaseEntity implements Serializable { @ApiModelProperty(value = "默认值") private String isDefaultValue; + @Column(name = "is_input_class") + @ApiModelProperty(value = "著录形式") + private String isInputClass; + @Column(name = "is_data_type") @ApiModelProperty(value = "数据类型") private String isDataType; diff --git a/archives/src/main/java/com/storeroom/modules/dictionary/domain/ArchivesType.java b/archives/src/main/java/com/storeroom/modules/dictionary/domain/ArchivesType.java index eec2dd8..164b73a 100644 --- a/archives/src/main/java/com/storeroom/modules/dictionary/domain/ArchivesType.java +++ b/archives/src/main/java/com/storeroom/modules/dictionary/domain/ArchivesType.java @@ -37,15 +37,15 @@ public class ArchivesType extends BaseEntity implements Serializable { @Column(name = "is_type") @ApiModelProperty(value = "门类级别") - private long isType; + private Integer isType; @Column(name = "is_type_metic") @ApiModelProperty(value = "是否为模板档案") - private long isTypeMetic; + private Integer isTypeMetic; @Column(name = "category_seq") @ApiModelProperty(value = "排序") - private long categorySeq; + private Integer categorySeq; @Column(name = "remark") @ApiModelProperty(value = "备注") diff --git a/archives/src/main/java/com/storeroom/modules/dictionary/domain/vo/NewTableVo.java b/archives/src/main/java/com/storeroom/modules/dictionary/domain/vo/NewTableVo.java new file mode 100644 index 0000000..bfac0fa --- /dev/null +++ b/archives/src/main/java/com/storeroom/modules/dictionary/domain/vo/NewTableVo.java @@ -0,0 +1,32 @@ +package com.storeroom.modules.dictionary.domain.vo; + + +import lombok.Data; + +import java.util.List; + +@Data +public class NewTableVo { + + public NewTableVo() { + + } + + public NewTableVo(String tableName) { + this.tableName = tableName; + } + + private String tableName; + private List list; + + @Data + static class Vo { + //字段名称 + private String fieldName; + //字段类型 + private String fieldType; + //数据长度 + private Integer fieldExtent; + } + +} diff --git a/archives/src/main/java/com/storeroom/modules/dictionary/service/ArchivesTypeService.java b/archives/src/main/java/com/storeroom/modules/dictionary/service/ArchivesTypeService.java index 21bedcc..53f75ed 100644 --- a/archives/src/main/java/com/storeroom/modules/dictionary/service/ArchivesTypeService.java +++ b/archives/src/main/java/com/storeroom/modules/dictionary/service/ArchivesTypeService.java @@ -1,8 +1,14 @@ package com.storeroom.modules.dictionary.service; +import com.storeroom.modules.dictionary.service.dto.ArchivesTypeDTO; + public interface ArchivesTypeService { - void create(); + /** + * 创建门类 + * @param archivesTypeDTO + */ + void create(ArchivesTypeDTO archivesTypeDTO); } diff --git a/archives/src/main/java/com/storeroom/modules/dictionary/service/dto/ArchivesTypeDTO.java b/archives/src/main/java/com/storeroom/modules/dictionary/service/dto/ArchivesTypeDTO.java index 48561a6..9394cf7 100644 --- a/archives/src/main/java/com/storeroom/modules/dictionary/service/dto/ArchivesTypeDTO.java +++ b/archives/src/main/java/com/storeroom/modules/dictionary/service/dto/ArchivesTypeDTO.java @@ -3,14 +3,43 @@ package com.storeroom.modules.dictionary.service.dto; import com.storeroom.base.BaseDTO; import lombok.Data; +import lombok.Getter; +import lombok.Setter; import java.io.Serializable; +import java.util.Objects; -@Data +@Getter +@Setter public class ArchivesTypeDTO extends BaseDTO implements Serializable { + private String id; + private String cnName; + private String enName; + private String pid; + + private Integer isType; + + private Integer isTypeMetic; + + private Integer categorySeq; + + private String remark; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ArchivesTypeDTO that = (ArchivesTypeDTO) o; + return Objects.equals(id, that.id) && Objects.equals(cnName, that.cnName) && Objects.equals(enName, that.enName) && Objects.equals(pid, that.pid) && Objects.equals(isType, that.isType) && Objects.equals(isTypeMetic, that.isTypeMetic) && Objects.equals(categorySeq, that.categorySeq) && Objects.equals(remark, that.remark); + } + + @Override + public int hashCode() { + return Objects.hash(id, cnName, enName, pid, isType, isTypeMetic, categorySeq, remark); + } } diff --git a/archives/src/main/java/com/storeroom/modules/dictionary/service/impl/ArchivesTypeServiceImpl.java b/archives/src/main/java/com/storeroom/modules/dictionary/service/impl/ArchivesTypeServiceImpl.java index 998c32e..ac0086a 100644 --- a/archives/src/main/java/com/storeroom/modules/dictionary/service/impl/ArchivesTypeServiceImpl.java +++ b/archives/src/main/java/com/storeroom/modules/dictionary/service/impl/ArchivesTypeServiceImpl.java @@ -1,9 +1,69 @@ package com.storeroom.modules.dictionary.service.impl; +import com.storeroom.modules.common.ArchivesListToMap; +import com.storeroom.modules.dictionary.domain.ArchivesType; +import com.storeroom.modules.dictionary.domain.vo.FieldVO; import com.storeroom.modules.dictionary.service.ArchivesTypeService; +import com.storeroom.modules.dictionary.service.FieldService; +import com.storeroom.modules.dictionary.service.dto.ArchivesTypeDTO; +import com.storeroom.modules.dictionary.service.mapstruct.ArchivesTypeMapper; +import com.storeroom.utils.DateUtils; +import lombok.RequiredArgsConstructor; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + @Service +@RequiredArgsConstructor public class ArchivesTypeServiceImpl implements ArchivesTypeService { + + + private final ArchivesTypeMapper archivesTypeMapper; + + private final FieldService fieldService; + + private final JdbcTemplate jdbcTemplate; + + @Value("${table-template.name}") + private String tableName; + + + + @Override + public void create(ArchivesTypeDTO archivesTypeDTO) { + + ArchivesType archivesType = archivesTypeMapper.toEntity(archivesTypeDTO); + switch (archivesType.getIsType()) { + case 1 -> dynamicSystemTable(archivesType.getIsType()); + case 2 -> dynameicTemplateTable(archivesType.getIsType()); + } + + } + + public void dynamicSystemTable(Integer type) { + tableName += tableName + DateUtils.getNowDateTime(); + + List list = fieldService.findGroupType(type); + if (!list.isEmpty()) { + String sqlstr = ArchivesListToMap.getMaps(list, tableName); + jdbcTemplate.execute(sqlstr); + } + + + } + + public void dynameicTemplateTable(Integer type) { + + } + } diff --git a/archives/src/test/java/ArchivesTypeTest.java b/archives/src/test/java/ArchivesTypeTest.java new file mode 100644 index 0000000..a1153e8 --- /dev/null +++ b/archives/src/test/java/ArchivesTypeTest.java @@ -0,0 +1,9 @@ +import org.junit.jupiter.api.Test; + +public class ArchivesTypeTest { + + @Test + public void dynamicSystemTableTest() { + + } +} diff --git a/common/src/main/java/com/storeroom/utils/DateUtils.java b/common/src/main/java/com/storeroom/utils/DateUtils.java new file mode 100644 index 0000000..c23a209 --- /dev/null +++ b/common/src/main/java/com/storeroom/utils/DateUtils.java @@ -0,0 +1,189 @@ +package com.storeroom.utils; + +import java.text.SimpleDateFormat; +import java.time.*; +import java.time.format.DateTimeFormatter; +import java.util.Calendar; +import java.util.Date; + + +/** + * 时间工具类 + */ +public class DateUtils { + + + public static final DateTimeFormatter DFY_MD_HMS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + public static final DateTimeFormatter DFY_MD = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + /** + * LocalDateTime 转时间戳 + * + * @param localDateTime / + * @return / + */ + public static Long getTimeStamp(LocalDateTime localDateTime) { + return localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond(); + } + + /** + * 时间戳转LocalDateTime + * + * @param timeStamp / + * @return / + */ + public static LocalDateTime fromTimeStamp(Long timeStamp) { + return LocalDateTime.ofEpochSecond(timeStamp, 0, OffsetDateTime.now().getOffset()); + } + + /** + * LocalDateTime 转 Date + * Jdk8 后 不推荐使用 {@link Date} Date + * + * @param localDateTime / + * @return / + */ + public static Date toDate(LocalDateTime localDateTime) { + return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); + } + + /** + * LocalDate 转 Date + * Jdk8 后 不推荐使用 {@link Date} Date + * + * @param localDate / + * @return / + */ + public static Date toDate(LocalDate localDate) { + return toDate(localDate.atTime(LocalTime.now(ZoneId.systemDefault()))); + } + + + /** + * Date转 LocalDateTime + * Jdk8 后 不推荐使用 {@link Date} Date + * + * @param date / + * @return / + */ + public static LocalDateTime toLocalDateTime(Date date) { + return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); + } + + /** + * 日期 格式化 + * + * @param localDateTime / + * @param patten / + * @return / + */ + public static String localDateTimeFormat(LocalDateTime localDateTime, String patten) { + DateTimeFormatter df = DateTimeFormatter.ofPattern(patten); + return df.format(localDateTime); + } + + /** + * 日期 格式化 + * + * @param localDateTime / + * @param df / + * @return / + */ + public static String localDateTimeFormat(LocalDateTime localDateTime, DateTimeFormatter df) { + return df.format(localDateTime); + } + + /** + * 日期格式化 yyyy-MM-dd HH:mm:ss + * + * @param localDateTime / + * @return / + */ + public static String localDateTimeFormatyMdHms(LocalDateTime localDateTime) { + return DFY_MD_HMS.format(localDateTime); + } + + /** + * 日期格式化 yyyy-MM-dd + * + * @param localDateTime / + * @return / + */ + public String localDateTimeFormatyMd(LocalDateTime localDateTime) { + return DFY_MD.format(localDateTime); + } + + /** + * 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd + * + * @param localDateTime / + * @return / + */ + public static LocalDateTime parseLocalDateTimeFormat(String localDateTime, String pattern) { + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); + return LocalDateTime.from(dateTimeFormatter.parse(localDateTime)); + } + + /** + * 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd + * + * @param localDateTime / + * @return / + */ + public static LocalDateTime parseLocalDateTimeFormat(String localDateTime, DateTimeFormatter dateTimeFormatter) { + return LocalDateTime.from(dateTimeFormatter.parse(localDateTime)); + } + + /** + * 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd HH:mm:ss + * + * @param localDateTime / + * @return / + */ + public static LocalDateTime parseLocalDateTimeFormatyMdHms(String localDateTime) { + return LocalDateTime.from(DFY_MD_HMS.parse(localDateTime)); + } + + /** + * 获取当前年 + * + * @return + */ + public static int getNowYear() { + Calendar d = Calendar.getInstance(); + return d.get(Calendar.YEAR); + } + + /** + * 获取当前月份 + * + * @return + */ + public static int getNowMonth() { + Calendar d = Calendar.getInstance(); + return d.get(Calendar.MONTH) + 1; + } + + /** + * 获取当月天数 + * + * @return + */ + public static int getNowMonthDay() { + Calendar d = Calendar.getInstance(); + return d.getActualMaximum(Calendar.DATE); + } + + /** + * 生成表获取当前时间和时间戳 + * @return + */ + public static String getNowDateTime(){ + Calendar d = Calendar.getInstance(); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_"); + return formatter.format(d.getTime()) + d.getTimeInMillis(); + } + + + +} diff --git a/common/src/test/java/TestDate.java b/common/src/test/java/TestDate.java new file mode 100644 index 0000000..924447b --- /dev/null +++ b/common/src/test/java/TestDate.java @@ -0,0 +1,15 @@ +import com.storeroom.utils.DateUtils; +import org.junit.jupiter.api.Test; + +import java.text.SimpleDateFormat; +import java.util.Calendar; + +public class TestDate { + + @Test + public void getDate() { + Calendar calendar = Calendar.getInstance(); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_"); + System.out.println("tb_" + formatter.format(calendar.getTime()) + calendar.getTimeInMillis()); + } +} diff --git a/system/src/main/java/com/storeroom/modules/system/controller/TestController.java b/system/src/main/java/com/storeroom/modules/system/controller/TestController.java index 5abc282..fb8dde2 100644 --- a/system/src/main/java/com/storeroom/modules/system/controller/TestController.java +++ b/system/src/main/java/com/storeroom/modules/system/controller/TestController.java @@ -81,7 +81,6 @@ public class TestController { if (list.size() - 1 == i) { stringBuilder.append(")"); } - } System.out.println("sql语句:" + stringBuilder); jdbcTemplate.execute(stringBuilder.toString()); diff --git a/system/src/main/resources/application.yml b/system/src/main/resources/application.yml index c8dc866..735d1c3 100644 --- a/system/src/main/resources/application.yml +++ b/system/src/main/resources/application.yml @@ -69,4 +69,9 @@ user-cache: # 最小回收间隔 min-evictable-interval: 1800000 # 最小存活时间 (ms) - min-idle-time: 3600000 \ No newline at end of file + min-idle-time: 3600000 + +# 生成表模板 +table-template: + #表名称 + name: tb_ \ No newline at end of file