2026-03-27提交:下载导入模板
This commit is contained in:
parent
ceff36d410
commit
8fda70baf9
@ -5,8 +5,12 @@ import com.southern.power.grid.entity.DnerEvent;
|
||||
import com.southern.power.grid.entity.DnerEventVO;
|
||||
import com.southern.power.grid.service.DnerEventService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -31,11 +35,11 @@ public class DnerEventController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID删除事件(级联删除附件、导出记录)
|
||||
* 根据ID删除事件(还包括删除附件和导出记录)
|
||||
*/
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public Result<Boolean> delete(@PathVariable Long id) {
|
||||
return Result.success(dnerEventService.removeById(id));
|
||||
return Result.success(dnerEventService.removeDataById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -43,6 +47,7 @@ public class DnerEventController {
|
||||
*/
|
||||
@PutMapping("/update")
|
||||
public Result<Boolean> update(@RequestBody DnerEvent event) {
|
||||
event.setUpdateTime(LocalDateTime.now());
|
||||
return Result.success(dnerEventService.updateById(event));
|
||||
}
|
||||
|
||||
@ -62,8 +67,35 @@ public class DnerEventController {
|
||||
return Result.success(dnerEventService.listVO());
|
||||
}
|
||||
|
||||
/**
|
||||
* 备份
|
||||
*
|
||||
* @param eventId 事件ID
|
||||
* @return 结果
|
||||
*/
|
||||
@GetMapping("/copy/{eventId}")
|
||||
public Result<Boolean> copy(@PathVariable Long eventId) {
|
||||
return Result.success(dnerEventService.copy(eventId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入模板
|
||||
*
|
||||
* @param file 模板文件
|
||||
* @return 结果
|
||||
*/
|
||||
@PostMapping("/uploadTemplate")
|
||||
public Result<Boolean> uploadTemplate(@RequestParam("file") MultipartFile file) {
|
||||
return Result.success(dnerEventService.uploadTemplate(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载模板
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@PostMapping("/downloadTemplate")
|
||||
public ResponseEntity<Resource> downloadTemplate() {
|
||||
return dnerEventService.downloadTemplate();
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,9 @@ package com.southern.power.grid.service;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.southern.power.grid.entity.DnerEvent;
|
||||
import com.southern.power.grid.entity.DnerEventVO;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -27,4 +30,27 @@ public interface DnerEventService extends IService<DnerEvent> {
|
||||
* @return 是否成功
|
||||
*/
|
||||
Boolean copy(Long eventId);
|
||||
|
||||
/**
|
||||
* 删除事件、附件、导出记录
|
||||
*
|
||||
* @param id 事件ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
Boolean removeDataById(Long id);
|
||||
|
||||
/**
|
||||
* 上传附件
|
||||
*
|
||||
* @param file 附件
|
||||
* @return 是否成功
|
||||
*/
|
||||
Boolean uploadTemplate(MultipartFile file);
|
||||
|
||||
/**
|
||||
* 下载模板
|
||||
*
|
||||
* @return 响应体
|
||||
*/
|
||||
ResponseEntity<Resource> downloadTemplate();
|
||||
}
|
||||
|
||||
@ -18,4 +18,11 @@ public interface IFileService {
|
||||
* @param eventId 事件ID
|
||||
*/
|
||||
Long uploadExcel(MultipartFile file, Long eventId) throws IOException;
|
||||
|
||||
/**
|
||||
* 上传导入模板
|
||||
*
|
||||
* @param file 文件
|
||||
*/
|
||||
void uploadTemplate(MultipartFile file) throws IOException;
|
||||
}
|
||||
|
||||
@ -5,12 +5,19 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.southern.power.grid.dao.*;
|
||||
import com.southern.power.grid.entity.*;
|
||||
import com.southern.power.grid.service.DnerEventAttachmentService;
|
||||
import com.southern.power.grid.service.DnerEventService;
|
||||
import com.southern.power.grid.service.IFileService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -42,6 +49,12 @@ public class DnerEventServiceImpl extends ServiceImpl<DnerEventMapper, DnerEvent
|
||||
@Autowired
|
||||
private DnerDailyPowerOutageEventMapper dnerDailyPowerOutageEventMapper;
|
||||
|
||||
@Autowired
|
||||
private IFileService fileService;
|
||||
|
||||
@Autowired
|
||||
private DnerEventAttachmentService dnerEventAttachmentService;
|
||||
|
||||
@Override
|
||||
public List<DnerEventVO> listVO() {
|
||||
List<DnerEventVO> dnerEventVOS = dnerEventMapper.listVO();
|
||||
@ -113,4 +126,36 @@ public class DnerEventServiceImpl extends ServiceImpl<DnerEventMapper, DnerEvent
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean removeDataById(Long id) {
|
||||
removeById(id);
|
||||
dnerEventAttachmentMapper.delete(
|
||||
new LambdaQueryWrapper<DnerEventAttachment>().eq(DnerEventAttachment::getEventId, id));
|
||||
dnerEventExportRecordMapper.delete(
|
||||
new LambdaQueryWrapper<DnerEventExportRecord>().eq(DnerEventExportRecord::getEventId, id));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean uploadTemplate(MultipartFile file) {
|
||||
try {
|
||||
fileService.uploadTemplate(file);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Resource> downloadTemplate() {
|
||||
DnerEventAttachment templateAttachment = dnerEventAttachmentMapper.selectOne(
|
||||
new LambdaQueryWrapper<DnerEventAttachment>().eq(DnerEventAttachment::getEventId, 0));
|
||||
if (Objects.isNull(templateAttachment)) {
|
||||
log.error("Template is not found!");
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
|
||||
}
|
||||
return dnerEventAttachmentService.downloadExcel(templateAttachment.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.southern.power.grid.service.impl;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.southern.power.grid.dao.DnerDailyPowerOutageEventMapper;
|
||||
@ -21,6 +22,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 文件操作 服务实现类
|
||||
@ -90,4 +92,42 @@ public class FileServiceImpl implements IFileService {
|
||||
|
||||
return record.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadTemplate(MultipartFile file) throws IOException {
|
||||
// 上传导入模板,只需要维护附件表中eventId=0的一条记录即可
|
||||
// 1、校验是否是 Excel
|
||||
if (!FileUploadUtil.isExcel(file)) {
|
||||
throw new RuntimeException("Only .xlsx or .xls format Excel files are allowed to be uploaded.");
|
||||
}
|
||||
|
||||
// 2、删除旧的模板和记录
|
||||
DnerEventAttachment eventAttachment = dnerEventAttachmentMapper.selectOne(
|
||||
new LambdaQueryWrapper<DnerEventAttachment>().eq(DnerEventAttachment::getEventId, 0L));
|
||||
if (!Objects.isNull(eventAttachment)) {
|
||||
String filePath = eventAttachment.getFilePath();
|
||||
FileUtil.del(filePath);
|
||||
dnerEventAttachmentMapper.deleteById(eventAttachment.getId());
|
||||
}
|
||||
// 3. 获取文件信息
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
String suffix = FileUploadUtil.getFileSuffix(originalFilename);
|
||||
String storedFilename = FileUploadUtil.generateUniqueFileName(suffix);
|
||||
String absolutePath = uploadPath + storedFilename;
|
||||
// 4. 保存到本地
|
||||
FileUploadUtil.saveFile(file, absolutePath);
|
||||
// 5. 记录到数据库
|
||||
DnerEventAttachment record = new DnerEventAttachment();
|
||||
record.setFileName(originalFilename);
|
||||
record.setStoredFileName(storedFilename);
|
||||
record.setFilePath(absolutePath);
|
||||
record.setFileSize(file.getSize());
|
||||
record.setFileType(suffix);
|
||||
record.setCreateTime(LocalDateTime.now());
|
||||
record.setCreator("admin"); // 默认admin
|
||||
record.setEventId(0L);
|
||||
record.setIsLatest(1);
|
||||
// 插入记录
|
||||
dnerEventAttachmentMapper.insert(record);
|
||||
}
|
||||
}
|
||||
|
||||
@ -322,11 +322,7 @@ CREATE TABLE `dner_event_attachment`
|
||||
`is_latest` TINYINT NOT NULL DEFAULT '1' COMMENT '是否最新附件(1:是 0:否)',
|
||||
`creator` VARCHAR(64) NOT NULL COMMENT '上传人',
|
||||
`create_time` DATETIME default current_timestamp NOT NULL COMMENT '上传时间',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_event_id` (`event_id`),
|
||||
CONSTRAINT `fk_attachment_event` FOREIGN KEY (`event_id`) REFERENCES `dner_event` (`id`)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE RESTRICT
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE = InnoDB
|
||||
DEFAULT CHARSET = utf8mb4 COMMENT ='事件附件表';
|
||||
|
||||
@ -340,10 +336,6 @@ CREATE TABLE `dner_event_export_record`
|
||||
`file_name` VARCHAR(255) NOT NULL COMMENT '导出文件名',
|
||||
`file_path` VARCHAR(512) NOT NULL COMMENT '导出文件路径',
|
||||
`file_type` VARCHAR(32) NOT NULL COMMENT '导出文件类型',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_event_id` (`event_id`),
|
||||
CONSTRAINT `fk_export_event` FOREIGN KEY (`event_id`) REFERENCES `dner_event` (`id`)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE RESTRICT
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE = InnoDB
|
||||
DEFAULT CHARSET = utf8mb4 COMMENT ='事件导出记录表';
|
||||
Loading…
x
Reference in New Issue
Block a user