机器代理管理规范化
This commit is contained in:
parent
926c1af1e1
commit
66bc6a1250
@ -13,13 +13,19 @@ public interface MachineErrorCodeConstants {
|
||||
ErrorCode FILENAME_NULL = new ErrorCode(1_003_000_003, "文件名为空");
|
||||
ErrorCode READ_FILE_FAIL = new ErrorCode(1_003_000_004, "读取文件失败");
|
||||
ErrorCode DELETE_FILE_FAIL = new ErrorCode(1_003_000_005, "删除文件失败");
|
||||
ErrorCode MACHINE_PROXY_DTO_NULL = new ErrorCode(1_003_000_006, "MachineProxyDTO对象为空");
|
||||
ErrorCode MACHINE_PROXY_NULL = new ErrorCode(1_003_000_007, "MachineProxy代理不存在");
|
||||
|
||||
ErrorCode PARAMETER_ERROR = new ErrorCode(1_003_000_008, "参数错误");
|
||||
// ========== 机器环境变量模块 1-003-000-009 ==========
|
||||
ErrorCode MACHINE_ENV_NULL = new ErrorCode(1_003_000_009, "机器环境变量为空");
|
||||
ErrorCode MACHINE_ENV_NOT_EXISTS = new ErrorCode(1_003_000_009, "机器不存在");
|
||||
ErrorCode MACHINE_ENV_EXISTS = new ErrorCode(1_003_000_009, "机器已存在");
|
||||
ErrorCode MACHINE_ENV_KEY_ILLEGAL = new ErrorCode(1_003_000_010, "机器环境变量键不合法");
|
||||
// ========== 机器代理模块 1-003-000-006 ==========
|
||||
ErrorCode MACHINE_PROXY_HOST_IP_NULL = new ErrorCode(1_003_000_006, "机器代理主机地址为空");
|
||||
ErrorCode MACHINE_PROXY_USER_NAME_NULL = new ErrorCode(1_003_000_006, "机器代理用户名为空");
|
||||
ErrorCode MACHINE_PROXY_NOT_EXISTS = new ErrorCode(1_003_000_007, "机器代理不存在");
|
||||
ErrorCode MACHINE_PROXY_TYPE_NOT_EXISTS = new ErrorCode(1_003_000_007, "机器代理类型不存在");
|
||||
ErrorCode MACHINE_PROXY_IS_ONLINE = new ErrorCode(1_003_000_007, "机器代理在线,不能删除");
|
||||
|
||||
ErrorCode OSS_PARAM_NULL = new ErrorCode(1_003_000_011, "oss参数无法读取");
|
||||
ErrorCode SECRETKEY_NULL = new ErrorCode(1_003_000_012, "密钥为空");
|
||||
}
|
||||
|
@ -1,67 +1,72 @@
|
||||
//package cd.casic.module.machine.controller;
|
||||
//
|
||||
//import cd.casic.framework.commons.pojo.CommonResult;
|
||||
//import cd.casic.module.machine.service.MachineProxyService;
|
||||
//import cd.casic.module.machine.controller.vo.MachineProxyDTO;
|
||||
//
|
||||
//import io.swagger.v3.oas.annotations.Operation;
|
||||
//import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
//import jakarta.annotation.Resource;
|
||||
//import lombok.RequiredArgsConstructor;
|
||||
//import org.springframework.web.bind.annotation.*;
|
||||
//
|
||||
//import static cd.casic.framework.commons.pojo.CommonResult.success;
|
||||
//
|
||||
///**
|
||||
// * 机器代理控制器
|
||||
// */
|
||||
//@RestController
|
||||
//@RequestMapping("/api/machineProxy")
|
||||
//@Tag(name = "机器代理管理")
|
||||
//@RequiredArgsConstructor
|
||||
//public class MachineProxyController {
|
||||
//
|
||||
// @Resource
|
||||
// private MachineProxyService machineProxyService;
|
||||
//
|
||||
// @PostMapping("/register")
|
||||
// @Operation(summary = "注册新的机器代理")
|
||||
// public CommonResult register(@RequestBody MachineProxyDTO machineProxyDTO) {
|
||||
// machineProxyService.register(machineProxyDTO);
|
||||
// return success(true);
|
||||
// }
|
||||
//
|
||||
// @PostMapping("/list")
|
||||
// @Operation(summary = "获取代理列表")
|
||||
// public CommonResult list(@RequestBody MachineProxyDTO machineProxyDTO) {
|
||||
// return success(machineProxyService.list(machineProxyDTO));
|
||||
// }
|
||||
//
|
||||
// @PutMapping("/updateStatus")
|
||||
// @Operation(summary = "更新代理状态")
|
||||
// public CommonResult updateStatus(@RequestBody MachineProxyDTO machineProxyDTO) {
|
||||
// machineProxyService.updateStatus(machineProxyDTO);
|
||||
// return success(true);
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/statistics/status")
|
||||
// @Operation(summary = "获取所有代理的状态统计")
|
||||
// public CommonResult getStatusStatistics() {
|
||||
// return success(machineProxyService.getStatusStatistics());
|
||||
// }
|
||||
//
|
||||
// @PutMapping("/update")
|
||||
// @Operation(summary = "更新代理信息")
|
||||
// public CommonResult updateConfig(@RequestBody MachineProxyDTO machineProxyDTO) {
|
||||
// machineProxyService.update(machineProxyDTO);
|
||||
// return success(true);
|
||||
// }
|
||||
//
|
||||
// @DeleteMapping("/batch")
|
||||
// @Operation(summary = "批量删除代理")
|
||||
// public CommonResult deleteBatch(@RequestParam String ids) {
|
||||
// machineProxyService.delete(ids);
|
||||
// return success(true);
|
||||
// }
|
||||
//
|
||||
//}
|
||||
package cd.casic.module.machine.controller;
|
||||
import cd.casic.framework.commons.pojo.CommonResult;
|
||||
import cd.casic.framework.commons.pojo.PageResult;
|
||||
import cd.casic.framework.commons.util.object.BeanUtils;
|
||||
import cd.casic.module.machine.controller.vo.MachineProxyVO;
|
||||
import cd.casic.module.machine.dal.dataobject.MachineProxyDO;
|
||||
import cd.casic.module.machine.service.MachineProxyService;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import java.util.Map;
|
||||
import static cd.casic.framework.commons.pojo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* 机器代理控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/ci/machineProxy")
|
||||
@Tag(name = "机器代理管理")
|
||||
@Validated
|
||||
public class MachineProxyController {
|
||||
|
||||
@Resource
|
||||
private MachineProxyService machineProxyService;
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "注册新的机器代理")
|
||||
@PreAuthorize("@ss.hasPermission('ci:machineProxy:create')")
|
||||
public CommonResult<Long> createProxy(@Valid @RequestBody MachineProxyVO machineProxyVO) {
|
||||
Long id = machineProxyService.createProxy(machineProxyVO);
|
||||
return success(id);
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
@Operation(summary = "修改代理")
|
||||
@PreAuthorize("@ss.hasPermission('ci:machineProxy:update')")
|
||||
public CommonResult<Boolean> updateProxy(@Valid @RequestBody MachineProxyVO machineProxyVO) {
|
||||
machineProxyService.updateProxy(machineProxyVO);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
@Operation(summary = "获取代理列表")
|
||||
public CommonResult<PageResult<MachineProxyVO>> getProxyPage(@Valid @RequestBody MachineProxyVO machineProxyVO) {
|
||||
PageResult<MachineProxyDO> pageResult = machineProxyService.getProxyPage(machineProxyVO);
|
||||
if (CollUtil.isEmpty(pageResult.getList())) {
|
||||
return success(new PageResult<>(pageResult.getTotal()));
|
||||
}
|
||||
return success(BeanUtils.toBean(pageResult, MachineProxyVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/allStatus")
|
||||
@Operation(summary = "获取所有代理的状态统计")
|
||||
public CommonResult<Map<Integer, Long>> getStatusStatistics() {
|
||||
return success(machineProxyService.getAllProxyStatus());
|
||||
}
|
||||
|
||||
|
||||
@DeleteMapping("/deleteList")
|
||||
@Operation(summary = "批量删除代理")
|
||||
@PreAuthorize("@ss.hasPermission('ci:machineProxy:delete')")
|
||||
public CommonResult<Boolean> deleteProxyList(@RequestParam String ids) {
|
||||
machineProxyService.deleteProxyList(ids);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,46 +1,50 @@
|
||||
package cd.casic.module.machine.controller.vo;
|
||||
|
||||
import cd.casic.module.machine.enums.MachineProxyStatus;
|
||||
import cd.casic.framework.commons.pojo.PageParam;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
import lombok.experimental.Accessors;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 机器代理数据传输对象
|
||||
*/
|
||||
@Schema(description = "管理后台 - 机器代理信息 Response VO")
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MachineProxyVO extends PageDto implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Accessors(chain = true)
|
||||
public class MachineProxyVO extends PageParam {
|
||||
|
||||
@Schema(description = "代理ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "用户名", requiredMode = Schema.RequiredMode.REQUIRED, example = "admin")
|
||||
private String username;
|
||||
private String proxyType;
|
||||
|
||||
@Schema(description = "代理类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "HTTP")
|
||||
private Integer proxyType;
|
||||
|
||||
@Schema(description = "版本号", example = "1.0.0")
|
||||
private String version;
|
||||
|
||||
@Schema(description = "状态(ONLINE:在线,OFFLINE:离线)", requiredMode = Schema.RequiredMode.REQUIRED, example = "ONLINE")
|
||||
private String status;
|
||||
private Date lastHeartbeatTime;
|
||||
|
||||
@Schema(description = "描述信息", example = "用于生产环境的代理服务器")
|
||||
private String description;
|
||||
|
||||
@Schema(description = "主机IP", requiredMode = Schema.RequiredMode.REQUIRED, example = "192.168.1.100")
|
||||
private String hostIp;
|
||||
|
||||
@Schema(description = "SSH端口", requiredMode = Schema.RequiredMode.REQUIRED, example = "22")
|
||||
private String sshPort;
|
||||
private Date createDate;
|
||||
private Date updateDate;
|
||||
|
||||
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "2023-06-15T10:30:00")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@Schema(description = "更新时间", example = "2023-06-15T10:30:00")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
@Schema(description = "密码", example = "******")
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 计算代理是否在线
|
||||
*/
|
||||
public boolean isOnline() {
|
||||
if (status == null || lastHeartbeatTime == null) {
|
||||
return false;
|
||||
}
|
||||
// 假设5分钟内有心跳为在线
|
||||
long fiveMinutes = 5 * 60 * 1000;
|
||||
return Objects.equals(MachineProxyStatus.ONLINE.getMessage(), status) &&
|
||||
System.currentTimeMillis() - lastHeartbeatTime.getTime() < fiveMinutes;
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package cd.casic.module.machine.controller.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PageDto {
|
||||
private int pageIndex = 1;
|
||||
|
||||
private int pageSize = 10;
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
package cd.casic.module.machine.dal.dataobject;
|
||||
|
||||
import cd.casic.framework.mybatis.core.dataobject.BaseDO;
|
||||
import cd.casic.module.machine.enums.MachineProxyStatus;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import cd.casic.module.machine.enums.MachineProxyType;
|
||||
import lombok.*;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
@ -13,12 +15,18 @@ import java.util.Date;
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@TableName("machine_proxy")
|
||||
public class MachineProxy extends BaseEntity implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
public class MachineProxyDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 代理id
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
|
||||
@TableField(value = "host_ip")
|
||||
private String hostIp;
|
||||
@ -26,43 +34,23 @@ public class MachineProxy extends BaseEntity implements Serializable {
|
||||
@TableField(value = "ssh_port")
|
||||
private String sshPort;
|
||||
|
||||
/**
|
||||
* 代理类型
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private MachineProxyType proxyType;
|
||||
//todo 字典??
|
||||
@TableField(value = "proxy_type")
|
||||
private int proxyType;
|
||||
|
||||
@TableField(value = "proxy_type_code")
|
||||
private int proxyTypeCode;
|
||||
|
||||
/**
|
||||
* 代理版本
|
||||
*/
|
||||
@TableField(value = "version")
|
||||
private String version;
|
||||
|
||||
/**
|
||||
* 代理状态 (online, offline, installing, updating, error)
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private MachineProxyStatus status;
|
||||
|
||||
@TableField(value = "status_code")
|
||||
private int statusCode;
|
||||
//todo 字典??
|
||||
@TableField(value = "status")
|
||||
private int status;
|
||||
|
||||
@TableField(value = "username")
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 最后心跳时间
|
||||
*/
|
||||
private Date lastHeartbeatTime;
|
||||
|
||||
@TableField(value = "password")
|
||||
private String password;
|
||||
/**
|
||||
* 描述信息
|
||||
*/
|
||||
|
||||
@TableField(value = "description")
|
||||
private String description;
|
||||
|
@ -1,11 +1,44 @@
|
||||
package cd.casic.module.machine.dal.mysql;
|
||||
import cd.casic.module.machine.dal.dataobject.MachineProxy;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import cd.casic.framework.commons.pojo.PageResult;
|
||||
import cd.casic.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cd.casic.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cd.casic.module.machine.controller.vo.MachineEnvVO;
|
||||
import cd.casic.module.machine.controller.vo.MachineProxyVO;
|
||||
import cd.casic.module.machine.dal.dataobject.MachineEnvDO;
|
||||
import cd.casic.module.machine.dal.dataobject.MachineProxyDO;
|
||||
import cd.casic.module.machine.enums.MachineProxyStatus;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.baomidou.mybatisplus.extension.toolkit.Db.listObjs;
|
||||
|
||||
/**
|
||||
* 机器代理Mapper接口
|
||||
*/
|
||||
@Mapper
|
||||
public interface MachineProxyMapper extends BaseMapper<MachineProxy> {
|
||||
public interface MachineProxyMapper extends BaseMapperX<MachineProxyDO> {
|
||||
|
||||
|
||||
|
||||
default PageResult<MachineProxyDO> selectPage(MachineProxyVO machineProxyVO) {
|
||||
return selectPage(machineProxyVO, new LambdaQueryWrapperX<MachineProxyDO>()
|
||||
.eqIfPresent(MachineProxyDO::getHostIp, machineProxyVO.getHostIp())
|
||||
.eqIfPresent(MachineProxyDO::getProxyType, machineProxyVO.getProxyType())
|
||||
.eqIfPresent(MachineProxyDO::getStatus, machineProxyVO.getStatus())
|
||||
.likeIfPresent(MachineProxyDO::getDescription, machineProxyVO.getDescription())
|
||||
);
|
||||
}
|
||||
default List<Long> getOnlineProxyListInIds(List<Long> ids) {
|
||||
LambdaQueryWrapperX<MachineProxyDO> queryWrapper = new LambdaQueryWrapperX<>();
|
||||
|
||||
// 1. 状态为在线(eqIfPresent 确保状态非空时拼接条件)
|
||||
queryWrapper.eqIfPresent(MachineProxyDO::getStatus, MachineProxyStatus.ONLINE.getCode());
|
||||
|
||||
// 2. 传入的 ID 列表非空时,添加 IN 条件(使用 inIfPresent 自动过滤空值)
|
||||
queryWrapper.inIfPresent(MachineProxyDO::getId, ids);
|
||||
|
||||
// 4. 仅查询 ID 字段,提升性能
|
||||
return listObjs(queryWrapper.select(MachineProxyDO::getId));
|
||||
}
|
||||
}
|
@ -1,17 +1,30 @@
|
||||
package cd.casic.module.machine.enums;
|
||||
|
||||
|
||||
import cd.casic.framework.commons.core.IntArrayValuable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum MachineProxyType implements CodeEnum {
|
||||
public enum MachineProxyType implements IntArrayValuable {
|
||||
HTTP(1, "http"),
|
||||
SOCKS4(2, "socks4"),
|
||||
SOCKS5(3, "socks5");
|
||||
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(MachineProxyType::getCode).toArray();
|
||||
|
||||
private final int code;
|
||||
|
||||
/**
|
||||
* 状态值
|
||||
*/
|
||||
private final Integer code;
|
||||
/**
|
||||
* 状态名
|
||||
*/
|
||||
private final String message;
|
||||
|
||||
|
||||
@Override
|
||||
public int[] array() {
|
||||
return ARRAYS;
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +0,0 @@
|
||||
package cd.casic.module.machine.enums;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum RequestExceptionEnum implements CodeEnum {
|
||||
REQUEST_TYPE_NOT_JSON(1, "传递参数格式错误,请使用json格式"),
|
||||
REQUEST_JSON_ERROR(2, "json格式错误"),
|
||||
REQUEST_METHOD_NOT_POST(3, "不支持该请求方法,请求方法应为POST"),
|
||||
REQUEST_METHOD_NOT_GET(4, "不支持该请求方法,请求方法应为GET"),
|
||||
PARAM_ERROR(5, "参数错误");
|
||||
|
||||
private final int code;
|
||||
|
||||
private final String message;
|
||||
}
|
@ -2,24 +2,23 @@ package cd.casic.module.machine.service;
|
||||
|
||||
import cd.casic.framework.commons.pojo.PageResult;
|
||||
import cd.casic.module.machine.controller.vo.MachineProxyVO;
|
||||
import cd.casic.module.machine.dal.dataobject.MachineProxy;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import cd.casic.module.machine.dal.dataobject.MachineProxyDO;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 机器代理服务接口
|
||||
*/
|
||||
public interface MachineProxyService extends IService<MachineProxy> {
|
||||
public interface MachineProxyService {
|
||||
/**
|
||||
* 注册新的机器代理
|
||||
*/
|
||||
boolean register(MachineProxyVO machineProxyVO);
|
||||
Long createProxy(MachineProxyVO machineProxyVO);
|
||||
|
||||
/**
|
||||
* 更新代理状态
|
||||
*/
|
||||
boolean updateStatus(MachineProxyVO machineProxyVO);
|
||||
void updateProxy(MachineProxyVO machineProxyVO);
|
||||
|
||||
|
||||
/**
|
||||
@ -27,12 +26,8 @@ public interface MachineProxyService extends IService<MachineProxy> {
|
||||
*
|
||||
* @return 状态统计Map
|
||||
*/
|
||||
Map<String, Long> getStatusStatistics();
|
||||
Map<Integer, Long> getAllProxyStatus();
|
||||
|
||||
/**
|
||||
* 更新代理配置
|
||||
*/
|
||||
void update(MachineProxyVO machineProxyVO);
|
||||
|
||||
|
||||
/**
|
||||
@ -40,7 +35,7 @@ public interface MachineProxyService extends IService<MachineProxy> {
|
||||
*
|
||||
* @param proxyIds 代理ID列表
|
||||
*/
|
||||
void delete(String proxyIds);
|
||||
void deleteProxyList(String proxyIds);
|
||||
|
||||
PageResult<MachineProxyVO> list(MachineProxyVO machineProxyVO);
|
||||
PageResult<MachineProxyDO> getProxyPage(MachineProxyVO machineProxyVO);
|
||||
}
|
||||
|
@ -1,161 +1,132 @@
|
||||
//package cd.casic.module.machine.service.impl;
|
||||
//
|
||||
//import cd.casic.module.machine.controller.vo.MachineProxyDTO;
|
||||
//import cd.casic.module.machine.dal.dataobject.MachineProxy;
|
||||
//import cd.casic.module.machine.enums.MachineProxyStatus;
|
||||
//import cd.casic.module.machine.enums.MachineProxyType;
|
||||
//import cd.casic.module.machine.exception.ServiceException;
|
||||
//import cd.casic.module.machine.dal.mysql.MachineProxyMapper;
|
||||
//import cd.casic.module.machine.service.MachineProxyService;
|
||||
//import cd.casic.module.machine.utils.EnumUtils;
|
||||
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
//
|
||||
//import jakarta.annotation.Resource;
|
||||
//import org.springframework.beans.BeanUtils;
|
||||
//import org.springframework.stereotype.Service;
|
||||
//import org.springframework.transaction.annotation.Transactional;
|
||||
//import org.springframework.util.CollectionUtils;
|
||||
//
|
||||
//import java.util.*;
|
||||
//import java.util.function.Function;
|
||||
//import java.util.stream.Collectors;
|
||||
//
|
||||
///**
|
||||
// * 机器代理服务实现类
|
||||
// */
|
||||
//@Service
|
||||
//public class MachineProxyServiceImpl extends ServiceImpl<MachineProxyMapper, MachineProxy> implements MachineProxyService {
|
||||
// @Resource
|
||||
// private MachineProxyMapper machineProxyMapper;
|
||||
//
|
||||
// @Override
|
||||
// @Transactional(rollbackFor = Exception.class)
|
||||
// public boolean register(MachineProxyDTO machineProxyDTO) {
|
||||
// // 创建代理记录
|
||||
// MachineProxy proxy = new MachineProxy();
|
||||
// BeanUtils.copyProperties(machineProxyDTO, proxy);
|
||||
// proxy.setProxyTypeCode(EnumUtils.getEnumByMessage(machineProxyDTO.getProxyType(), MachineProxyType.class).getCode());
|
||||
// proxy.setVersion("1.0.0");
|
||||
// proxy.setStatusCode(MachineProxyStatus.ONLINE.getCode());
|
||||
// return save(proxy);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public boolean updateStatus(MachineProxyDTO machineProxyDTO) {
|
||||
// // 参数校验
|
||||
// if (machineProxyDTO == null) {
|
||||
// throw new ServiceException(ServiceException.MACHINE_PROXY_DTO_NULL, "MachineProxyDTO对象为空");
|
||||
// }
|
||||
//
|
||||
// // 查询代理
|
||||
// MachineProxy proxy = this.getById(machineProxyDTO.getId());
|
||||
//
|
||||
// if (proxy == null) {
|
||||
// throw new ServiceException(ServiceException.MACHINE_PROXY_NULL, "代理不存在");
|
||||
// }
|
||||
//
|
||||
// // 更新状态
|
||||
// proxy.setStatusCode(EnumUtils.getEnumByMessage(machineProxyDTO.getStatus(), MachineProxyStatus.class).getCode());
|
||||
// proxy.setUpdateTime(new Date());
|
||||
// return updateById(proxy);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @Override
|
||||
// public Map<String, Long> getStatusStatistics() {
|
||||
// List<MachineProxy> proxyList = list();
|
||||
//
|
||||
// if (CollectionUtils.isEmpty(proxyList)) {
|
||||
// return Collections.emptyMap();
|
||||
// }
|
||||
// return proxyList.stream()
|
||||
// .map(proxy -> EnumUtils.getEnumByCode(proxy.getStatusCode(), MachineProxyStatus.class).getMessage())
|
||||
// .collect(Collectors.groupingBy(
|
||||
// Function.identity(),
|
||||
// Collectors.counting() // 统计每个分组的元素数量
|
||||
// ));
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void update(MachineProxyDTO machineProxyDTO) {
|
||||
// // 参数校验
|
||||
// if (machineProxyDTO == null) {
|
||||
// throw new ServiceException(ServiceException.MACHINE_PROXY_DTO_NULL, "MachineProxyDTO对象为空");
|
||||
// }
|
||||
// MachineProxy machineProxy = new MachineProxy();
|
||||
// BeanUtils.copyProperties(machineProxyDTO, machineProxy);
|
||||
// if (machineProxyDTO.getProxyType() != null && !machineProxyDTO.getProxyType().isEmpty()) {
|
||||
// machineProxy.setProxyTypeCode(EnumUtils.getEnumByMessage(machineProxyDTO.getProxyType(), MachineProxyType.class).getCode());
|
||||
// }
|
||||
// if (machineProxyDTO.getStatus() != null && !machineProxyDTO.getStatus().isEmpty()) {
|
||||
// machineProxy.setStatusCode(EnumUtils.getEnumByMessage(machineProxyDTO.getStatus(), MachineProxyStatus.class).getCode());
|
||||
// }
|
||||
// this.updateById(machineProxy);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @Override
|
||||
// @Transactional(rollbackFor = Exception.class)
|
||||
// public void delete(String ids) {
|
||||
// List<Long> machineProxyIds = Arrays.stream(ids.split(","))
|
||||
// .map(String::trim)
|
||||
// .filter(s -> !s.isEmpty())
|
||||
// .map(Long::parseLong)
|
||||
// .toList();
|
||||
// // 参数校验
|
||||
// if (CollectionUtils.isEmpty(machineProxyIds)) {
|
||||
// throw new ServiceException(ServiceException.PARAMETER_ERROR, "参数错误");
|
||||
// }
|
||||
// // 批量逻辑删除
|
||||
// remove(new LambdaQueryWrapper<MachineProxy>()
|
||||
// .in(MachineProxy::getId, machineProxyIds)
|
||||
// .ne(MachineProxy::getStatus, MachineProxyStatus.ONLINE.getCode()));
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public PageResult<MachineProxyDTO> list(MachineProxyDTO machineProxyDTO) {
|
||||
// QueryWrapper<MachineProxy> queryWrapper = getMachineProxyQueryWrapper(machineProxyDTO);
|
||||
// Page<MachineProxy> page = machineProxyMapper.selectPage(new Page<>(machineProxyDTO.getPageIndex(), machineProxyDTO.getPageSize()), queryWrapper);
|
||||
// List<MachineProxyDTO> machineProxyDtos = page.getRecords().stream().map(machineProxy -> {
|
||||
// MachineProxyDTO dto = new MachineProxyDTO();
|
||||
// BeanUtils.copyProperties(machineProxy, dto);
|
||||
// dto.setProxyType(EnumUtils.getEnumByCode(machineProxy.getProxyTypeCode(), MachineProxyType.class).getMessage());
|
||||
// dto.setStatus(EnumUtils.getEnumByCode(machineProxy.getStatusCode(), MachineProxyStatus.class).getMessage());
|
||||
// return dto;
|
||||
// }).toList();
|
||||
// return new PageResult<>(
|
||||
// page.getCurrent(),
|
||||
// page.getSize(),
|
||||
// page.getTotal(),
|
||||
// page.getPages(),
|
||||
// machineProxyDtos
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// private QueryWrapper<MachineProxy> getMachineProxyQueryWrapper(MachineProxyDTO machineProxyDTO) {
|
||||
// QueryWrapper<MachineProxy> queryWrapper = new QueryWrapper<>();
|
||||
// if (machineProxyDTO.getHostIp() != null && !machineProxyDTO.getHostIp().isEmpty()) {
|
||||
// queryWrapper.like("host_ip", machineProxyDTO.getHostIp());
|
||||
// }
|
||||
// if (machineProxyDTO.getSshPort() != null && !machineProxyDTO.getSshPort().isEmpty()) {
|
||||
// queryWrapper.like("ssh_port", machineProxyDTO.getSshPort());
|
||||
// }
|
||||
// if (machineProxyDTO.getUsername() != null && !machineProxyDTO.getUsername().isEmpty()) {
|
||||
// queryWrapper.like("username", machineProxyDTO.getUsername());
|
||||
// }
|
||||
// if (machineProxyDTO.getDescription() != null && !machineProxyDTO.getDescription().isEmpty()) {
|
||||
// queryWrapper.like("description", machineProxyDTO.getDescription());
|
||||
// }
|
||||
// if (machineProxyDTO.getStatus() != null && !machineProxyDTO.getStatus().isEmpty()) {
|
||||
// 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");
|
||||
// }
|
||||
//
|
||||
//}
|
||||
package cd.casic.module.machine.service.impl;
|
||||
import cd.casic.framework.commons.pojo.PageResult;
|
||||
import cd.casic.module.machine.controller.vo.MachineProxyVO;
|
||||
import cd.casic.module.machine.dal.dataobject.MachineProxyDO;
|
||||
import cd.casic.module.machine.enums.MachineProxyStatus;
|
||||
import cd.casic.module.machine.enums.MachineProxyType;
|
||||
import cd.casic.module.machine.dal.mysql.MachineProxyMapper;
|
||||
import cd.casic.module.machine.service.MachineProxyService;
|
||||
import cd.casic.module.machine.utils.EnumUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import jakarta.annotation.Resource;
|
||||
import cd.casic.framework.commons.util.object.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cd.casic.framework.commons.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cd.casic.module.machine.contants.MachineErrorCodeConstants.*;
|
||||
import static com.baomidou.mybatisplus.extension.toolkit.Db.save;
|
||||
import static org.apache.catalina.security.SecurityUtil.remove;
|
||||
|
||||
/**
|
||||
* 机器代理服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class MachineProxyServiceImpl implements MachineProxyService {
|
||||
@Resource
|
||||
private MachineProxyMapper machineProxyMapper;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long createProxy(MachineProxyVO machineProxyVO) {
|
||||
validateMachineProxyAdd(machineProxyVO);
|
||||
// 创建代理记录
|
||||
MachineProxyDO machineProxyDO = BeanUtils.toBean(machineProxyVO, MachineProxyDO.class);;
|
||||
save(machineProxyDO);
|
||||
return machineProxyDO.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateProxy(MachineProxyVO machineProxyVO) {
|
||||
// 参数校验
|
||||
MachineProxyDO machineProxyDO = validateMachineProxyExists(machineProxyVO.getId());
|
||||
// 更新状态
|
||||
BeanUtils.copyProperties(machineProxyVO, machineProxyDO);
|
||||
machineProxyMapper.updateById(machineProxyDO);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Map<Integer, Long> getAllProxyStatus() {
|
||||
List<MachineProxyDO> proxyList = machineProxyMapper.selectList(new QueryWrapper<>());
|
||||
if (CollectionUtils.isEmpty(proxyList)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return proxyList.stream()
|
||||
.map(MachineProxyDO::getStatus)
|
||||
.collect(Collectors.groupingBy(
|
||||
Function.identity(),
|
||||
// 统计每个分组的元素数量
|
||||
Collectors.counting()
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteProxyList(String ids) {
|
||||
if (ids == null) {
|
||||
return;
|
||||
}
|
||||
List<Long> machineProxyIds = Arrays.stream(ids.split(","))
|
||||
.map(String::trim)
|
||||
.filter(s -> !s.isEmpty())
|
||||
.map(Long::parseLong)
|
||||
.toList();
|
||||
//检查是否存在在线的代理
|
||||
validateMachineProxyOnline(machineProxyIds);
|
||||
// 批量逻辑删除
|
||||
machineProxyMapper.deleteBatchIds(machineProxyIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<MachineProxyDO> getProxyPage(MachineProxyVO machineProxyVO) {
|
||||
return machineProxyMapper.selectPage(machineProxyVO);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@VisibleForTesting
|
||||
MachineProxyDO validateMachineProxyExists(Long id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
MachineProxyDO machineProxyDO = machineProxyMapper.selectById(id);
|
||||
if (machineProxyDO == null) {
|
||||
throw exception(MACHINE_PROXY_NOT_EXISTS);
|
||||
}
|
||||
return machineProxyDO;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void validateMachineProxyOnline(List<Long> ids) {
|
||||
List<Long> idList = machineProxyMapper.getOnlineProxyListInIds(ids);
|
||||
if(!idList.isEmpty()){
|
||||
throw exception(MACHINE_PROXY_IS_ONLINE,idList);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void validateMachineProxyAdd(MachineProxyVO machineProxyVO) {
|
||||
if (machineProxyVO.getHostIp()==null) {
|
||||
throw exception(MACHINE_PROXY_HOST_IP_NULL);
|
||||
}
|
||||
if (machineProxyVO.getUsername()==null) {
|
||||
throw exception(MACHINE_PROXY_USER_NAME_NULL);
|
||||
}
|
||||
|
||||
// 校验代理类型
|
||||
int[] arrays = MachineProxyType.ARRAYS;
|
||||
if (Arrays.stream(arrays).filter(i -> i == machineProxyVO.getProxyType()).findAny().isEmpty()) {
|
||||
throw exception(MACHINE_PROXY_TYPE_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user