package com.qmth.ops.api.controller.admin; import com.baomidou.mybatisplus.core.metadata.IPage; import com.qmth.boot.api.annotation.Aac; import com.qmth.boot.api.annotation.BOOL; import com.qmth.boot.api.exception.ApiException; import com.qmth.boot.core.exception.ParameterException; import com.qmth.boot.tools.device.DeviceInfo; import com.qmth.ops.api.constants.OpsApiConstants; import com.qmth.ops.api.dto.DeployForm; import com.qmth.ops.api.security.AdminSession; import com.qmth.ops.api.security.Permission; import com.qmth.ops.api.vo.*; import com.qmth.ops.biz.domain.*; import com.qmth.ops.biz.query.DeployQuery; import com.qmth.ops.biz.query.OrgQuery; import com.qmth.ops.biz.service.*; import org.apache.commons.lang3.StringUtils; import org.springframework.http.HttpStatus; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @RestController @RequestMapping(OpsApiConstants.ADMIN_URI_PREFIX + "/deploy") public class DeployController { @Resource private DeployService deployService; @Resource private ControlParamService controlParamService; @Resource private AppService appService; @Resource private VersionService versionService; @Resource private FileService fileService; @Resource private LicenseService licenseService; @Resource private OrgService orgService; @RequestMapping("/modes") @Aac(auth = BOOL.FALSE) public Object getModes() { return Arrays.stream(DeployMode.values()).map(item -> new CodeNameVO(item.getCode(), item.getName())).toArray(); } @RequestMapping("/control/params") public List listAppControlParam(@RequestParam Long appId) { return controlParamService.listByAppId(appId); } @PostMapping("/query") public IPage query(DeployQuery query) { return deployService.query(query).convert(deploy -> new DeployVO(deploy, appService)); } @PostMapping("/list") public List listAppDeploy() { return deployService.listAppDeploy(); } @PostMapping("/insert") public DeployVO insert(@RequestAttribute AdminSession adminSession, @Validated(DeployForm.InsertGroup.class) @RequestBody DeployForm form) { adminSession.hasPermission(Permission.DEPLOY_INSERT); return new DeployVO(deployService.insert(form.build()), appService); } @PostMapping("/update") public DeployVO update(@RequestAttribute AdminSession adminSession, @Validated(DeployForm.UpdateGroup.class) @RequestBody DeployForm form) { adminSession.hasPermission(Permission.DEPLOY_EDIT, form.getId()); return new DeployVO(deployService.update(form.build()), appService); } @PostMapping("/secret") public SecretVO getSecret(@RequestAttribute AdminSession adminSession, @RequestParam Long id) { adminSession.hasPermission(Permission.DEPLOY_SECRET_VIEW, id); Deploy deploy = deployService.findById(id); return new SecretVO(deploy.getAccessKey(), deploy.getAccessSecret()); } @PostMapping("/device/save") public Object saveDevice(@RequestAttribute AdminSession adminSession, @RequestParam Long id, @RequestParam MultipartFile deviceInfo, @RequestParam(required = false) String remark) throws Exception { adminSession.hasPermission(Permission.DEPLOY_DEVICE_EDIT, id); Deploy deploy = deployService.findById(id); if (deploy != null) { DeviceInfo info = licenseService.parseDeviceInfo(deviceInfo); if (info == null) { throw new ApiException(HttpStatus.BAD_REQUEST, 400200, "DeviceInfo parse faile", null); } String deviceId = info.uuid(); fileService.uploadDeviceInfo(deviceId, info); deployService.saveDevice(deploy, deviceId, StringUtils.trimToEmpty(remark)); return new SuccessVO(true); } else { throw new ApiException(HttpStatus.BAD_REQUEST, 400100, "deploy unexists", null); } } @PostMapping("/device/delete") public Object deleteDevice(@RequestAttribute AdminSession adminSession, @RequestParam Long id, @RequestParam String deviceId) { adminSession.hasPermission(Permission.DEPLOY_DEVICE_EDIT, id); Deploy deploy = deployService.findById(id); if (deploy != null) { deployService.deleteDevice(deploy, deviceId); return new SuccessVO(true); } else { throw new ApiException(HttpStatus.BAD_REQUEST, 400100, "deploy unexists", null); } } @PostMapping("/device/list") public Object listDevice(@RequestParam Long id) { return deployService.listDevice(id); } @PostMapping("/device/info") public Object getDeviceInfo(@RequestParam String deviceId) throws Exception { return fileService.getDeviceInfo(deviceId).toString(); } @PostMapping("/license/download") public void licenseDownload(@RequestAttribute AdminSession adminSession, HttpServletResponse response, @RequestParam Long id, @RequestParam(required = false) String deviceId, @RequestParam(required = false) Long versionId) throws Exception { adminSession.hasPermission(Permission.DEPLOY_LICENSE_DOWNLOAD, id); Deploy deploy = deployService.findById(id); if (deploy == null) { throw new ParameterException("invalid deploy id"); } String versionName = null; if (versionId != null) { Version version = versionService.getById(versionId); if (version == null || version.getArchived() || !version.getAppId().equals(deploy.getAppId())) { throw new ParameterException("invalid versionId"); } versionName = version.getName(); } response.setContentType("application/octet-stream; charset=utf-8"); response.setHeader("Content-Disposition", "attachment; filename=app.lic"); licenseService.buildLicense(deploy, appService.getById(deploy.getAppId()), deviceId, versionName, response.getOutputStream()); } @PostMapping("/org/list") public List listOrg(@RequestParam Long id) { OrgQuery query = new OrgQuery(); query.setDeployId(id); query.setEnable(true); query.setPageNumber(1); query.setPageSize(Long.MAX_VALUE); return orgService.listByQuery(query).stream().map(OrgVO::new).collect(Collectors.toList()); } @PostMapping("/org/update") public Object updateOrg(@RequestAttribute AdminSession adminSession, @RequestParam Long id, @RequestParam Long[] orgId) { adminSession.hasPermission(Permission.DEPLOY_ORG_EDIT, id); Deploy deploy = deployService.findById(id); if (deploy != null) { deployService.updateOrg(deploy, orgId); return orgId; } else { throw new ApiException(HttpStatus.BAD_REQUEST, 400100, "deploy unexists", null); } } }