代码优化

pull/214/head
dqjdda 2019-11-25 13:51:54 +08:00
parent 2853f394e7
commit 6d941c09ef
27 changed files with 212 additions and 354 deletions

View File

@ -13,7 +13,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>eladmin-monitor</artifactId>
<version>2.2</version>
<version>2.3</version>
<name>客户端监控模块</name>

View File

@ -3,8 +3,11 @@ package me.zhengjie.modules.mnt.domain;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Set;
/**
@ -37,6 +40,9 @@ public class Deploy implements Serializable {
@JoinTable(name = "mnt_deploy_server", joinColumns = {@JoinColumn(name = "deploy_id",referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "server_id",referencedColumnName = "id")})
private Set<ServerDeploy> deploys;
@CreationTimestamp
private Timestamp createTime;
public void copy(Deploy source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}

View File

@ -3,8 +3,11 @@ package me.zhengjie.modules.mnt.domain;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* @author zhanghouying
@ -37,8 +40,9 @@ public class DeployHistory implements Serializable {
/**
*
*/
@Column(name = "deploy_date",nullable = false)
private String deployDate;
@Column(name = "deploy_date")
@CreationTimestamp
private Timestamp deployDate;
/**
*

View File

@ -8,5 +8,5 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
* @author zhanghouying
* @date 2019-08-24
*/
public interface DatabaseRepository extends JpaRepository<Database, String>, JpaSpecificationExecutor {
public interface DatabaseRepository extends JpaRepository<Database, String>, JpaSpecificationExecutor<Database> {
}

View File

@ -8,5 +8,5 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
* @author zhanghouying
* @date 2019-08-24
*/
public interface DeployHistoryRepository extends JpaRepository<DeployHistory, String>, JpaSpecificationExecutor {
public interface DeployHistoryRepository extends JpaRepository<DeployHistory, String>, JpaSpecificationExecutor<DeployHistory> {
}

View File

@ -12,9 +12,5 @@ import org.springframework.data.jpa.repository.Query;
*/
public interface ServerDeployRepository extends JpaRepository<ServerDeploy, Long>, JpaSpecificationExecutor<ServerDeploy> {
@Modifying
@Query(value = "update mnt_server set account_id = null where account_id = ?1", nativeQuery = true)
void changeByAccount(String id);
ServerDeploy findByIp(String ip);
}

View File

@ -6,7 +6,6 @@ import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.mnt.domain.App;
import me.zhengjie.modules.mnt.service.AppService;
import me.zhengjie.modules.mnt.service.dto.AppQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -23,31 +22,30 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/api/app")
public class AppController {
@Autowired
private AppService appService;
private final AppService appService;
public AppController(AppService appService){
this.appService = this.appService;
this.appService = appService;
}
@Log("查询App")
@ApiOperation(value = "查询App")
@Log("查询应用")
@ApiOperation(value = "查询应用")
@GetMapping
@PreAuthorize("@el.check('app:list')")
public ResponseEntity getApps(AppQueryCriteria criteria, Pageable pageable){
return new ResponseEntity(appService.queryAll(criteria,pageable),HttpStatus.OK);
return new ResponseEntity<>(appService.queryAll(criteria,pageable),HttpStatus.OK);
}
@Log("新增App")
@ApiOperation(value = "新增App")
@Log("新增应用")
@ApiOperation(value = "新增应用")
@PostMapping
@PreAuthorize("@el.check('app:add')")
public ResponseEntity create(@Validated @RequestBody App resources){
return new ResponseEntity(appService.create(resources),HttpStatus.CREATED);
return new ResponseEntity<>(appService.create(resources),HttpStatus.CREATED);
}
@Log("修改App")
@ApiOperation(value = "修改App")
@Log("修改应用")
@ApiOperation(value = "修改应用")
@PutMapping
@PreAuthorize("@el.check('app:edit')")
public ResponseEntity update(@Validated @RequestBody App resources){
@ -55,8 +53,8 @@ public class AppController {
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@Log("删除App")
@ApiOperation(value = "删除App")
@Log("删除应用")
@ApiOperation(value = "删除应用")
@DeleteMapping(value = "/{id}")
@PreAuthorize("@el.check('app:del')")
public ResponseEntity delete(@PathVariable Long id){

View File

@ -6,7 +6,6 @@ import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.mnt.domain.Database;
import me.zhengjie.modules.mnt.service.DatabaseService;
import me.zhengjie.modules.mnt.service.dto.DatabaseQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -23,27 +22,30 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/api/database")
public class DatabaseController {
@Autowired
private DatabaseService databaseService;
private final DatabaseService databaseService;
@Log("查询Database")
@ApiOperation(value = "查询Database")
public DatabaseController(DatabaseService databaseService) {
this.databaseService = databaseService;
}
@Log("查询数据库")
@ApiOperation(value = "查询数据库")
@GetMapping
@PreAuthorize("@el.check('database:list')")
public ResponseEntity getDatabases(DatabaseQueryCriteria criteria, Pageable pageable){
return new ResponseEntity(databaseService.queryAll(criteria,pageable),HttpStatus.OK);
return new ResponseEntity<>(databaseService.queryAll(criteria,pageable),HttpStatus.OK);
}
@Log("新增Database")
@ApiOperation(value = "新增Database")
@Log("新增数据库")
@ApiOperation(value = "新增数据库")
@PostMapping
@PreAuthorize("@el.check('database:add')")
public ResponseEntity create(@Validated @RequestBody Database resources){
return new ResponseEntity(databaseService.create(resources),HttpStatus.CREATED);
return new ResponseEntity<>(databaseService.create(resources),HttpStatus.CREATED);
}
@Log("修改Database")
@ApiOperation(value = "修改Database")
@Log("修改数据库")
@ApiOperation(value = "修改数据库")
@PutMapping
@PreAuthorize("@el.check('database:edit')")
public ResponseEntity update(@Validated @RequestBody Database resources){
@ -51,8 +53,8 @@ public class DatabaseController {
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@Log("删除Database")
@ApiOperation(value = "删除Database")
@Log("删除数据库")
@ApiOperation(value = "删除数据库")
@DeleteMapping(value = "/{id}")
@PreAuthorize("@el.check('database:del')")
public ResponseEntity delete(@PathVariable String id){

View File

@ -8,7 +8,6 @@ import me.zhengjie.modules.mnt.domain.DeployHistory;
import me.zhengjie.modules.mnt.service.DeployService;
import me.zhengjie.modules.mnt.service.dto.DeployQueryCriteria;
import me.zhengjie.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -16,12 +15,11 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* @author zhanghouying
@ -34,27 +32,30 @@ public class DeployController {
private String fileSavePath = System.getProperty("java.io.tmpdir");
@Autowired
private DeployService deployService;
private final DeployService deployService;
@Log("查询Deploy")
@ApiOperation(value = "查询Deploy")
public DeployController(DeployService deployService) {
this.deployService = deployService;
}
@Log("查询部署")
@ApiOperation(value = "查询部署")
@GetMapping
@PreAuthorize("@el.check('deploy:list')")
public ResponseEntity getDeploys(DeployQueryCriteria criteria, Pageable pageable){
return new ResponseEntity(deployService.queryAll(criteria,pageable),HttpStatus.OK);
return new ResponseEntity<>(deployService.queryAll(criteria,pageable),HttpStatus.OK);
}
@Log("新增Deploy")
@ApiOperation(value = "新增Deploy")
@Log("新增部署")
@ApiOperation(value = "新增部署")
@PostMapping
@PreAuthorize("@el.check('deploy:add')")
public ResponseEntity create(@Validated @RequestBody Deploy resources){
return new ResponseEntity(deployService.create(resources),HttpStatus.CREATED);
return new ResponseEntity<>(deployService.create(resources),HttpStatus.CREATED);
}
@Log("修改Deploy")
@ApiOperation(value = "修改Deploy")
@Log("修改部署")
@ApiOperation(value = "修改部署")
@PutMapping
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity update(@Validated @RequestBody Deploy resources){
@ -62,8 +63,8 @@ public class DeployController {
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@Log("删除Deploy")
@ApiOperation(value = "删除Deploy")
@Log("删除部署")
@ApiOperation(value = "删除部署")
@DeleteMapping(value = "/{id}")
@PreAuthorize("@el.check('deploy:del')")
public ResponseEntity delete(@PathVariable Long id){
@ -71,11 +72,11 @@ public class DeployController {
return new ResponseEntity(HttpStatus.OK);
}
@Log("上传文件Deploy")
@ApiOperation(value = "上传文件Deploy")
@Log("上传文件部署")
@ApiOperation(value = "上传文件部署")
@PostMapping(value = "/upload")
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity upload(@RequestBody MultipartFile file, HttpServletRequest request, HttpServletResponse response)throws Exception{
public ResponseEntity upload(@RequestBody MultipartFile file, HttpServletRequest request)throws Exception{
Long id = Long.valueOf(request.getParameter("id"));
String fileName = "";
if(file != null){
@ -88,42 +89,42 @@ public class DeployController {
}else{
System.out.println("没有找到相对应的文件");
}
System.out.println("文件上传的原名称为:"+file.getOriginalFilename());
Map map = new HashMap(2);
System.out.println("文件上传的原名称为:"+ Objects.requireNonNull(file).getOriginalFilename());
Map<String,Object> map = new HashMap<>(2);
map.put("errno",0);
map.put("id",fileName);
return new ResponseEntity(map,HttpStatus.OK);
return new ResponseEntity<>(map,HttpStatus.OK);
}
@Log("系统还原")
@ApiOperation(value = "系统还原")
@PostMapping(value = "/serverReduction")
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity serverReduction(@Validated @RequestBody DeployHistory resources, HttpServletRequest request, HttpServletResponse response)throws Exception{
public ResponseEntity serverReduction(@Validated @RequestBody DeployHistory resources){
String result = deployService.serverReduction(resources);
return new ResponseEntity(result,HttpStatus.OK);
return new ResponseEntity<>(result,HttpStatus.OK);
}
@Log("服务运行状态")
@ApiOperation(value = "服务运行状态")
@PostMapping(value = "/serverStatus")
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity serverStatus(@Validated @RequestBody Deploy resources, HttpServletRequest request, HttpServletResponse response)throws Exception{
public ResponseEntity serverStatus(@Validated @RequestBody Deploy resources){
String result = deployService.serverStatus(resources);
return new ResponseEntity(result,HttpStatus.OK);
return new ResponseEntity<>(result,HttpStatus.OK);
}
@Log("启动服务")
@ApiOperation(value = "启动服务")
@PostMapping(value = "/startServer")
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity startServer(@Validated @RequestBody Deploy resources, HttpServletRequest request, HttpServletResponse response)throws Exception{
public ResponseEntity startServer(@Validated @RequestBody Deploy resources){
String result = deployService.startServer(resources);
return new ResponseEntity(result,HttpStatus.OK);
return new ResponseEntity<>(result,HttpStatus.OK);
}
@Log("停止服务")
@ApiOperation(value = "停止服务")
@PostMapping(value = "/stopServer")
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity stopServer(@Validated @RequestBody Deploy resources, HttpServletRequest request, HttpServletResponse response)throws Exception{
public ResponseEntity stopServer(@Validated @RequestBody Deploy resources){
String result = deployService.stopServer(resources);
return new ResponseEntity(result,HttpStatus.OK);
return new ResponseEntity<>(result,HttpStatus.OK);
}
}

View File

@ -23,19 +23,22 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/api/deployHistory")
public class DeployHistoryController {
@Autowired
private DeployHistoryService deployhistoryService;
private final DeployHistoryService deployhistoryService;
@Log("查询DeployHistory")
@ApiOperation(value = "查询DeployHistory")
public DeployHistoryController(DeployHistoryService deployhistoryService) {
this.deployhistoryService = deployhistoryService;
}
@Log("查询部署历史")
@ApiOperation(value = "查询部署历史")
@GetMapping
@PreAuthorize("@el.check('deployHistory:list')")
public ResponseEntity getDeployHistorys(DeployHistoryQueryCriteria criteria, Pageable pageable){
return new ResponseEntity(deployhistoryService.queryAll(criteria,pageable),HttpStatus.OK);
return new ResponseEntity<>(deployhistoryService.queryAll(criteria,pageable),HttpStatus.OK);
}
@Log("删除DeployHistory")
@ApiOperation(value = "删除DeployHistory")
@ApiOperation(value = "删除部署历史")
@DeleteMapping(value = "/{id}")
@PreAuthorize("hasAnyRole('deployHistory:del')")
public ResponseEntity delete(@PathVariable String id){

View File

@ -6,7 +6,6 @@ import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.mnt.domain.ServerDeploy;
import me.zhengjie.modules.mnt.service.ServerDeployService;
import me.zhengjie.modules.mnt.service.dto.ServerDeployQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -18,32 +17,35 @@ import org.springframework.web.bind.annotation.*;
* @author zhanghouying
* @date 2019-08-24
*/
@Api(tags = "服务器部署管理")
@Api(tags = "服务器管理")
@RestController
@RequestMapping("/api/serverDeploy")
public class ServerDeployController {
@Autowired
private ServerDeployService serverDeployService;
private final ServerDeployService serverDeployService;
@Log("查询Server")
@ApiOperation(value = "查询Server")
public ServerDeployController(ServerDeployService serverDeployService) {
this.serverDeployService = serverDeployService;
}
@Log("查询服务器")
@ApiOperation(value = "查询服务器")
@GetMapping
@PreAuthorize("@el.check('serverDeploy:list')")
public ResponseEntity getServers(ServerDeployQueryCriteria criteria, Pageable pageable){
return new ResponseEntity(serverDeployService.queryAll(criteria,pageable),HttpStatus.OK);
return new ResponseEntity<>(serverDeployService.queryAll(criteria,pageable),HttpStatus.OK);
}
@Log("新增Server")
@ApiOperation(value = "新增Server")
@Log("新增服务器")
@ApiOperation(value = "新增服务器")
@PostMapping
@PreAuthorize("@el.check('serverDeploy:add')")
public ResponseEntity create(@Validated @RequestBody ServerDeploy resources){
return new ResponseEntity(serverDeployService.create(resources),HttpStatus.CREATED);
return new ResponseEntity<>(serverDeployService.create(resources),HttpStatus.CREATED);
}
@Log("修改Server")
@ApiOperation(value = "修改Server")
@Log("修改服务器")
@ApiOperation(value = "修改服务器")
@PutMapping
@PreAuthorize("@el.check('serverDeploy:edit')")
public ResponseEntity update(@Validated @RequestBody ServerDeploy resources){
@ -51,7 +53,7 @@ public class ServerDeployController {
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@Log("删除Server")
@Log("删除服务器")
@ApiOperation(value = "删除Server")
@DeleteMapping(value = "/{id:.+}")
@PreAuthorize("@el.check('serverDeploy:del')")

View File

@ -11,44 +11,15 @@ import org.springframework.data.domain.Pageable;
*/
public interface AppService {
/**
* queryAll
* @param criteria
* @param pageable
* @return
*/
Object queryAll(AppQueryCriteria criteria, Pageable pageable);
/**
* queryAll
* @param criteria
* @return
*/
public Object queryAll(AppQueryCriteria criteria);
Object queryAll(AppQueryCriteria criteria);
/**
* findById
* @param id
* @return
*/
AppDTO findById(Long id);
/**
* create
* @param resources
* @return
*/
AppDTO create(App resources);
/**
* update
* @param resources
*/
void update(App resources);
/**
* delete
* @param id
*/
void delete(Long id);
}

View File

@ -6,49 +6,20 @@ import me.zhengjie.modules.mnt.service.dto.DatabaseQueryCriteria;
import org.springframework.data.domain.Pageable;
/**
* @author: ZhangHouYing
* @author ZhangHouYing
* @date 2019-08-24
*/
public interface DatabaseService {
/**
* queryAll
* @param criteria
* @param pageable
* @return
*/
Object queryAll(DatabaseQueryCriteria criteria, Pageable pageable);
/**
* queryAll
* @param criteria
* @return
*/
public Object queryAll(DatabaseQueryCriteria criteria);
Object queryAll(DatabaseQueryCriteria criteria);
/**
* findById
* @param id
* @return
*/
DatabaseDTO findById(String id);
/**
* create
* @param resources
* @return
*/
DatabaseDTO create(Database resources);
/**
* update
* @param resources
*/
void update(Database resources);
/**
* delete
* @param id
*/
void delete(String id);
}

View File

@ -5,45 +5,15 @@ import me.zhengjie.modules.mnt.service.dto.DeployHistoryDTO;
import me.zhengjie.modules.mnt.service.dto.DeployHistoryQueryCriteria;
import org.springframework.data.domain.Pageable;
/**
*
* @author: ZhangHouYing
* @date 2019-08-24
*/
public interface DeployHistoryService {
/**
* queryAll
* @param criteria
* @param pageable
* @return
*/
Object queryAll(DeployHistoryQueryCriteria criteria, Pageable pageable);
/**
* queryAll
* @param criteria
* @return
*/
public Object queryAll(DeployHistoryQueryCriteria criteria);
Object queryAll(DeployHistoryQueryCriteria criteria);
/**
* findById
* @param id
* @return
*/
DeployHistoryDTO findById(String id);
/**
* create
* @param resources
* @return
*/
DeployHistoryDTO create(DeployHistory resources);
/**
* delete
* @param id
*/
void delete(String id);
}

View File

@ -12,81 +12,49 @@ import org.springframework.data.domain.Pageable;
*/
public interface DeployService {
/**
* queryAll
* @param criteria
* @param pageable
* @return
*/
Object queryAll(DeployQueryCriteria criteria, Pageable pageable);
/**
* queryAll
* @param criteria
* @return
*/
public Object queryAll(DeployQueryCriteria criteria);
Object queryAll(DeployQueryCriteria criteria);
/**
* findById
* @param id
* @return
*/
DeployDTO findById(Long id);
/**
* create
* @CacheEvict(allEntries = true)
* @param resources
* @return
*/
DeployDTO create(Deploy resources);
/**
* update
* @CacheEvict(allEntries = true)
* @param resources
*/
void update(Deploy resources);
/**
* delete
* @CacheEvict(allEntries = true)
* @param id
*/
void delete(Long id);
/**
*
* @param fileSavePath
* @param appId
* @return
* @param fileSavePath
* @param appId ID
* @return /
*/
public String deploy(String fileSavePath, Long appId);
String deploy(String fileSavePath, Long appId);
/**
*
* @param resources
* @return
* @param resources /
* @return /
*/
public String serverStatus(Deploy resources);
String serverStatus(Deploy resources);
/**
*
* @param resources
* @return
* @param resources /
* @return /
*/
public String startServer(Deploy resources);
String startServer(Deploy resources);
/**
*
* @param resources
* @return
* @param resources /
* @return /
*/
public String stopServer(Deploy resources);
String stopServer(Deploy resources);
/**
*
* @param resources
* @return
* @param resources /
* @return /
*/
public String serverReduction(DeployHistory resources);
String serverReduction(DeployHistory resources);
}

View File

@ -11,45 +11,16 @@ import org.springframework.data.domain.Pageable;
*/
public interface ServerDeployService {
/**
* queryAll
* @param criteria
* @param pageable
* @return
*/
Object queryAll(ServerDeployQueryCriteria criteria, Pageable pageable);
/**
* queryAll
* @param criteria
* @return
*/
public Object queryAll(ServerDeployQueryCriteria criteria);
Object queryAll(ServerDeployQueryCriteria criteria);
/**
* findById
* @param id
* @return
*/
ServerDeployDTO findById(Long id);
/**
* create
* @param resources
* @return
*/
ServerDeployDTO create(ServerDeploy resources);
/**
* update
* @param resources
*/
void update(ServerDeploy resources);
/**
* delete
* @param id
*/
void delete(Long id);
ServerDeployDTO findByIp(String ip);

View File

@ -16,7 +16,7 @@ public class AppDTO implements Serializable {
/**
*
*/
private String id;
private Long id;
/**
*

View File

@ -1,8 +1,11 @@
package me.zhengjie.modules.mnt.service.dto;
import cn.hutool.core.collection.CollectionUtil;
import lombok.Data;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Set;
import java.util.stream.Collectors;
/**
@ -24,9 +27,19 @@ public class DeployDTO implements Serializable {
*/
private Set<ServerDeployDTO> deploys;
private String servers;
/**
*
*/
private String status;
private Timestamp createTime;
public String getServers() {
if(CollectionUtil.isNotEmpty(deploys)){
return deploys.stream().map(ServerDeployDTO::getName).collect(Collectors.joining(","));
}
return servers;
}
}

View File

@ -2,6 +2,7 @@ package me.zhengjie.modules.mnt.service.dto;
import lombok.Data;
import java.io.Serializable;
import java.sql.Timestamp;
/**
@ -29,7 +30,7 @@ public class DeployHistoryDTO implements Serializable {
/**
*
*/
private String deployDate;
private Timestamp deployDate;
/**
*

View File

@ -2,6 +2,8 @@ package me.zhengjie.modules.mnt.service.dto;
import lombok.Data;
import me.zhengjie.annotation.Query;
import java.sql.Timestamp;
import java.util.List;
/**
* @author zhanghouying
@ -15,4 +17,7 @@ public class DeployHistoryQueryCriteria{
*/
@Query(blurry = "appName,ip,deployUser,deployId")
private String blurry;
@Query(type = Query.Type.BETWEEN)
private List<Timestamp> deployDate;
}

View File

@ -2,6 +2,8 @@ package me.zhengjie.modules.mnt.service.dto;
import lombok.Data;
import me.zhengjie.annotation.Query;
import java.sql.Timestamp;
import java.util.List;
/**
* @author zhanghouying
@ -13,13 +15,10 @@ public class DeployQueryCriteria{
/**
*
*/
@Query(type = Query.Type.EQUAL)
private String appId;
@Query(type = Query.Type.INNER_LIKE, propName = "name", joinName = "app")
private String appName;
/**
*
*/
@Query(type = Query.Type.INNER_LIKE)
private String ip;
@Query(type = Query.Type.BETWEEN)
private List<Timestamp> createTime;
}

View File

@ -14,7 +14,6 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/**
* @author zhanghouying
@ -46,9 +45,9 @@ public class AppServiceImpl implements AppService {
@Override
public AppDTO findById(Long id) {
Optional<App> app = appRepository.findById(id);
ValidationUtil.isNull(app,"App","id",id);
return appMapper.toDto(app.get());
App app = appRepository.findById(id).orElseGet(App::new);
ValidationUtil.isNull(app.getId(),"App","id",id);
return appMapper.toDto(app);
}
@Override
@ -60,11 +59,10 @@ public class AppServiceImpl implements AppService {
@Override
@Transactional(rollbackFor = Exception.class)
public void update(App resources) {
Optional<App> optionalApp = appRepository.findById(resources.getId());
ValidationUtil.isNull( optionalApp,"App","id",resources.getId());
App App = optionalApp.get();
App.copy(resources);
appRepository.save(App);
App app = appRepository.findById(resources.getId()).orElseGet(App::new);
ValidationUtil.isNull(app.getId(),"App","id",resources.getId());
app.copy(resources);
appRepository.save(app);
}
@Override

View File

@ -16,8 +16,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/**
* @author zhanghouying
* @date 2019-08-24
@ -48,9 +46,9 @@ public class DatabaseServiceImpl implements DatabaseService {
@Override
public DatabaseDTO findById(String id) {
Optional<Database> database = databaseRepository.findById(id);
ValidationUtil.isNull(database,"Database","id",id);
return databaseMapper.toDto(database.get());
Database database = databaseRepository.findById(id).orElseGet(Database::new);
ValidationUtil.isNull(database.getId(),"Database","id",id);
return databaseMapper.toDto(database);
}
@Override
@ -63,9 +61,8 @@ public class DatabaseServiceImpl implements DatabaseService {
@Override
@Transactional(rollbackFor = Exception.class)
public void update(Database resources) {
Optional<Database> optionalDatabase = databaseRepository.findById(resources.getId());
ValidationUtil.isNull( optionalDatabase,"Database","id",resources.getId());
Database database = optionalDatabase.get();
Database database = databaseRepository.findById(resources.getId()).orElseGet(Database::new);
ValidationUtil.isNull(database.getId(),"Database","id",resources.getId());
database.copy(resources);
databaseRepository.save(database);
}

View File

@ -10,15 +10,12 @@ import me.zhengjie.modules.mnt.service.mapper.DeployHistoryMapper;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/**
* @author zhanghouying
* @date 2019-08-24
@ -27,11 +24,14 @@ import java.util.Optional;
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeployHistoryServiceImpl implements DeployHistoryService {
@Autowired
private DeployHistoryRepository deployhistoryRepository;
private final DeployHistoryRepository deployhistoryRepository;
@Autowired
private DeployHistoryMapper deployhistoryMapper;
private final DeployHistoryMapper deployhistoryMapper;
public DeployHistoryServiceImpl(DeployHistoryRepository deployhistoryRepository, DeployHistoryMapper deployhistoryMapper) {
this.deployhistoryRepository = deployhistoryRepository;
this.deployhistoryMapper = deployhistoryMapper;
}
@Override
public Object queryAll(DeployHistoryQueryCriteria criteria, Pageable pageable){
@ -46,9 +46,9 @@ public class DeployHistoryServiceImpl implements DeployHistoryService {
@Override
public DeployHistoryDTO findById(String id) {
Optional<DeployHistory> deployhistory = deployhistoryRepository.findById(id);
ValidationUtil.isNull(deployhistory,"DeployHistory","id",id);
return deployhistoryMapper.toDto(deployhistory.get());
DeployHistory deployhistory = deployhistoryRepository.findById(id).orElseGet(DeployHistory::new);
ValidationUtil.isNull(deployhistory.getId(),"DeployHistory","id",id);
return deployhistoryMapper.toDto(deployhistory);
}
@Override

View File

@ -2,7 +2,6 @@ package me.zhengjie.modules.mnt.service.impl;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.mnt.domain.App;
@ -22,18 +21,16 @@ import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Set;
/**
@ -47,23 +44,21 @@ public class DeployServiceImpl implements DeployService {
private final String FILE_SEPARATOR = File.separatorChar + "";
@Autowired
private DeployRepository deployRepository;
private final DeployRepository deployRepository;
@Autowired
private DeployMapper deployMapper;
private final DeployMapper deployMapper;
@Autowired
private AppService appService;
private final ServerDeployService serverDeployService;
@Autowired
private ServerDeployService serverDeployService;
private final DeployHistoryService deployHistoryService;
@Autowired
private DeployHistoryService deployHistoryService;
public DeployServiceImpl(DeployRepository deployRepository, DeployMapper deployMapper, ServerDeployService serverDeployService, DeployHistoryService deployHistoryService) {
this.deployRepository = deployRepository;
this.deployMapper = deployMapper;
this.serverDeployService = serverDeployService;
this.deployHistoryService = deployHistoryService;
}
@Autowired
private DatabaseService databaseService;
@Override
public Object queryAll(DeployQueryCriteria criteria, Pageable pageable) {
@ -78,9 +73,9 @@ public class DeployServiceImpl implements DeployService {
@Override
public DeployDTO findById(Long id) {
Optional<Deploy> Deploy = deployRepository.findById(id);
ValidationUtil.isNull(Deploy, "Deploy", "id", id);
return deployMapper.toDto(Deploy.get());
Deploy Deploy = deployRepository.findById(id).orElseGet(Deploy::new);
ValidationUtil.isNull(Deploy.getId(), "Deploy", "id", id);
return deployMapper.toDto(Deploy);
}
@Override
@ -92,9 +87,8 @@ public class DeployServiceImpl implements DeployService {
@Override
@Transactional(rollbackFor = Exception.class)
public void update(Deploy resources) {
Optional<Deploy> optionalDeploy = deployRepository.findById(resources.getId());
ValidationUtil.isNull(optionalDeploy, "Deploy", "id", resources.getId());
Deploy Deploy = optionalDeploy.get();
Deploy Deploy = deployRepository.findById(resources.getId()).orElseGet(Deploy::new);
ValidationUtil.isNull(Deploy.getId(), "Deploy", "id", resources.getId());
Deploy.copy(resources);
deployRepository.save(Deploy);
}
@ -112,8 +106,8 @@ public class DeployServiceImpl implements DeployService {
/**
* @param fileSavePath
* @param id
* @return
* @param id ID
* @return string
*/
private String deployApp(String fileSavePath, Long id) {
@ -131,7 +125,7 @@ public class DeployServiceImpl implements DeployService {
//这个是服务器部署路径
String uploadPath = app.getUploadPath();
StringBuilder sb = new StringBuilder();
String msg = "";
String msg;
Set<ServerDeployDTO> deploys = deploy.getDeploys();
for (ServerDeployDTO deployDTO : deploys) {
String ip = deployDTO.getIp();
@ -199,7 +193,6 @@ public class DeployServiceImpl implements DeployService {
//还原信息入库
DeployHistory deployHistory = new DeployHistory();
deployHistory.setAppName(appName);
deployHistory.setDeployDate(deployDate);
deployHistory.setDeployUser(SecurityUtils.getUsername());
deployHistory.setIp(ip);
deployHistory.setDeployId(id);
@ -209,9 +202,8 @@ public class DeployServiceImpl implements DeployService {
/**
* App
*
* @param port
* @param executeShellUtil
* @return
* @param port
* @param executeShellUtil /
*/
private void stopApp(int port, ExecuteShellUtil executeShellUtil) {
//发送停止命令
@ -222,17 +214,13 @@ public class DeployServiceImpl implements DeployService {
/**
*
*
* @param port
* @param executeShellUtil
* @param port
* @param executeShellUtil /
* @return true false
*/
private boolean checkIsRunningStatus(int port, ExecuteShellUtil executeShellUtil) {
String statusResult = executeShellUtil.executeForResult(String.format("fuser -n tcp %d", port));
if ("".equals(statusResult.trim())) {
return false;
} else {
return true;
}
return !"".equals(statusResult.trim());
}
private void sendMsg(String msg, MsgType msgType) {
@ -265,20 +253,17 @@ public class DeployServiceImpl implements DeployService {
}
private boolean checkFile(ExecuteShellUtil executeShellUtil, AppDTO appDTO) {
StringBuffer sb = new StringBuffer();
sb.append("find ");
sb.append(appDTO.getDeployPath());
sb.append(" -name ");
sb.append(appDTO.getName());
boolean flag = executeShellUtil.executeShell(sb.toString());
return flag;
String sb = "find " +
appDTO.getDeployPath() +
" -name " +
appDTO.getName();
return executeShellUtil.executeShell(sb);
}
/**
*
*
* @param resources
* @return
* @param resources /
* @return /
*/
@Override
public String startServer(Deploy resources) {
@ -303,15 +288,15 @@ public class DeployServiceImpl implements DeployService {
/**
*
* @param resources
* @return
* @param resources /
* @return /
*/
@Override
public String stopServer(Deploy resources) {
Set<ServerDeploy> deploys = resources.getDeploys();
App app = resources.getApp();
for (ServerDeploy deploy : deploys) {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
ExecuteShellUtil executeShellUtil = getExecuteShellUtil(deploy.getIp());
sb.append("服务器:").append(deploy.getName()).append("<br>应用:").append(app.getName());
sendMsg("下发停止命令", MsgType.INFO);
@ -334,8 +319,8 @@ public class DeployServiceImpl implements DeployService {
@Override
public String serverReduction(DeployHistory resources) {
Long deployId = resources.getDeployId();
Deploy deployInfo = deployRepository.findById(deployId).get();
String deployDate = resources.getDeployDate();
Deploy deployInfo = deployRepository.findById(deployId).orElseGet(Deploy::new);
Timestamp deployDate = resources.getDeployDate();
App app = deployInfo.getApp();
if (app == null) {
sendMsg("应用信息不存在:" + resources.getAppName(), MsgType.ERROR);
@ -350,7 +335,7 @@ public class DeployServiceImpl implements DeployService {
String deployPath = app.getDeployPath();
String ip = resources.getIp();
ExecuteShellUtil executeShellUtil = getExecuteShellUtil(ip);
String msg = "";
String msg;
msg = String.format("登陆到服务器:%s", ip);
log.info(msg);
@ -398,7 +383,7 @@ public class DeployServiceImpl implements DeployService {
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) {
sb.append("<br>启动成功!");
sendMsg(sb.toString(), MsgType.INFO);

View File

@ -15,8 +15,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/**
* @author zhanghouying
* @date 2019-08-24
@ -47,9 +45,9 @@ public class ServerDeployServiceImpl implements ServerDeployService {
@Override
public ServerDeployDTO findById(Long id) {
Optional<ServerDeploy> server = serverDeployRepository.findById(id);
ValidationUtil.isNull(server,"ServerDeploy","id",id);
return serverDeployMapper.toDto(server.get());
ServerDeploy server = serverDeployRepository.findById(id).orElseGet(ServerDeploy::new);
ValidationUtil.isNull(server.getId(),"ServerDeploy","id",id);
return serverDeployMapper.toDto(server);
}
@Override
@ -67,9 +65,8 @@ public class ServerDeployServiceImpl implements ServerDeployService {
@Override
@Transactional(rollbackFor = Exception.class)
public void update(ServerDeploy resources) {
Optional<ServerDeploy> optionalServer = serverDeployRepository.findById(resources.getId());
ValidationUtil.isNull( optionalServer,"ServerDeploy","id",resources.getId());
ServerDeploy serverDeploy = optionalServer.get();
ServerDeploy serverDeploy = serverDeployRepository.findById(resources.getId()).orElseGet(ServerDeploy::new);
ValidationUtil.isNull( serverDeploy.getId(),"ServerDeploy","id",resources.getId());
serverDeploy.copy(resources);
serverDeployRepository.save(serverDeploy);
}

View File

@ -10,7 +10,7 @@ import org.mapstruct.ReportingPolicy;
* @author zhanghouying
* @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> {
}