xin
2025-04-15 8342e6a5ed78fc007c45609507f0a914c43fec4d
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
package com.oying.modules.maint.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oying.modules.maint.domain.Server;
import com.oying.modules.maint.domain.dto.ServerQueryCriteria;
import com.oying.modules.maint.mapper.DeployServerMapper;
import com.oying.modules.maint.mapper.ServerMapper;
import lombok.RequiredArgsConstructor;
import com.oying.modules.maint.service.ServerService;
import com.oying.modules.maint.util.ExecuteShellUtil;
import com.oying.utils.FileUtil;
import com.oying.utils.PageResult;
import com.oying.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
 
/**
* @author Z
* @date 2019-08-24
*/
@Service
@RequiredArgsConstructor
public class ServerServiceImpl extends ServiceImpl<ServerMapper, Server> implements ServerService {
 
    private final ServerMapper serverMapper;
    private final DeployServerMapper deployServerMapper;
 
    @Override
    public PageResult<Server> queryAll(ServerQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(serverMapper.findAll(criteria, page));
    }
 
    @Override
    public List<Server> queryAll(ServerQueryCriteria criteria){
        return serverMapper.findAll(criteria);
    }
 
    @Override
    public Server findByIp(String ip) {
        return serverMapper.findByIp(ip);
    }
 
    @Override
    public Boolean testConnect(Server resources) {
        ExecuteShellUtil executeShellUtil = null;
        try {
            executeShellUtil = new ExecuteShellUtil(resources.getIp(), resources.getAccount(), resources.getPassword(),resources.getPort());
            return executeShellUtil.execute("ls")==0;
        } catch (Exception e) {
            return false;
        }finally {
            if (executeShellUtil != null) {
                executeShellUtil.close();
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Server resources) {
        save(resources);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Server resources) {
        Server server = getById(resources.getId());
        server.copy(resources);
        saveOrUpdate(server);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Set<Long> ids) {
        removeBatchByIds(ids);
        // 删除与之关联的服务
        deployServerMapper.deleteByServerIds(ids);
    }
 
    @Override
    public void download(List<Server> servers, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Server deploy : servers) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("服务器名称", deploy.getName());
            map.put("服务器IP", deploy.getIp());
            map.put("端口", deploy.getPort());
            map.put("账号", deploy.getAccount());
            map.put("创建日期", deploy.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}