DataInterfaceController.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. package jnpf.base.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.alibaba.fastjson.serializer.SerializerFeature;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import io.swagger.v3.oas.annotations.Operation;
  8. import io.swagger.v3.oas.annotations.Parameter;
  9. import io.swagger.v3.oas.annotations.Parameters;
  10. import io.swagger.v3.oas.annotations.tags.Tag;
  11. import jnpf.base.*;
  12. import jnpf.base.entity.DataInterfaceEntity;
  13. import jnpf.base.entity.DictionaryDataEntity;
  14. import jnpf.base.entity.InterfaceOauthEntity;
  15. import jnpf.base.model.datainterface.*;
  16. import jnpf.base.service.DataInterfaceService;
  17. import jnpf.base.service.DictionaryDataService;
  18. import jnpf.base.service.DictionaryTypeService;
  19. import jnpf.base.service.InterfaceOauthService;
  20. import jnpf.base.util.interfaceUtil.InterfaceUtil;
  21. import jnpf.base.vo.PageListVO;
  22. import jnpf.constant.DbSensitiveConstant;
  23. import jnpf.base.vo.DownloadVO;
  24. import jnpf.base.vo.ListVO;
  25. import jnpf.base.vo.PaginationVO;
  26. import jnpf.config.ConfigValueUtil;
  27. import jnpf.constant.FileTypeConstant;
  28. import jnpf.constant.MsgCode;
  29. import jnpf.emnus.ModuleTypeEnum;
  30. import jnpf.exception.DataException;
  31. import jnpf.database.util.*;
  32. import jnpf.util.*;
  33. import jnpf.util.enums.DictionaryDataEnum;
  34. import jnpf.util.treeutil.SumTree;
  35. import jnpf.util.treeutil.TreeDotUtils;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.http.MediaType;
  38. import org.springframework.web.bind.annotation.*;
  39. import org.springframework.web.multipart.MultipartFile;
  40. import jakarta.validation.Valid;
  41. import java.util.*;
  42. /**
  43. * 数据接口
  44. *
  45. * @author JNPF开发平台组
  46. * @version V3.1.0
  47. * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
  48. * @date 2021-03-15 10:29
  49. */
  50. @Tag(name = "数据接口", description = "DataInterface")
  51. @RestController
  52. @RequestMapping(value = "/api/system/DataInterface")
  53. public class DataInterfaceController extends SuperController<DataInterfaceService, DataInterfaceEntity> {
  54. @Autowired
  55. private DataInterfaceService dataInterfaceService;
  56. @Autowired
  57. private DictionaryDataService dictionaryDataService;
  58. @Autowired
  59. private DictionaryTypeService dictionaryTypeService;
  60. @Autowired
  61. private ConfigValueUtil configValueUtil;
  62. @Autowired
  63. private InterfaceOauthService interfaceOauthService;
  64. @Autowired
  65. private DataFileExport fileExport;
  66. /**
  67. * 获取接口列表(分页)
  68. *
  69. * @param pagination 分页模型
  70. * @return
  71. */
  72. @Operation(summary = "获取接口列表(分页)")
  73. @SaCheckPermission("dataCenter.dataInterface")
  74. @GetMapping
  75. public ActionResult<PageListVO<DataInterfaceListVO>> getList(PaginationDataInterface pagination) {
  76. List<DataInterfaceEntity> data = dataInterfaceService.getList(pagination, 0);
  77. List<DataInterfaceListVO> list = JsonUtil.getJsonToList(data, DataInterfaceListVO.class);
  78. // 添加tenantId字段
  79. for (DataInterfaceListVO vo : list) {
  80. // 类别转换
  81. if ("1".equals(vo.getType())) {
  82. vo.setType("SQL操作");
  83. } else if ("2".equals(vo.getType())) {
  84. vo.setType("静态数据");
  85. } else if ("3".equals(vo.getType())) {
  86. vo.setType("API操作");
  87. }
  88. if (StringUtil.isNotEmpty(UserProvider.getUser().getTenantId())) {
  89. vo.setTenantId(UserProvider.getUser().getTenantId());
  90. }
  91. }
  92. PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
  93. return ActionResult.page(list, paginationVO);
  94. }
  95. /**
  96. * 获取接口列表(工作流选择时调用)
  97. *
  98. * @param pagination 分页参数
  99. * @return ignore
  100. */
  101. @Operation(summary = "获取接口列表(工作流选择时调用)")
  102. @GetMapping("/getList")
  103. public ActionResult<PageListVO<DataInterfaceGetListVO>> getLists(PaginationDataInterfaceSelector pagination) {
  104. List<DataInterfaceEntity> data = dataInterfaceService.getList(pagination);
  105. List<DataInterfaceGetListVO> list = JsonUtil.getJsonToList(data, DataInterfaceGetListVO.class);
  106. for (DataInterfaceGetListVO vo : list) {
  107. // 类别转换
  108. if ("1".equals(vo.getType())) {
  109. vo.setType("SQL操作");
  110. } else if ("2".equals(vo.getType())) {
  111. vo.setType("静态数据");
  112. } else if ("3".equals(vo.getType())) {
  113. vo.setType("API操作");
  114. }
  115. }
  116. PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
  117. return ActionResult.page(list, paginationVO);
  118. }
  119. /**
  120. * 获取接口列表下拉框
  121. *
  122. * @return
  123. */
  124. @Operation(summary = "获取接口列表下拉框")
  125. @GetMapping("/Selector")
  126. public ActionResult<List<DataInterfaceTreeVO>> getSelector() {
  127. List<DataInterfaceTreeModel> tree = new ArrayList<>();
  128. List<DataInterfaceEntity> data = dataInterfaceService.getList(false);
  129. List<DictionaryDataEntity> dataEntityList = dictionaryDataService.getList(dictionaryTypeService.getInfoByEnCode(DictionaryDataEnum.SYSTEM_DATAINTERFACE.getDictionaryTypeId()).getId());
  130. // 获取数据接口外层菜单
  131. for (DictionaryDataEntity dictionaryDataEntity : dataEntityList) {
  132. DataInterfaceTreeModel firstModel = JsonUtil.getJsonToBean(dictionaryDataEntity, DataInterfaceTreeModel.class);
  133. firstModel.setId(dictionaryDataEntity.getId());
  134. firstModel.setCategory("0");
  135. long num = data.stream().filter(t -> t.getCategory().equals(dictionaryDataEntity.getId())).count();
  136. if (num > 0) {
  137. tree.add(firstModel);
  138. }
  139. }
  140. for (DataInterfaceEntity entity : data) {
  141. DataInterfaceTreeModel treeModel = JsonUtil.getJsonToBean(entity, DataInterfaceTreeModel.class);
  142. treeModel.setCategory("1");
  143. treeModel.setParentId(entity.getCategory());
  144. treeModel.setId(entity.getId());
  145. DictionaryDataEntity dataEntity = dictionaryDataService.getInfo(entity.getCategory());
  146. if (dataEntity != null) {
  147. tree.add(treeModel);
  148. }
  149. }
  150. List<SumTree<DataInterfaceTreeModel>> sumTrees = TreeDotUtils.convertListToTreeDot(tree);
  151. List<DataInterfaceTreeVO> list = JsonUtil.getJsonToList(sumTrees, DataInterfaceTreeVO.class);
  152. ListVO<DataInterfaceTreeVO> vo = new ListVO<>();
  153. vo.setList(list);
  154. return ActionResult.success(list);
  155. }
  156. /**
  157. * 获取接口参数列表下拉框
  158. *
  159. * @param id 主键
  160. * @return ignore
  161. */
  162. @Operation(summary = "获取接口参数列表下拉框")
  163. @Parameter(name = "id", description = "主键", required = true)
  164. @SaCheckPermission("dataCenter.dataInterface")
  165. @GetMapping("/GetParam/{id}")
  166. public ActionResult<List<DataInterfaceModel>> getSelector(@PathVariable("id") String id) {
  167. DataInterfaceEntity entity = dataInterfaceService.getInfo(id);
  168. if (entity!=null) {
  169. String parameterJson = entity.getParameterJson();
  170. List<DataInterfaceModel> jsonToList = JsonUtil.getJsonToList(parameterJson, DataInterfaceModel.class);
  171. return ActionResult.success(jsonToList == null ? new ArrayList<>() : jsonToList);
  172. }
  173. return ActionResult.fail(MsgCode.FA001.get());
  174. }
  175. /**
  176. * 获取接口数据
  177. *
  178. * @param id 主键
  179. * @return
  180. */
  181. @Operation(summary = "获取接口数据")
  182. @Parameter(name = "id", description = "主键", required = true)
  183. @SaCheckPermission("dataCenter.dataInterface")
  184. @GetMapping("/{id}")
  185. public ActionResult<DataInterfaceVo> getInfo(@PathVariable("id") String id) throws DataException {
  186. DataInterfaceEntity entity = dataInterfaceService.getInfo(id);
  187. DataInterfaceVo vo = JsonUtil.getJsonToBean(entity, DataInterfaceVo.class);
  188. return ActionResult.success(vo);
  189. }
  190. /**
  191. * 添加接口
  192. *
  193. * @param dataInterfaceCrForm 实体模型
  194. * @return
  195. */
  196. @Operation(summary = "添加接口")
  197. @Parameter(name = "dataInterfaceCrForm", description = "实体模型", required = true)
  198. @SaCheckPermission("dataCenter.dataInterface")
  199. @PostMapping
  200. public ActionResult create(@RequestBody @Valid DataInterfaceCrForm dataInterfaceCrForm) throws DataException {
  201. DataInterfaceEntity entity = JsonUtil.getJsonToBean(dataInterfaceCrForm, DataInterfaceEntity.class);
  202. // 判断是否有敏感字
  203. String containsSensitive = containsSensitive(entity);
  204. if (StringUtil.isNotEmpty(containsSensitive)) {
  205. return ActionResult.fail(MsgCode.SYS006.get(containsSensitive));
  206. }
  207. if (dataInterfaceService.isExistByFullNameOrEnCode(entity.getId(), entity.getFullName(), null)) {
  208. return ActionResult.fail(MsgCode.EXIST001.get());
  209. }
  210. if (dataInterfaceService.isExistByFullNameOrEnCode(entity.getId(), null, entity.getEnCode())) {
  211. return ActionResult.fail(MsgCode.EXIST002.get());
  212. }
  213. dataInterfaceService.create(entity);
  214. return ActionResult.success(MsgCode.SYS005.get());
  215. }
  216. /**
  217. * 判断是否有敏感字
  218. *
  219. * @param entity
  220. * @return
  221. */
  222. private String containsSensitive(DataInterfaceEntity entity) {
  223. // 判断是否有敏感字
  224. if (entity.getType() == 1 && (entity.getAction() != null && entity.getAction() == 3)) {
  225. DataConfigJsonModel dataConfigJsonModel = JsonUtil.getJsonToBean(entity.getDataConfigJson(), DataConfigJsonModel.class);
  226. String sql = dataConfigJsonModel.getSqlData().getSql();
  227. if (StringUtil.isNotEmpty(sql)) {
  228. return ParameterUtil.checkContainsSensitive(sql, DbSensitiveConstant.SENSITIVE);
  229. }
  230. }
  231. return "";
  232. }
  233. /**
  234. * 修改接口
  235. *
  236. * @param dataInterfaceUpForm 实体模型
  237. * @param id 主键
  238. * @return
  239. */
  240. @Operation(summary = "修改接口")
  241. @Parameters({
  242. @Parameter(name = "dataInterfaceUpForm", description = "实体模型", required = true),
  243. @Parameter(name = "id", description = "主键", required = true)
  244. })
  245. @SaCheckPermission("dataCenter.dataInterface")
  246. @PutMapping("/{id}")
  247. public ActionResult update(@RequestBody @Valid DataInterfaceUpForm dataInterfaceUpForm, @PathVariable("id") String id) throws DataException {
  248. DataInterfaceEntity entity = JsonUtilEx.getJsonToBeanEx(dataInterfaceUpForm, DataInterfaceEntity.class);
  249. // 判断是否有敏感字
  250. String containsSensitive = containsSensitive(entity);
  251. if (StringUtil.isNotEmpty(containsSensitive)) {
  252. return ActionResult.fail(MsgCode.SYS006.get(containsSensitive));
  253. }
  254. if (dataInterfaceService.isExistByFullNameOrEnCode(id, entity.getFullName(), null)) {
  255. return ActionResult.fail(MsgCode.EXIST001.get());
  256. }
  257. if (dataInterfaceService.isExistByFullNameOrEnCode(id, null, entity.getEnCode())) {
  258. return ActionResult.fail(MsgCode.EXIST002.get());
  259. }
  260. boolean flag = dataInterfaceService.update(entity, id);
  261. if (!flag) {
  262. return ActionResult.fail(MsgCode.FA001.get());
  263. }
  264. return ActionResult.success(MsgCode.SU004.get());
  265. }
  266. /**
  267. * 删除接口
  268. *
  269. * @param id 主键
  270. * @return
  271. */
  272. @Operation(summary = "删除接口")
  273. @Parameters({
  274. @Parameter(name = "id", description = "主键", required = true)
  275. })
  276. @SaCheckPermission("dataCenter.dataInterface")
  277. @DeleteMapping("/{id}")
  278. public ActionResult delete(@PathVariable String id) {
  279. DataInterfaceEntity entity = dataInterfaceService.getInfo(id);
  280. if (entity != null) {
  281. dataInterfaceService.delete(entity);
  282. return ActionResult.success(MsgCode.SU003.get());
  283. }
  284. return ActionResult.fail(MsgCode.FA001.get());
  285. }
  286. /**
  287. * 更新接口状态
  288. *
  289. * @param id 主键
  290. * @return
  291. */
  292. @Operation(summary = "更新接口状态")
  293. @Parameters({
  294. @Parameter(name = "id", description = "主键", required = true)
  295. })
  296. @SaCheckPermission("dataCenter.dataInterface")
  297. @PutMapping("/{id}/Actions/State")
  298. public ActionResult update(@PathVariable("id") String id) throws DataException {
  299. DataInterfaceEntity entity = dataInterfaceService.getInfo(id);
  300. if (entity != null) {
  301. if (entity.getEnabledMark() == 0) {
  302. entity.setEnabledMark(1);
  303. } else {
  304. entity.setEnabledMark(0);
  305. }
  306. dataInterfaceService.update(entity, id);
  307. return ActionResult.success(MsgCode.SU014.get());
  308. }
  309. return ActionResult.fail(MsgCode.FA001.get());
  310. }
  311. /**
  312. * 获取接口分页数据
  313. *
  314. * @param id 主键
  315. * @param page 分页参数
  316. * @return
  317. */
  318. @Operation(summary = "获取接口分页数据")
  319. @Parameters({
  320. @Parameter(name = "id", description = "主键", required = true),
  321. @Parameter(name = "page", description = "分页参数", required = true)
  322. })
  323. @PostMapping("/{id}/Actions/List")
  324. public ActionResult infoToIdPageList(@PathVariable("id") String id, @RequestBody DataInterfacePage page) {
  325. ActionResult result = dataInterfaceService.infoToIdPageList(id, page);
  326. return result;
  327. }
  328. /**
  329. * 获取接口详情数据
  330. *
  331. * @param id 主键
  332. * @param page 分页参数
  333. * @return
  334. */
  335. @Operation(summary = "获取接口详情数据")
  336. @Parameters({
  337. @Parameter(name = "id", description = "主键", required = true),
  338. @Parameter(name = "page", description = "分页参数", required = true)
  339. })
  340. @PostMapping("/{id}/Actions/InfoByIds")
  341. public ActionResult<List<Map<String, Object>>> infoByIds(@PathVariable("id") String id, @RequestBody DataInterfacePage page) {
  342. List<Map<String, Object>> data = dataInterfaceService.infoToInfo(id, page);
  343. return ActionResult.success(data);
  344. }
  345. /**
  346. * 测试接口
  347. *
  348. * @param id 主键
  349. * @param objectMap 参数、参数值对象
  350. * @return
  351. */
  352. @Operation(summary = "测试接口")
  353. @Parameters({
  354. @Parameter(name = "id", description = "主键", required = true),
  355. @Parameter(name = "objectMap", description = "参数、参数值对象")
  356. })
  357. @PostMapping("/{id}/Actions/Preview")
  358. @NoDataSourceBind
  359. public ActionResult callPreview(@PathVariable("id") String id, @RequestBody(required = false) Map<String, Object> objectMap) {
  360. DataInterfaceParamModel model = JsonUtil.getJsonToBean(objectMap, DataInterfaceParamModel.class);
  361. Map<String, String> map = null;
  362. if (model != null) {
  363. if (configValueUtil.isMultiTenancy()) {
  364. //切换成租户库
  365. try{
  366. TenantDataSourceUtil.switchTenant(model.getTenantId());
  367. }catch (Exception e){
  368. return ActionResult.fail(ActionResultCode.SessionOverdue.getMessage());
  369. }
  370. }
  371. if (model.getParamList() != null && !model.getParamList().isEmpty()) {
  372. map = new HashMap<>(16);
  373. List<DataInterfaceModel> jsonToList = JsonUtil.getJsonToList(model.getParamList(), DataInterfaceModel.class);
  374. dataInterfaceService.paramSourceTypeReplaceValue(jsonToList, map);
  375. }
  376. }
  377. ActionResult actionResult = dataInterfaceService.infoToId(id, null, map);
  378. if (actionResult.getCode() == 200) {
  379. actionResult.setMsg(MsgCode.SYS007.get());
  380. }
  381. return actionResult;
  382. }
  383. /**
  384. * 访问接口GET
  385. *
  386. * @param id 主键
  387. * @param map 参数、参数值对象
  388. * @return
  389. */
  390. @Operation(summary = "访问接口GET")
  391. @GetMapping("/{id}/Actions/Response")
  392. @Parameters({
  393. @Parameter(name = "id", description = "主键", required = true),
  394. @Parameter(name = "map", description = "参数、参数值对象")
  395. })
  396. @NoDataSourceBind
  397. public ActionResult getResponse(@PathVariable("id") String id,@RequestParam(required = false) Map<String,String> map) {
  398. DataInterfaceActionModel entity;
  399. try{
  400. entity= dataInterfaceService.checkParams(map);
  401. entity.setInvokType("GET");
  402. }catch (Exception e){
  403. return ActionResult.fail(e.getMessage());
  404. }
  405. String name = null;
  406. if (configValueUtil.isMultiTenancy()) {
  407. //切换成租户库
  408. try{
  409. TenantDataSourceUtil.switchTenant(entity.getTenantId());
  410. }catch (Exception e){
  411. return ActionResult.fail(ActionResultCode.SessionOverdue.getMessage());
  412. }
  413. }
  414. return dataInterfaceService.infoToIdNew(id, name, entity);
  415. }
  416. /**
  417. * 访问接口POST
  418. *
  419. * @param id 主键
  420. * @param map 参数、参数值对象
  421. * @return
  422. */
  423. @Operation(summary = "访问接口POST")
  424. @Parameters({
  425. @Parameter(name = "id", description = "主键", required = true),
  426. @Parameter(name = "map", description = "参数、参数值对象")
  427. })
  428. @PostMapping("/{id}/Actions/Response")
  429. @NoDataSourceBind
  430. public ActionResult postResponse(@PathVariable("id") String id, @RequestBody(required = false) Map<String,String> map) {
  431. DataInterfaceActionModel entity;
  432. try{
  433. entity= dataInterfaceService.checkParams(map);
  434. entity.setInvokType("POST");
  435. }catch (Exception e){
  436. return ActionResult.fail(e.getMessage());
  437. }
  438. String name = null;
  439. if (configValueUtil.isMultiTenancy()) {
  440. //切换成租户库
  441. try{
  442. TenantDataSourceUtil.switchTenant(entity.getTenantId());
  443. }catch (Exception e){
  444. return ActionResult.fail(ActionResultCode.SessionOverdue.getMessage());
  445. }
  446. }
  447. return dataInterfaceService.infoToIdNew(id, name, entity);
  448. }
  449. /**
  450. * 外部接口获取authorization
  451. *
  452. * @param appId 应用id
  453. * @param intefaceId 接口id
  454. * @param map 参数、参数值对象
  455. * @return
  456. */
  457. @Operation(summary = "外部接口获取authorization")
  458. @Parameters({
  459. @Parameter(name = "appId", description = "应用id", required = true),
  460. @Parameter(name = "intefaceId", description = "接口id"),
  461. @Parameter(name = "map", description = "参数、参数值对象")
  462. })
  463. @PostMapping("/Actions/GetAuth")
  464. @NoDataSourceBind
  465. public ActionResult getAuthorization(@RequestParam("appId") String appId,@RequestParam("intefaceId") String intefaceId, @RequestBody(required = false) Map<String,String> map) {
  466. InterfaceOauthEntity infoByAppId = interfaceOauthService.getInfoByAppId(appId);
  467. if(infoByAppId==null){
  468. return ActionResult.fail(MsgCode.SYS127.get());
  469. }
  470. Map<String, String> authorization = InterfaceUtil.getAuthorization(intefaceId,appId,infoByAppId.getAppSecret(), map);
  471. return ActionResult.success(MsgCode.SU005.get(),authorization);
  472. }
  473. /**
  474. * 数据接口导出功能
  475. *
  476. * @param id 接口id
  477. */
  478. @Operation(summary = "导出数据接口数据")
  479. @Parameters({
  480. @Parameter(name = "id", description = "主键", required = true)
  481. })
  482. @SaCheckPermission("dataCenter.dataInterface")
  483. @GetMapping("/{id}/Actions/Export")
  484. public ActionResult exportFile(@PathVariable("id") String id) {
  485. DataInterfaceEntity entity = dataInterfaceService.getInfo(id);
  486. //导出文件
  487. DownloadVO downloadVO = fileExport.exportFile(entity, FileTypeConstant.TEMPORARY, entity.getFullName(), ModuleTypeEnum.SYSTEM_DATAINTEFASE.getTableName());
  488. return ActionResult.success(downloadVO);
  489. }
  490. /**
  491. * 数据接口导入功能
  492. *
  493. * @param multipartFile
  494. * @return
  495. * @throws DataException
  496. */
  497. @Operation(summary = "数据接口导入功能")
  498. @SaCheckPermission("dataCenter.dataInterface")
  499. @PostMapping(value = "/Actions/Import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  500. public ActionResult importFile(@RequestPart("file") MultipartFile multipartFile,
  501. @RequestParam("type") Integer type) throws DataException {
  502. //判断是否为.json结尾
  503. if (FileUtil.existsSuffix(multipartFile, ModuleTypeEnum.SYSTEM_DATAINTEFASE.getTableName())) {
  504. return ActionResult.fail(MsgCode.IMP002.get());
  505. }
  506. try {
  507. //读取文件内容
  508. String fileContent = FileUtil.getFileContent(multipartFile);
  509. DataInterfaceEntity entity = JsonUtil.getJsonToBean(fileContent, DataInterfaceEntity.class);
  510. // 验证数据是否正常
  511. if (dictionaryDataService.getInfo(entity.getCategory()) == null) {
  512. return ActionResult.fail(MsgCode.IMP004.get());
  513. }
  514. StringJoiner stringJoiner = new StringJoiner("、");
  515. QueryWrapper<DataInterfaceEntity> queryWrapper = new QueryWrapper<>();
  516. queryWrapper.lambda().eq(DataInterfaceEntity::getId, entity.getId());
  517. if (dataInterfaceService.count(queryWrapper) > 0) {
  518. if (Objects.equals(type, 0)) {
  519. stringJoiner.add("ID");
  520. } else {
  521. entity.setId(RandomUtil.uuId());
  522. }
  523. }
  524. queryWrapper = new QueryWrapper<>();
  525. queryWrapper.lambda().eq(DataInterfaceEntity::getEnCode, entity.getEnCode());
  526. if (dataInterfaceService.count(queryWrapper) > 0) {
  527. stringJoiner.add(MsgCode.IMP009.get());
  528. }
  529. queryWrapper = new QueryWrapper<>();
  530. queryWrapper.lambda().eq(DataInterfaceEntity::getFullName, entity.getFullName());
  531. if (dataInterfaceService.count(queryWrapper) > 0) {
  532. stringJoiner.add(MsgCode.IMP008.get());
  533. }
  534. if (stringJoiner.length() > 0 && ObjectUtil.equal(type, 1)) {
  535. String copyNum = UUID.randomUUID().toString().substring(0, 5);
  536. entity.setFullName(entity.getFullName() + ".副本" + copyNum);
  537. entity.setEnCode(entity.getEnCode() + copyNum);
  538. } else if (ObjectUtil.equal(type, 0) && stringJoiner.length() > 0) {
  539. return ActionResult.fail(stringJoiner.toString() + MsgCode.IMP007.get());
  540. }
  541. entity.setCreatorTime(new Date());
  542. entity.setCreatorUserId(UserProvider.getLoginUserId());
  543. entity.setLastModifyTime(null);
  544. entity.setLastModifyUserId(null);
  545. try {
  546. dataInterfaceService.setIgnoreLogicDelete().removeById(entity);
  547. entity.setEnabledMark(0);
  548. dataInterfaceService.setIgnoreLogicDelete().saveOrUpdate(entity);
  549. } catch (Exception e) {
  550. throw new DataException(MsgCode.IMP003.get());
  551. }finally {
  552. dataInterfaceService.clearIgnoreLogicDelete();
  553. }
  554. return ActionResult.success(MsgCode.IMP001.get());
  555. } catch (Exception e) {
  556. return ActionResult.fail(MsgCode.IMP004.get());
  557. }
  558. }
  559. /**
  560. * 获取接口字段
  561. *
  562. * @param id 主键
  563. * @param objectMap 参数、参数值
  564. * @return
  565. */
  566. @Operation(summary = "获取接口字段")
  567. @Parameters({
  568. @Parameter(name = "id", description = "主键", required = true),
  569. @Parameter(name = "objectMap", description = "参数、参数值")
  570. })
  571. @PostMapping("/{id}/Actions/GetFields")
  572. public ActionResult getFields(@PathVariable("id") String id, @RequestBody(required = false) Map<String, Object> objectMap) {
  573. DataInterfacePage model = JsonUtil.getJsonToBean(objectMap, DataInterfacePage.class);
  574. ActionResult actionResult = dataInterfaceService.infoToIdPageList(id, model);
  575. if (actionResult.getCode() == 200) {
  576. try{
  577. Object data = actionResult.getData();
  578. if (data instanceof List) {
  579. List<Map<String,Object>> list=(List)data;
  580. List<String> listKey=new ArrayList();
  581. for(String key:list.get(0).keySet()){
  582. listKey.add(key);
  583. }
  584. actionResult.setData(listKey);
  585. }else{
  586. Map<String,Object> map=JsonUtil.stringToMap(JSONObject.toJSONString(data, SerializerFeature.WriteMapNullValue));
  587. List<Map<String,Object>> list=(List)map.get("list");
  588. List<String> listKey=new ArrayList();
  589. for(String key:list.get(0).keySet()){
  590. listKey.add(key);
  591. }
  592. actionResult.setData(listKey);
  593. }
  594. }catch (Exception e){
  595. return ActionResult.fail(MsgCode.SYS008.get());
  596. }
  597. }
  598. return actionResult;
  599. }
  600. /**
  601. * 复制数据接口
  602. *
  603. * @param id 数据接口ID
  604. * @return 执行结构
  605. * @throws DataException ignore
  606. */
  607. @Operation(summary = "复制数据接口")
  608. @Parameters({
  609. @Parameter(name = "id", description = "数据接口ID", required = true)
  610. })
  611. @SaCheckPermission("dataCenter.dataInterface")
  612. @PostMapping("/{id}/Actions/Copy")
  613. public ActionResult<?> Copy(@PathVariable("id") String id) throws DataException {
  614. String copyNum = UUID.randomUUID().toString().substring(0, 5);
  615. DataInterfaceEntity entity = dataInterfaceService.getInfo(id);
  616. entity.setFullName(entity.getFullName() + ".副本" + copyNum);
  617. if(entity.getFullName().length() > 50) return ActionResult.fail(MsgCode.COPY001.get());
  618. entity.setEnCode(entity.getEnCode() + copyNum);
  619. entity.setEnabledMark(0);
  620. dataInterfaceService.create(entity);
  621. return ActionResult.success(MsgCode.SU007.get());
  622. }
  623. }