Commit 6d941c09 authored by dqjdda's avatar dqjdda
Browse files

代码优化

parent 2853f394
...@@ -2,6 +2,8 @@ package me.zhengjie.modules.mnt.service.dto; ...@@ -2,6 +2,8 @@ package me.zhengjie.modules.mnt.service.dto;
import lombok.Data; import lombok.Data;
import me.zhengjie.annotation.Query; import me.zhengjie.annotation.Query;
import java.sql.Timestamp;
import java.util.List;
/** /**
* @author zhanghouying * @author zhanghouying
...@@ -13,13 +15,10 @@ public class DeployQueryCriteria{ ...@@ -13,13 +15,10 @@ public class DeployQueryCriteria{
/** /**
* 模糊 * 模糊
*/ */
@Query(type = Query.Type.EQUAL) @Query(type = Query.Type.INNER_LIKE, propName = "name", joinName = "app")
private String appId; private String appName;
/** @Query(type = Query.Type.BETWEEN)
* 模糊 private List<Timestamp> createTime;
*/
@Query(type = Query.Type.INNER_LIKE)
private String ip;
} }
...@@ -14,7 +14,6 @@ import org.springframework.data.domain.Pageable; ...@@ -14,7 +14,6 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/** /**
* @author zhanghouying * @author zhanghouying
...@@ -46,9 +45,9 @@ public class AppServiceImpl implements AppService { ...@@ -46,9 +45,9 @@ public class AppServiceImpl implements AppService {
@Override @Override
public AppDTO findById(Long id) { public AppDTO findById(Long id) {
Optional<App> app = appRepository.findById(id); App app = appRepository.findById(id).orElseGet(App::new);
ValidationUtil.isNull(app,"App","id",id); ValidationUtil.isNull(app.getId(),"App","id",id);
return appMapper.toDto(app.get()); return appMapper.toDto(app);
} }
@Override @Override
...@@ -60,11 +59,10 @@ public class AppServiceImpl implements AppService { ...@@ -60,11 +59,10 @@ public class AppServiceImpl implements AppService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void update(App resources) { public void update(App resources) {
Optional<App> optionalApp = appRepository.findById(resources.getId()); App app = appRepository.findById(resources.getId()).orElseGet(App::new);
ValidationUtil.isNull( optionalApp,"App","id",resources.getId()); ValidationUtil.isNull(app.getId(),"App","id",resources.getId());
App App = optionalApp.get(); app.copy(resources);
App.copy(resources); appRepository.save(app);
appRepository.save(App);
} }
@Override @Override
......
...@@ -16,8 +16,6 @@ import org.springframework.stereotype.Service; ...@@ -16,8 +16,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/** /**
* @author zhanghouying * @author zhanghouying
* @date 2019-08-24 * @date 2019-08-24
...@@ -48,9 +46,9 @@ public class DatabaseServiceImpl implements DatabaseService { ...@@ -48,9 +46,9 @@ public class DatabaseServiceImpl implements DatabaseService {
@Override @Override
public DatabaseDTO findById(String id) { public DatabaseDTO findById(String id) {
Optional<Database> database = databaseRepository.findById(id); Database database = databaseRepository.findById(id).orElseGet(Database::new);
ValidationUtil.isNull(database,"Database","id",id); ValidationUtil.isNull(database.getId(),"Database","id",id);
return databaseMapper.toDto(database.get()); return databaseMapper.toDto(database);
} }
@Override @Override
...@@ -63,9 +61,8 @@ public class DatabaseServiceImpl implements DatabaseService { ...@@ -63,9 +61,8 @@ public class DatabaseServiceImpl implements DatabaseService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void update(Database resources) { public void update(Database resources) {
Optional<Database> optionalDatabase = databaseRepository.findById(resources.getId()); Database database = databaseRepository.findById(resources.getId()).orElseGet(Database::new);
ValidationUtil.isNull( optionalDatabase,"Database","id",resources.getId()); ValidationUtil.isNull(database.getId(),"Database","id",resources.getId());
Database database = optionalDatabase.get();
database.copy(resources); database.copy(resources);
databaseRepository.save(database); databaseRepository.save(database);
} }
......
...@@ -10,15 +10,12 @@ import me.zhengjie.modules.mnt.service.mapper.DeployHistoryMapper; ...@@ -10,15 +10,12 @@ import me.zhengjie.modules.mnt.service.mapper.DeployHistoryMapper;
import me.zhengjie.utils.PageUtil; import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp; import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/** /**
* @author zhanghouying * @author zhanghouying
* @date 2019-08-24 * @date 2019-08-24
...@@ -27,11 +24,14 @@ import java.util.Optional; ...@@ -27,11 +24,14 @@ import java.util.Optional;
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeployHistoryServiceImpl implements DeployHistoryService { public class DeployHistoryServiceImpl implements DeployHistoryService {
@Autowired private final DeployHistoryRepository deployhistoryRepository;
private DeployHistoryRepository deployhistoryRepository;
private final DeployHistoryMapper deployhistoryMapper;
@Autowired public DeployHistoryServiceImpl(DeployHistoryRepository deployhistoryRepository, DeployHistoryMapper deployhistoryMapper) {
private DeployHistoryMapper deployhistoryMapper; this.deployhistoryRepository = deployhistoryRepository;
this.deployhistoryMapper = deployhistoryMapper;
}
@Override @Override
public Object queryAll(DeployHistoryQueryCriteria criteria, Pageable pageable){ public Object queryAll(DeployHistoryQueryCriteria criteria, Pageable pageable){
...@@ -46,9 +46,9 @@ public class DeployHistoryServiceImpl implements DeployHistoryService { ...@@ -46,9 +46,9 @@ public class DeployHistoryServiceImpl implements DeployHistoryService {
@Override @Override
public DeployHistoryDTO findById(String id) { public DeployHistoryDTO findById(String id) {
Optional<DeployHistory> deployhistory = deployhistoryRepository.findById(id); DeployHistory deployhistory = deployhistoryRepository.findById(id).orElseGet(DeployHistory::new);
ValidationUtil.isNull(deployhistory,"DeployHistory","id",id); ValidationUtil.isNull(deployhistory.getId(),"DeployHistory","id",id);
return deployhistoryMapper.toDto(deployhistory.get()); return deployhistoryMapper.toDto(deployhistory);
} }
@Override @Override
......
...@@ -2,7 +2,6 @@ package me.zhengjie.modules.mnt.service.impl; ...@@ -2,7 +2,6 @@ package me.zhengjie.modules.mnt.service.impl;
import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.mnt.domain.App; import me.zhengjie.modules.mnt.domain.App;
...@@ -22,18 +21,16 @@ import me.zhengjie.utils.PageUtil; ...@@ -22,18 +21,16 @@ import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp; import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.SecurityUtils; import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set; import java.util.Set;
/** /**
...@@ -47,23 +44,21 @@ public class DeployServiceImpl implements DeployService { ...@@ -47,23 +44,21 @@ public class DeployServiceImpl implements DeployService {
private final String FILE_SEPARATOR = File.separatorChar + ""; private final String FILE_SEPARATOR = File.separatorChar + "";
@Autowired private final DeployRepository deployRepository;
private DeployRepository deployRepository;
@Autowired private final DeployMapper deployMapper;
private DeployMapper deployMapper;
@Autowired private final ServerDeployService serverDeployService;
private AppService appService;
@Autowired private final DeployHistoryService deployHistoryService;
private ServerDeployService serverDeployService;
@Autowired public DeployServiceImpl(DeployRepository deployRepository, DeployMapper deployMapper, ServerDeployService serverDeployService, DeployHistoryService deployHistoryService) {
private DeployHistoryService deployHistoryService; this.deployRepository = deployRepository;
this.deployMapper = deployMapper;
this.serverDeployService = serverDeployService;
this.deployHistoryService = deployHistoryService;
}
@Autowired
private DatabaseService databaseService;
@Override @Override
public Object queryAll(DeployQueryCriteria criteria, Pageable pageable) { public Object queryAll(DeployQueryCriteria criteria, Pageable pageable) {
...@@ -78,9 +73,9 @@ public class DeployServiceImpl implements DeployService { ...@@ -78,9 +73,9 @@ public class DeployServiceImpl implements DeployService {
@Override @Override
public DeployDTO findById(Long id) { public DeployDTO findById(Long id) {
Optional<Deploy> Deploy = deployRepository.findById(id); Deploy Deploy = deployRepository.findById(id).orElseGet(Deploy::new);
ValidationUtil.isNull(Deploy, "Deploy", "id", id); ValidationUtil.isNull(Deploy.getId(), "Deploy", "id", id);
return deployMapper.toDto(Deploy.get()); return deployMapper.toDto(Deploy);
} }
@Override @Override
...@@ -92,9 +87,8 @@ public class DeployServiceImpl implements DeployService { ...@@ -92,9 +87,8 @@ public class DeployServiceImpl implements DeployService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void update(Deploy resources) { public void update(Deploy resources) {
Optional<Deploy> optionalDeploy = deployRepository.findById(resources.getId()); Deploy Deploy = deployRepository.findById(resources.getId()).orElseGet(Deploy::new);
ValidationUtil.isNull(optionalDeploy, "Deploy", "id", resources.getId()); ValidationUtil.isNull(Deploy.getId(), "Deploy", "id", resources.getId());
Deploy Deploy = optionalDeploy.get();
Deploy.copy(resources); Deploy.copy(resources);
deployRepository.save(Deploy); deployRepository.save(Deploy);
} }
...@@ -112,8 +106,8 @@ public class DeployServiceImpl implements DeployService { ...@@ -112,8 +106,8 @@ public class DeployServiceImpl implements DeployService {
/** /**
* @param fileSavePath 本机路径 * @param fileSavePath 本机路径
* @param id * @param id ID
* @return * @return string
*/ */
private String deployApp(String fileSavePath, Long id) { private String deployApp(String fileSavePath, Long id) {
...@@ -131,7 +125,7 @@ public class DeployServiceImpl implements DeployService { ...@@ -131,7 +125,7 @@ public class DeployServiceImpl implements DeployService {
//这个是服务器部署路径 //这个是服务器部署路径
String uploadPath = app.getUploadPath(); String uploadPath = app.getUploadPath();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
String msg = ""; String msg;
Set<ServerDeployDTO> deploys = deploy.getDeploys(); Set<ServerDeployDTO> deploys = deploy.getDeploys();
for (ServerDeployDTO deployDTO : deploys) { for (ServerDeployDTO deployDTO : deploys) {
String ip = deployDTO.getIp(); String ip = deployDTO.getIp();
...@@ -199,7 +193,6 @@ public class DeployServiceImpl implements DeployService { ...@@ -199,7 +193,6 @@ public class DeployServiceImpl implements DeployService {
//还原信息入库 //还原信息入库
DeployHistory deployHistory = new DeployHistory(); DeployHistory deployHistory = new DeployHistory();
deployHistory.setAppName(appName); deployHistory.setAppName(appName);
deployHistory.setDeployDate(deployDate);
deployHistory.setDeployUser(SecurityUtils.getUsername()); deployHistory.setDeployUser(SecurityUtils.getUsername());
deployHistory.setIp(ip); deployHistory.setIp(ip);
deployHistory.setDeployId(id); deployHistory.setDeployId(id);
...@@ -209,9 +202,8 @@ public class DeployServiceImpl implements DeployService { ...@@ -209,9 +202,8 @@ public class DeployServiceImpl implements DeployService {
/** /**
* 停App * 停App
* *
* @param port * @param port 端口
* @param executeShellUtil * @param executeShellUtil /
* @return
*/ */
private void stopApp(int port, ExecuteShellUtil executeShellUtil) { private void stopApp(int port, ExecuteShellUtil executeShellUtil) {
//发送停止命令 //发送停止命令
...@@ -222,17 +214,13 @@ public class DeployServiceImpl implements DeployService { ...@@ -222,17 +214,13 @@ public class DeployServiceImpl implements DeployService {
/** /**
* 指定端口程序是否在运行 * 指定端口程序是否在运行
* *
* @param port * @param port 端口
* @param executeShellUtil * @param executeShellUtil /
* @return true 正在运行 false 已经停止 * @return true 正在运行 false 已经停止
*/ */
private boolean checkIsRunningStatus(int port, ExecuteShellUtil executeShellUtil) { private boolean checkIsRunningStatus(int port, ExecuteShellUtil executeShellUtil) {
String statusResult = executeShellUtil.executeForResult(String.format("fuser -n tcp %d", port)); String statusResult = executeShellUtil.executeForResult(String.format("fuser -n tcp %d", port));
if ("".equals(statusResult.trim())) { return !"".equals(statusResult.trim());
return false;
} else {
return true;
}
} }
private void sendMsg(String msg, MsgType msgType) { private void sendMsg(String msg, MsgType msgType) {
...@@ -265,20 +253,17 @@ public class DeployServiceImpl implements DeployService { ...@@ -265,20 +253,17 @@ public class DeployServiceImpl implements DeployService {
} }
private boolean checkFile(ExecuteShellUtil executeShellUtil, AppDTO appDTO) { private boolean checkFile(ExecuteShellUtil executeShellUtil, AppDTO appDTO) {
StringBuffer sb = new StringBuffer(); String sb = "find " +
sb.append("find "); appDTO.getDeployPath() +
sb.append(appDTO.getDeployPath()); " -name " +
sb.append(" -name "); appDTO.getName();
sb.append(appDTO.getName()); return executeShellUtil.executeShell(sb);
boolean flag = executeShellUtil.executeShell(sb.toString());
return flag;
} }
/** /**
* 启动服务 * 启动服务
* * @param resources /
* @param resources * @return /
* @return
*/ */
@Override @Override
public String startServer(Deploy resources) { public String startServer(Deploy resources) {
...@@ -303,15 +288,15 @@ public class DeployServiceImpl implements DeployService { ...@@ -303,15 +288,15 @@ public class DeployServiceImpl implements DeployService {
/** /**
* 停止服务 * 停止服务
* @param resources * @param resources /
* @return * @return /
*/ */
@Override @Override
public String stopServer(Deploy resources) { public String stopServer(Deploy resources) {
Set<ServerDeploy> deploys = resources.getDeploys(); Set<ServerDeploy> deploys = resources.getDeploys();
App app = resources.getApp(); App app = resources.getApp();
for (ServerDeploy deploy : deploys) { for (ServerDeploy deploy : deploys) {
StringBuffer sb = new StringBuffer(); StringBuilder sb = new StringBuilder();
ExecuteShellUtil executeShellUtil = getExecuteShellUtil(deploy.getIp()); ExecuteShellUtil executeShellUtil = getExecuteShellUtil(deploy.getIp());
sb.append("服务器:").append(deploy.getName()).append("<br>应用:").append(app.getName()); sb.append("服务器:").append(deploy.getName()).append("<br>应用:").append(app.getName());
sendMsg("下发停止命令", MsgType.INFO); sendMsg("下发停止命令", MsgType.INFO);
...@@ -334,8 +319,8 @@ public class DeployServiceImpl implements DeployService { ...@@ -334,8 +319,8 @@ public class DeployServiceImpl implements DeployService {
@Override @Override
public String serverReduction(DeployHistory resources) { public String serverReduction(DeployHistory resources) {
Long deployId = resources.getDeployId(); Long deployId = resources.getDeployId();
Deploy deployInfo = deployRepository.findById(deployId).get(); Deploy deployInfo = deployRepository.findById(deployId).orElseGet(Deploy::new);
String deployDate = resources.getDeployDate(); Timestamp deployDate = resources.getDeployDate();
App app = deployInfo.getApp(); App app = deployInfo.getApp();
if (app == null) { if (app == null) {
sendMsg("应用信息不存在:" + resources.getAppName(), MsgType.ERROR); sendMsg("应用信息不存在:" + resources.getAppName(), MsgType.ERROR);
...@@ -350,7 +335,7 @@ public class DeployServiceImpl implements DeployService { ...@@ -350,7 +335,7 @@ public class DeployServiceImpl implements DeployService {
String deployPath = app.getDeployPath(); String deployPath = app.getDeployPath();
String ip = resources.getIp(); String ip = resources.getIp();
ExecuteShellUtil executeShellUtil = getExecuteShellUtil(ip); ExecuteShellUtil executeShellUtil = getExecuteShellUtil(ip);
String msg = ""; String msg;
msg = String.format("登陆到服务器:%s", ip); msg = String.format("登陆到服务器:%s", ip);
log.info(msg); log.info(msg);
...@@ -398,7 +383,7 @@ public class DeployServiceImpl implements DeployService { ...@@ -398,7 +383,7 @@ public class DeployServiceImpl implements DeployService {
return ScpClientUtil.getInstance(ip, serverDeployDTO.getPort(), serverDeployDTO.getAccount(), serverDeployDTO.getPassword()); return ScpClientUtil.getInstance(ip, serverDeployDTO.getPort(), serverDeployDTO.getAccount(), serverDeployDTO.getPassword());
} }
public void sendResultMsg(boolean result, StringBuilder sb) { private void sendResultMsg(boolean result, StringBuilder sb) {
if (result) { if (result) {
sb.append("<br>启动成功!"); sb.append("<br>启动成功!");
sendMsg(sb.toString(), MsgType.INFO); sendMsg(sb.toString(), MsgType.INFO);
......
...@@ -15,8 +15,6 @@ import org.springframework.stereotype.Service; ...@@ -15,8 +15,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/** /**
* @author zhanghouying * @author zhanghouying
* @date 2019-08-24 * @date 2019-08-24
...@@ -47,9 +45,9 @@ public class ServerDeployServiceImpl implements ServerDeployService { ...@@ -47,9 +45,9 @@ public class ServerDeployServiceImpl implements ServerDeployService {
@Override @Override
public ServerDeployDTO findById(Long id) { public ServerDeployDTO findById(Long id) {
Optional<ServerDeploy> server = serverDeployRepository.findById(id); ServerDeploy server = serverDeployRepository.findById(id).orElseGet(ServerDeploy::new);
ValidationUtil.isNull(server,"ServerDeploy","id",id); ValidationUtil.isNull(server.getId(),"ServerDeploy","id",id);
return serverDeployMapper.toDto(server.get()); return serverDeployMapper.toDto(server);
} }
@Override @Override
...@@ -67,9 +65,8 @@ public class ServerDeployServiceImpl implements ServerDeployService { ...@@ -67,9 +65,8 @@ public class ServerDeployServiceImpl implements ServerDeployService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void update(ServerDeploy resources) { public void update(ServerDeploy resources) {
Optional<ServerDeploy> optionalServer = serverDeployRepository.findById(resources.getId()); ServerDeploy serverDeploy = serverDeployRepository.findById(resources.getId()).orElseGet(ServerDeploy::new);
ValidationUtil.isNull( optionalServer,"ServerDeploy","id",resources.getId()); ValidationUtil.isNull( serverDeploy.getId(),"ServerDeploy","id",resources.getId());
ServerDeploy serverDeploy = optionalServer.get();
serverDeploy.copy(resources); serverDeploy.copy(resources);
serverDeployRepository.save(serverDeploy); serverDeployRepository.save(serverDeploy);
} }
......
...@@ -10,7 +10,7 @@ import org.mapstruct.ReportingPolicy; ...@@ -10,7 +10,7 @@ import org.mapstruct.ReportingPolicy;
* @author zhanghouying * @author zhanghouying
* @date 2019-08-24 * @date 2019-08-24
*/ */
@Mapper(componentModel = "spring",uses = {},unmappedTargetPolicy = ReportingPolicy.IGNORE) @Mapper(componentModel = "spring",uses = {AppMapper.class, ServerDeployMapper.class},unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface DeployMapper extends BaseMapper<DeployDTO, Deploy> { public interface DeployMapper extends BaseMapper<DeployDTO, Deploy> {
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment