ViewController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2019 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * >>
  17. *
  18. */
  19. package edp.davinci.controller;
  20. import edp.core.annotation.CurrentUser;
  21. import edp.core.model.Paginate;
  22. import edp.core.model.PaginateWithQueryColumns;
  23. import edp.core.utils.CollectionUtils;
  24. import edp.davinci.common.controller.BaseController;
  25. import edp.davinci.core.common.Constants;
  26. import edp.davinci.core.common.ResultMap;
  27. import edp.davinci.core.utils.DacChannelUtil;
  28. import edp.davinci.dto.viewDto.*;
  29. import edp.davinci.model.DacChannel;
  30. import edp.davinci.model.QualityTask;
  31. import edp.davinci.model.User;
  32. import edp.davinci.service.QualityTaskService;
  33. import edp.davinci.service.ViewService;
  34. import io.swagger.annotations.Api;
  35. import io.swagger.annotations.ApiOperation;
  36. import io.swagger.annotations.ApiResponse;
  37. import io.swagger.annotations.ApiResponses;
  38. import lombok.extern.slf4j.Slf4j;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import org.springframework.http.MediaType;
  41. import org.springframework.http.ResponseEntity;
  42. import org.springframework.validation.BindingResult;
  43. import org.springframework.web.bind.annotation.*;
  44. import springfox.documentation.annotations.ApiIgnore;
  45. import javax.servlet.http.HttpServletRequest;
  46. import javax.validation.Valid;
  47. import java.sql.SQLException;
  48. import java.util.Collections;
  49. import java.util.List;
  50. import java.util.Map;
  51. @Api(value = "/views", tags = "views", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  52. @ApiResponses(@ApiResponse(code = 404, message = "view not found"))
  53. @Slf4j
  54. @RestController
  55. @RequestMapping(value = Constants.BASE_API_PATH + "/views", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  56. public class ViewController extends BaseController
  57. {
  58. @Autowired
  59. private ViewService viewService;
  60. @Autowired
  61. private QualityTaskService qualityTaskService;
  62. @Autowired
  63. private DacChannelUtil dacChannelUtil;
  64. /**
  65. * 获取view
  66. *
  67. * @param projectId
  68. * @param user
  69. * @param request
  70. * @return
  71. */
  72. @ApiOperation(value = "get views")
  73. @GetMapping
  74. public ResponseEntity getViews(@RequestParam Long projectId,
  75. @ApiIgnore @CurrentUser User user,
  76. HttpServletRequest request)
  77. {
  78. if (invalidId(projectId))
  79. {
  80. ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message("Invalid project id");
  81. return ResponseEntity.status(resultMap.getCode()).body(resultMap);
  82. }
  83. List<ViewBaseInfo> views = viewService.getViews(projectId, user);
  84. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payloads(views));
  85. }
  86. /**
  87. * 获取view
  88. *
  89. * @param projectId
  90. * @param parentId
  91. * @param user
  92. * @param request
  93. * @return
  94. */
  95. @ApiOperation(value = "get ViewsByParentId")
  96. @GetMapping(value = "/getViewsByParentId")
  97. public ResponseEntity getViewsByParentId(@RequestParam Long projectId,
  98. @RequestParam Long parentId,
  99. @ApiIgnore @CurrentUser User user,
  100. HttpServletRequest request)
  101. {
  102. if (invalidId(projectId))
  103. {
  104. ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message("Invalid project id");
  105. return ResponseEntity.status(resultMap.getCode()).body(resultMap);
  106. }
  107. List<ViewBaseInfo> views = viewService.getViewBaseInfoByParentId(projectId, parentId, user);
  108. for(ViewBaseInfo viewBaseInfo : views){
  109. List<QualityTask> qualityTasks = qualityTaskService.getQualityTaskListByViewId(viewBaseInfo.getId());
  110. if(!CollectionUtils.isEmpty(qualityTasks)){
  111. QualityTask qualityTask = qualityTasks.get(0);
  112. viewBaseInfo.setMetadataConfig(qualityTask.getMetadataConfig());
  113. viewBaseInfo.setRelationQuality(true);
  114. }
  115. }
  116. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payloads(views));
  117. }
  118. /**
  119. * get view info
  120. *
  121. * @param id
  122. * @param user
  123. * @param request
  124. * @return
  125. */
  126. @ApiOperation(value = "get view info")
  127. @GetMapping("/{id}")
  128. public ResponseEntity getView(@PathVariable Long id,
  129. @ApiIgnore @CurrentUser User user,
  130. HttpServletRequest request)
  131. {
  132. if (invalidId(id))
  133. {
  134. ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message("Invalid view id");
  135. return ResponseEntity.status(resultMap.getCode()).body(resultMap);
  136. }
  137. ViewWithSourceBaseInfo view = viewService.getView(id, user);
  138. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(view));
  139. }
  140. /**
  141. * 新建view
  142. *
  143. * @param view
  144. * @param bindingResult
  145. * @param user
  146. * @param request
  147. * @return
  148. */
  149. @ApiOperation(value = "create view")
  150. @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
  151. public ResponseEntity createView(@Valid @RequestBody ViewCreate view,
  152. @ApiIgnore BindingResult bindingResult,
  153. @ApiIgnore @CurrentUser User user,
  154. HttpServletRequest request)
  155. {
  156. if (bindingResult.hasErrors())
  157. {
  158. ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message(bindingResult.getFieldErrors().get(0).getDefaultMessage());
  159. return ResponseEntity.status(resultMap.getCode()).body(resultMap);
  160. }
  161. ViewWithSourceBaseInfo viewWithSourceBaseInfo = viewService.createView(view, user);
  162. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(viewWithSourceBaseInfo));
  163. }
  164. /**
  165. * 修改View
  166. *
  167. * @param id
  168. * @param viewUpdate
  169. * @param bindingResult
  170. * @param user
  171. * @param request
  172. * @return
  173. */
  174. @ApiOperation(value = "update view")
  175. @PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
  176. public ResponseEntity updateView(@PathVariable Long id,
  177. @Valid @RequestBody ViewUpdate viewUpdate,
  178. @ApiIgnore BindingResult bindingResult,
  179. @ApiIgnore @CurrentUser User user,
  180. HttpServletRequest request)
  181. {
  182. if (invalidId(id) || !id.equals(viewUpdate.getId()))
  183. {
  184. ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message("Invalid view id");
  185. return ResponseEntity.status(resultMap.getCode()).body(resultMap);
  186. }
  187. if (bindingResult.hasErrors())
  188. {
  189. ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message(bindingResult.getFieldErrors().get(0).getDefaultMessage());
  190. return ResponseEntity.status(resultMap.getCode()).body(resultMap);
  191. }
  192. viewService.updateView(viewUpdate, user);
  193. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request));
  194. }
  195. /**
  196. * 删除View
  197. *
  198. * @param id
  199. * @param user
  200. * @param request
  201. * @return
  202. */
  203. @ApiOperation(value = "delete view")
  204. @DeleteMapping("/{id}")
  205. public ResponseEntity deleteView(@PathVariable Long id,
  206. @ApiIgnore @CurrentUser User user,
  207. HttpServletRequest request)
  208. {
  209. if (invalidId(id))
  210. {
  211. ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message("Invalid view id");
  212. return ResponseEntity.status(resultMap.getCode()).body(resultMap);
  213. }
  214. viewService.deleteView(id, user);
  215. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request));
  216. }
  217. /**
  218. * 执行sql
  219. *
  220. * @param executeSql
  221. * @param bindingResult
  222. * @param user
  223. * @param request
  224. * @return
  225. */
  226. @ApiOperation(value = "executesql")
  227. @PostMapping(value = "/executesql", consumes = MediaType.APPLICATION_JSON_VALUE)
  228. public ResponseEntity executeSql(@Valid @RequestBody ViewExecuteSql executeSql,
  229. @ApiIgnore BindingResult bindingResult,
  230. @ApiIgnore @CurrentUser User user,
  231. HttpServletRequest request)
  232. {
  233. if (bindingResult.hasErrors())
  234. {
  235. ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message(bindingResult.getFieldErrors().get(0).getDefaultMessage());
  236. return ResponseEntity.status(resultMap.getCode()).body(resultMap);
  237. }
  238. PaginateWithQueryColumns paginateWithQueryColumns = viewService.executeSql(executeSql, user);
  239. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(paginateWithQueryColumns));
  240. }
  241. /**
  242. * 获取当前view对应的源数据
  243. *
  244. * @param id
  245. * @param executeParam
  246. * @param user
  247. * @param request
  248. * @return
  249. */
  250. @ApiOperation(value = "get data")
  251. @PostMapping(value = "/{id}/getdata", consumes = MediaType.APPLICATION_JSON_VALUE)
  252. public ResponseEntity getData(@PathVariable Long id,
  253. @RequestBody(required = false) ViewExecuteParam executeParam,
  254. @ApiIgnore @CurrentUser User user,
  255. HttpServletRequest request) throws SQLException
  256. {
  257. if (invalidId(id))
  258. {
  259. ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message("Invalid view id");
  260. return ResponseEntity.status(resultMap.getCode()).body(resultMap);
  261. }
  262. Paginate<Map<String, Object>> paginate = viewService.getData(id, executeParam, user);
  263. return ResponseEntity.ok().body(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(paginate));
  264. }
  265. @ApiOperation(value = "get distinct value")
  266. @PostMapping(value = "/{id}/getdistinctvalue", consumes = MediaType.APPLICATION_JSON_VALUE)
  267. public ResponseEntity getDistinctValue(@PathVariable Long id,
  268. @Valid @RequestBody DistinctParam param,
  269. @ApiIgnore BindingResult bindingResult,
  270. @ApiIgnore @CurrentUser User user,
  271. HttpServletRequest request)
  272. {
  273. if (invalidId(id))
  274. {
  275. ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message("Invalid view id");
  276. return ResponseEntity.status(resultMap.getCode()).body(resultMap);
  277. }
  278. if (bindingResult.hasErrors())
  279. {
  280. ResultMap resultMap = new ResultMap(tokenUtils).failAndRefreshToken(request).message(bindingResult.getFieldErrors().get(0).getDefaultMessage());
  281. return ResponseEntity.status(resultMap.getCode()).body(resultMap);
  282. }
  283. List<Map<String, Object>> distinctValue = viewService.getDistinctValue(id, param, user);
  284. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payloads(distinctValue));
  285. }
  286. @ApiOperation(value = "get dac channels")
  287. @GetMapping("/dac/channels")
  288. public ResponseEntity getDacChannels(@ApiIgnore @CurrentUser User user, HttpServletRequest request)
  289. {
  290. Map<String, DacChannel> dacMap = DacChannelUtil.dacMap;
  291. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payloads(dacMap.keySet()));
  292. }
  293. @ApiOperation(value = "get dac tenants")
  294. @GetMapping("/dac/{dacName}/tenants")
  295. public ResponseEntity getDacTenants(@PathVariable String dacName, @ApiIgnore @CurrentUser User user, HttpServletRequest request)
  296. {
  297. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payloads(dacChannelUtil.getTenants(dacName)));
  298. }
  299. @ApiOperation(value = "get dac bizs")
  300. @GetMapping("/dac/{dacName}/tenants/{tenantId}/bizs")
  301. public ResponseEntity getDacBizs(@PathVariable String dacName,
  302. @PathVariable String tenantId,
  303. @ApiIgnore @CurrentUser User user,
  304. HttpServletRequest request)
  305. {
  306. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payloads(dacChannelUtil.getBizs(dacName, tenantId)));
  307. }
  308. }