机器管理代码格式化提交

This commit is contained in:
zyj 2025-06-04 14:19:29 +08:00
parent c26a183209
commit ea35ed1f0b
21 changed files with 177 additions and 175 deletions

View File

@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
import static cd.casic.framework.commons.pojo.CommonResult.success; import static cd.casic.framework.commons.pojo.CommonResult.success;
@ -51,7 +52,7 @@ public class MachineEnvController {
@DeleteMapping("/deleteList") @DeleteMapping("/deleteList")
@Operation(summary = "批量删除机器环境变量") @Operation(summary = "批量删除机器环境变量")
public CommonResult deleteList(@RequestParam String ids){ public CommonResult deleteList(@RequestParam String ids) {
machineEnvService.deleteList(ids); machineEnvService.deleteList(ids);
return success(true); return success(true);
} }
@ -64,7 +65,7 @@ public class MachineEnvController {
@PostMapping("/list") @PostMapping("/list")
@Operation(summary ="获取环境变量列表") @Operation(summary = "获取环境变量列表")
public CommonResult list(@RequestBody MachineEnvDTO machineEnvDTO) { public CommonResult list(@RequestBody MachineEnvDTO machineEnvDTO) {
return success(machineEnvService.listEnv(machineEnvDTO)); return success(machineEnvService.listEnv(machineEnvDTO));
} }

View File

@ -27,42 +27,43 @@ public class MachineProxyController {
private MachineProxyService machineProxyService; private MachineProxyService machineProxyService;
@PostMapping("/register") @PostMapping("/register")
@Operation(summary ="注册新的机器代理") @Operation(summary = "注册新的机器代理")
public CommonResult register(@RequestBody MachineProxyDTO machineProxyDTO) { public CommonResult register(@RequestBody MachineProxyDTO machineProxyDTO) {
machineProxyService.register(machineProxyDTO); machineProxyService.register(machineProxyDTO);
return success(true); return success(true);
} }
@PostMapping("/list") @PostMapping("/list")
@Operation(summary ="获取代理列表") @Operation(summary = "获取代理列表")
public CommonResult list(MachineProxyDTO machineProxyDTO) { public CommonResult list(@RequestBody MachineProxyDTO machineProxyDTO) {
return success(machineProxyService.list(machineProxyDTO)); return success(machineProxyService.list(machineProxyDTO));
} }
@PutMapping("/updateStatus") @PutMapping("/updateStatus")
@Operation(summary ="更新代理状态") @Operation(summary = "更新代理状态")
public CommonResult updateStatus(@RequestBody MachineProxyDTO machineProxyDTO) { public CommonResult updateStatus(@RequestBody MachineProxyDTO machineProxyDTO) {
machineProxyService.updateStatus(machineProxyDTO); machineProxyService.updateStatus(machineProxyDTO);
return success(true); return success(true);
} }
@GetMapping("/statistics/status") @GetMapping("/statistics/status")
@Operation(summary ="获取所有代理的状态统计") @Operation(summary = "获取所有代理的状态统计")
public CommonResult getStatusStatistics() { public CommonResult getStatusStatistics() {
return success(machineProxyService.getStatusStatistics()); return success(machineProxyService.getStatusStatistics());
} }
@PutMapping("/update") @PutMapping("/update")
@Operation(summary ="更新代理信息") @Operation(summary = "更新代理信息")
public CommonResult updateConfig(@RequestBody MachineProxyDTO machineProxyDTO) { public CommonResult updateConfig(@RequestBody MachineProxyDTO machineProxyDTO) {
machineProxyService.update(machineProxyDTO); machineProxyService.update(machineProxyDTO);
return success(true); return success(true);
} }
@DeleteMapping("/batch") @DeleteMapping("/batch")
@Operation(summary ="批量删除代理") @Operation(summary = "批量删除代理")
public CommonResult deleteBatch(@RequestParam String ids) { public CommonResult deleteBatch(@RequestParam String ids) {
return success(machineProxyService.delete(ids)); machineProxyService.delete(ids);
return success(true);
} }
} }

View File

@ -1,4 +1,5 @@
package cd.casic.module.machine.controller; package cd.casic.module.machine.controller;
import cd.casic.framework.commons.pojo.CommonResult; import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.module.machine.entity.SecretKey; import cd.casic.module.machine.entity.SecretKey;
import cd.casic.module.machine.service.SecretKeyService; import cd.casic.module.machine.service.SecretKeyService;
@ -29,45 +30,45 @@ public class SecretKeyController {
private SecretKeyService secretKeyService; private SecretKeyService secretKeyService;
@PostMapping(value = "/add", consumes = "multipart/form-data") @PostMapping(value = "/add", consumes = "multipart/form-data")
@Operation(summary ="新增密钥") @Operation(summary = "新增密钥")
public CommonResult<Boolean> add(@RequestBody SecretKeyDto secretKeyDto, @RequestPart("file") MultipartFile file) { public CommonResult<Boolean> add(@RequestBody SecretKeyDto secretKeyDto, @RequestPart("file") MultipartFile file) {
return success(secretKeyService.addSecretKey(secretKeyDto, file)); return success(secretKeyService.addSecretKey(secretKeyDto, file));
} }
@PutMapping("/bindingMachine") @PutMapping("/bindingMachine")
@Operation(summary ="绑定机器") @Operation(summary = "绑定机器")
public CommonResult<Boolean> bindingMachine(@RequestBody SecretKeyDto secretKeyDto) { public CommonResult<Boolean> bindingMachine(@RequestBody SecretKeyDto secretKeyDto) {
secretKeyService.bindingMachine(secretKeyDto); secretKeyService.bindingMachine(secretKeyDto);
return success(true); return success(true);
} }
@PutMapping("/update") @PutMapping("/update")
@Operation(summary ="编辑密钥信息") @Operation(summary = "编辑密钥信息")
public CommonResult<Boolean> update(@RequestBody SecretKeyDto secretKeyDto, @RequestPart(value = "file", required = false) MultipartFile file) { public CommonResult<Boolean> update(@RequestBody SecretKeyDto secretKeyDto, @RequestPart(value = "file", required = false) MultipartFile file) {
return success(secretKeyService.updateSecretKey(secretKeyDto, file)); return success(secretKeyService.updateSecretKey(secretKeyDto, file));
} }
@DeleteMapping("/delete") @DeleteMapping("/delete")
@Operation(summary ="删除密钥") @Operation(summary = "删除密钥")
public CommonResult<Boolean> delete(@RequestParam Long secretKeyId) { public CommonResult<Boolean> delete(@RequestParam Long secretKeyId) {
return success(secretKeyService.deleteSecretKey(secretKeyId)); return success(secretKeyService.deleteSecretKey(secretKeyId));
} }
@DeleteMapping("/deleteList") @DeleteMapping("/deleteList")
@Operation(summary ="批量删除密钥") @Operation(summary = "批量删除密钥")
public CommonResult<Boolean> deleteList(@RequestParam String secretKeyIds) { public CommonResult<Boolean> deleteList(@RequestParam String secretKeyIds) {
return success(secretKeyService.deleteList(secretKeyIds)); return success(secretKeyService.deleteList(secretKeyIds));
} }
@PostMapping("/list") @PostMapping("/list")
@Operation(summary ="获取密钥信息列表") @Operation(summary = "获取密钥信息列表")
public CommonResult<PageResult<SecretKey>> list(@RequestBody SecretKeyDto secretKeyDto) { public CommonResult<PageResult<SecretKey>> list(@RequestBody SecretKeyDto secretKeyDto) {
return success(secretKeyService.listSecretKey(secretKeyDto)); return success(secretKeyService.listSecretKey(secretKeyDto));
} }
@GetMapping("/downloadSecretKeyFile") @GetMapping("/downloadSecretKeyFile")
@Operation(summary ="下载密钥文件") @Operation(summary = "下载密钥文件")
public CommonResult<InputStreamResource> downloadSecretKeyFile(@RequestParam("secretKeyId") Long secretKeyId) { public CommonResult<InputStreamResource> downloadSecretKeyFile(@RequestParam("secretKeyId") Long secretKeyId) {
return success(secretKeyService.downloadSecretKeyFile(secretKeyId).getBody()); return success(secretKeyService.downloadSecretKeyFile(secretKeyId).getBody());
} }

View File

@ -1,4 +1,5 @@
package cd.casic.module.machine.dto; package cd.casic.module.machine.dto;
import cd.casic.module.machine.enums.MachineProxyStatus; import cd.casic.module.machine.enums.MachineProxyStatus;
import lombok.*; import lombok.*;

View File

@ -3,7 +3,7 @@ package cd.casic.module.machine.dto;
import lombok.Data; import lombok.Data;
@Data @Data
public class PageDto{ public class PageDto {
private int pageIndex = 1; private int pageIndex = 1;
private int pageSize = 10; private int pageSize = 10;

View File

@ -15,7 +15,7 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@TableName(value = "machine_info") @TableName(value = "machine_info")
public class MachineInfo extends BaseEntity{ public class MachineInfo extends BaseEntity {
@TableField(value = "name") @TableField(value = "name")
private String name; private String name;

View File

@ -1,4 +1,6 @@
package cd.casic.module.machine.entity; package cd.casic.module.machine.entity;
import cd.casic.module.machine.enums.MachineProxyStatus;
import com.baomidou.mybatisplus.annotation.*; import com.baomidou.mybatisplus.annotation.*;
import cd.casic.module.machine.enums.MachineInfoStatus; import cd.casic.module.machine.enums.MachineInfoStatus;
import cd.casic.module.machine.enums.MachineProxyType; import cd.casic.module.machine.enums.MachineProxyType;
@ -45,7 +47,7 @@ public class MachineProxy extends BaseEntity implements Serializable {
* 代理状态 (online, offline, installing, updating, error) * 代理状态 (online, offline, installing, updating, error)
*/ */
@TableField(exist = false) @TableField(exist = false)
private MachineInfoStatus status; private MachineProxyStatus status;
@TableField(value = "status_code") @TableField(value = "status_code")
private int statusCode; private int statusCode;
@ -67,5 +69,4 @@ public class MachineProxy extends BaseEntity implements Serializable {
private String description; private String description;
} }

View File

@ -10,11 +10,11 @@ public enum MachineProxyStatus implements CodeEnum {
/** /**
* 代理状态 (online, offline, installing, updating, error) * 代理状态 (online, offline, installing, updating, error)
*/ */
ONLINE(1,"online"), ONLINE(1, "online"),
OFFLINE(2,"offline"), OFFLINE(2, "offline"),
INSTALLING(3,"installing"), INSTALLING(3, "installing"),
UPDATING(4,"updating"), UPDATING(4, "updating"),
ERROR(5,"error"); ERROR(5, "error");
private final int code; private final int code;

View File

@ -7,9 +7,9 @@ import lombok.Getter;
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public enum MachineProxyType implements CodeEnum { public enum MachineProxyType implements CodeEnum {
HTTP(1,"http"), HTTP(1, "http"),
SOCKS4(2,"socks4"), SOCKS4(2, "socks4"),
SOCKS5(3,"socks5"); SOCKS5(3, "socks5");
private final int code; private final int code;

View File

@ -7,8 +7,8 @@ import lombok.Getter;
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public enum RequestExceptionEnum implements CodeEnum { public enum RequestExceptionEnum implements CodeEnum {
REQUEST_TYPE_NOT_JSON(1,"传递参数格式错误请使用json格式"), REQUEST_TYPE_NOT_JSON(1, "传递参数格式错误请使用json格式"),
REQUEST_JSON_ERROR(2,"json格式错误"), REQUEST_JSON_ERROR(2, "json格式错误"),
REQUEST_METHOD_NOT_POST(3, "不支持该请求方法请求方法应为POST"), REQUEST_METHOD_NOT_POST(3, "不支持该请求方法请求方法应为POST"),
REQUEST_METHOD_NOT_GET(4, "不支持该请求方法请求方法应为GET"), REQUEST_METHOD_NOT_GET(4, "不支持该请求方法请求方法应为GET"),
PARAM_ERROR(5, "参数错误"); PARAM_ERROR(5, "参数错误");

View File

@ -120,7 +120,7 @@ public class ConnectionSession implements AutoCloseable {
/** /**
* 配置认证方式密码或密钥 * 配置认证方式密码或密钥
*/ */
private void configureAuthentication(JSch jsch) throws JSchException{ private void configureAuthentication(JSch jsch) throws JSchException {
if (machineInfo.getAuthenticationType() == AuthenticationType.SECRET_KEY) { if (machineInfo.getAuthenticationType() == AuthenticationType.SECRET_KEY) {
// 密钥认证 // 密钥认证
if (machineInfo.getSecretKeyId() == null) { if (machineInfo.getSecretKeyId() == null) {
@ -261,7 +261,7 @@ public class ConnectionSession implements AutoCloseable {
// 使用线程中断机制实现超时控制 // 使用线程中断机制实现超时控制
Thread readerThread = new Thread(() -> { Thread readerThread = new Thread(() -> {
int bytesRead ; int bytesRead;
try { try {
while ((bytesRead = inputStream.read(buffer)) != -1) { while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead); outputStream.write(buffer, 0, bytesRead);
@ -482,11 +482,13 @@ public class ConnectionSession implements AutoCloseable {
log.debug("Created remote directory: {}", directory); log.debug("Created remote directory: {}", directory);
} }
} }
// 获取路径的父目录 // 获取路径的父目录
private String getParentDirectory(String path) { private String getParentDirectory(String path) {
int lastSlash = path.lastIndexOf('/'); int lastSlash = path.lastIndexOf('/');
return lastSlash > 0 ? path.substring(0, lastSlash) : ""; return lastSlash > 0 ? path.substring(0, lastSlash) : "";
} }
// 断开SFTP通道 // 断开SFTP通道
private void disconnectChannel(Channel channel) { private void disconnectChannel(Channel channel) {
if (channel != null && channel.isConnected()) { if (channel != null && channel.isConnected()) {
@ -513,6 +515,7 @@ public class ConnectionSession implements AutoCloseable {
disconnectChannel(channel); disconnectChannel(channel);
} }
} }
// 增强的进度监控器 // 增强的进度监控器
private static class ProgressMonitorAdapter implements SftpProgressMonitor { private static class ProgressMonitorAdapter implements SftpProgressMonitor {
private final long totalBytes; private final long totalBytes;
@ -575,7 +578,6 @@ public class ConnectionSession implements AutoCloseable {
} }
/** /**
* 检查连接状态 * 检查连接状态
*/ */

View File

@ -16,6 +16,7 @@ public interface MachineEnvService extends IService<MachineEnv> {
* 创建或更新机器的环境变量一对一关系 * 创建或更新机器的环境变量一对一关系
*/ */
boolean add(MachineEnvDTO machineEnvDTO); boolean add(MachineEnvDTO machineEnvDTO);
/** /**
* 删除机器的环境变量 * 删除机器的环境变量
*/ */
@ -23,6 +24,7 @@ public interface MachineEnvService extends IService<MachineEnv> {
/** /**
* 获取机器的环境变量 * 获取机器的环境变量
*
* @param machineId 机器ID * @param machineId 机器ID
* @return 环境变量DTO * @return 环境变量DTO
*/ */

View File

@ -25,9 +25,9 @@ public interface MachineInfoService extends IService<MachineInfo> {
void deleteMachineInfo(Long machineInfoId); void deleteMachineInfo(Long machineInfoId);
/** /**
* 测试机器连接 * 测试机器连接
*
* @param machineInfo 机器信息 * @param machineInfo 机器信息
* @return 连接是否成功 * @return 连接是否成功
*/ */
@ -35,6 +35,7 @@ public interface MachineInfoService extends IService<MachineInfo> {
/** /**
* 获取机器连接状态 * 获取机器连接状态
*
* @param machineName 机器名称 * @param machineName 机器名称
* @return 连接状态 * @return 连接状态
*/ */
@ -42,12 +43,14 @@ public interface MachineInfoService extends IService<MachineInfo> {
/** /**
* 获取所有连接状态 * 获取所有连接状态
*
* @return 机器名称到连接状态的映射 * @return 机器名称到连接状态的映射
*/ */
Map<String, ConnectionStatus> getAllConnectionStatus(); Map<String, ConnectionStatus> getAllConnectionStatus();
/** /**
* 建立机器连接 * 建立机器连接
*
* @param machineInfo 机器信息 * @param machineInfo 机器信息
* @return 连接会话ID * @return 连接会话ID
*/ */
@ -55,6 +58,7 @@ public interface MachineInfoService extends IService<MachineInfo> {
/** /**
* 断开机器连接 * 断开机器连接
*
* @param sessionId 会话ID * @param sessionId 会话ID
* @return 操作结果 * @return 操作结果
*/ */
@ -62,6 +66,7 @@ public interface MachineInfoService extends IService<MachineInfo> {
/** /**
* 执行远程命令 * 执行远程命令
*
* @param sessionId 会话ID * @param sessionId 会话ID
* @param command 命令 * @param command 命令
* @return 命令执行结果 * @return 命令执行结果
@ -70,6 +75,7 @@ public interface MachineInfoService extends IService<MachineInfo> {
/** /**
* 上传文件到远程机器 * 上传文件到远程机器
*
* @param sessionId 会话ID * @param sessionId 会话ID
* @param localFilePath 本地文件路径 * @param localFilePath 本地文件路径
* @param remoteFilePath 远程文件路径 * @param remoteFilePath 远程文件路径
@ -79,6 +85,7 @@ public interface MachineInfoService extends IService<MachineInfo> {
/** /**
* 从远程机器下载文件 * 从远程机器下载文件
*
* @param sessionId 会话ID * @param sessionId 会话ID
* @param remoteFilePath 远程文件路径 * @param remoteFilePath 远程文件路径
* @param localFilePath 本地文件路径 * @param localFilePath 本地文件路径

View File

@ -1,4 +1,5 @@
package cd.casic.module.machine.service; package cd.casic.module.machine.service;
import cd.casic.module.machine.dto.MachineProxyDTO; import cd.casic.module.machine.dto.MachineProxyDTO;
import cd.casic.module.machine.entity.MachineProxy; import cd.casic.module.machine.entity.MachineProxy;
import cd.casic.module.machine.utils.PageResult; import cd.casic.module.machine.utils.PageResult;
@ -22,7 +23,6 @@ public interface MachineProxyService extends IService<MachineProxy> {
boolean updateStatus(MachineProxyDTO machineProxyDTO); boolean updateStatus(MachineProxyDTO machineProxyDTO);
/** /**
* 获取所有代理的状态统计 * 获取所有代理的状态统计
* *
@ -38,9 +38,10 @@ public interface MachineProxyService extends IService<MachineProxy> {
/** /**
* 批量删除代理 * 批量删除代理
*
* @param proxyIds 代理ID列表 * @param proxyIds 代理ID列表
*/ */
String delete(String proxyIds); void delete(String proxyIds);
PageResult<MachineProxyDTO> list(MachineProxyDTO machineProxyDTO); PageResult<MachineProxyDTO> list(MachineProxyDTO machineProxyDTO);
} }

View File

@ -15,7 +15,7 @@ public interface SecretKeyService extends IService<SecretKey> {
void bindingMachine(SecretKeyDto secretKeyDto); void bindingMachine(SecretKeyDto secretKeyDto);
boolean updateSecretKey(SecretKeyDto secretKeyDto,MultipartFile file); boolean updateSecretKey(SecretKeyDto secretKeyDto, MultipartFile file);
boolean deleteSecretKey(Long secretKeyId); boolean deleteSecretKey(Long secretKeyId);

View File

@ -1,4 +1,5 @@
package cd.casic.module.machine.service.impl; package cd.casic.module.machine.service.impl;
import cd.casic.module.machine.dto.MachineEnvDTO; import cd.casic.module.machine.dto.MachineEnvDTO;
import cd.casic.module.machine.entity.MachineEnv; import cd.casic.module.machine.entity.MachineEnv;
import cd.casic.module.machine.exception.ServiceException; import cd.casic.module.machine.exception.ServiceException;
@ -32,13 +33,13 @@ public class MachineEnvServiceImpl extends ServiceImpl<MachineEnvMapper, Machine
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean add(MachineEnvDTO machineEnvDTO) { public boolean add(MachineEnvDTO machineEnvDTO) {
// 参数校验 // 参数校验
if (machineEnvDTO==null) { if (machineEnvDTO == null) {
throw new ServiceException(ServiceException.MACHINE_ENV_NULL,"环境变量不能为空"); throw new ServiceException(ServiceException.MACHINE_ENV_NULL, "环境变量不能为空");
} }
// 检查键是否合法 // 检查键是否合法
if (!isValidKey(machineEnvDTO.getEnvKey())) { if (!isValidKey(machineEnvDTO.getEnvKey())) {
throw new ServiceException(ServiceException.MACHINE_ENV_KEY_ILLEGAL,"环境变量键不合法"); throw new ServiceException(ServiceException.MACHINE_ENV_KEY_ILLEGAL, "环境变量键不合法");
} }
// 判断是否敏感变量 // 判断是否敏感变量
@ -55,7 +56,6 @@ public class MachineEnvServiceImpl extends ServiceImpl<MachineEnvMapper, Machine
} }
@Override @Override
public MachineEnvDTO getByMachineId(Long machineId) { public MachineEnvDTO getByMachineId(Long machineId) {
if (machineId == null) { if (machineId == null) {
@ -141,7 +141,7 @@ public class MachineEnvServiceImpl extends ServiceImpl<MachineEnvMapper, Machine
@Override @Override
public boolean update(MachineEnvDTO machineEnvDTO) { public boolean update(MachineEnvDTO machineEnvDTO) {
MachineEnv machineEnv = new MachineEnv(); MachineEnv machineEnv = new MachineEnv();
BeanUtils.copyProperties(machineEnvDTO,machineEnv); BeanUtils.copyProperties(machineEnvDTO, machineEnv);
return this.updateById(machineEnv); return this.updateById(machineEnv);
} }

View File

@ -1,4 +1,5 @@
package cd.casic.module.machine.service.impl; package cd.casic.module.machine.service.impl;
import cd.casic.module.machine.dto.MachineProxyDTO; import cd.casic.module.machine.dto.MachineProxyDTO;
import cd.casic.module.machine.entity.MachineProxy; import cd.casic.module.machine.entity.MachineProxy;
import cd.casic.module.machine.enums.MachineProxyStatus; import cd.casic.module.machine.enums.MachineProxyStatus;
@ -18,6 +19,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.*; import java.util.*;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -38,7 +40,7 @@ public class MachineProxyServiceImpl extends ServiceImpl<MachineProxyMapper, Mac
BeanUtils.copyProperties(machineProxyDTO, proxy); BeanUtils.copyProperties(machineProxyDTO, proxy);
proxy.setProxyTypeCode(EnumUtils.getEnumByMessage(machineProxyDTO.getProxyType(), MachineProxyType.class).getCode()); proxy.setProxyTypeCode(EnumUtils.getEnumByMessage(machineProxyDTO.getProxyType(), MachineProxyType.class).getCode());
proxy.setVersion("1.0.0"); proxy.setVersion("1.0.0");
proxy.setStatusCode(MachineProxyStatus.INSTALLING.getCode()); proxy.setStatusCode(MachineProxyStatus.ONLINE.getCode());
return save(proxy); return save(proxy);
} }
@ -46,24 +48,23 @@ public class MachineProxyServiceImpl extends ServiceImpl<MachineProxyMapper, Mac
public boolean updateStatus(MachineProxyDTO machineProxyDTO) { public boolean updateStatus(MachineProxyDTO machineProxyDTO) {
// 参数校验 // 参数校验
if (machineProxyDTO == null) { if (machineProxyDTO == null) {
throw new ServiceException(ServiceException.MACHINE_PROXY_DTO_NULL,"MachineProxyDTO对象为空"); throw new ServiceException(ServiceException.MACHINE_PROXY_DTO_NULL, "MachineProxyDTO对象为空");
} }
// 查询代理 // 查询代理
MachineProxy proxy = this.getById(machineProxyDTO.getId()); MachineProxy proxy = this.getById(machineProxyDTO.getId());
if (proxy == null) { if (proxy == null) {
throw new ServiceException(ServiceException.MACHINE_PROXY_NULL,"代理不存在"); throw new ServiceException(ServiceException.MACHINE_PROXY_NULL, "代理不存在");
} }
// 更新状态 // 更新状态
proxy.setStatusCode(EnumUtils.getEnumByMessage(machineProxyDTO.getStatus(),MachineProxyStatus.class).getCode()); proxy.setStatusCode(EnumUtils.getEnumByMessage(machineProxyDTO.getStatus(), MachineProxyStatus.class).getCode());
proxy.setUpdateDate(new Date()); proxy.setUpdateDate(new Date());
return updateById(proxy); return updateById(proxy);
} }
@Override @Override
public Map<String, Long> getStatusStatistics() { public Map<String, Long> getStatusStatistics() {
List<MachineProxy> proxyList = list(); List<MachineProxy> proxyList = list();
@ -83,24 +84,23 @@ public class MachineProxyServiceImpl extends ServiceImpl<MachineProxyMapper, Mac
public void update(MachineProxyDTO machineProxyDTO) { public void update(MachineProxyDTO machineProxyDTO) {
// 参数校验 // 参数校验
if (machineProxyDTO == null) { if (machineProxyDTO == null) {
throw new ServiceException(ServiceException.MACHINE_PROXY_DTO_NULL,"MachineProxyDTO对象为空"); throw new ServiceException(ServiceException.MACHINE_PROXY_DTO_NULL, "MachineProxyDTO对象为空");
} }
MachineProxy machineProxy = new MachineProxy(); MachineProxy machineProxy = new MachineProxy();
BeanUtils.copyProperties(machineProxyDTO, machineProxy); BeanUtils.copyProperties(machineProxyDTO, machineProxy);
if (machineProxyDTO.getProxyType()!= null && !machineProxyDTO.getProxyType().isEmpty()){ if (machineProxyDTO.getProxyType() != null && !machineProxyDTO.getProxyType().isEmpty()) {
machineProxy.setProxyTypeCode(EnumUtils.getEnumByMessage(machineProxyDTO.getProxyType(), MachineProxyType.class).getCode()); machineProxy.setProxyTypeCode(EnumUtils.getEnumByMessage(machineProxyDTO.getProxyType(), MachineProxyType.class).getCode());
} }
if (machineProxyDTO.getStatus()!= null && !machineProxyDTO.getStatus().isEmpty()){ if (machineProxyDTO.getStatus() != null && !machineProxyDTO.getStatus().isEmpty()) {
machineProxy.setStatusCode(EnumUtils.getEnumByMessage(machineProxyDTO.getStatus(), MachineProxyStatus.class).getCode()); machineProxy.setStatusCode(EnumUtils.getEnumByMessage(machineProxyDTO.getStatus(), MachineProxyStatus.class).getCode());
} }
this.updateById(machineProxy); this.updateById(machineProxy);
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public String delete(String ids) { public void delete(String ids) {
List<Long> machineProxyIds = Arrays.stream(ids.split(",")) List<Long> machineProxyIds = Arrays.stream(ids.split(","))
.map(String::trim) .map(String::trim)
.filter(s -> !s.isEmpty()) .filter(s -> !s.isEmpty())
@ -108,28 +108,12 @@ public class MachineProxyServiceImpl extends ServiceImpl<MachineProxyMapper, Mac
.toList(); .toList();
// 参数校验 // 参数校验
if (CollectionUtils.isEmpty(machineProxyIds)) { if (CollectionUtils.isEmpty(machineProxyIds)) {
throw new ServiceException(ServiceException.PARAMETER_ERROR,"参数错误"); throw new ServiceException(ServiceException.PARAMETER_ERROR, "参数错误");
} }
// 查询在线代理
List<MachineProxy> onlineProxies = list(new LambdaQueryWrapper<MachineProxy>()
.in(MachineProxy::getId, machineProxyIds)
.eq(MachineProxy::getStatusCode, MachineProxyStatus.ONLINE.getCode()));
if (!CollectionUtils.isEmpty(onlineProxies)) {
List<Long> onlineIds = onlineProxies.stream()
.map(MachineProxy::getId)
.toList();
// [10, 9] 转换为 "10, 9"
String idStr = onlineIds.toString().replaceAll("[\\[\\]]", "");
return "以下代理处于在线状态,无法删除: " + idStr;
}
// 批量逻辑删除 // 批量逻辑删除
remove(new LambdaQueryWrapper<MachineProxy>() remove(new LambdaQueryWrapper<MachineProxy>()
.in(MachineProxy::getId, machineProxyIds)); .in(MachineProxy::getId, machineProxyIds)
return ""; .ne(MachineProxy::getStatus, MachineProxyStatus.ONLINE.getCode()));
} }
@Override @Override
@ -152,7 +136,7 @@ public class MachineProxyServiceImpl extends ServiceImpl<MachineProxyMapper, Mac
); );
} }
private QueryWrapper<MachineProxy> getMachineProxyQueryWrapper(MachineProxyDTO machineProxyDTO){ private QueryWrapper<MachineProxy> getMachineProxyQueryWrapper(MachineProxyDTO machineProxyDTO) {
QueryWrapper<MachineProxy> queryWrapper = new QueryWrapper<>(); QueryWrapper<MachineProxy> queryWrapper = new QueryWrapper<>();
if (machineProxyDTO.getHostIp() != null && !machineProxyDTO.getHostIp().isEmpty()) { if (machineProxyDTO.getHostIp() != null && !machineProxyDTO.getHostIp().isEmpty()) {
queryWrapper.like("host_ip", machineProxyDTO.getHostIp()); queryWrapper.like("host_ip", machineProxyDTO.getHostIp());
@ -167,7 +151,10 @@ public class MachineProxyServiceImpl extends ServiceImpl<MachineProxyMapper, Mac
queryWrapper.like("description", machineProxyDTO.getDescription()); queryWrapper.like("description", machineProxyDTO.getDescription());
} }
if (machineProxyDTO.getStatus() != null && !machineProxyDTO.getStatus().isEmpty()) { if (machineProxyDTO.getStatus() != null && !machineProxyDTO.getStatus().isEmpty()) {
queryWrapper.like("status_code", EnumUtils.getEnumByMessage(machineProxyDTO.getStatus(),MachineProxyStatus.class).getCode()); queryWrapper.like("status_code", EnumUtils.getEnumByMessage(machineProxyDTO.getStatus(), MachineProxyStatus.class).getCode());
}
if (machineProxyDTO.getProxyType() != null && !machineProxyDTO.getProxyType().isEmpty()) {
queryWrapper.like("proxy_type_code", EnumUtils.getEnumByMessage(machineProxyDTO.getProxyType(), MachineProxyType.class).getCode());
} }
return queryWrapper.orderByDesc("create_date"); return queryWrapper.orderByDesc("create_date");
} }

View File

@ -37,8 +37,8 @@ import java.util.stream.Collectors;
@Service @Service
public class MachineinfoServiceImpl extends ServiceImpl<MachineInfoMapper, MachineInfo> implements MachineInfoService { public class MachineinfoServiceImpl extends ServiceImpl<MachineInfoMapper, MachineInfo> implements MachineInfoService {
int ENABLE=1; int ENABLE = 1;
int UN_ENABLE=0; int UN_ENABLE = 0;
@Resource @Resource
private MachineInfoMapper machineInfoMapper; private MachineInfoMapper machineInfoMapper;
@ -57,6 +57,7 @@ public class MachineinfoServiceImpl extends ServiceImpl<MachineInfoMapper, Machi
* 机器名称 -> 会话ID * 机器名称 -> 会话ID
*/ */
private final Map<String, String> machineSessionMapping = new ConcurrentHashMap<>(); private final Map<String, String> machineSessionMapping = new ConcurrentHashMap<>();
@Override @Override
public boolean addMachineInfo(MachineInfoDto machineInfoDto) { public boolean addMachineInfo(MachineInfoDto machineInfoDto) {
if (machineInfoDto == null) { if (machineInfoDto == null) {
@ -135,7 +136,7 @@ public class MachineinfoServiceImpl extends ServiceImpl<MachineInfoMapper, Machi
@Override @Override
public boolean bindingSecretKey(MachineInfoDto machineInfoDto) { public boolean bindingSecretKey(MachineInfoDto machineInfoDto) {
UpdateWrapper<MachineInfo> updateWrapper = new UpdateWrapper<>(); UpdateWrapper<MachineInfo> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("id", machineInfoDto.getId()).set("secret_key_id", machineInfoDto.getSecretKeyId()).set("authentication_type_code",2); updateWrapper.eq("id", machineInfoDto.getId()).set("secret_key_id", machineInfoDto.getSecretKeyId()).set("authentication_type_code", 2);
return this.update(updateWrapper); return this.update(updateWrapper);
} }
@ -147,7 +148,7 @@ public class MachineinfoServiceImpl extends ServiceImpl<MachineInfoMapper, Machi
.map(Long::parseLong) .map(Long::parseLong)
.collect(Collectors.toList()); .collect(Collectors.toList());
machineInfoMapper.selectBatchIds(machineInfoIdList).forEach(machineInfo -> { machineInfoMapper.selectBatchIds(machineInfoIdList).forEach(machineInfo -> {
if (machineInfo.getStatusCode() == 1){ if (machineInfo.getStatusCode() == 1) {
this.removeById(machineInfo.getId()); this.removeById(machineInfo.getId());
} }
}); });
@ -156,14 +157,14 @@ public class MachineinfoServiceImpl extends ServiceImpl<MachineInfoMapper, Machi
@Override @Override
public void deleteMachineInfo(Long machineInfoId) { public void deleteMachineInfo(Long machineInfoId) {
MachineInfo machineInfo = this.getById(machineInfoId); MachineInfo machineInfo = this.getById(machineInfoId);
if (machineInfo.getStatusCode() == 1){ if (machineInfo.getStatusCode() == 1) {
this.removeById(machineInfoId); this.removeById(machineInfoId);
} }
} }
private QueryWrapper<MachineInfo> getMachineInfoQueryWrapper(MachineInfoDto machineInfoDto) { private QueryWrapper<MachineInfo> getMachineInfoQueryWrapper(MachineInfoDto machineInfoDto) {
QueryWrapper<MachineInfo> queryWrapper = new QueryWrapper<>(); QueryWrapper<MachineInfo> queryWrapper = new QueryWrapper<>();
if (machineInfoDto.getStatus() != null && !machineInfoDto.getStatus().isEmpty()){ if (machineInfoDto.getStatus() != null && !machineInfoDto.getStatus().isEmpty()) {
queryWrapper.eq("status_code", EnumUtils.getEnumByMessage(machineInfoDto.getStatus(), MachineInfoStatus.class).getCode()); queryWrapper.eq("status_code", EnumUtils.getEnumByMessage(machineInfoDto.getStatus(), MachineInfoStatus.class).getCode());
} }
if (machineInfoDto.getName() != null && !machineInfoDto.getName().isEmpty()) { if (machineInfoDto.getName() != null && !machineInfoDto.getName().isEmpty()) {
@ -182,11 +183,9 @@ public class MachineinfoServiceImpl extends ServiceImpl<MachineInfoMapper, Machi
} }
@Override @Override
public boolean testConnection(MachineInfo machineInfo) { public boolean testConnection(MachineInfo machineInfo) {
if (machineInfo.getStatus().getCode()==UN_ENABLE) { if (machineInfo.getStatus().getCode() == UN_ENABLE) {
throw new RuntimeException("机器不可用"); throw new RuntimeException("机器不可用");
} }
log.info("测试机器连接: {}", machineInfo.getHostIp()); log.info("测试机器连接: {}", machineInfo.getHostIp());
@ -225,7 +224,7 @@ public class MachineinfoServiceImpl extends ServiceImpl<MachineInfoMapper, Machi
@Override @Override
public String connect(MachineInfo machineInfo) { public String connect(MachineInfo machineInfo) {
if (machineInfo.getStatus().getCode()==UN_ENABLE) { if (machineInfo.getStatus().getCode() == UN_ENABLE) {
throw new RuntimeException("机器不可用"); throw new RuntimeException("机器不可用");
} }
log.info("建立机器连接: {}", machineInfo.getHostIp()); log.info("建立机器连接: {}", machineInfo.getHostIp());

View File

@ -36,14 +36,13 @@ public class SecretKeyServiceImpl extends ServiceImpl<SecretServiceMapper, Secre
@Resource @Resource
private MachineInfoService machineInfoService; private MachineInfoService machineInfoService;
// @Resource // @Resource
private AliYunConfig aliYunConfig; private AliYunConfig aliYunConfig;
@Resource @Resource
private SecretServiceMapper secretServiceMapper; private SecretServiceMapper secretServiceMapper;
//todo public方便测试后面改为private //todo public方便测试后面改为private
public static final ExecutorService FILE_DELETE_EXECUTOR = Executors.newFixedThreadPool(10); public static final ExecutorService FILE_DELETE_EXECUTOR = Executors.newFixedThreadPool(10);
@ -57,7 +56,7 @@ public class SecretKeyServiceImpl extends ServiceImpl<SecretServiceMapper, Secre
secretKeyDto.setPath(path); secretKeyDto.setPath(path);
secretKeyDto.setFileName(fileName); secretKeyDto.setFileName(fileName);
SecretKey secretKey = new SecretKey(); SecretKey secretKey = new SecretKey();
BeanUtils.copyProperties(secretKeyDto,secretKey); BeanUtils.copyProperties(secretKeyDto, secretKey);
return this.save(secretKey); return this.save(secretKey);
} }
@ -70,7 +69,7 @@ public class SecretKeyServiceImpl extends ServiceImpl<SecretServiceMapper, Secre
@Override @Override
public boolean updateSecretKey(SecretKeyDto secretKeyDto, MultipartFile file) { public boolean updateSecretKey(SecretKeyDto secretKeyDto, MultipartFile file) {
if (file != null){ if (file != null) {
secretKeyDto.setFile(file); secretKeyDto.setFile(file);
String fileName = aliOssUtil.save(file); String fileName = aliOssUtil.save(file);
String path = "https://" + aliYunConfig.getBucketName() + "." + aliYunConfig.getEndpoint() + "/" + fileName; String path = "https://" + aliYunConfig.getBucketName() + "." + aliYunConfig.getEndpoint() + "/" + fileName;
@ -78,7 +77,7 @@ public class SecretKeyServiceImpl extends ServiceImpl<SecretServiceMapper, Secre
secretKeyDto.setFileName(fileName); secretKeyDto.setFileName(fileName);
} }
SecretKey secretKey = new SecretKey(); SecretKey secretKey = new SecretKey();
BeanUtils.copyProperties(secretKeyDto,secretKey); BeanUtils.copyProperties(secretKeyDto, secretKey);
return this.updateById(secretKey); return this.updateById(secretKey);
} }
@ -92,10 +91,10 @@ public class SecretKeyServiceImpl extends ServiceImpl<SecretServiceMapper, Secre
@Override @Override
public PageResult<SecretKey> listSecretKey(SecretKeyDto secretKeyDto) { public PageResult<SecretKey> listSecretKey(SecretKeyDto secretKeyDto) {
QueryWrapper<SecretKey> queryWrapper = new QueryWrapper<>(); QueryWrapper<SecretKey> queryWrapper = new QueryWrapper<>();
if (secretKeyDto.getName() != null && !secretKeyDto.getName().isEmpty()){ if (secretKeyDto.getName() != null && !secretKeyDto.getName().isEmpty()) {
queryWrapper.like("name", secretKeyDto.getName()); queryWrapper.like("name", secretKeyDto.getName());
} }
if (secretKeyDto.getDescription() != null && !secretKeyDto.getDescription().isEmpty()){ if (secretKeyDto.getDescription() != null && !secretKeyDto.getDescription().isEmpty()) {
queryWrapper.like("description", secretKeyDto.getDescription()); queryWrapper.like("description", secretKeyDto.getDescription());
} }
queryWrapper.orderByDesc("create_date"); queryWrapper.orderByDesc("create_date");
@ -127,15 +126,15 @@ public class SecretKeyServiceImpl extends ServiceImpl<SecretServiceMapper, Secre
FILE_DELETE_EXECUTOR.execute(() -> { FILE_DELETE_EXECUTOR.execute(() -> {
try { try {
for (SecretKey secretKey : secretKeys) { for (SecretKey secretKey : secretKeys) {
if (secretKey.getFileName() != null && !secretKey.getFileName().isEmpty()){ if (secretKey.getFileName() != null && !secretKey.getFileName().isEmpty()) {
aliOssUtil.deleteFile(secretKey.getFileName()); aliOssUtil.deleteFile(secretKey.getFileName());
} }
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("异步删除文件失败:{}", e.getMessage()); logger.error("异步删除文件失败:{}", e.getMessage());
throw new ServiceException(ServiceException.DELETE_FILE_FAIL,"异步删除文件失败:"+e.getMessage()); throw new ServiceException(ServiceException.DELETE_FILE_FAIL, "异步删除文件失败:" + e.getMessage());
} }
}); });
return secretServiceMapper.deleteBatchIds(secretKeyIds) > 0 ; return secretServiceMapper.deleteBatchIds(secretKeyIds) > 0;
} }
} }

View File

@ -21,12 +21,12 @@ import java.nio.charset.StandardCharsets;
@Component @Component
public class AliOssUtil{ public class AliOssUtil {
// @Autowired // @Autowired
private OSS ossClient; private OSS ossClient;
// @Resource // @Resource
private AliYunConfig aliyunConfig; private AliYunConfig aliyunConfig;
private static final Logger logger = LoggerFactory.getLogger(AliOssUtil.class); private static final Logger logger = LoggerFactory.getLogger(AliOssUtil.class);