代码优化
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package com.rj.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.parser.JsqlParserFunction;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.rj.common.DataEnrichmentUtil;
|
||||
import com.rj.entity.Dealership;
|
||||
import com.rj.entity.ProjectManagement;
|
||||
import com.rj.service.IDealershipService;
|
||||
@@ -9,6 +11,7 @@ import com.rj.service.IProjectManagementService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -32,13 +35,14 @@ import java.util.stream.Collectors;
|
||||
@RestController
|
||||
@RequestMapping("/api/projectManagement")
|
||||
@Tag(name = "项目管理", description = "项目管理相关接口")
|
||||
@Slf4j
|
||||
public class ProjectManagementController {
|
||||
|
||||
@Autowired
|
||||
private IProjectManagementService projectManagementService;
|
||||
|
||||
@Autowired
|
||||
private IDealershipService dealershipService;
|
||||
private DataEnrichmentUtil dataEnrichmentUtil;
|
||||
|
||||
/**
|
||||
* 新增项目
|
||||
@@ -164,29 +168,18 @@ public class ProjectManagementController {
|
||||
queryWrapper.orderByDesc(ProjectManagement::getUpdateTime);
|
||||
|
||||
Page<ProjectManagement> projectPage = projectManagementService.page(page, queryWrapper);
|
||||
JsqlParserFunction<Dealership, String> getId = Dealership::getId;
|
||||
|
||||
// 获取项目列表中的门店ID集合
|
||||
List<ProjectManagement> records = projectPage.getRecords();
|
||||
List<String> dealershipIds = records.stream()
|
||||
.map(ProjectManagement::getDealershipId)
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 批量获取门店信息
|
||||
Map<String, String> dealershipNameMap;
|
||||
if (!dealershipIds.isEmpty()) {
|
||||
LambdaQueryWrapper<Dealership> dealershipQueryWrapper = new LambdaQueryWrapper<>();
|
||||
dealershipQueryWrapper.in(Dealership::getId, dealershipIds);
|
||||
List<Dealership> dealerships = dealershipService.list(dealershipQueryWrapper);
|
||||
dealershipNameMap = dealerships.stream()
|
||||
.collect(Collectors.toMap(Dealership::getId, Dealership::getDealershipName));
|
||||
} else {
|
||||
dealershipNameMap = new HashMap<>();
|
||||
}
|
||||
|
||||
// 使用工具类提取ID列表
|
||||
List<String> dealershipIds = DataEnrichmentUtil.extractIds(projectPage.getRecords(), ProjectManagement::getDealershipId);
|
||||
|
||||
// 使用工具类批量获取关联数据
|
||||
Map<String, String> dealershipNameMap = dataEnrichmentUtil.getDealershipIdNameMap(dealershipIds);
|
||||
|
||||
// 构造返回数据,包含门店名称
|
||||
List<Map<String, Object>> projectDataList = records.stream().map(project -> {
|
||||
List<Map<String, Object>> projectDataList = projectPage.getRecords().stream().map(project -> {
|
||||
Map<String, Object> projectMap = new HashMap<>();
|
||||
projectMap.put("id", project.getId());
|
||||
projectMap.put("dealershipId", project.getDealershipId());
|
||||
@@ -215,6 +208,7 @@ public class ProjectManagementController {
|
||||
|
||||
return ResponseEntity.ok(result);
|
||||
} catch (Exception e) {
|
||||
log.error("查询异常:", e.getMessage());
|
||||
result.put("success", false);
|
||||
result.put("message", "查询异常:" + e.getMessage());
|
||||
return ResponseEntity.internalServerError().body(result);
|
||||
|
||||
Reference in New Issue
Block a user