付延余
2022-12-16 f0f8ee8c4a945adbc742d9bab69382b28ad311fb
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.wgcloud.service;
 
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wgcloud.entity.HostGroup;
import com.wgcloud.entity.PortInfo;
import com.wgcloud.mapper.PortInfoMapper;
import com.wgcloud.util.DateUtil;
import com.wgcloud.util.HostUtil;
import com.wgcloud.util.UUIDUtil;
import com.wgcloud.util.staticvar.StaticKeys;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @version v3.3
 * @ClassName:PortInfoService.java
 * @author: http://www.wgstart.com
 * @date: 2021年1月16日
 * @Description: PortInfoService.java
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Service
public class PortInfoService {
 
    private static final Logger logger = LoggerFactory.getLogger(PortInfoService.class);
 
    public PageInfo selectByParams(Map<String, Object> params, int currPage, int pageSize) throws Exception {
        PageHelper.startPage(currPage, pageSize);
        List<PortInfo> list = portInfoMapper.selectByParams(params);
        PageInfo<PortInfo> pageInfo = new PageInfo<PortInfo>(list);
        return pageInfo;
    }
 
    public void save(PortInfo PortInfo, HttpServletRequest request) throws Exception {
        PortInfo.setId(UUIDUtil.getUUID());
        Date nowDate = DateUtil.getNowTime();
        PortInfo.setCreateTime(nowDate);
        if (!StringUtils.isEmpty(PortInfo.getPort())) {
            PortInfo.setPort(PortInfo.getPort().trim());
        }
        if (StringUtils.isEmpty(PortInfo.getPort())) {
            return;
        }
        portInfoMapper.save(PortInfo);
        //保存页面动态添加的端口数据
        addExtDataForm(PortInfo, request, nowDate);
    }
 
    /**
     * 保存页面动态添加的端口数据
     *
     * @param portInfo
     * @param request
     */
    private void addExtDataForm(PortInfo portInfo, HttpServletRequest request, Date nowDate) throws Exception {
        String dataFromIndex = request.getParameter("dataFromIndex");
        int rowsLen = 0;
        if (!StringUtils.isEmpty(dataFromIndex)) {
            for (int i = 0; i <= Integer.valueOf(dataFromIndex); i++) {
 
                String port = request.getParameter("port_" + i);
                String portName = request.getParameter("portName_" + i);
                if (StringUtils.isEmpty(port) || StringUtils.isEmpty(portName)) {
                    continue;
                }
                PortInfo portInfoExt = new PortInfo();
                BeanUtil.copyProperties(portInfo, portInfoExt, true);
                portInfoExt.setId(UUIDUtil.getUUID());
                portInfoExt.setCreateTime(nowDate);
                portInfoExt.setPort(port.trim());
                portInfoExt.setPortName(portName.trim());
                portInfoMapper.save(portInfoExt);
 
                saveLog(request, StaticKeys.LOG_ADD, portInfoExt);
                rowsLen += 1;
            }
        }
    }
 
    public int deleteByHostName(Map<String, Object> map) throws Exception {
        return portInfoMapper.deleteByHostName(map);
    }
 
    @Transactional
    public void saveRecord(List<PortInfo> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        for (PortInfo as : recordList) {
            as.setId(UUIDUtil.getUUID());
        }
        portInfoMapper.insertList(recordList);
    }
 
    public void downByHostName(List<String> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        portInfoMapper.downByHostName(recordList);
    }
 
    public int countByParams(Map<String, Object> params) throws Exception {
        return portInfoMapper.countByParams(params);
    }
 
    @Transactional
    public int deleteById(String[] id) throws Exception {
        return portInfoMapper.deleteById(id);
    }
 
    @Transactional
    public void updateRecord(List<PortInfo> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        portInfoMapper.updateList(recordList);
    }
 
    public void updateById(PortInfo PortInfo)
            throws Exception {
        portInfoMapper.updateById(PortInfo);
    }
 
    public PortInfo selectById(String id) throws Exception {
        return portInfoMapper.selectById(id);
    }
 
    public List<PortInfo> selectAllByParams(Map<String, Object> params) throws Exception {
        return portInfoMapper.selectAllByParams(params);
    }
 
    /**
     * 设置资源分组 begin
     *
     * @param recordList
     */
    public List<HostGroup> setGroupInList(List<PortInfo> recordList) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("groupType", StaticKeys.HOST_GROUP_1);
        List<HostGroup> hostGroupList = hostGroupService.selectAllByParams(params);
        //查询所有主机分组列表,封装为map
        Map<String, String> hostGroupMap = new HashMap<>();
        for (HostGroup hostGroup : hostGroupList) {
            hostGroupMap.put(hostGroup.getId(), hostGroup.getGroupName());
        }
        for (PortInfo portInfo : recordList) {
            if (!StringUtils.isEmpty(portInfo.getGroupId())) {
                portInfo.setGroupId(hostGroupMap.get(portInfo.getGroupId()));
            }
        }
        return hostGroupList;
    }
 
    /**
     * 保存操作日志
     *
     * @param request 获取当前登录用户
     * @param action  操作标识
     */
    public void saveLog(HttpServletRequest request, String action, PortInfo portInfo) {
        if (null == portInfo) {
            return;
        }
        logInfoService.save(HostUtil.getAccountByRequest(request).getAccount() + action + "端口监测信息:" + portInfo.getHostname() + "," + portInfo.getPortName(),
                "telnet " + portInfo.getTelnetIp() + " " + portInfo.getPort(), StaticKeys.LOG_XTCZ);
    }
 
    @Autowired
    private PortInfoMapper portInfoMapper;
    @Resource
    private HostGroupService hostGroupService;
    @Autowired
    private LogInfoService logInfoService;
 
 
}