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;
|
|
|
}
|