Files
api/src/main/java/com/bim/api/controller/PartCodeController.java
2026-05-06 10:45:44 +08:00

180 lines
6.6 KiB
Java

package com.bim.api.controller;
import com.bim.api.entity.PartCodeRelation;
import com.bim.api.service.PartCodeRelationService;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/partCode")
public class PartCodeController {
private final PartCodeRelationService service;
public PartCodeController(PartCodeRelationService service) {
this.service = service;
}
@GetMapping("/byPart/{partId}")
@ResponseBody
public Map<String, Object> getByPartId(@PathVariable String partId) {
Map<String, Object> result = new HashMap<>();
try {
List<PartCodeRelation> list = service.findByPartId(partId);
result.put("code", 200);
result.put("data", list);
} catch (Exception e) {
result.put("code", 500);
result.put("message", e.getMessage());
}
return result;
}
@GetMapping("/byCode/{codeId}")
@ResponseBody
public Map<String, Object> getByCodeId(@PathVariable String codeId) {
Map<String, Object> result = new HashMap<>();
try {
Map<String, Object> list = service.findByCodeId(codeId);
result.put("code", 200);
result.put("data", list);
} catch (Exception e) {
result.put("code", 500);
result.put("message", e.getMessage());
}
return result;
}
@GetMapping("/codeWbsMappings")
@ResponseBody
public Map<String, Object> getCodeWbsMappings() {
Map<String, Object> result = new HashMap<>();
try {
result.put("code", 200);
result.put("data", service.findCodeWbsMappings());
} catch (Exception e) {
result.put("code", 500);
result.put("message", e.getMessage());
}
return result;
}
@PostMapping("/batch")
@ResponseBody
public Map<String, Object> saveBatch(@RequestBody Map<String, Object> params) {
Map<String, Object> result = new HashMap<>();
try {
Object partIdsObj = params.get("partIds");
Object codeIdsObj = params.get("codeIds");
if (partIdsObj == null || codeIdsObj == null) {
result.put("code", 500);
result.put("message", "参数不能为空");
return result;
}
List<Map<String, Object>> partInfoList;
List<Map<String, Object>> codeInfoList;
if (partIdsObj instanceof List) {
partInfoList = (List<Map<String, Object>>) partIdsObj;
} else {
partInfoList = List.of((Map<String, Object>) partIdsObj);
}
if (codeIdsObj instanceof List) {
codeInfoList = (List<Map<String, Object>>) codeIdsObj;
} else {
codeInfoList = List.of((Map<String, Object>) codeIdsObj);
}
// 从partInfos中提取id和createDate
String[] partIds = new String[partInfoList.size()];
String[] createDates = new String[partInfoList.size()];
for (int i = 0; i < partInfoList.size(); i++) {
Map<String, Object> partInfo = partInfoList.get(i);
partIds[i] = (String) partInfo.get("id");
createDates[i] = (String) partInfo.get("createDate");
}
// 从codeInfos中提取code和data
String[] codeIds = new String[codeInfoList.size()];
String[] codeDatas = new String[codeInfoList.size()];
for (int i = 0; i < codeInfoList.size(); i++) {
Map<String, Object> codeInfo = codeInfoList.get(i);
codeIds[i] = (String) codeInfo.get("code");
Map<String, Object> data = (Map<String, Object>) codeInfo.get("data");
codeDatas[i] = data != null ? data.toString() : "";
}
List<PartCodeRelation> list = service.saveBatchWithData(partIds, codeIds, codeDatas, createDates);
result.put("code", 200);
result.put("data", list);
} catch (Exception e) {
e.printStackTrace();
result.put("code", 500);
result.put("message", e.getMessage());
}
return result;
}
@DeleteMapping("/batch")
@ResponseBody
public Map<String, Object> deleteBatch(@RequestBody Map<String, Object> params) {
Map<String, Object> result = new HashMap<>();
try {
Object partIdsObj = params.get("partIds");
Object codeIdsObj = params.get("codeIds");
if (partIdsObj == null || codeIdsObj == null) {
result.put("code", 500);
result.put("message", "鍙傛暟涓嶈兘涓虹┖");
return result;
}
List<Map<String, Object>> partInfoList;
List<Map<String, Object>> codeInfoList;
if (partIdsObj instanceof List) {
partInfoList = (List<Map<String, Object>>) partIdsObj;
} else {
partInfoList = List.of((Map<String, Object>) partIdsObj);
}
if (codeIdsObj instanceof List) {
codeInfoList = (List<Map<String, Object>>) codeIdsObj;
} else {
codeInfoList = List.of((Map<String, Object>) codeIdsObj);
}
String[] partIds = new String[partInfoList.size()];
String[] createDates = new String[partInfoList.size()];
for (int i = 0; i < partInfoList.size(); i++) {
Map<String, Object> partInfo = partInfoList.get(i);
partIds[i] = (String) partInfo.get("id");
createDates[i] = (String) partInfo.get("createDate");
}
String[] codeIds = new String[codeInfoList.size()];
String[] codeDatas = new String[codeInfoList.size()];
for (int i = 0; i < codeInfoList.size(); i++) {
Map<String, Object> codeInfo = codeInfoList.get(i);
codeIds[i] = (String) codeInfo.get("code");
Map<String, Object> data = (Map<String, Object>) codeInfo.get("data");
codeDatas[i] = data != null ? data.toString() : "";
}
List<PartCodeRelation> list = service.deleteBatch(partIds, codeIds, codeDatas, createDates);
result.put("code", 200);
result.put("data", list);
} catch (Exception e) {
result.put("code", 500);
result.put("message", e.getMessage());
}
return result;
}
}