388 lines
17 KiB
Java
388 lines
17 KiB
Java
|
|
package com.yj.earth.business.controller;
|
||
|
|
|
||
|
|
import cn.hutool.core.io.FileUtil;
|
||
|
|
import cn.hutool.core.lang.UUID;
|
||
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||
|
|
import com.yj.earth.annotation.CheckAuth;
|
||
|
|
import com.yj.earth.business.domain.MilitaryLibrary;
|
||
|
|
import com.yj.earth.business.domain.MilitaryType;
|
||
|
|
import com.yj.earth.business.service.FileInfoService;
|
||
|
|
import com.yj.earth.business.service.MilitaryLibraryService;
|
||
|
|
import com.yj.earth.common.util.ApiResponse;
|
||
|
|
import com.yj.earth.common.util.SQLiteUtil;
|
||
|
|
import com.yj.earth.dto.militaryLibrary.AddMilitaryTypeDto;
|
||
|
|
import com.yj.earth.dto.militaryLibrary.CreateMilitaryLibraryDto;
|
||
|
|
import com.yj.earth.dto.militaryLibrary.DragMilitaryTypeDto;
|
||
|
|
import com.yj.earth.dto.militaryLibrary.UpdateMilitaryTypeNameDto;
|
||
|
|
import com.yj.earth.vo.MilitaryTypeVo;
|
||
|
|
import com.yj.earth.vo.MilitaryVo;
|
||
|
|
import io.swagger.v3.oas.annotations.Operation;
|
||
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
||
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||
|
|
import org.springframework.util.StringUtils;
|
||
|
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
import org.springframework.web.multipart.MultipartFile;
|
||
|
|
|
||
|
|
import javax.annotation.Resource;
|
||
|
|
import java.io.File;
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.sql.SQLException;
|
||
|
|
import java.time.LocalDateTime;
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
import java.util.stream.Collectors;
|
||
|
|
|
||
|
|
@Tag(name = "军标库管理")
|
||
|
|
@CheckAuth
|
||
|
|
@RestController
|
||
|
|
@RequestMapping("/militaryLibrary")
|
||
|
|
public class MilitaryLibraryController {
|
||
|
|
@Resource
|
||
|
|
private MilitaryLibraryService militaryLibraryService;
|
||
|
|
@Resource
|
||
|
|
private FileInfoService fileInfoService;
|
||
|
|
|
||
|
|
@Operation(summary = "创建军标库")
|
||
|
|
@PostMapping("/createMilitaryLibrary")
|
||
|
|
public ApiResponse createMilitaryLibrary(@RequestBody CreateMilitaryLibraryDto createMilitaryLibraryDto) {
|
||
|
|
try {
|
||
|
|
// 参数校验
|
||
|
|
String folderPath = createMilitaryLibraryDto.getPath();
|
||
|
|
String militaryName = createMilitaryLibraryDto.getName();
|
||
|
|
// 处理路径、组合为完整军标库文件路径
|
||
|
|
File parentDir = new File(folderPath);
|
||
|
|
File militaryFile = new File(parentDir, militaryName);
|
||
|
|
String militaryPath = militaryFile.getAbsolutePath().replace("\\", "/");
|
||
|
|
// 检查父目录是否存在、不存在则创建
|
||
|
|
if (!parentDir.exists()) {
|
||
|
|
boolean mkdirsSuccess = parentDir.mkdirs();
|
||
|
|
if (!mkdirsSuccess) {
|
||
|
|
return ApiResponse.failure("创建父目录失败:" + folderPath);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// 检查军标库文件是否已存在
|
||
|
|
if (militaryFile.exists()) {
|
||
|
|
if (militaryFile.isDirectory()) {
|
||
|
|
return ApiResponse.failure("同名目录已存在、无法创建文件:" + militaryPath);
|
||
|
|
}
|
||
|
|
return ApiResponse.failure("军标库文件已存在:" + militaryPath);
|
||
|
|
}
|
||
|
|
// 创建军标库文件
|
||
|
|
boolean createSuccess = militaryFile.createNewFile();
|
||
|
|
if (!createSuccess) {
|
||
|
|
return ApiResponse.failure("创建军标库文件失败:" + militaryPath);
|
||
|
|
}
|
||
|
|
// 添加军标库信息
|
||
|
|
addMilitaryLibrary(militaryPath);
|
||
|
|
SQLiteUtil.initializationMilitary(militaryPath);
|
||
|
|
return ApiResponse.success(null);
|
||
|
|
} catch (Exception e) {
|
||
|
|
return ApiResponse.failure("创建军标库失败:" + e.getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Operation(summary = "导入军标库")
|
||
|
|
@PostMapping("/importMilitaryLibrary")
|
||
|
|
public ApiResponse importMilitaryLibrary(@RequestParam("militaryPath") @Parameter(description = "军标库路径") String militaryPath) {
|
||
|
|
addMilitaryLibrary(militaryPath);
|
||
|
|
return ApiResponse.success(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Operation(summary = "添加军标类型")
|
||
|
|
@PostMapping("/addMilitaryType")
|
||
|
|
public ApiResponse addMilitaryType(@RequestBody AddMilitaryTypeDto addMilitaryTypeDto) throws SQLException, IllegalAccessException, InstantiationException {
|
||
|
|
String militaryPath = getMilitaryLibrary();
|
||
|
|
if (militaryPath == null) {
|
||
|
|
return ApiResponse.failure("请先创建或导入军标库");
|
||
|
|
}
|
||
|
|
// 检查父级是否存在
|
||
|
|
String parentId = addMilitaryTypeDto.getParentId();
|
||
|
|
if (parentId != null) {
|
||
|
|
String sql = "SELECT * FROM military_type WHERE id = ?";
|
||
|
|
List<Object> params = new ArrayList<>();
|
||
|
|
params.add(parentId);
|
||
|
|
MilitaryType militaryType = SQLiteUtil.queryForObject(militaryPath, sql, params, MilitaryType.class);
|
||
|
|
if (militaryType == null) {
|
||
|
|
return ApiResponse.failure("父级军标类型不存在");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
String sql = "INSERT INTO military_type " +
|
||
|
|
"(id, name, parent_id, tree_index, created_at) " +
|
||
|
|
"VALUES (?, ?, ?, ?, ?)";
|
||
|
|
List<Object> params = new ArrayList<>();
|
||
|
|
params.add(UUID.fastUUID().toString(true));
|
||
|
|
params.add(addMilitaryTypeDto.getName());
|
||
|
|
params.add(addMilitaryTypeDto.getParentId());
|
||
|
|
params.add(0);
|
||
|
|
params.add(LocalDateTime.now());
|
||
|
|
SQLiteUtil.executeUpdate(militaryPath, sql, params);
|
||
|
|
return ApiResponse.success(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Operation(summary = "删除军标类型")
|
||
|
|
@PostMapping("/deleteMilitaryType")
|
||
|
|
public ApiResponse deleteMilitaryType(@Parameter(description = "军标类型ID") @RequestParam("militaryTypeId") String militaryTypeId) throws SQLException {
|
||
|
|
String militaryPath = getMilitaryLibrary();
|
||
|
|
if (militaryPath == null) {
|
||
|
|
return ApiResponse.failure("请先创建或导入军标库");
|
||
|
|
}
|
||
|
|
String sql = "DELETE FROM military_type WHERE id = ?";
|
||
|
|
List<Object> params = new ArrayList<>();
|
||
|
|
params.add(militaryTypeId);
|
||
|
|
SQLiteUtil.executeUpdate(militaryPath, sql, params);
|
||
|
|
return ApiResponse.success(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Operation(summary = "修改军标类型名称")
|
||
|
|
@PostMapping("/updateMilitaryTypeName")
|
||
|
|
public ApiResponse updateMilitaryTypeName(@RequestBody UpdateMilitaryTypeNameDto updateMilitaryTypeNameDto) throws SQLException {
|
||
|
|
String militaryPath = getMilitaryLibrary();
|
||
|
|
if (militaryPath == null) {
|
||
|
|
return ApiResponse.failure("请先创建或导入军标库");
|
||
|
|
}
|
||
|
|
String sql = "UPDATE military_type SET name = ? WHERE id = ?";
|
||
|
|
List<Object> params = new ArrayList<>();
|
||
|
|
params.add(updateMilitaryTypeNameDto.getName());
|
||
|
|
params.add(updateMilitaryTypeNameDto.getId());
|
||
|
|
SQLiteUtil.executeUpdate(militaryPath, sql, params);
|
||
|
|
return ApiResponse.success(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Operation(summary = "军标类型列表")
|
||
|
|
@GetMapping("/militaryTypeList")
|
||
|
|
public ApiResponse militaryTypeTree() throws SQLException, IllegalAccessException, InstantiationException {
|
||
|
|
String militaryPath = getMilitaryLibrary();
|
||
|
|
if (militaryPath == null) {
|
||
|
|
return ApiResponse.failure("请先创建或导入军标库");
|
||
|
|
}
|
||
|
|
// 查询所有军标类型
|
||
|
|
String sql = """
|
||
|
|
SELECT id, name, parent_id as parentId,
|
||
|
|
tree_index as treeIndex, created_at as createdAt,
|
||
|
|
updated_at as updatedAt FROM military_type ORDER BY tree_index ASC
|
||
|
|
""";
|
||
|
|
List<MilitaryType> militaryTypes = SQLiteUtil.queryForList(militaryPath, sql, null, MilitaryType.class);
|
||
|
|
// 转换为树形结构
|
||
|
|
List<MilitaryTypeVo> treeList = buildMilitaryTypeTree(militaryTypes);
|
||
|
|
return ApiResponse.success(treeList);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Operation(summary = "拖动军标类型树")
|
||
|
|
@PostMapping("/dragMilitaryType")
|
||
|
|
public ApiResponse dragMilitaryType(@RequestBody DragMilitaryTypeDto dragMilitaryTypeDto) throws SQLException {
|
||
|
|
String militaryPath = getMilitaryLibrary();
|
||
|
|
if (militaryPath == null) {
|
||
|
|
return ApiResponse.failure("请先创建或导入军标库");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 动态构建SQL和参数
|
||
|
|
List<String> updateFields = new ArrayList<>();
|
||
|
|
List<Object> params = new ArrayList<>();
|
||
|
|
|
||
|
|
// 判断 parentId 是否存在
|
||
|
|
if (dragMilitaryTypeDto.getParentId() != null) {
|
||
|
|
updateFields.add("parent_id = ?");
|
||
|
|
params.add(dragMilitaryTypeDto.getParentId());
|
||
|
|
}
|
||
|
|
|
||
|
|
// 判断 treeIndex 是否存在
|
||
|
|
if (dragMilitaryTypeDto.getTreeIndex() != null) {
|
||
|
|
updateFields.add("tree_index = ?");
|
||
|
|
params.add(dragMilitaryTypeDto.getTreeIndex());
|
||
|
|
}
|
||
|
|
|
||
|
|
// 构建完整 SQL
|
||
|
|
String sql = "UPDATE military_type SET " + String.join(", ", updateFields) + " WHERE id = ?";
|
||
|
|
params.add(dragMilitaryTypeDto.getId());
|
||
|
|
SQLiteUtil.executeUpdate(militaryPath, sql, params);
|
||
|
|
return ApiResponse.success(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Operation(summary = "添加军标文件")
|
||
|
|
@PostMapping("/addMilitaryFile")
|
||
|
|
public ApiResponse addMilitaryFile(@RequestParam("files") MultipartFile[] files, @Parameter(description = "军标类型ID") @RequestParam("militaryTypeId") String militaryTypeId) throws IOException, SQLException {
|
||
|
|
// 获取最新的军标库路径
|
||
|
|
String militaryPath = getMilitaryLibrary();
|
||
|
|
if (militaryPath == null) {
|
||
|
|
return ApiResponse.failure("请先创建或导入军标库");
|
||
|
|
}
|
||
|
|
// 循环处理每个上传的文件
|
||
|
|
for (MultipartFile file : files) {
|
||
|
|
// 跳过空文件
|
||
|
|
if (file.isEmpty()) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
// 获取文件信息
|
||
|
|
String fileName = file.getOriginalFilename();
|
||
|
|
if (fileName == null) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
String fileSuffix = FileUtil.extName(fileName);
|
||
|
|
String fileNameWithoutSuffix = FileUtil.mainName(fileName);
|
||
|
|
|
||
|
|
// 构建插入SQL
|
||
|
|
String sql = "INSERT INTO military " +
|
||
|
|
"(id, military_type_id, military_name, military_type, data, created_at) " +
|
||
|
|
"VALUES (?, ?, ?, ?, ?, ?)";
|
||
|
|
|
||
|
|
String url = fileInfoService.uploadWithPreview(file);
|
||
|
|
List<Object> params = new ArrayList<>();
|
||
|
|
params.add(UUID.fastUUID().toString(true));
|
||
|
|
params.add(militaryTypeId);
|
||
|
|
params.add(fileNameWithoutSuffix);
|
||
|
|
params.add(fileSuffix);
|
||
|
|
params.add(url);
|
||
|
|
params.add(LocalDateTime.now());
|
||
|
|
|
||
|
|
// 执行插入操作
|
||
|
|
SQLiteUtil.executeUpdate(militaryPath, sql, params);
|
||
|
|
}
|
||
|
|
|
||
|
|
return ApiResponse.success(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Operation(summary = "根据军标类型查看军标列表")
|
||
|
|
@PostMapping("/militaryList")
|
||
|
|
public ApiResponse militaryList(@Parameter(description = "军标类型ID") @RequestParam("militaryTypeId") String militaryTypeId) throws SQLException, IllegalAccessException, InstantiationException {
|
||
|
|
// 获取最新的军标库
|
||
|
|
String militaryPath = getMilitaryLibrary();
|
||
|
|
if (militaryPath == null) {
|
||
|
|
return ApiResponse.failure("请先创建或导入军标库");
|
||
|
|
}
|
||
|
|
// 多表联查、查询所有的军标
|
||
|
|
String sql = """
|
||
|
|
SELECT
|
||
|
|
military.id,
|
||
|
|
military.military_type_id as militaryTypeId,
|
||
|
|
military.military_name as militaryName,
|
||
|
|
military.military_type as militaryType,
|
||
|
|
military.data,
|
||
|
|
military.view,
|
||
|
|
military.created_at as createdAt,
|
||
|
|
military.updated_at as updatedAt,
|
||
|
|
military_type.name as militaryTypeName from
|
||
|
|
military JOIN military_type ON military.military_type_id = military_type.id
|
||
|
|
WHERE military.military_type_id = ?
|
||
|
|
""";
|
||
|
|
List<Object> params = new ArrayList<>();
|
||
|
|
params.add(militaryTypeId);
|
||
|
|
List<MilitaryVo> militaryVos = SQLiteUtil.queryForList(militaryPath, sql, params, MilitaryVo.class);
|
||
|
|
return ApiResponse.success(militaryVos);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Operation(summary = "更新军标信息")
|
||
|
|
@PostMapping("/uploadMilitaryInfo")
|
||
|
|
public ApiResponse uploadMilitaryInfo(
|
||
|
|
@Parameter(description = "军标ID") @RequestParam("militaryId") String militaryId,
|
||
|
|
@Parameter(description = "军标名称") @RequestParam(value = "militaryName", required = false) String militaryName)
|
||
|
|
throws SQLException {
|
||
|
|
|
||
|
|
// 获取最新的军标库路径
|
||
|
|
String militaryPath = getMilitaryLibrary();
|
||
|
|
if (militaryPath == null) {
|
||
|
|
return ApiResponse.failure("请先创建或导入军标库");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 如果没有需要更新的字段、直接返回成功
|
||
|
|
if (!StringUtils.hasText(militaryName)) {
|
||
|
|
return ApiResponse.success(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 构建更新SQL和参数
|
||
|
|
String sql = "UPDATE military SET updated_at = ?, military_name = ? WHERE id = ?";
|
||
|
|
List<Object> params = new ArrayList<>();
|
||
|
|
params.add(LocalDateTime.now());
|
||
|
|
params.add(militaryName);
|
||
|
|
params.add(militaryId);
|
||
|
|
|
||
|
|
// 执行更新
|
||
|
|
SQLiteUtil.executeUpdate(militaryPath, sql, params);
|
||
|
|
return ApiResponse.success(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@Operation(summary = "删除军标")
|
||
|
|
@PostMapping("/deleteMilitary")
|
||
|
|
public ApiResponse deleteMilitary(@Parameter(description = "军标ID") @RequestParam("militaryId") String militaryId) throws SQLException {
|
||
|
|
String militaryPath = getMilitaryLibrary();
|
||
|
|
if (militaryPath == null) {
|
||
|
|
return ApiResponse.failure("请先创建或导入军标库");
|
||
|
|
}
|
||
|
|
String sql = "DELETE FROM military WHERE id = ?";
|
||
|
|
List<Object> params = new ArrayList<>();
|
||
|
|
params.add(militaryId);
|
||
|
|
SQLiteUtil.executeUpdate(militaryPath, sql, params);
|
||
|
|
return ApiResponse.success(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
private List<MilitaryTypeVo> buildMilitaryTypeTree(List<MilitaryType> militaryTypes) {
|
||
|
|
List<MilitaryTypeVo> treeNodes = militaryTypes.stream()
|
||
|
|
.map(militaryType -> new MilitaryTypeVo(militaryType))
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
// 构建节点ID到节点的映射
|
||
|
|
Map<String, MilitaryTypeVo> nodeMap = treeNodes.stream()
|
||
|
|
.collect(Collectors.toMap(MilitaryTypeVo::getId, node -> node));
|
||
|
|
// 根节点列表
|
||
|
|
List<MilitaryTypeVo> rootNodes = new ArrayList<>();
|
||
|
|
// 为每个节点添加子节点
|
||
|
|
for (MilitaryTypeVo node : treeNodes) {
|
||
|
|
String parentId = node.getParentId();
|
||
|
|
if (parentId == null || parentId.isEmpty()) {
|
||
|
|
// 没有父节点的是根节点
|
||
|
|
rootNodes.add(node);
|
||
|
|
} else {
|
||
|
|
// 找到父节点并添加为子节点
|
||
|
|
MilitaryTypeVo parentNode = nodeMap.get(parentId);
|
||
|
|
if (parentNode != null) {
|
||
|
|
parentNode.getChildren().add(node);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return rootNodes;
|
||
|
|
}
|
||
|
|
|
||
|
|
private String getMilitaryLibrary() {
|
||
|
|
// 获取启用的军标库
|
||
|
|
LambdaQueryWrapper<MilitaryLibrary> queryWrapper = new LambdaQueryWrapper<>();
|
||
|
|
queryWrapper.eq(MilitaryLibrary::getIsEnable, 1);
|
||
|
|
MilitaryLibrary militaryLibrary = militaryLibraryService.getOne(queryWrapper);
|
||
|
|
if (militaryLibrary == null) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return militaryLibrary.getPath();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void addMilitaryLibrary(String militaryPath) {
|
||
|
|
// 查询系统所有的军标库
|
||
|
|
List<MilitaryLibrary> militaryLibraries = militaryLibraryService.list();
|
||
|
|
// 遍历并更新状态
|
||
|
|
for (MilitaryLibrary militaryLibrary : militaryLibraries) {
|
||
|
|
// 设置启用状态为0
|
||
|
|
militaryLibrary.setIsEnable(0);
|
||
|
|
militaryLibraryService.updateById(militaryLibrary);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查相同路径的军标库是否已存在
|
||
|
|
LambdaQueryWrapper<MilitaryLibrary> pathWrapper = new LambdaQueryWrapper<>();
|
||
|
|
pathWrapper.eq(MilitaryLibrary::getPath, militaryPath);
|
||
|
|
MilitaryLibrary existingMilitary = militaryLibraryService.getOne(pathWrapper);
|
||
|
|
// 若存在相同路径的军标库、不做处理、仅仅更新状态为显示
|
||
|
|
if (existingMilitary != null) {
|
||
|
|
existingMilitary.setIsEnable(1);
|
||
|
|
militaryLibraryService.updateById(existingMilitary);
|
||
|
|
return;
|
||
|
|
} else {
|
||
|
|
// 新增军标库
|
||
|
|
MilitaryLibrary newMilitary = new MilitaryLibrary();
|
||
|
|
File file = FileUtil.file(militaryPath);
|
||
|
|
newMilitary.setId(UUID.fastUUID().toString(true));
|
||
|
|
newMilitary.setPath(militaryPath);
|
||
|
|
newMilitary.setName(FileUtil.extName(file));
|
||
|
|
newMilitary.setIsEnable(1);
|
||
|
|
militaryLibraryService.save(newMilitary);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|