付延余
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
package com.wgcloud.service;
 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wgcloud.entity.ShellInfo;
import com.wgcloud.entity.ShellState;
import com.wgcloud.mapper.ShellInfoMapper;
import com.wgcloud.mapper.ShellStateMapper;
import com.wgcloud.util.DateUtil;
import com.wgcloud.util.UUIDUtil;
import com.wgcloud.util.staticvar.StaticKeys;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * @version v3.3
 * @ClassName:ShellInfoService.java
 * @author: http://www.wgstart.com
 * @date: 2021年8月19日
 * @Description: ShellInfoService.java
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Service
public class ShellInfoService {
 
    public PageInfo selectByParams(Map<String, Object> params, int currPage, int pageSize) throws Exception {
        PageHelper.startPage(currPage, pageSize);
        List<ShellInfo> list = shellInfoMapper.selectByParams(params);
        PageInfo<ShellInfo> pageInfo = new PageInfo<ShellInfo>(list);
        return pageInfo;
    }
 
    public void save(ShellInfo shellInfo, String[] hostnames) throws Exception {
        shellInfo.setId(UUIDUtil.getUUID());
        shellInfo.setState(StaticKeys.SHELL_STATE_1);
        shellInfo.setCreateTime(DateUtil.getNowTime());
        if (!StringUtils.isEmpty(shellInfo.getShell())) {
            shellInfo.setShell(shellInfo.getShell().trim());
        }
        List<ShellState> shellStateList = new ArrayList<>();
        for (String hostname : hostnames) {
            ShellState shellState = new ShellState();
            shellState.setShellId(shellInfo.getId());
            shellState.setHostname(hostname);
            shellState.setCreateTime(shellInfo.getCreateTime());
            shellState.setShell(shellInfo.getShell());
            shellState.setState(StaticKeys.SHELL_RUN_STATE_1);
            if (StaticKeys.SHELL_TYPE_2.equals(shellInfo.getShellType())) {
                shellState.setShellTime(DateUtil.getDate(shellInfo.getShellTime()).getTime() + "");
            } else {
                shellState.setShellTime("0");
            }
            shellStateList.add(shellState);
        }
        shellInfoMapper.save(shellInfo);
        shellStateService.saveRecord(shellStateList);
    }
 
 
    @Transactional
    public void saveRecord(List<ShellInfo> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        for (ShellInfo as : recordList) {
            as.setId(UUIDUtil.getUUID());
        }
        shellInfoMapper.insertList(recordList);
    }
 
    public int countByParams(Map<String, Object> params) throws Exception {
        return shellInfoMapper.countByParams(params);
    }
 
    @Transactional
    public int deleteById(String[] id) throws Exception {
        for (String shellInfoId : id) {
            shellStateMapper.cancelByShellId(shellInfoId);
        }
        return shellInfoMapper.deleteById(id);
    }
 
    @Transactional
    public void updateRecord(List<ShellInfo> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        shellInfoMapper.updateList(recordList);
    }
 
    public void updateById(ShellInfo ShellInfo) throws Exception {
        shellInfoMapper.updateById(ShellInfo);
    }
 
    @Transactional
    public void cancelShell(String id) throws Exception {
        ShellInfo shellInfo = shellInfoMapper.selectById(id);
        shellInfo.setState(StaticKeys.SHELL_STATE_2);
        shellInfoMapper.updateById(shellInfo);
        shellStateMapper.cancelByShellId(id);
    }
 
    @Transactional
    public void restart(String id) throws Exception {
        ShellInfo shellInfo = shellInfoMapper.selectById(id);
        shellInfo.setState(StaticKeys.SHELL_STATE_1);
        shellInfo.setCreateTime(new Date());
        //设置下发时间 begin
       /* if (StaticKeys.SHELL_TYPE_2.equals(shellInfo.getShellType()) && !StringUtils.isEmpty(shellInfo.getShellTime())) {
            shellInfo.setShellTime(DateUtil.getTargetTime(shellInfo.getShellTime(), shellInfo.getShellDay()));
        }*/
        //设置下发时间 end
        shellInfoMapper.updateById(shellInfo);
        String shellTime = "0";
        if (StaticKeys.SHELL_TYPE_2.equals(shellInfo.getShellType())) {
            shellTime = DateUtil.getDate(shellInfo.getShellTime()).getTime() + "";
        }
        shellStateMapper.restartByShellId(id, shellTime);
    }
 
    public ShellInfo selectById(String id) throws Exception {
        return shellInfoMapper.selectById(id);
    }
 
    public List<ShellInfo> selectAllByParams(Map<String, Object> params) throws Exception {
        return shellInfoMapper.selectAllByParams(params);
    }
 
 
    @Autowired
    private ShellInfoMapper shellInfoMapper;
    @Autowired
    private ShellStateMapper shellStateMapper;
    @Autowired
    private ShellStateService shellStateService;
 
 
}