TemplateModelConverter.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.hccake.ballcat.codegen.converter;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.hccake.ballcat.codegen.constant.TemplateEntryTypeEnum;
  4. import com.hccake.ballcat.codegen.model.bo.FileEntry;
  5. import com.hccake.ballcat.codegen.model.bo.TemplateEntryFileTree;
  6. import com.hccake.ballcat.codegen.model.dto.TemplateEntryCreateDTO;
  7. import com.hccake.ballcat.codegen.model.dto.TemplateEntryUpdateDTO;
  8. import com.hccake.ballcat.codegen.model.entity.TemplateEntry;
  9. import com.hccake.ballcat.codegen.model.entity.TemplateGroup;
  10. import com.hccake.ballcat.codegen.model.vo.GeneratePreviewFileVO;
  11. import com.hccake.ballcat.codegen.model.vo.TemplateEntryTree;
  12. import com.hccake.ballcat.codegen.model.vo.TemplateEntryVO;
  13. import com.hccake.ballcat.codegen.model.vo.TemplateGroupPageVO;
  14. import org.mapstruct.Mapper;
  15. import org.mapstruct.Mapping;
  16. import org.mapstruct.factory.Mappers;
  17. import java.nio.charset.StandardCharsets;
  18. /**
  19. * @author Hccake
  20. * @version 1.0
  21. * @date 2020/6/19 20:01
  22. */
  23. @Mapper
  24. public interface TemplateModelConverter {
  25. TemplateModelConverter INSTANCE = Mappers.getMapper(TemplateModelConverter.class);
  26. /**
  27. * 文件内容转模板文件内容,byte[] to string
  28. * @param templateEntry 模板目录下
  29. * @return 字符串
  30. */
  31. default String mapTemplateContent(TemplateEntry templateEntry) {
  32. if (TemplateEntryTypeEnum.TEMPLATE_FILE.getType().equals(templateEntry.getType())) {
  33. return StrUtil.str(templateEntry.getFileContent(), StandardCharsets.UTF_8);
  34. }
  35. return null;
  36. }
  37. /**
  38. * 文件内容转模板文件内容,byte[] to string
  39. * @param fileContent 模板文件内容
  40. * @return 字符串
  41. */
  42. default String mapTemplateContent(byte[] fileContent) {
  43. return StrUtil.str(fileContent, StandardCharsets.UTF_8);
  44. }
  45. /**
  46. * 数值类型转枚举
  47. * @param type 文件类型
  48. * @return TemplateEntryTypeEnum
  49. */
  50. default TemplateEntryTypeEnum mapType(Integer type) {
  51. switch (type) {
  52. case 1:
  53. return TemplateEntryTypeEnum.FOLDER;
  54. case 2:
  55. return TemplateEntryTypeEnum.TEMPLATE_FILE;
  56. case 3:
  57. return TemplateEntryTypeEnum.BINARY_FILE;
  58. default:
  59. throw new RuntimeException("模板文件的类型转换异常,未知的类型:" + type);
  60. }
  61. }
  62. /**
  63. * 转枚举数值类型
  64. * @param type 文件类型
  65. * @return Integer
  66. */
  67. default Integer mapType(TemplateEntryTypeEnum type) {
  68. return type.getType();
  69. }
  70. /**
  71. * 模板组 PO 转换为 PageVO
  72. * @param templateGroup 模板组实体
  73. * @return TemplateGroupPageVO 模板组分页VO
  74. */
  75. TemplateGroupPageVO groupPoToPageVo(TemplateGroup templateGroup);
  76. /**
  77. * 转换 TemplateEntry 为 TemplateEntryVO
  78. * @param templateEntry templateEntry
  79. * @return VO
  80. */
  81. @Mapping(target = "templateContent", source = "templateEntry")
  82. TemplateEntryVO entryPoToVo(TemplateEntry templateEntry);
  83. /**
  84. * 转换 TemplateEntryCreateDTO to TemplateEntry
  85. * @param entryCreateDTO entryCreateDTO
  86. * @return TemplateDirectoryEntry 持久对象
  87. */
  88. TemplateEntry entryCreateDtoToPo(TemplateEntryCreateDTO entryCreateDTO);
  89. /**
  90. * 转换 TemplateEntryUpdateDTO to TemplateEntry
  91. * @param entryUpdateDTO entryUpdateDTO
  92. * @return TemplateEntry 持久对象
  93. */
  94. TemplateEntry entryUpdateDtoToPo(TemplateEntryUpdateDTO entryUpdateDTO);
  95. /**
  96. * 转换为目录树
  97. * @param templateEntry templateDirectoryEntry
  98. * @return TemplateDirectoryTree
  99. */
  100. @Mapping(target = "type", source = "type")
  101. TemplateEntryTree entryPoToTree(TemplateEntry templateEntry);
  102. /**
  103. * 树转换为 po
  104. * @param templateEntryFileTree TemplateEntryTree
  105. * @return TemplateEntry
  106. */
  107. TemplateEntry entryFileTreeToPo(TemplateEntryFileTree templateEntryFileTree);
  108. /**
  109. * 文件项转预览文件VO
  110. * @param fileEntry 文件项
  111. * @return GeneratePreviewFileVO
  112. */
  113. @Mapping(target = "templateContent", source = "fileContent")
  114. @Mapping(target = "type", source = "type")
  115. GeneratePreviewFileVO fileEntryToPreviewVo(FileEntry fileEntry);
  116. }