2026-03-26提交:新增备份接口
This commit is contained in:
parent
b49bab1ccc
commit
7ac794a68f
@ -1,29 +1,122 @@
|
||||
package com.southern.power.grid.common;
|
||||
|
||||
import com.southern.power.grid.enums.ResultCode;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Result<T> {
|
||||
private String code;
|
||||
private String message;
|
||||
/**
|
||||
* 响应状态码
|
||||
*/
|
||||
private int code;
|
||||
|
||||
/**
|
||||
* 响应提示信息
|
||||
*/
|
||||
private String msg;
|
||||
|
||||
/**
|
||||
* 响应数据
|
||||
*/
|
||||
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) {
|
||||
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.setCode("ok");
|
||||
result.setMessage("success");
|
||||
result.setCode(code);
|
||||
result.setMsg(msg);
|
||||
result.setData(data);
|
||||
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) {
|
||||
Result<T> result = new Result<>();
|
||||
result.setCode(code);
|
||||
result.setMessage(message);
|
||||
return result;
|
||||
public ResultBuilder<T> code(int code) {
|
||||
this.code = code;
|
||||
return this;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ public class DnerEventController {
|
||||
*/
|
||||
@DeleteMapping("/delete/{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() {
|
||||
return Result.success(dnerEventService.listVO());
|
||||
}
|
||||
|
||||
@GetMapping("/copy/{eventId}")
|
||||
public Result<Boolean> copy(@PathVariable Long eventId) {
|
||||
return Result.success(dnerEventService.copy(eventId));
|
||||
}
|
||||
}
|
||||
|
||||
35
src/main/java/com/southern/power/grid/enums/ResultCode.java
Normal file
35
src/main/java/com/southern/power/grid/enums/ResultCode.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -21,10 +21,10 @@ public interface DnerEventService extends IService<DnerEvent> {
|
||||
List<DnerEventVO> listVO();
|
||||
|
||||
/**
|
||||
* 根据ID删除数据
|
||||
* 备份事件
|
||||
*
|
||||
* @param id 事件ID
|
||||
* @return 结果
|
||||
* @param eventId 事件ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
Boolean removeDataById(Long id);
|
||||
Boolean copy(Long eventId);
|
||||
}
|
||||
|
||||
@ -3,19 +3,18 @@ package com.southern.power.grid.service.impl;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.southern.power.grid.dao.DnerEventAttachmentMapper;
|
||||
import com.southern.power.grid.dao.DnerEventExportRecordMapper;
|
||||
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.dao.*;
|
||||
import com.southern.power.grid.entity.*;
|
||||
import com.southern.power.grid.service.DnerEventService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -25,6 +24,7 @@ import java.util.stream.Collectors;
|
||||
* @date: 2026/3/23
|
||||
**/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class DnerEventServiceImpl extends ServiceImpl<DnerEventMapper, DnerEvent>
|
||||
implements DnerEventService {
|
||||
@Autowired
|
||||
@ -36,6 +36,12 @@ public class DnerEventServiceImpl extends ServiceImpl<DnerEventMapper, DnerEvent
|
||||
@Autowired
|
||||
private DnerEventAttachmentMapper dnerEventAttachmentMapper;
|
||||
|
||||
@Autowired
|
||||
private DnerHourlyPowerOutageEventMapper dnerHourlyPowerOutageEventMapper;
|
||||
|
||||
@Autowired
|
||||
private DnerDailyPowerOutageEventMapper dnerDailyPowerOutageEventMapper;
|
||||
|
||||
@Override
|
||||
public List<DnerEventVO> listVO() {
|
||||
List<DnerEventVO> dnerEventVOS = dnerEventMapper.listVO();
|
||||
@ -58,13 +64,53 @@ public class DnerEventServiceImpl extends ServiceImpl<DnerEventMapper, DnerEvent
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean removeDataById(Long id) {
|
||||
// 根据事件ID查询事件的所有附件
|
||||
List<DnerEventAttachment> dnerEventAttachments = dnerEventAttachmentMapper.selectList(
|
||||
new LambdaQueryWrapper<DnerEventAttachment>().eq(DnerEventAttachment::getEventId, id));
|
||||
|
||||
// 级联删除所有记录
|
||||
removeById(id);
|
||||
return null;
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean copy(Long eventId) {
|
||||
// 查询事件、附件,并复制一份插入数据库。 删除时附件不会被删除,后续考虑用定时任务删除无用附件释放磁盘空间
|
||||
DnerEvent event = getById(eventId);
|
||||
if (!Objects.isNull(event)) {
|
||||
event.setId(null);
|
||||
event.setEventName(event.getEventName() + "-备份");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user