DataInterfaceVariateController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package jnpf.base.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import io.swagger.v3.oas.annotations.Operation;
  4. import io.swagger.v3.oas.annotations.Parameter;
  5. import io.swagger.v3.oas.annotations.Parameters;
  6. import io.swagger.v3.oas.annotations.tags.Tag;
  7. import jnpf.base.ActionResult;
  8. import jnpf.base.Page;
  9. import jnpf.base.entity.DataInterfaceEntity;
  10. import jnpf.base.entity.DataInterfaceVariateEntity;
  11. import jnpf.base.model.datainterfacevariate.DataInterfaceVariateListVO;
  12. import jnpf.base.model.datainterfacevariate.DataInterfaceVariateModel;
  13. import jnpf.base.model.datainterfacevariate.DataInterfaceVariateSelectorVO;
  14. import jnpf.base.model.datainterfacevariate.DataInterfaceVariateVO;
  15. import jnpf.base.service.DataInterfaceService;
  16. import jnpf.base.service.DataInterfaceVariateService;
  17. import jnpf.base.vo.DownloadVO;
  18. import jnpf.base.vo.ListVO;
  19. import jnpf.config.ConfigValueUtil;
  20. import jnpf.constant.FileTypeConstant;
  21. import jnpf.constant.MsgCode;
  22. import jnpf.emnus.ModuleTypeEnum;
  23. import jnpf.exception.DataException;
  24. import jnpf.permission.entity.UserEntity;
  25. import jnpf.permission.service.UserService;
  26. import jnpf.util.*;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.http.MediaType;
  29. import org.springframework.web.bind.annotation.*;
  30. import org.springframework.web.multipart.MultipartFile;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.UUID;
  34. import java.util.stream.Collectors;
  35. /**
  36. * 数据接口变量
  37. *
  38. * @author JNPF开发平台组
  39. * @version V3.1.0
  40. * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
  41. * @date 2021-03-15 10:29
  42. */
  43. @Tag(name = "数据接口变量", description = "DataInterfaceVariate")
  44. @RestController
  45. @RequestMapping(value = "/api/system/DataInterfaceVariate")
  46. public class DataInterfaceVariateController {
  47. @Autowired
  48. private DataInterfaceVariateService dataInterfaceVariateService;
  49. @Autowired
  50. private UserService userApi;
  51. @Autowired
  52. private DataInterfaceService dataInterfaceService;
  53. @Autowired
  54. private DataFileExport fileExport;
  55. @Autowired
  56. private ConfigValueUtil configValueUtil;
  57. /**
  58. * 获取数据接口变量
  59. *
  60. * @param id 主键
  61. * @return
  62. */
  63. @Operation(summary = "获取数据接口变量")
  64. @SaCheckPermission("dataCenter.dataInterface")
  65. @Parameter(name = "id", description = "自然主键")
  66. @GetMapping("/{id}")
  67. public ActionResult<ListVO<DataInterfaceVariateListVO>> list(@PathVariable("id") String id, Page page) {
  68. List<DataInterfaceVariateListVO> list = new ArrayList<>();
  69. List<DataInterfaceVariateEntity> data = dataInterfaceVariateService.getList(id, page);
  70. data.forEach(t -> {
  71. DataInterfaceVariateListVO vo = new DataInterfaceVariateListVO();
  72. vo.setId(t.getId());
  73. vo.setInterfaceId(t.getInterfaceId());
  74. vo.setFullName(t.getFullName());
  75. vo.setValue(t.getValue());
  76. vo.setCreatorTime(t.getCreatorTime() != null ? t.getCreatorTime().getTime() : null);
  77. vo.setLastModifyTime(t.getLastModifyTime() != null ? t.getLastModifyTime().getTime() : null);
  78. UserEntity userEntity = userApi.getInfo(t.getCreatorUserId());
  79. vo.setCreatorUser(userEntity != null ? userEntity.getRealName() + "/" + userEntity.getAccount() : null);
  80. list.add(vo);
  81. });
  82. ListVO<DataInterfaceVariateListVO> listVO = new ListVO<>();
  83. listVO.setList(list);
  84. return ActionResult.success(listVO);
  85. }
  86. /**
  87. * 下拉列表
  88. *
  89. * @return
  90. */
  91. @Operation(summary = "下拉列表")
  92. @SaCheckPermission("dataCenter.dataInterface")
  93. @GetMapping("/Selector")
  94. public ActionResult<List<DataInterfaceVariateSelectorVO>> selector() {
  95. List<DataInterfaceVariateEntity> data = dataInterfaceVariateService.getList(null, null);
  96. List<DataInterfaceEntity> list = dataInterfaceService.getList(data.stream().map(DataInterfaceVariateEntity::getInterfaceId).collect(Collectors.toList()));
  97. List<DataInterfaceVariateSelectorVO> jsonToList = JsonUtil.getJsonToList(list, DataInterfaceVariateSelectorVO.class);
  98. jsonToList.forEach(t -> {
  99. t.setParentId("-1");
  100. t.setType(0);
  101. });
  102. jsonToList.forEach(t -> {
  103. List<DataInterfaceVariateEntity> collect = data.stream().filter(variateEntity -> t.getId().equals(variateEntity.getInterfaceId())).collect(Collectors.toList());
  104. List<DataInterfaceVariateSelectorVO> selectorVOS = JsonUtil.getJsonToList(collect, DataInterfaceVariateSelectorVO.class);
  105. selectorVOS.forEach(selectorVO -> {
  106. selectorVO.setParentId(t.getId());
  107. selectorVO.setType(1);
  108. });
  109. t.setChildren(selectorVOS);
  110. });
  111. return ActionResult.success(jsonToList);
  112. }
  113. /**
  114. * 详情
  115. *
  116. * @param id 主键
  117. * @return
  118. */
  119. @Operation(summary = "详情")
  120. @SaCheckPermission("dataCenter.dataInterface")
  121. @Parameter(name = "id", description = "自然主键")
  122. @GetMapping("/{id}/Info")
  123. public ActionResult<DataInterfaceVariateVO> info(@PathVariable("id") String id) {
  124. DataInterfaceVariateEntity entity = dataInterfaceVariateService.getInfo(id);
  125. DataInterfaceVariateVO vo = JsonUtil.getJsonToBean(entity, DataInterfaceVariateVO.class);
  126. return ActionResult.success(vo);
  127. }
  128. /**
  129. * 导出
  130. *
  131. * @param id 自然主键
  132. * @return
  133. */
  134. @Operation(summary = "导出")
  135. @SaCheckPermission("dataCenter.dataInterface")
  136. @Parameter(name = "id", description = "自然主键")
  137. @GetMapping("/{id}/Actions/Export")
  138. public ActionResult<DownloadVO> export(@PathVariable("id") String id) {
  139. DataInterfaceVariateEntity entity = dataInterfaceVariateService.getInfo(id);
  140. DownloadVO downloadVO = fileExport.exportFile(entity, FileTypeConstant.TEMPORARY, entity.getFullName(), ModuleTypeEnum.SYSTEM_DATAINTEFASE_VARIATE.getTableName());
  141. return ActionResult.success(downloadVO);
  142. }
  143. /**
  144. * 添加
  145. *
  146. * @param dataInterfaceVariateModel 模型
  147. * @return
  148. */
  149. @Operation(summary = "添加")
  150. @SaCheckPermission("dataCenter.dataInterface")
  151. @Parameter(name = "dataInterfaceVariateModel", description = "模型")
  152. @PostMapping
  153. public ActionResult<String> create(@RequestBody DataInterfaceVariateModel dataInterfaceVariateModel) {
  154. DataInterfaceVariateEntity entity = JsonUtil.getJsonToBean(dataInterfaceVariateModel, DataInterfaceVariateEntity.class);
  155. if (entity.getFullName().contains("@")) {
  156. return ActionResult.fail(MsgCode.SYS009.get());
  157. }
  158. if (dataInterfaceVariateService.isExistByFullName(entity)) {
  159. return ActionResult.fail(MsgCode.SYS010.get());
  160. }
  161. dataInterfaceVariateService.create(entity);
  162. return ActionResult.success(MsgCode.SU001.get());
  163. }
  164. /**
  165. * 修改
  166. *
  167. * @param id 自然主键
  168. * @param dataInterfaceVariateModel 模型
  169. * @return
  170. */
  171. @Operation(summary = "修改")
  172. @SaCheckPermission("dataCenter.dataInterface")
  173. @Parameters({
  174. @Parameter(name = "id", description = "自然主键"),
  175. @Parameter(name = "dataInterfaceVariateModel", description = "模型")
  176. })
  177. @PutMapping("/{id}")
  178. public ActionResult<String> update(@PathVariable("id") String id, @RequestBody DataInterfaceVariateModel dataInterfaceVariateModel) {
  179. DataInterfaceVariateEntity entity = JsonUtil.getJsonToBean(dataInterfaceVariateModel, DataInterfaceVariateEntity.class);
  180. if (entity.getFullName().contains("@")) {
  181. return ActionResult.fail(MsgCode.SYS009.get());
  182. }
  183. entity.setId(id);
  184. if (dataInterfaceVariateService.isExistByFullName(entity)) {
  185. return ActionResult.fail(MsgCode.SYS010.get());
  186. }
  187. dataInterfaceVariateService.update(entity);
  188. return ActionResult.success(MsgCode.SU004.get());
  189. }
  190. /**
  191. * 删除
  192. *
  193. * @param id 自然主键
  194. * @return
  195. */
  196. @Operation(summary = "删除")
  197. @SaCheckPermission("dataCenter.dataInterface")
  198. @Parameters({
  199. @Parameter(name = "id", description = "自然主键")
  200. })
  201. @DeleteMapping("/{id}")
  202. public ActionResult<String> delete(@PathVariable("id") String id) {
  203. DataInterfaceVariateEntity entity = dataInterfaceVariateService.getInfo(id);
  204. if (entity == null) {
  205. return ActionResult.fail(MsgCode.FA001.get());
  206. }
  207. dataInterfaceVariateService.delete(entity);
  208. return ActionResult.success(MsgCode.SU003.get());
  209. }
  210. /**
  211. * 导入
  212. *
  213. * @param multipartFile 文件
  214. * @return
  215. */
  216. @Operation(summary = "导入")
  217. @SaCheckPermission("dataCenter.dataInterface")
  218. @PostMapping(value = "/Actions/Import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  219. public ActionResult<String> delete(@RequestPart("file") MultipartFile multipartFile) {
  220. //判断是否为.json结尾
  221. if (FileUtil.existsSuffix(multipartFile, ModuleTypeEnum.SYSTEM_DATAINTEFASE_VARIATE.getTableName())) {
  222. return ActionResult.fail(MsgCode.IMP002.get());
  223. }
  224. //读取文件内容
  225. String fileContent = FileUtil.getFileContent(multipartFile);
  226. try {
  227. DataInterfaceVariateEntity entity = JsonUtil.getJsonToBean(fileContent, DataInterfaceVariateEntity.class);
  228. if (dataInterfaceVariateService.getInfo(entity.getId()) == null &&
  229. !dataInterfaceVariateService.isExistByFullName(entity)) {
  230. dataInterfaceVariateService.create(entity);
  231. return ActionResult.success(MsgCode.IMP001.get());
  232. }
  233. } catch (Exception e) {
  234. throw new DataException(MsgCode.IMP004.get());
  235. }
  236. return ActionResult.fail(MsgCode.IMP003.get());
  237. }
  238. /**
  239. * 复制
  240. *
  241. * @param id 自然主键
  242. * @return
  243. */
  244. @Operation(summary = "复制")
  245. @SaCheckPermission("dataCenter.dataInterface")
  246. @Parameter(name = "id", description = "自然主键", required = true)
  247. @PostMapping("/{id}/Actions/Copy")
  248. public ActionResult<String> copy(@PathVariable("id") String id) {
  249. String copyNum = UUID.randomUUID().toString().substring(0, 5);
  250. DataInterfaceVariateEntity entity = dataInterfaceVariateService.getInfo(id);
  251. entity.setFullName(entity.getFullName() + ".副本" + copyNum);
  252. if(entity.getFullName().length() > 50) return ActionResult.fail(MsgCode.COPY001.get());
  253. entity.setLastModifyTime(null);
  254. entity.setLastModifyUserId(null);
  255. dataInterfaceVariateService.create(entity);
  256. return ActionResult.success(MsgCode.SU007.get());
  257. }
  258. }