1.0
xin
2025-04-15 719d2454c260d49cb9f92a1a73b31d98c1083d82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.oying.modules.maint.rest;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oying.modules.maint.domain.Deploy;
import com.oying.modules.maint.domain.DeployHistory;
import com.oying.modules.maint.domain.dto.DeployQueryCriteria;
import com.oying.modules.maint.service.DeployService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import com.oying.annotation.Log;
import com.oying.utils.FileUtil;
import com.oying.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
 
/**
* @author Z
* @date 2019-08-24
*/
@RestController
@Api(tags = "运维:部署管理")
@RequiredArgsConstructor
@RequestMapping("/api/deploy")
public class DeployController {
 
    private final String fileSavePath = FileUtil.getTmpDirPath()+"/";
    private final DeployService deployService;
 
    @ApiOperation("导出部署数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('database:list')")
    public void exportDeployData(HttpServletResponse response, DeployQueryCriteria criteria) throws IOException {
        deployService.download(deployService.queryAll(criteria), response);
    }
 
    @ApiOperation(value = "查询部署")
    @GetMapping
    @PreAuthorize("@el.check('deploy:list')")
    public ResponseEntity<PageResult<Deploy>> queryDeployData(DeployQueryCriteria criteria){
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(deployService.queryAll(criteria, page),HttpStatus.OK);
    }
 
    @Log("新增部署")
    @ApiOperation(value = "新增部署")
    @PostMapping
    @PreAuthorize("@el.check('deploy:add')")
    public ResponseEntity<Object> createDeploy(@Validated @RequestBody Deploy resources){
        deployService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
 
    @Log("修改部署")
    @ApiOperation(value = "修改部署")
    @PutMapping
    @PreAuthorize("@el.check('deploy:edit')")
    public ResponseEntity<Object> updateDeploy(@Validated @RequestBody Deploy resources){
        deployService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
 
    @Log("删除部署")
    @ApiOperation(value = "删除部署")
    @DeleteMapping
    @PreAuthorize("@el.check('deploy:del')")
    public ResponseEntity<Object> deleteDeploy(@RequestBody Set<Long> ids){
        deployService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
 
    @Log("上传文件部署")
    @ApiOperation(value = "上传文件部署")
    @PostMapping(value = "/upload")
    @PreAuthorize("@el.check('deploy:edit')")
    public ResponseEntity<Object> uploadDeploy(@RequestBody MultipartFile file, HttpServletRequest request)throws Exception{
        Long id = Long.valueOf(request.getParameter("id"));
        String fileName = "";
        if(file != null){
            fileName = FileUtil.verifyFilename(file.getOriginalFilename());
            File deployFile = new File(fileSavePath+fileName);
            FileUtil.del(deployFile);
            file.transferTo(deployFile);
            //文件下一步要根据文件名字来
            deployService.deploy(fileSavePath+fileName ,id);
        }else{
            System.out.println("没有找到相对应的文件");
        }
        System.out.println("文件上传的原名称为:"+ Objects.requireNonNull(file).getOriginalFilename());
        Map<String,Object> map = new HashMap<>(2);
        map.put("errno",0);
        map.put("id",fileName);
        return new ResponseEntity<>(map,HttpStatus.OK);
    }
 
    @Log("系统还原")
    @ApiOperation(value = "系统还原")
    @PostMapping(value = "/serverReduction")
    @PreAuthorize("@el.check('deploy:edit')")
    public ResponseEntity<String> serverReduction(@Validated @RequestBody DeployHistory resources){
        String result = deployService.serverReduction(resources);
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
 
    @Log("服务运行状态")
    @ApiOperation(value = "服务运行状态")
    @PostMapping(value = "/serverStatus")
    @PreAuthorize("@el.check('deploy:edit')")
    public ResponseEntity<String> serverStatus(@Validated @RequestBody Deploy resources){
        String result = deployService.serverStatus(resources);
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
 
    @Log("启动服务")
    @ApiOperation(value = "启动服务")
    @PostMapping(value = "/startServer")
    @PreAuthorize("@el.check('deploy:edit')")
    public ResponseEntity<String> startServer(@Validated @RequestBody Deploy resources){
        String result = deployService.startServer(resources);
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
 
    @Log("停止服务")
    @ApiOperation(value = "停止服务")
    @PostMapping(value = "/stopServer")
    @PreAuthorize("@el.check('deploy:edit')")
    public ResponseEntity<String> stopServer(@Validated @RequestBody Deploy resources){
        String result = deployService.stopServer(resources);
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
}