|
|
@@ -0,0 +1,293 @@
|
|
|
+package org.springblade.manager.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springblade.business.feign.InformationQueryClient;
|
|
|
+import org.springblade.core.mp.base.BaseServiceImpl;
|
|
|
+import org.springblade.manager.entity.WbsDivide;
|
|
|
+import org.springblade.manager.mapper.WbsDivideMapper;
|
|
|
+import org.springblade.manager.service.IWbsDivideService;
|
|
|
+import org.springblade.manager.vo.DivideClientReq;
|
|
|
+import org.springblade.manager.vo.DivideClientVo;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * WBS划分 Service 实现类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class WbsDivideServiceImpl extends BaseServiceImpl<WbsDivideMapper, WbsDivide> implements IWbsDivideService {
|
|
|
+
|
|
|
+ private final InformationQueryClient informationQueryClient;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<WbsDivide> getByPKeyId(Long pKeyId) {
|
|
|
+ return baseMapper.selectByPKeyId(pKeyId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean bind(Long pKeyId, DivideClientVo divideClientVo, String projectId, String contractId) {
|
|
|
+ // 检查是否已存在相同的pKeyId和divideId的记录
|
|
|
+ Integer count = baseMapper.checkExists(pKeyId, divideClientVo.getId());
|
|
|
+ if (count != null && count > 0) {
|
|
|
+ // 已存在,不生成新记录
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建新记录
|
|
|
+ WbsDivide wbsDivide = new WbsDivide();
|
|
|
+ wbsDivide.setPKeyId(pKeyId);
|
|
|
+ wbsDivide.setDivideId(divideClientVo.getId());
|
|
|
+ wbsDivide.setName(divideClientVo.getName());
|
|
|
+ wbsDivide.setParentId(divideClientVo.getParentId());
|
|
|
+ wbsDivide.setContractId(contractId);
|
|
|
+ wbsDivide.setDivideNum(divideClientVo.getDivideNum());
|
|
|
+ wbsDivide.setProjectId(projectId);
|
|
|
+
|
|
|
+ return save(wbsDivide);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean unbind(String divideId, Long pKeyId) {
|
|
|
+ Integer rows = baseMapper.unbind(divideId, pKeyId);
|
|
|
+ return rows != null && rows > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<WbsDivide> getByDivideNum(String divideNum) {
|
|
|
+ return baseMapper.selectByDivideNum(divideNum);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DivideClientReq getChildNodes(String contractId, String parentId) {
|
|
|
+ CloseableHttpClient httpClient = null;
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 构建请求URL
|
|
|
+ String url = String.format("https://hccq.cqfjgs.com/fengjian/getChildNodes?contractId=%s&parentId=%s",
|
|
|
+ contractId, parentId);
|
|
|
+
|
|
|
+ // 创建HttpClient
|
|
|
+ httpClient = HttpClients.createDefault();
|
|
|
+
|
|
|
+ // 创建HttpGet请求
|
|
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+
|
|
|
+ // 获取响应实体
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+
|
|
|
+ if (entity != null) {
|
|
|
+ // 读取响应内容
|
|
|
+ String responseBody = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
|
|
+
|
|
|
+ // 将JSON字符串转换为对象
|
|
|
+ DivideClientReq divideClientReq = JSON.parseObject(responseBody, DivideClientReq.class);
|
|
|
+
|
|
|
+ return divideClientReq;
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("调用getChildNodes接口失败,contractId={}, parentId={}, error={}",
|
|
|
+ contractId, parentId, e.getMessage(), e);
|
|
|
+ } finally {
|
|
|
+ // 关闭资源
|
|
|
+ try {
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ if (httpClient != null) {
|
|
|
+ httpClient.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("关闭HTTP连接失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回空对象
|
|
|
+ return new DivideClientReq();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int batchBind(Long pKeyId, List<DivideClientVo> divideClientVos, String projectId, String contractId) {
|
|
|
+ if (divideClientVos == null || divideClientVos.isEmpty()) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ int successCount = 0;
|
|
|
+ for (DivideClientVo divideClientVo : divideClientVos) {
|
|
|
+ boolean result = bind(pKeyId, divideClientVo, projectId, contractId);
|
|
|
+ if (result) {
|
|
|
+ successCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("批量绑定节点,pKeyId={}, 总数={}, 成功={}", pKeyId, divideClientVos.size(), successCount);
|
|
|
+ return successCount;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<String> getBindedDivideIds(Long pKeyId) {
|
|
|
+ List<WbsDivide> list = baseMapper.selectByPKeyId(pKeyId);
|
|
|
+ List<String> divideIds = new ArrayList<>();
|
|
|
+ if (list != null && !list.isEmpty()) {
|
|
|
+ for (WbsDivide wbsDivide : list) {
|
|
|
+ divideIds.add(wbsDivide.getDivideId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return divideIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<String> getPdfUrlsByDivideNum(String divideNum) {
|
|
|
+ List<String> pdfUrls = new ArrayList<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 1. 根据divideNum查询所有记录
|
|
|
+ List<WbsDivide> wbsDivideList = baseMapper.selectByDivideNum(divideNum);
|
|
|
+
|
|
|
+ if (wbsDivideList == null || wbsDivideList.isEmpty()) {
|
|
|
+ log.warn("根据divideNum={}未查询到任何记录", divideNum);
|
|
|
+ return pdfUrls;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("根据divideNum={}查询到{}条记录", divideNum, wbsDivideList.size());
|
|
|
+
|
|
|
+ // 2. 遍历所有记录,取出pKeyId
|
|
|
+ for (WbsDivide wbsDivide : wbsDivideList) {
|
|
|
+ Long pKeyId = wbsDivide.getPKeyId();
|
|
|
+
|
|
|
+ if (pKeyId == null) {
|
|
|
+ log.warn("记录id={}的pKeyId为空,跳过", wbsDivide.getId());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 3. 使用informationQueryClient.getInfoByWbsId获取InformationQuery对象
|
|
|
+ // 第二个参数classify传0
|
|
|
+ org.springblade.business.entity.InformationQuery informationQuery =
|
|
|
+ informationQueryClient.getInfoByWbsId(pKeyId, 0);
|
|
|
+
|
|
|
+ if (informationQuery != null && informationQuery.getPdfUrl() != null
|
|
|
+ && !informationQuery.getPdfUrl().trim().isEmpty()) {
|
|
|
+ // 4. 取出pdfUrl添加到列表
|
|
|
+ pdfUrls.add(informationQuery.getPdfUrl());
|
|
|
+ log.debug("pKeyId={}对应的pdfUrl={}", pKeyId, informationQuery.getPdfUrl());
|
|
|
+ } else {
|
|
|
+ log.debug("pKeyId={}未查询到有效的pdfUrl", pKeyId);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("查询pKeyId={}的InformationQuery信息失败, error={}", pKeyId, e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("根据divideNum={}最终获取到{}个pdfUrl", divideNum, pdfUrls.size());
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("根据divideNum={}获取PDF URL列表失败, error={}", divideNum, e.getMessage(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return pdfUrls;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testCompleteFlow() {
|
|
|
+ log.info("========== 开始测试完整流程 ==========");
|
|
|
+
|
|
|
+ // 1. 模拟WBS树节点的pKeyId
|
|
|
+ Long pKeyId = 1001L;
|
|
|
+ String projectId = "project_001";
|
|
|
+ String contractId = "54e8a314de8a493d9f08a9d3cf7c";
|
|
|
+
|
|
|
+ log.info("步骤1: 用户点击WBS树节点,pKeyId={}", pKeyId);
|
|
|
+
|
|
|
+ // 2. 获取第三方树的根节点(parentId为0)
|
|
|
+ log.info("步骤2: 获取第三方树根节点,contractId={}, parentId=0", contractId);
|
|
|
+ DivideClientReq rootNodes = this.getChildNodes(contractId, "0");
|
|
|
+
|
|
|
+ if (rootNodes != null && rootNodes.getTrees() != null && !rootNodes.getTrees().isEmpty()) {
|
|
|
+ log.info("获取到 {} 个根节点", rootNodes.getTrees().size());
|
|
|
+ log.info("根节点数据: {}", JSON.toJSONString(rootNodes.getTrees()));
|
|
|
+
|
|
|
+ // 3. 模拟用户展开第一个根节点,获取子节点
|
|
|
+ DivideClientVo firstRoot = rootNodes.getTrees().get(0);
|
|
|
+ log.info("步骤3: 用户展开根节点: {}, 获取子节点", firstRoot.getName());
|
|
|
+
|
|
|
+ DivideClientReq childNodes = this.getChildNodes(contractId, firstRoot.getId());
|
|
|
+ if (childNodes != null && childNodes.getTrees() != null) {
|
|
|
+ log.info("获取到 {} 个子节点", childNodes.getTrees().size());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 模拟用户勾选部分节点进行绑定
|
|
|
+ log.info("步骤4: 用户勾选节点并保存");
|
|
|
+ List<DivideClientVo> selectedNodes = new ArrayList<>();
|
|
|
+
|
|
|
+ // 假设用户勾选了前2个节点
|
|
|
+ int selectCount = Math.min(2, childNodes.getTrees().size());
|
|
|
+ for (int i = 0; i < selectCount; i++) {
|
|
|
+ selectedNodes.add(childNodes.getTrees().get(i));
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("用户勾选了 {} 个节点", selectedNodes.size());
|
|
|
+
|
|
|
+ // 5. 执行批量绑定
|
|
|
+ int successCount = this.batchBind(pKeyId, selectedNodes, projectId, contractId);
|
|
|
+ log.info("步骤5: 批量绑定完成,成功绑定 {} 个节点", successCount);
|
|
|
+
|
|
|
+ // 6. 查询已绑定的节点
|
|
|
+ log.info("步骤6: 查询已绑定的节点");
|
|
|
+ List<WbsDivide> bindedNodes = this.getByPKeyId(pKeyId);
|
|
|
+ log.info("查询到 {} 个已绑定节点", bindedNodes.size());
|
|
|
+ for (WbsDivide node : bindedNodes) {
|
|
|
+ log.info("已绑定节点: divideId={}, name={}, divideNum={}",
|
|
|
+ node.getDivideId(), node.getName(), node.getDivideNum());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 7. 查询已绑定的divideId列表(用于前端标记已关联状态)
|
|
|
+ log.info("步骤7: 查询已绑定的divideId列表");
|
|
|
+ List<String> bindedDivideIds = this.getBindedDivideIds(pKeyId);
|
|
|
+ log.info("已绑定的divideId列表: {}", bindedDivideIds);
|
|
|
+
|
|
|
+ // 8. 模拟用户解绑第一个节点
|
|
|
+ if (!bindedNodes.isEmpty()) {
|
|
|
+ log.info("步骤8: 用户解绑第一个节点");
|
|
|
+ WbsDivide firstNode = bindedNodes.get(0);
|
|
|
+ boolean unbindResult = this.unbind(firstNode.getDivideId(), pKeyId);
|
|
|
+ log.info("解绑结果: {}, divideId={}", unbindResult, firstNode.getDivideId());
|
|
|
+
|
|
|
+ // 9. 再次查询已绑定节点,验证解绑结果
|
|
|
+ log.info("步骤9: 再次查询已绑定节点");
|
|
|
+ List<WbsDivide> remainingNodes = this.getByPKeyId(pKeyId);
|
|
|
+ log.info("剩余已绑定节点数: {}", remainingNodes.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ log.warn("未能从第三方接口获取到树节点数据");
|
|
|
+ log.info("模拟数据进行测试...");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("========== 测试完整流程结束 ==========");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|