DeployController.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.qmth.ops.api.controller.admin;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.qmth.boot.api.annotation.Aac;
  4. import com.qmth.boot.api.annotation.BOOL;
  5. import com.qmth.boot.api.exception.ApiException;
  6. import com.qmth.boot.core.exception.ParameterException;
  7. import com.qmth.boot.tools.device.DeviceInfo;
  8. import com.qmth.ops.api.constants.OpsApiConstants;
  9. import com.qmth.ops.api.dto.DeployForm;
  10. import com.qmth.ops.api.security.AdminSession;
  11. import com.qmth.ops.api.security.Permission;
  12. import com.qmth.ops.api.vo.*;
  13. import com.qmth.ops.biz.domain.*;
  14. import com.qmth.ops.biz.query.DeployQuery;
  15. import com.qmth.ops.biz.query.OrgQuery;
  16. import com.qmth.ops.biz.service.*;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.http.HttpStatus;
  19. import org.springframework.validation.annotation.Validated;
  20. import org.springframework.web.bind.annotation.*;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import javax.annotation.Resource;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.util.Arrays;
  25. import java.util.List;
  26. import java.util.stream.Collectors;
  27. @RestController
  28. @RequestMapping(OpsApiConstants.ADMIN_URI_PREFIX + "/deploy")
  29. public class DeployController {
  30. @Resource
  31. private DeployService deployService;
  32. @Resource
  33. private ControlParamService controlParamService;
  34. @Resource
  35. private AppService appService;
  36. @Resource
  37. private VersionService versionService;
  38. @Resource
  39. private FileService fileService;
  40. @Resource
  41. private LicenseService licenseService;
  42. @Resource
  43. private OrgService orgService;
  44. @RequestMapping("/modes")
  45. @Aac(auth = BOOL.FALSE)
  46. public Object getModes() {
  47. return Arrays.stream(DeployMode.values()).map(item -> new CodeNameVO(item.getCode(), item.getName())).toArray();
  48. }
  49. @RequestMapping("/control/params")
  50. public List<ControlParam> listAppControlParam(@RequestParam Long appId) {
  51. return controlParamService.listByAppId(appId);
  52. }
  53. @PostMapping("/query")
  54. public IPage<DeployVO> query(DeployQuery query) {
  55. return deployService.query(query).convert(deploy -> new DeployVO(deploy, appService));
  56. }
  57. @PostMapping("/list")
  58. public List<AppDeploy> listAppDeploy() {
  59. return deployService.listAppDeploy();
  60. }
  61. @PostMapping("/insert")
  62. public DeployVO insert(@RequestAttribute AdminSession adminSession,
  63. @Validated(DeployForm.InsertGroup.class) @RequestBody DeployForm form) {
  64. adminSession.hasPermission(Permission.DEPLOY_INSERT);
  65. return new DeployVO(deployService.insert(form.build()), appService);
  66. }
  67. @PostMapping("/update")
  68. public DeployVO update(@RequestAttribute AdminSession adminSession,
  69. @Validated(DeployForm.UpdateGroup.class) @RequestBody DeployForm form) {
  70. adminSession.hasPermission(Permission.DEPLOY_EDIT, form.getId());
  71. return new DeployVO(deployService.update(form.build()), appService);
  72. }
  73. @PostMapping("/secret")
  74. public SecretVO getSecret(@RequestAttribute AdminSession adminSession, @RequestParam Long id) {
  75. adminSession.hasPermission(Permission.DEPLOY_SECRET_VIEW, id);
  76. Deploy deploy = deployService.findById(id);
  77. return new SecretVO(deploy.getAccessKey(), deploy.getAccessSecret());
  78. }
  79. @PostMapping("/device/save")
  80. public Object saveDevice(@RequestAttribute AdminSession adminSession, @RequestParam Long id,
  81. @RequestParam MultipartFile deviceInfo, @RequestParam(required = false) String remark) throws Exception {
  82. adminSession.hasPermission(Permission.DEPLOY_DEVICE_EDIT, id);
  83. Deploy deploy = deployService.findById(id);
  84. if (deploy != null) {
  85. DeviceInfo info = licenseService.parseDeviceInfo(deviceInfo);
  86. if (info == null) {
  87. throw new ApiException(HttpStatus.BAD_REQUEST, 400200, "DeviceInfo parse faile", null);
  88. }
  89. String deviceId = info.uuid();
  90. fileService.uploadDeviceInfo(deviceId, info);
  91. deployService.saveDevice(deploy, deviceId, StringUtils.trimToEmpty(remark));
  92. return new SuccessVO(true);
  93. } else {
  94. throw new ApiException(HttpStatus.BAD_REQUEST, 400100, "deploy unexists", null);
  95. }
  96. }
  97. @PostMapping("/device/delete")
  98. public Object deleteDevice(@RequestAttribute AdminSession adminSession, @RequestParam Long id,
  99. @RequestParam String deviceId) {
  100. adminSession.hasPermission(Permission.DEPLOY_DEVICE_EDIT, id);
  101. Deploy deploy = deployService.findById(id);
  102. if (deploy != null) {
  103. deployService.deleteDevice(deploy, deviceId);
  104. return new SuccessVO(true);
  105. } else {
  106. throw new ApiException(HttpStatus.BAD_REQUEST, 400100, "deploy unexists", null);
  107. }
  108. }
  109. @PostMapping("/device/list")
  110. public Object listDevice(@RequestParam Long id) {
  111. return deployService.listDevice(id);
  112. }
  113. @PostMapping("/device/info")
  114. public Object getDeviceInfo(@RequestParam String deviceId) throws Exception {
  115. return fileService.getDeviceInfo(deviceId).toString();
  116. }
  117. @PostMapping("/license/download")
  118. public void licenseDownload(@RequestAttribute AdminSession adminSession, HttpServletResponse response,
  119. @RequestParam Long id, @RequestParam(required = false) String deviceId,
  120. @RequestParam(required = false) Long versionId) throws Exception {
  121. adminSession.hasPermission(Permission.DEPLOY_LICENSE_DOWNLOAD, id);
  122. Deploy deploy = deployService.findById(id);
  123. if (deploy == null) {
  124. throw new ParameterException("invalid deploy id");
  125. }
  126. String versionName = null;
  127. if (versionId != null) {
  128. Version version = versionService.getById(versionId);
  129. if (version == null || version.getArchived() || !version.getAppId().equals(deploy.getAppId())) {
  130. throw new ParameterException("invalid versionId");
  131. }
  132. versionName = version.getName();
  133. }
  134. response.setContentType("application/octet-stream; charset=utf-8");
  135. response.setHeader("Content-Disposition", "attachment; filename=app.lic");
  136. licenseService.buildLicense(deploy, appService.getById(deploy.getAppId()), deviceId, versionName,
  137. response.getOutputStream());
  138. }
  139. @PostMapping("/org/list")
  140. public List<OrgVO> listOrg(@RequestParam Long id) {
  141. OrgQuery query = new OrgQuery();
  142. query.setDeployId(id);
  143. query.setEnable(true);
  144. query.setPageNumber(1);
  145. query.setPageSize(Long.MAX_VALUE);
  146. return orgService.listByQuery(query).stream().map(OrgVO::new).collect(Collectors.toList());
  147. }
  148. @PostMapping("/org/update")
  149. public Object updateOrg(@RequestAttribute AdminSession adminSession, @RequestParam Long id,
  150. @RequestParam Long[] orgId) {
  151. adminSession.hasPermission(Permission.DEPLOY_ORG_EDIT, id);
  152. Deploy deploy = deployService.findById(id);
  153. if (deploy != null) {
  154. deployService.updateOrg(deploy, orgId);
  155. return orgId;
  156. } else {
  157. throw new ApiException(HttpStatus.BAD_REQUEST, 400100, "deploy unexists", null);
  158. }
  159. }
  160. }