From f50dd02b7be499846327f93c95ea193ec3ecde9e Mon Sep 17 00:00:00 2001 From: Syliang <1439806354@qq.com> Date: Wed, 6 May 2026 10:45:44 +0800 Subject: [PATCH] =?UTF-8?q?1\=E9=A1=B5=E9=9D=A2=E5=90=84=E7=A7=8D=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/bim/api/controller/BimController.java | 17 +- .../api/controller/PartCodeController.java | 64 ++++++- .../api/service/PartCodeRelationService.java | 52 +++-- .../bim/api/service/ProgressDataService.java | 180 ++++++++++++++++++ 4 files changed, 291 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/bim/api/service/ProgressDataService.java diff --git a/src/main/java/com/bim/api/controller/BimController.java b/src/main/java/com/bim/api/controller/BimController.java index 6d01f68..5c8dd32 100644 --- a/src/main/java/com/bim/api/controller/BimController.java +++ b/src/main/java/com/bim/api/controller/BimController.java @@ -4,6 +4,7 @@ import com.bim.api.entity.ActAmtResponse; import com.bim.api.entity.PjDayListResponse; import com.bim.api.entity.WbsResponse; import com.bim.api.query.ProjectProgressParams; +import com.bim.api.service.ProgressDataService; import com.bim.api.util.ThirdPartyAuthUtil; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; @@ -16,9 +17,11 @@ import java.util.Map; public class BimController { private final ThirdPartyAuthUtil authUtil; + private final ProgressDataService progressDataService; - public BimController(ThirdPartyAuthUtil authUtil) { + public BimController(ThirdPartyAuthUtil authUtil, ProgressDataService progressDataService) { this.authUtil = authUtil; + this.progressDataService = progressDataService; } @GetMapping("/wbs") @@ -43,6 +46,16 @@ public class BimController { return result; } + @GetMapping("/progressData") + public Map getProgressData() { + return progressDataService.getOrCreateTask(); + } + + @GetMapping("/progressData/{taskId}") + public Map getProgressDataResult(@PathVariable String taskId) { + return progressDataService.getTaskResult(taskId); + } + @PostMapping("/getPjDayListByPositionId") public Map getPjDayListByPositionId(@Valid @RequestBody ProjectProgressParams progressParams) { Map result = new HashMap<>(); @@ -63,6 +76,7 @@ public class BimController { return result; } + @PostMapping("/findActAmtByPositiond") public Map findActAmtByPositionId(@RequestBody Map params) { Map result = new HashMap<>(); @@ -86,5 +100,4 @@ public class BimController { } return result; } - } \ No newline at end of file diff --git a/src/main/java/com/bim/api/controller/PartCodeController.java b/src/main/java/com/bim/api/controller/PartCodeController.java index 3cd4767..1abb54f 100644 --- a/src/main/java/com/bim/api/controller/PartCodeController.java +++ b/src/main/java/com/bim/api/controller/PartCodeController.java @@ -48,6 +48,20 @@ public class PartCodeController { return result; } + @GetMapping("/codeWbsMappings") + @ResponseBody + public Map getCodeWbsMappings() { + Map 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 saveBatch(@RequestBody Map params) { @@ -112,16 +126,54 @@ public class PartCodeController { public Map deleteBatch(@RequestBody Map params) { Map result = new HashMap<>(); try { - List partIdsList = (List) params.get("partIds"); - List codeIdsList = (List) params.get("codeIds"); - String[] partIds = partIdsList.toArray(new String[0]); - String[] codeIds = codeIdsList.toArray(new String[0]); - service.deleteBatch(partIds, codeIds); + 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> partInfoList; + List> codeInfoList; + + if (partIdsObj instanceof List) { + partInfoList = (List>) partIdsObj; + } else { + partInfoList = List.of((Map) partIdsObj); + } + + if (codeIdsObj instanceof List) { + codeInfoList = (List>) codeIdsObj; + } else { + codeInfoList = List.of((Map) codeIdsObj); + } + + String[] partIds = new String[partInfoList.size()]; + String[] createDates = new String[partInfoList.size()]; + for (int i = 0; i < partInfoList.size(); i++) { + Map 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 codeInfo = codeInfoList.get(i); + codeIds[i] = (String) codeInfo.get("code"); + Map data = (Map) codeInfo.get("data"); + codeDatas[i] = data != null ? data.toString() : ""; + } + + List 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; } -} \ No newline at end of file +} diff --git a/src/main/java/com/bim/api/service/PartCodeRelationService.java b/src/main/java/com/bim/api/service/PartCodeRelationService.java index 0119ebc..852c921 100644 --- a/src/main/java/com/bim/api/service/PartCodeRelationService.java +++ b/src/main/java/com/bim/api/service/PartCodeRelationService.java @@ -12,9 +12,13 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Set; @Service public class PartCodeRelationService extends ServiceImpl { @@ -31,6 +35,31 @@ public class PartCodeRelationService extends ServiceImpl().eq(PartCodeRelation::getPartId, partId)); } + public List> findCodeWbsMappings() { + List relations = list(new LambdaQueryWrapper() + .orderByAsc(PartCodeRelation::getCodeId) + .orderByAsc(PartCodeRelation::getId)); + + Map> grouped = new LinkedHashMap<>(); + for (PartCodeRelation relation : relations) { + String codeId = relation.getCodeId(); + String partId = relation.getPartId(); + if (codeId == null || codeId.isBlank() || partId == null || partId.isBlank()) { + continue; + } + grouped.computeIfAbsent(codeId, key -> new LinkedHashSet<>()).add(partId); + } + + List> result = new ArrayList<>(); + for (Map.Entry> entry : grouped.entrySet()) { + Map item = new LinkedHashMap<>(); + item.put("id", entry.getKey()); + item.put("wbsCode", String.join(",", entry.getValue())); + result.add(item); + } + return result; + } + public Map findByCodeId(String codeId) { Map result = new HashMap<>(); @@ -188,19 +217,14 @@ public class PartCodeRelationService extends ServiceImpl() - .eq(PartCodeRelation::getPartId, partIds[0])); - if (count !=0 ) { - remove(new LambdaQueryWrapper() - .eq(PartCodeRelation::getPartId, partIds[0])); + public List deleteBatch(String[] partIds, String[] codeIds, String[] codeDatas, String[] createDates) { + if (partIds == null || partIds.length == 0) { + return List.of(); } - for (String codeId : codeIds) { - PartCodeRelation relation = new PartCodeRelation(); - relation.setPartId(partIds[0]); - relation.setCodeId(codeId); - relation.setCreateTime(LocalDateTime.now()); - save(relation); - } + + remove(new LambdaQueryWrapper() + .in(PartCodeRelation::getPartId, Arrays.asList(partIds))); + + return saveBatchWithData(partIds, codeIds, codeDatas, createDates); } -} \ No newline at end of file +} diff --git a/src/main/java/com/bim/api/service/ProgressDataService.java b/src/main/java/com/bim/api/service/ProgressDataService.java new file mode 100644 index 0000000..28dbdc4 --- /dev/null +++ b/src/main/java/com/bim/api/service/ProgressDataService.java @@ -0,0 +1,180 @@ +package com.bim.api.service; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.bim.api.entity.PartCodeRelation; +import com.bim.api.entity.PjDayListResponse; +import com.bim.api.query.ProjectProgressParams; +import com.bim.api.mapper.PartCodeRelationMapper; +import com.bim.api.util.ThirdPartyAuthUtil; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import jakarta.annotation.PostConstruct; +import java.time.LocalDateTime; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; + +@Service +public class ProgressDataService extends ServiceImpl { + + private static final String PROJECT_ID = "5e4bde33ec084f1a8673eb59b190dce7"; + + private static final String KEY_TASK_STATUS = "progress:task:"; + private static final String KEY_TASK_TOTAL = ":total"; + private static final String KEY_TASK_CURRENT = ":current"; + private static final String KEY_TASK_RESULT = ":result"; + private static final String KEY_PROCESSING = "progress:processing"; + private static final String KEY_COMPLETED = "progress:completed"; + private static final String KEY_LATEST_TASK = "progress:latest:taskId"; + + private static final Map TASK_STATUS_CACHE = new ConcurrentHashMap<>(); + + private final ThirdPartyAuthUtil thirdPartyAuthUtil; + private final RedisTemplate redisTemplate; + + public ProgressDataService(ThirdPartyAuthUtil thirdPartyAuthUtil, RedisTemplate redisTemplate) { + this.thirdPartyAuthUtil = thirdPartyAuthUtil; + this.redisTemplate = redisTemplate; + } + + public Map getOrCreateTask() { + Map result = new HashMap<>(); + + String latestTaskId = (String) redisTemplate.opsForValue().get(KEY_LATEST_TASK); + if (latestTaskId != null) { + String status = getTaskStatus(latestTaskId); + if ("COMPLETED".equals(status)) { + result.put("taskId", latestTaskId); + result.put("status", "COMPLETED"); + return result; + } + } + + String processingTaskId = (String) redisTemplate.opsForValue().get(KEY_PROCESSING); + if (processingTaskId != null) { + result.put("taskId", processingTaskId); + result.put("status", "PROCESSING"); + return result; + } + + String taskId = UUID.randomUUID().toString().replace("-", ""); + initTask(taskId); + calculateProgressAsync(taskId); + + result.put("taskId", taskId); + result.put("status", "STARTED"); + return result; + } + + public Map getTaskResult(String taskId) { + Map result = new HashMap<>(); + + String status = getTaskStatus(taskId); + result.put("taskId", taskId); + result.put("status", status); + + if (status == null) { + result.put("status", "NOT_FOUND"); + return result; + } + + Integer total = (Integer) redisTemplate.opsForValue().get(KEY_TASK_STATUS + taskId + KEY_TASK_TOTAL); + Integer current = (Integer) redisTemplate.opsForValue().get(KEY_TASK_STATUS + taskId + KEY_TASK_CURRENT); + + result.put("total", total != null ? total : 0); + result.put("current", current != null ? current : 0); + + double progress = 0; + if (total != null && total > 0 && current != null) { + progress = (double) current / total; + } + result.put("progress", progress); + + if ("COMPLETED".equals(status)) { + Object data = redisTemplate.opsForValue().get(KEY_TASK_STATUS + taskId + KEY_TASK_RESULT); + result.put("data", data); + } + + return result; + } + + private String getTaskStatus(String taskId) { + if (taskId == null) return null; + return TASK_STATUS_CACHE.get(taskId); + } + + private void initTask(String taskId) { + TASK_STATUS_CACHE.put(taskId, "STARTED"); + redisTemplate.opsForValue().set(KEY_PROCESSING, taskId); + } + + private void calculateProgressAsync(String taskId) { + TASK_STATUS_CACHE.put(taskId, "PROCESSING"); + + new Thread(() -> { + try { + List relations = list(); + int total = relations.size(); + + redisTemplate.opsForValue().set(KEY_TASK_STATUS + taskId + KEY_TASK_TOTAL, total); + redisTemplate.opsForValue().set(KEY_TASK_STATUS + taskId + KEY_TASK_CURRENT, 0); + + List> results = new ArrayList<>(); + + for (PartCodeRelation relation : relations) { + Map item = new HashMap<>(); + item.put("partId", relation.getPartId()); + item.put("codeData", relation.getCodeData()); + + try { + LocalDateTime createTime = relation.getCreateTime(); + String period = createTime != null ? createTime.toString().replace("T", " ") : null; + + ProjectProgressParams params = new ProjectProgressParams(); + params.setProjectId(PROJECT_ID); + params.setPositionIds(relation.getPartId()); + params.setPeriod(period); + + PjDayListResponse pjDayResponse = thirdPartyAuthUtil.findPjDayListByPositionId(params); + + double progressData = 0; + if (pjDayResponse != null && pjDayResponse.getData() != null && !pjDayResponse.getData().isEmpty()) { + PjDayListResponse.PjDayData dayData = pjDayResponse.getData().get(0); + Double totalNum = dayData.getMeteringNum(); + Double meteringNum = dayData.getMeteringAmt(); + + if (totalNum != null && meteringNum != null && meteringNum != 0) { + progressData = totalNum / meteringNum; + } + } + item.put("progressData", progressData); + } catch (Exception e) { + item.put("progressData", 0); + item.put("error", e.getMessage()); + } + + results.add(item); + + Integer current = (Integer) redisTemplate.opsForValue().get(KEY_TASK_STATUS + taskId + KEY_TASK_CURRENT); + redisTemplate.opsForValue().set(KEY_TASK_STATUS + taskId + KEY_TASK_CURRENT, current + 1); + } + + redisTemplate.opsForValue().set(KEY_TASK_STATUS + taskId + KEY_TASK_RESULT, results); + redisTemplate.opsForValue().set(KEY_TASK_STATUS + taskId + KEY_TASK_CURRENT, total); + + redisTemplate.delete(KEY_PROCESSING); + redisTemplate.opsForValue().set(KEY_COMPLETED, taskId); + redisTemplate.opsForValue().set(KEY_LATEST_TASK, taskId); + + TASK_STATUS_CACHE.put(taskId, "COMPLETED"); + + } catch (Exception e) { + TASK_STATUS_CACHE.put(taskId, "FAILED"); + redisTemplate.delete(KEY_PROCESSING); + } + }).start(); + } +} \ No newline at end of file