IntegrateController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package jnpf.integrate.controller;
  2. import cn.hutool.core.util.ObjectUtil;
  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.controller.SuperController;
  9. import jnpf.base.vo.DownloadVO;
  10. import jnpf.base.vo.PageListVO;
  11. import jnpf.base.vo.PaginationVO;
  12. import jnpf.config.ConfigValueUtil;
  13. import jnpf.constant.FileTypeConstant;
  14. import jnpf.constant.MsgCode;
  15. import jnpf.emnus.ModuleTypeEnum;
  16. import jnpf.exception.WorkFlowException;
  17. import jnpf.integrate.entity.IntegrateEntity;
  18. import jnpf.integrate.entity.IntegrateQueueEntity;
  19. import jnpf.integrate.job.IntegrateJobUtil;
  20. import jnpf.integrate.model.integrate.*;
  21. import jnpf.integrate.model.nodeJson.IntegrateModel;
  22. import jnpf.integrate.service.IntegrateQueueService;
  23. import jnpf.integrate.service.IntegrateService;
  24. import jnpf.integrate.util.IntegrateHttpModel;
  25. import jnpf.integrate.util.IntegrateUtil;
  26. import jnpf.permission.entity.UserEntity;
  27. import jnpf.permission.service.UserService;
  28. import jnpf.util.*;
  29. import lombok.extern.slf4j.Slf4j;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.http.MediaType;
  32. import org.springframework.web.bind.annotation.*;
  33. import org.springframework.web.multipart.MultipartFile;
  34. import jakarta.validation.Valid;
  35. import java.util.ArrayList;
  36. import java.util.Date;
  37. import java.util.List;
  38. import java.util.UUID;
  39. import java.util.stream.Collectors;
  40. @Slf4j
  41. @Tag(name = "集成助手", description = "Integrate")
  42. @RestController
  43. @RequestMapping("/api/visualdev/Integrate")
  44. public class IntegrateController extends SuperController<IntegrateService, IntegrateEntity> {
  45. @Autowired
  46. private UserService userService;
  47. @Autowired
  48. private DataFileExport fileExport;
  49. @Autowired
  50. private ConfigValueUtil configValueUtil;
  51. @Autowired
  52. private IntegrateService integrateService;
  53. @Autowired
  54. private IntegrateQueueService integrateQueueService;
  55. @Autowired
  56. private IntegrateUtil integrateUtil;
  57. @Autowired
  58. private RedisUtil redisUtil;
  59. /**
  60. * 列表
  61. *
  62. * @return
  63. */
  64. @Operation(summary = "列表")
  65. @GetMapping
  66. public ActionResult<PageListVO<IntegrateListVO>> list(IntegratePagination pagination) {
  67. List<IntegrateEntity> data = integrateService.getList(pagination);
  68. List<String> userId = data.stream().map(t -> t.getCreatorUserId()).collect(Collectors.toList());
  69. List<UserEntity> userEntities = userService.getUserName(userId);
  70. List<IntegrateListVO> resultList = new ArrayList<>();
  71. for (IntegrateEntity entity : data) {
  72. IntegrateListVO vo = JsonUtil.getJsonToBean(entity, IntegrateListVO.class);
  73. UserEntity creatorUser = userEntities.stream().filter(t -> t.getId().equals(entity.getCreatorUserId())).findFirst().orElse(null);
  74. vo.setCreatorUser(creatorUser != null ? creatorUser.getRealName() + "/" + creatorUser.getAccount() : "");
  75. resultList.add(vo);
  76. }
  77. PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
  78. return ActionResult.page(resultList, paginationVO);
  79. }
  80. /**
  81. * 信息
  82. *
  83. * @param id 主键值
  84. * @return
  85. */
  86. @Operation(summary = "获取信息")
  87. @Parameters({
  88. @Parameter(name = "id", description = "主键", required = true)
  89. })
  90. @GetMapping("/{id}")
  91. public ActionResult info(@PathVariable("id") String id) {
  92. IntegrateEntity entity = integrateService.getInfo(id);
  93. if (entity == null) {
  94. return ActionResult.fail(MsgCode.FA001.get());
  95. }
  96. IntegrateInfoVO vo = JsonUtil.getJsonToBean(entity, IntegrateInfoVO.class);
  97. return ActionResult.success(vo);
  98. }
  99. /**
  100. * 新建
  101. *
  102. * @param integrateCrForm 实体对象
  103. * @return
  104. */
  105. @Operation(summary = "添加")
  106. @Parameters({
  107. @Parameter(name = "integrateCrForm", description = "实体对象", required = true)
  108. })
  109. @PostMapping
  110. public ActionResult create(@RequestBody @Valid IntegrateCrForm integrateCrForm) {
  111. IntegrateEntity entity = JsonUtil.getJsonToBean(integrateCrForm, IntegrateEntity.class);
  112. if (integrateService.isExistByFullName(entity.getFullName(), entity.getId())) {
  113. return ActionResult.fail(MsgCode.EXIST001.get());
  114. }
  115. if (integrateService.isExistByEnCode(entity.getEnCode(), entity.getId())) {
  116. return ActionResult.fail(MsgCode.EXIST002.get());
  117. }
  118. String id = RandomUtil.uuId();
  119. entity.setId(id);
  120. integrateService.create(entity);
  121. return ActionResult.success(MsgCode.SU001.get(), id);
  122. }
  123. /**
  124. * 更新
  125. *
  126. * @param id 主键值
  127. * @return
  128. */
  129. @Operation(summary = "修改")
  130. @Parameters({
  131. @Parameter(name = "id", description = "主键值", required = true),
  132. @Parameter(name = "integrateUpForm", description = "实体对象", required = true)
  133. })
  134. @PutMapping("/{id}")
  135. public ActionResult update(@PathVariable("id") String id, @RequestBody IntegrateUpForm integrateUpForm) {
  136. IntegrateEntity positionEntity = integrateService.getInfo(id);
  137. if (positionEntity == null) {
  138. return ActionResult.fail(MsgCode.FA002.get());
  139. }
  140. IntegrateEntity entity = JsonUtil.getJsonToBean(integrateUpForm, IntegrateEntity.class);
  141. if (integrateService.isExistByFullName(entity.getFullName(), id)) {
  142. return ActionResult.fail(MsgCode.EXIST001.get());
  143. }
  144. if (integrateService.isExistByEnCode(entity.getEnCode(), id)) {
  145. return ActionResult.fail(MsgCode.EXIST002.get());
  146. }
  147. boolean flag = integrateService.update(id, entity,false);
  148. if (flag == false) {
  149. return ActionResult.fail(MsgCode.FA002.get());
  150. }
  151. return ActionResult.success(MsgCode.SU004.get(), id);
  152. }
  153. /**
  154. * 删除
  155. *
  156. * @param id 主键值
  157. * @return
  158. */
  159. @Operation(summary = "删除")
  160. @Parameters({
  161. @Parameter(name = "id", description = "主键值", required = true)
  162. })
  163. @DeleteMapping("/{id}")
  164. public ActionResult delete(@PathVariable("id") String id) {
  165. IntegrateEntity entity = integrateService.getInfo(id);
  166. if (entity != null) {
  167. integrateService.delete(entity);
  168. return ActionResult.success(MsgCode.SU003.get());
  169. }
  170. return ActionResult.fail(MsgCode.FA003.get());
  171. }
  172. /**
  173. * 复制功能
  174. *
  175. * @param id 主键值
  176. * @return
  177. */
  178. @Operation(summary = "复制功能")
  179. @Parameters({
  180. @Parameter(name = "id", description = "主键"),
  181. })
  182. @PostMapping("/{id}/Actions/Copy")
  183. public ActionResult copyInfo(@PathVariable("id") String id) throws Exception {
  184. IntegrateEntity entity = integrateService.getInfo(id);
  185. entity.setEnabledMark(0);
  186. String copyNum = UUID.randomUUID().toString().substring(0, 5);
  187. entity.setFullName(entity.getFullName() + ".副本" + copyNum);
  188. entity.setLastModifyTime(null);
  189. entity.setLastModifyUserId(null);
  190. entity.setId(RandomUtil.uuId());
  191. entity.setEnCode(entity.getEnCode() + copyNum);
  192. entity.setCreatorTime(new Date());
  193. entity.setEnabledMark(0);
  194. entity.setCreatorUserId(UserProvider.getUser().getUserId());
  195. integrateService.create(entity);
  196. return ActionResult.success(MsgCode.SU007.get());
  197. }
  198. /**
  199. * 更新功能状态
  200. *
  201. * @param id 主键值
  202. * @return
  203. */
  204. @Operation(summary = "更新功能状态")
  205. @Parameters({
  206. @Parameter(name = "id", description = "主键"),
  207. })
  208. @PutMapping("/{id}/Actions/State")
  209. public ActionResult update(@PathVariable("id") String id) {
  210. IntegrateEntity entity = integrateService.getInfo(id);
  211. if (entity != null) {
  212. if (entity.getEnabledMark() == null || "1".equals(String.valueOf(entity.getEnabledMark()))) {
  213. entity.setEnabledMark(0);
  214. } else {
  215. entity.setEnabledMark(1);
  216. }
  217. boolean flag = integrateService.update(entity.getId(), entity,true);
  218. if (flag == false) {
  219. return ActionResult.fail(MsgCode.FA002.get());
  220. }
  221. }
  222. return ActionResult.success(MsgCode.SU004.get());
  223. }
  224. /**
  225. * 导出
  226. *
  227. * @param id 主键
  228. * @return
  229. */
  230. @Operation(summary = "导出")
  231. @PostMapping("/{id}/Actions/Export")
  232. @Parameters({
  233. @Parameter(name = "id", description = "主键", required = true),
  234. })
  235. public ActionResult<DownloadVO> exportData(@PathVariable("id") String id) {
  236. IntegrateEntity entity = integrateService.getInfo(id);
  237. DownloadVO downloadVO = fileExport.exportFile(entity, FileTypeConstant.TEMPORARY, entity.getFullName(), ModuleTypeEnum.BASE_INTEGRATE.getTableName());
  238. return ActionResult.success(downloadVO);
  239. }
  240. /**
  241. * 导入
  242. *
  243. * @param file 文件
  244. * @return
  245. */
  246. @Operation(summary = "导入")
  247. @PostMapping(value = "/Actions/Import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  248. public ActionResult ImportData(@RequestPart("file") MultipartFile file,@RequestParam("type") Integer type) throws WorkFlowException {
  249. //判断是否为.json结尾
  250. if (FileUtil.existsSuffix(file, ModuleTypeEnum.BASE_INTEGRATE.getTableName())) {
  251. return ActionResult.fail(MsgCode.IMP002.get());
  252. }
  253. try {
  254. String fileContent = FileUtil.getFileContent(file);
  255. IntegrateEntity entity = JsonUtil.getJsonToBean(fileContent, IntegrateEntity.class);
  256. return integrateService.ImportData(entity, type);
  257. } catch (Exception e) {
  258. throw new WorkFlowException(MsgCode.IMP004.get());
  259. }
  260. }
  261. /**
  262. * 集成助手事件触发
  263. * @param model
  264. * @return
  265. */
  266. @PostMapping(value = "/execute")
  267. public ActionResult execute(@RequestBody IntegrateHttpModel model) {
  268. integrateUtil.integrates(model.getDataInfoVOList(),model.getUserInfo());
  269. return ActionResult.success();
  270. }
  271. /**
  272. * 集成助手定时触发
  273. * @param model
  274. * @return
  275. */
  276. @PostMapping(value = "/executeQuery")
  277. public ActionResult executeQuery(@RequestBody IntegrateHttpModel model) {
  278. IntegrateModel integrateModel = JsonUtil.getJsonToBean(model, IntegrateModel.class);
  279. if(ObjectUtil.isNotEmpty(model.getId())){
  280. IntegrateQueueEntity entity = integrateQueueService.getById(model.getId());
  281. if(ObjectUtil.isNotEmpty(entity)) {
  282. integrateUtil.integrate(entity, model.getUserInfo());
  283. }
  284. }
  285. IntegrateJobUtil.removeIntegrate(integrateModel, redisUtil);
  286. return ActionResult.success();
  287. }
  288. }