2026-03-26提交:新增备份接口

This commit is contained in:
junzhangfm 2026-03-26 18:18:08 +08:00
parent b49bab1ccc
commit 7ac794a68f
5 changed files with 211 additions and 32 deletions

View File

@ -1,29 +1,122 @@
package com.southern.power.grid.common; package com.southern.power.grid.common;
import com.southern.power.grid.enums.ResultCode;
import lombok.Data; import lombok.Data;
@Data @Data
public class Result<T> { public class Result<T> {
private String code; /**
private String message; * 响应状态码
*/
private int code;
/**
* 响应提示信息
*/
private String msg;
/**
* 响应数据
*/
private T data; private T data;
/**
* 私有化构造器禁止外部直接创建
*/
private Result() {}
/**
* 快速构建成功响应无数据
*/
public static <T> Result<T> success() {
return buildResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
}
/**
* 快速构建成功响应带数据
*/
public static <T> Result<T> success(T data) { public static <T> Result<T> success(T data) {
return buildResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
}
/**
* 快速构建成功响应自定义提示+数据
*/
public static <T> Result<T> success(String msg, T data) {
return buildResult(ResultCode.SUCCESS.getCode(), msg, data);
}
/**
* 快速构建失败响应默认提示
*/
public static <T> Result<T> fail() {
return buildResult(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMsg(), null);
}
/**
* 快速构建失败响应自定义提示
*/
public static <T> Result<T> fail(String msg) {
return buildResult(ResultCode.FAIL.getCode(), msg, null);
}
/**
* 快速构建失败响应自定义状态码+提示
*/
public static <T> Result<T> fail(int code, String msg) {
return buildResult(code, msg, null);
}
/**
* 基于枚举构建响应
*/
public static <T> Result<T> fail(ResultCode resultCode) {
return buildResult(resultCode.getCode(), resultCode.getMsg(), null);
}
/**
* 链式构建响应灵活配置
*/
public static <T> ResultBuilder<T> builder() {
return new ResultBuilder<>();
}
/**
* 核心构建方法
*/
private static <T> Result<T> buildResult(int code, String msg, T data) {
Result<T> result = new Result<>(); Result<T> result = new Result<>();
result.setCode("ok"); result.setCode(code);
result.setMessage("success"); result.setMsg(msg);
result.setData(data); result.setData(data);
return result; return result;
} }
public static <T> Result<T> error(String message) { /**
return error("error", message); * 链式构建器可选简化多字段配置
} */
public static class ResultBuilder<T> {
private int code;
private String msg;
private T data;
public static <T> Result<T> error(String code, String message) { public ResultBuilder<T> code(int code) {
Result<T> result = new Result<>(); this.code = code;
result.setCode(code); return this;
result.setMessage(message); }
return result;
public ResultBuilder<T> msg(String msg) {
this.msg = msg;
return this;
}
public ResultBuilder<T> data(T data) {
this.data = data;
return this;
}
public Result<T> build() {
return buildResult(code, msg, data);
}
} }
} }

View File

@ -35,7 +35,7 @@ public class DnerEventController {
*/ */
@DeleteMapping("/delete/{id}") @DeleteMapping("/delete/{id}")
public Result<Boolean> delete(@PathVariable Long id) { public Result<Boolean> delete(@PathVariable Long id) {
return Result.success(dnerEventService.removeDataById(id)); return Result.success(dnerEventService.removeById(id));
} }
/** /**
@ -61,4 +61,9 @@ public class DnerEventController {
public Result<List<DnerEventVO>> list() { public Result<List<DnerEventVO>> list() {
return Result.success(dnerEventService.listVO()); return Result.success(dnerEventService.listVO());
} }
@GetMapping("/copy/{eventId}")
public Result<Boolean> copy(@PathVariable Long eventId) {
return Result.success(dnerEventService.copy(eventId));
}
} }

View File

@ -0,0 +1,35 @@
package com.southern.power.grid.enums;
import lombok.Getter;
/**
* 状态码枚举
*
* @Author junzhangfm
* @Date 2026-03-26
*/
@Getter
public enum ResultCode {
// 通用状态码
SUCCESS(200, "操作成功"),
FAIL(500, "操作失败"),
PARAM_ERROR(400, "参数错误"),
UNAUTHORIZED(401, "未授权"),
FORBIDDEN(403, "禁止访问"),
NOT_FOUND(404, "资源不存在"),
// 业务自定义状态码按需扩展
FILE_UPLOAD_FAIL(10001, "文件上传失败"),
FILE_DELETE_FAIL(10002, "文件删除失败"),
DATA_NOT_EXIST(10003, "数据不存在");
// getter
private final int code;
private final String msg;
ResultCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
}

View File

@ -21,10 +21,10 @@ public interface DnerEventService extends IService<DnerEvent> {
List<DnerEventVO> listVO(); List<DnerEventVO> listVO();
/** /**
* 根据ID删除数据 * 备份事件
* *
* @param id 事件ID * @param eventId 事件ID
* @return 结果 * @return 是否成功
*/ */
Boolean removeDataById(Long id); Boolean copy(Long eventId);
} }

View File

@ -3,19 +3,18 @@ package com.southern.power.grid.service.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southern.power.grid.dao.DnerEventAttachmentMapper; import com.southern.power.grid.dao.*;
import com.southern.power.grid.dao.DnerEventExportRecordMapper; import com.southern.power.grid.entity.*;
import com.southern.power.grid.dao.DnerEventMapper;
import com.southern.power.grid.entity.DnerEvent;
import com.southern.power.grid.entity.DnerEventAttachment;
import com.southern.power.grid.entity.DnerEventExportRecord;
import com.southern.power.grid.entity.DnerEventVO;
import com.southern.power.grid.service.DnerEventService; import com.southern.power.grid.service.DnerEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -25,6 +24,7 @@ import java.util.stream.Collectors;
* @date: 2026/3/23 * @date: 2026/3/23
**/ **/
@Service @Service
@Slf4j
public class DnerEventServiceImpl extends ServiceImpl<DnerEventMapper, DnerEvent> public class DnerEventServiceImpl extends ServiceImpl<DnerEventMapper, DnerEvent>
implements DnerEventService { implements DnerEventService {
@Autowired @Autowired
@ -36,6 +36,12 @@ public class DnerEventServiceImpl extends ServiceImpl<DnerEventMapper, DnerEvent
@Autowired @Autowired
private DnerEventAttachmentMapper dnerEventAttachmentMapper; private DnerEventAttachmentMapper dnerEventAttachmentMapper;
@Autowired
private DnerHourlyPowerOutageEventMapper dnerHourlyPowerOutageEventMapper;
@Autowired
private DnerDailyPowerOutageEventMapper dnerDailyPowerOutageEventMapper;
@Override @Override
public List<DnerEventVO> listVO() { public List<DnerEventVO> listVO() {
List<DnerEventVO> dnerEventVOS = dnerEventMapper.listVO(); List<DnerEventVO> dnerEventVOS = dnerEventMapper.listVO();
@ -58,13 +64,53 @@ public class DnerEventServiceImpl extends ServiceImpl<DnerEventMapper, DnerEvent
} }
@Override @Override
public Boolean removeDataById(Long id) { @Transactional(rollbackFor = Exception.class)
// 根据事件ID查询事件的所有附件 public Boolean copy(Long eventId) {
List<DnerEventAttachment> dnerEventAttachments = dnerEventAttachmentMapper.selectList( // 查询事件附件并复制一份插入数据库 删除时附件不会被删除后续考虑用定时任务删除无用附件释放磁盘空间
new LambdaQueryWrapper<DnerEventAttachment>().eq(DnerEventAttachment::getEventId, id)); DnerEvent event = getById(eventId);
if (!Objects.isNull(event)) {
// 级联删除所有记录 event.setId(null);
removeById(id); event.setEventName(event.getEventName() + "-备份");
return null; LocalDateTime now = LocalDateTime.now();
event.setCreateTime(now);
event.setUpdateTime(now);
save(event);
// 附件只查询最新的一条记录
DnerEventAttachment eventAttachment = dnerEventAttachmentMapper.selectOne(
new LambdaQueryWrapper<DnerEventAttachment>().eq(DnerEventAttachment::getEventId, eventId)
.eq(DnerEventAttachment::getIsLatest, 1));
if (!Objects.isNull(eventAttachment)) {
eventAttachment.setId(null);
eventAttachment.setEventId(event.getId());
eventAttachment.setCreateTime(now);
dnerEventAttachmentMapper.insert(eventAttachment);
}
// 复制分时图和K线图数据
List<DnerHourlyPowerOutageEvent> hourlyPowerOutageEvents = dnerHourlyPowerOutageEventMapper.selectList(
new LambdaQueryWrapper<DnerHourlyPowerOutageEvent>().eq(
DnerHourlyPowerOutageEvent::getEventId, eventId));
if (!CollectionUtil.isEmpty(hourlyPowerOutageEvents)) {
hourlyPowerOutageEvents.forEach(e -> {
e.setId(null);
e.setEventId(event.getId());
e.setCreateTime(now);
e.setUpdateTime(now);
});
dnerHourlyPowerOutageEventMapper.insert(hourlyPowerOutageEvents);
}
List<DnerDailyPowerOutageEvent> dnerDailyPowerOutageEvents = dnerDailyPowerOutageEventMapper.selectList(
new LambdaQueryWrapper<DnerDailyPowerOutageEvent>().eq(
DnerDailyPowerOutageEvent::getEventId, eventId));
if (!CollectionUtil.isEmpty(dnerDailyPowerOutageEvents)) {
dnerDailyPowerOutageEvents.forEach(e -> {
e.setId(null);
e.setEventId(event.getId());
e.setCreateTime(now);
e.setUpdateTime(now);
});
dnerDailyPowerOutageEventMapper.insert(dnerDailyPowerOutageEvents);
}
}
return Boolean.TRUE;
} }
} }