fix: 鱼类资源接口
This commit is contained in:
parent
e20cdb61cd
commit
9155ce1804
@ -1,5 +1,6 @@
|
||||
package com.yfd.platform.qgc_base.controller;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
@ -7,22 +8,29 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.yfd.platform.annotation.Log;
|
||||
import com.yfd.platform.common.DataSourceRequest;
|
||||
import com.yfd.platform.config.ResponseResult;
|
||||
import com.yfd.platform.qgc_base.domain.SdFishDictoryB;
|
||||
import com.yfd.platform.qgc_base.domain.SdEngInfoBHOperateRequest;
|
||||
import com.yfd.platform.qgc_base.domain.SdFishDictoryB;
|
||||
import com.yfd.platform.qgc_base.service.ISdFishDictoryBService;
|
||||
import com.yfd.platform.utils.DataSourceRequestUtil;
|
||||
import com.yfd.platform.qgc_data.service.AttachmentUploadService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 鱼类字典表 前端控制器
|
||||
* </p>
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/env/fishDictory")
|
||||
@Tag(name = "鱼类字典管理")
|
||||
@ -34,6 +42,9 @@ public class SdFishDictoryBController {
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Resource
|
||||
private AttachmentUploadService attachmentUploadService;
|
||||
|
||||
@PostMapping("/queryPageList")
|
||||
@Operation(summary = "分页查询鱼类字典列表(支持动态过滤和排序)")
|
||||
public ResponseResult queryPageList(@RequestBody DataSourceRequest request) {
|
||||
@ -44,11 +55,7 @@ public class SdFishDictoryBController {
|
||||
@PostMapping("/list")
|
||||
@Operation(summary = "查询鱼类字典列表(支持动态过滤和排序,不分页)")
|
||||
public ResponseResult list(@RequestBody DataSourceRequest request) {
|
||||
List<SdFishDictoryB> list = DataSourceRequestUtil.executeList(
|
||||
request,
|
||||
SdFishDictoryB.class,
|
||||
sdFishDictoryBService
|
||||
);
|
||||
List<SdFishDictoryB> list = sdFishDictoryBService.list();
|
||||
return ResponseResult.successData(list);
|
||||
}
|
||||
|
||||
@ -71,31 +78,84 @@ public class SdFishDictoryBController {
|
||||
@Log(module = "鱼类字典管理", value = "新增鱼类字典")
|
||||
@PostMapping("/add")
|
||||
@Operation(summary = "新增鱼类字典")
|
||||
public ResponseResult add(@RequestBody SdEngInfoBHOperateRequest request) {
|
||||
SdFishDictoryB entity = request == null || request.getEngInfo() == null ? null
|
||||
: objectMapper.convertValue(request.getEngInfo(), SdFishDictoryB.class);
|
||||
boolean result = sdFishDictoryBService.add(entity, request == null ? null : request.getSource());
|
||||
return result ? ResponseResult.success("新增成功") : ResponseResult.error("新增失败");
|
||||
public ResponseResult add(@RequestParam("data") String dataStr,
|
||||
@RequestParam(value = "files", required = false) List<MultipartFile> files) {
|
||||
try {
|
||||
SdEngInfoBHOperateRequest request = objectMapper.readValue(dataStr, SdEngInfoBHOperateRequest.class);
|
||||
SdFishDictoryB entity = request == null || request.getEngInfo() == null ? null
|
||||
: objectMapper.convertValue(request.getEngInfo(), SdFishDictoryB.class);
|
||||
if (entity == null) {
|
||||
return ResponseResult.error("数据不能为空");
|
||||
}
|
||||
entity.setId(IdUtil.simpleUUID());
|
||||
entity.setCode(entity.getId());
|
||||
|
||||
// 上传文件并设置附件ID到实体
|
||||
applyUploadedFiles(entity, files);
|
||||
|
||||
boolean result = sdFishDictoryBService.add(entity, request.getSource());
|
||||
return result ? ResponseResult.success("新增成功") : ResponseResult.error("新增失败");
|
||||
} catch (Exception e) {
|
||||
log.error("新增鱼类字典失败", e);
|
||||
return ResponseResult.error("新增失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Log(module = "鱼类字典管理", value = "修改鱼类字典")
|
||||
@PostMapping("/update")
|
||||
@Operation(summary = "修改鱼类字典")
|
||||
public ResponseResult update(@RequestBody SdEngInfoBHOperateRequest request) {
|
||||
SdFishDictoryB entity = request == null || request.getEngInfo() == null ? null
|
||||
: objectMapper.convertValue(request.getEngInfo(), SdFishDictoryB.class);
|
||||
boolean result = sdFishDictoryBService.update(entity, request == null ? null : request.getSource());
|
||||
return result ? ResponseResult.success("修改成功") : ResponseResult.error("修改失败");
|
||||
public ResponseResult update(@RequestParam("data") String dataStr,
|
||||
@RequestParam(value = "files", required = false) List<MultipartFile> files) {
|
||||
try {
|
||||
SdEngInfoBHOperateRequest request = objectMapper.readValue(dataStr, SdEngInfoBHOperateRequest.class);
|
||||
SdFishDictoryB entity = request == null || request.getEngInfo() == null ? null
|
||||
: objectMapper.convertValue(request.getEngInfo(), SdFishDictoryB.class);
|
||||
if (entity == null || entity.getId() == null) {
|
||||
return ResponseResult.error("数据或ID不能为空");
|
||||
}
|
||||
|
||||
// 获取修改前的实体,用于后续清理旧文件
|
||||
SdFishDictoryB before = sdFishDictoryBService.getById(entity.getId());
|
||||
List<String> oldAttachmentIds = before != null ? collectAttachmentIds(before) : Collections.emptyList();
|
||||
|
||||
// 上传新文件并设置附件ID
|
||||
applyUploadedFiles(entity, files);
|
||||
|
||||
boolean result = sdFishDictoryBService.update(entity, request.getSource());
|
||||
if (result) {
|
||||
// 清理被替换的旧文件
|
||||
deleteReplacedFiles(oldAttachmentIds, collectAttachmentIds(entity));
|
||||
}
|
||||
return result ? ResponseResult.success("修改成功") : ResponseResult.error("修改失败");
|
||||
} catch (Exception e) {
|
||||
log.error("修改鱼类字典失败", e);
|
||||
return ResponseResult.error("修改失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Log(module = "鱼类字典管理", value = "删除鱼类字典")
|
||||
@PostMapping("/delete")
|
||||
@Operation(summary = "删除鱼类字典")
|
||||
public ResponseResult delete(@RequestBody SdEngInfoBHOperateRequest request) {
|
||||
boolean result = sdFishDictoryBService.delete(
|
||||
request == null ? null : request.getIds(),
|
||||
request == null ? null : request.getSource()
|
||||
);
|
||||
List<String> ids = request == null ? null : request.getIds();
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return ResponseResult.error("ID不能为空");
|
||||
}
|
||||
|
||||
// 删除前先收集所有需要清理的附件ID
|
||||
List<String> allAttachmentIds = new ArrayList<>();
|
||||
for (String id : ids) {
|
||||
SdFishDictoryB entity = sdFishDictoryBService.getById(id);
|
||||
if (entity != null) {
|
||||
allAttachmentIds.addAll(collectAttachmentIds(entity));
|
||||
}
|
||||
}
|
||||
|
||||
boolean result = sdFishDictoryBService.delete(ids, request.getSource());
|
||||
if (result) {
|
||||
// 删除关联的附件
|
||||
deleteAttachments(allAttachmentIds);
|
||||
}
|
||||
return result ? ResponseResult.success("删除成功") : ResponseResult.error("删除失败");
|
||||
}
|
||||
|
||||
@ -106,4 +166,67 @@ public class SdFishDictoryBController {
|
||||
@RequestParam(required = false, defaultValue = "10") Integer limit) {
|
||||
return ResponseResult.successData(sdFishDictoryBService.findSimilarFish(name, limit));
|
||||
}
|
||||
|
||||
// ==================== 文件处理辅助方法 ====================
|
||||
|
||||
/**
|
||||
* 上传文件并追加附件ID到inffile字段(与已有ID以逗号拼接)
|
||||
*/
|
||||
private void applyUploadedFiles(SdFishDictoryB entity, List<MultipartFile> files) {
|
||||
if (files == null || files.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<String> attachmentIds = attachmentUploadService.uploadMultipartFiles(files);
|
||||
if (attachmentIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
// 新上传的文件ID
|
||||
String newIds = attachmentIds.stream()
|
||||
.filter(StrUtil::isNotBlank)
|
||||
.collect(Collectors.joining(","));
|
||||
if (StrUtil.isBlank(newIds)) {
|
||||
return;
|
||||
}
|
||||
// 追加到已有的inffile后面
|
||||
String existing = StrUtil.isNotBlank(entity.getInffile()) ? entity.getInffile() : "";
|
||||
String merged = StrUtil.isBlank(existing) ? newIds : existing + "," + newIds;
|
||||
entity.setInffile(merged);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收集实体中inffile字段的附件ID
|
||||
*/
|
||||
private List<String> collectAttachmentIds(SdFishDictoryB entity) {
|
||||
List<String> ids = new ArrayList<>();
|
||||
if (StrUtil.isNotBlank(entity.getInffile())) {
|
||||
Arrays.stream(entity.getInffile().split(","))
|
||||
.map(String::trim)
|
||||
.filter(StrUtil::isNotBlank)
|
||||
.forEach(ids::add);
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除被替换的旧文件(old中有但new中没有的)
|
||||
*/
|
||||
private void deleteReplacedFiles(List<String> oldIds, List<String> newIds) {
|
||||
for (String oldId : oldIds) {
|
||||
if (StrUtil.isBlank(oldId)) continue;
|
||||
if (!newIds.contains(oldId)) {
|
||||
attachmentUploadService.deleteFile(oldId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除附件
|
||||
*/
|
||||
private void deleteAttachments(List<String> attachmentIds) {
|
||||
for (String id : attachmentIds) {
|
||||
if (StrUtil.isNotBlank(id)) {
|
||||
attachmentUploadService.deleteFile(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ public class SdFishDictoryB implements Serializable {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId(type = IdType.INPUT)
|
||||
@TableId(type = IdType.ASSIGN_UUID)
|
||||
private String id;
|
||||
|
||||
/**
|
||||
|
||||
Loading…
Reference in New Issue
Block a user