feat: 增加远端服务器配置和下载的增删改查接口
parent
3a499165df
commit
7fc8867336
@ -0,0 +1,89 @@
|
||||
package com.xydl.cac.controller;
|
||||
|
||||
import com.xydl.cac.entity.RemoteConfig;
|
||||
import com.xydl.cac.entity.RemoteDownload;
|
||||
import com.xydl.cac.exception.BusinessException;
|
||||
import com.xydl.cac.model.Response;
|
||||
import com.xydl.cac.service.RemoteConfigService;
|
||||
import com.xydl.cac.service.RemoteDownloadService;
|
||||
import com.xydl.cac.util.SFTPTool;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@Api(tags = {"远端服务器相关接口"})
|
||||
@RequestMapping("remote")
|
||||
@Slf4j
|
||||
public class RemoteConfigController extends BasicController {
|
||||
|
||||
@Resource
|
||||
RemoteConfigService configService;
|
||||
@Resource
|
||||
RemoteDownloadService downloadService;
|
||||
@Resource
|
||||
SFTPTool sftpTool;
|
||||
|
||||
@GetMapping("listAll")
|
||||
@ApiOperation("查询全部列表")
|
||||
public Response<List<RemoteConfig>> listAll() {
|
||||
List<RemoteConfig> result = configService.listAll();
|
||||
return Response.success(result);
|
||||
}
|
||||
|
||||
@PostMapping("add")
|
||||
@ApiOperation("新增")
|
||||
public Response<RemoteConfig> add(@Validated @RequestBody RemoteConfig item) throws Exception {
|
||||
RemoteConfig result = configService.add(item);
|
||||
return Response.success(result);
|
||||
}
|
||||
|
||||
@PostMapping("update")
|
||||
@ApiOperation("更新")
|
||||
public Response<String> update(@Validated @RequestBody RemoteConfig item) throws Exception {
|
||||
if (item.getId() == null) {
|
||||
throw new BusinessException("id不能为空!");
|
||||
}
|
||||
configService.update(item);
|
||||
return Response.success("OK");
|
||||
}
|
||||
|
||||
@PostMapping("delete")
|
||||
@ApiOperation("删除")
|
||||
public Response<String> delete(@Validated @NotNull(message = "id不能为空!") Integer id) throws Exception {
|
||||
if (id == null) {
|
||||
throw new BusinessException("id不能为空!");
|
||||
}
|
||||
configService.delete(id);
|
||||
return Response.success("OK");
|
||||
}
|
||||
|
||||
@GetMapping("test")
|
||||
@ApiOperation("测试")
|
||||
public Response<String> test(Integer id) throws Exception {
|
||||
RemoteConfig item = configService.get(id);
|
||||
sftpTool.connect(item.getIp(), item.getPort(), item.getUser(), item.getPasswd());
|
||||
sftpTool.disconnect();
|
||||
return Response.success("正常");
|
||||
}
|
||||
|
||||
@GetMapping("listDownload")
|
||||
@ApiOperation("查询下载记录列表")
|
||||
public Response<Page<RemoteDownload>> list(@ApiParam("类型") @RequestParam(value = "configId", required = false) Integer configId,
|
||||
@ApiParam("页码") @RequestParam(value = "pageNum", required = false) Integer pageNum,
|
||||
@ApiParam("每页数量") @RequestParam(value = "pageSize", required = false) Integer pageSize) throws Exception {
|
||||
pageNum = this.initPageNum(pageNum);
|
||||
pageSize = this.initPageSize(pageSize);
|
||||
Page<RemoteDownload> result = downloadService.list(configId, pageNum, pageSize);
|
||||
return Response.success(result);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
package com.xydl.cac.entity;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "remote_config")
|
||||
@ApiModel("远端服务器信息表")
|
||||
public class RemoteConfig {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private Integer id;
|
||||
|
||||
@NotBlank(message = "名称不能为空")
|
||||
@ApiModelProperty("名称")
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@NotBlank(message = "IP不能为空")
|
||||
@ApiModelProperty("IP")
|
||||
@Column(name = "ip")
|
||||
private String ip;
|
||||
|
||||
@NotNull(message = "端口不能为空")
|
||||
@ApiModelProperty("端口")
|
||||
@Column(name = "port")
|
||||
private Integer port;
|
||||
|
||||
@NotBlank(message = "用户名不能为空")
|
||||
@ApiModelProperty("用户名")
|
||||
@Column(name = "user")
|
||||
private String user;
|
||||
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@ApiModelProperty("密码")
|
||||
@Column(name = "passwd")
|
||||
private String passwd;
|
||||
|
||||
@JsonIgnore
|
||||
@ApiModelProperty("目录")
|
||||
@Column(name = "path")
|
||||
private String path;
|
||||
|
||||
@ApiModelProperty("后缀名")
|
||||
@Column(name = "suffix")
|
||||
private String suffix;
|
||||
|
||||
@NotNull(message = "是否删除不能为空")
|
||||
@ApiModelProperty("下载后是否删除")
|
||||
@Column(name = "todel")
|
||||
private Integer todel;
|
||||
|
||||
@NotNull(message = "状态不能为空")
|
||||
@ApiModelProperty("状态 0:停用 1:启用")
|
||||
@Column(name = "active")
|
||||
private Integer active;
|
||||
|
||||
@Transient
|
||||
@ApiModelProperty("目录")
|
||||
List<String> pathList;
|
||||
|
||||
public void toList() {
|
||||
if (path != null) {
|
||||
pathList = Arrays.asList(path.split(","));
|
||||
}
|
||||
}
|
||||
|
||||
public void fromList() {
|
||||
if (pathList != null) {
|
||||
path = pathList.stream().collect(Collectors.joining(","));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.xydl.cac.entity;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "remote_download")
|
||||
@ApiModel("远端文件下载记录表")
|
||||
public class RemoteDownload {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty("服务器ID")
|
||||
@Column(name = "config_id")
|
||||
private Integer configId;
|
||||
|
||||
@ApiModelProperty("路径")
|
||||
@Column(name = "path")
|
||||
private String path;
|
||||
|
||||
@ApiModelProperty("文件名")
|
||||
@Column(name = "filename")
|
||||
private String filename;
|
||||
|
||||
@ApiModelProperty("创建时间")
|
||||
@Column(name = "create_time")
|
||||
private Date createTime;
|
||||
|
||||
@Transient
|
||||
RemoteConfig server;
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.xydl.cac.repository;
|
||||
|
||||
import com.xydl.cac.entity.RemoteConfig;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
|
||||
@Repository
|
||||
public interface RemoteConfigRepository extends JpaRepository<RemoteConfig, Integer>, JpaSpecificationExecutor<RemoteConfig> {
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.xydl.cac.repository;
|
||||
|
||||
import com.xydl.cac.entity.RemoteDownload;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
|
||||
@Repository
|
||||
public interface RemoteDownloadRepository extends JpaRepository<RemoteDownload, Integer>, JpaSpecificationExecutor<RemoteDownload> {
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.xydl.cac.service;
|
||||
|
||||
import com.xydl.cac.entity.RemoteConfig;
|
||||
import com.xydl.cac.exception.BusinessException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface RemoteConfigService {
|
||||
|
||||
List<RemoteConfig> listAll();
|
||||
|
||||
RemoteConfig get(Integer id) throws BusinessException;
|
||||
|
||||
RemoteConfig add(RemoteConfig item) throws Exception;
|
||||
|
||||
void update(RemoteConfig item) throws Exception;
|
||||
|
||||
void delete(Integer id);
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.xydl.cac.service;
|
||||
|
||||
import com.xydl.cac.entity.RemoteDownload;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
public interface RemoteDownloadService {
|
||||
|
||||
Page<RemoteDownload> list(Integer configId, int pageNum, int pageSize) throws Exception;
|
||||
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.xydl.cac.service.impl;
|
||||
|
||||
import com.xydl.cac.entity.RemoteConfig;
|
||||
import com.xydl.cac.entity.Zsb;
|
||||
import com.xydl.cac.exception.BusinessException;
|
||||
import com.xydl.cac.repository.RemoteConfigRepository;
|
||||
import com.xydl.cac.service.RemoteConfigService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class RemoteConfigServiceImpl implements RemoteConfigService {
|
||||
|
||||
@Resource
|
||||
RemoteConfigRepository repository;
|
||||
|
||||
@Override
|
||||
public List<RemoteConfig> listAll() {
|
||||
List<RemoteConfig> list = repository.findAll();
|
||||
for (RemoteConfig server : list) {
|
||||
server.toList();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteConfig get(Integer id) throws BusinessException {
|
||||
Optional<RemoteConfig> optional = repository.findById(id);
|
||||
if (!optional.isPresent()) {
|
||||
throw new BusinessException("未找到该记录");
|
||||
}
|
||||
return optional.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteConfig add(RemoteConfig item) throws Exception {
|
||||
item.setId(null);
|
||||
item.fromList();
|
||||
return repository.save(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(RemoteConfig item) throws Exception {
|
||||
item.fromList();
|
||||
repository.save(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Integer id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.xydl.cac.service.impl;
|
||||
|
||||
import com.xydl.cac.entity.*;
|
||||
import com.xydl.cac.repository.RemoteConfigRepository;
|
||||
import com.xydl.cac.repository.RemoteDownloadRepository;
|
||||
import com.xydl.cac.service.RemoteDownloadService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class RemoteDownloadServiceImpl implements RemoteDownloadService {
|
||||
|
||||
@Resource
|
||||
RemoteDownloadRepository repository;
|
||||
@Resource
|
||||
RemoteConfigRepository configRepository;
|
||||
|
||||
@Override
|
||||
public Page<RemoteDownload> list(Integer configId, int pageNum, int pageSize) throws Exception {
|
||||
PageRequest request = PageRequest.of(pageNum - 1, pageSize);
|
||||
Specification<RemoteDownload> specification = (root, query, builder) -> {
|
||||
Predicate predicate = builder.conjunction();
|
||||
if (configId != null) {
|
||||
predicate.getExpressions().add(builder.equal(root.get("configId"), configId));
|
||||
}
|
||||
query.orderBy(builder.asc(root.get("id")));
|
||||
return predicate;
|
||||
};
|
||||
Page<RemoteDownload> result = repository.findAll(specification, request);
|
||||
this.fillOtherNames(result.getContent());
|
||||
return result;
|
||||
}
|
||||
|
||||
private void fillOtherNames(List<RemoteDownload> list) {
|
||||
List<RemoteConfig> serverList = configRepository.findAll();
|
||||
if (!CollectionUtils.isEmpty(serverList) && !CollectionUtils.isEmpty(list)) {
|
||||
for (RemoteDownload item : list) {
|
||||
for (RemoteConfig server : serverList) {
|
||||
if (server.getId().equals(item.getConfigId())) {
|
||||
item.setServer(server);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue