package com.platform.service.impl; import cn.hutool.core.collection.CollUtil; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.platform.mapper.CaseMapper; import com.platform.model.dto.CaseDTO; import com.platform.model.dto.ExcelDataDTO; import com.platform.model.entity.CaseEntity; import com.platform.model.entity.ExcelDataEntity; import com.platform.model.vo.*; import com.platform.service.CaseService; import com.platform.service.ExcelDataService; import com.platform.utils.StringUtils; import org.apache.commons.compress.utils.Lists; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.*; @Service public class CaseServiceImpl extends ServiceImpl implements CaseService { @Resource private ExcelDataService excelDataService; @Override public List queryDataList(String id) { //查看案件所有的excelData List excelDataEntityList = excelDataService.queryByCaseId(id); excelDataEntityList.forEach(excelDataEntity -> { if (StringUtils.isNull(excelDataEntity.getPayUserName())) { excelDataEntity.setPayUserName("无对应人员的数据"); } }); // 调用分组方法 Map> groupedMap = groupByPayUserName(excelDataEntityList); //初始化集合 List caseVOList = Lists.newArrayList(); //遍历groupedMap for (Map.Entry> entry : groupedMap.entrySet()) { System.out.println("Key: " + entry.getKey() + ", Values: " + entry.getValue()); caseVOList.add(CaseVO.builder().excelDataList(entry.getValue()) .payUserName(entry.getKey()) .build()); } return caseVOList; } @Override public boolean updatePayUser(ExcelDataDTO excelDataDTO) { //查看fileId线管的excelData数据 List excelDataEntityList = excelDataService.queryListByFileId(excelDataDTO.getFileId(), excelDataDTO.getCaseId()); if (CollUtil.isNotEmpty(excelDataEntityList)) { excelDataEntityList.forEach(excelDataEntity -> excelDataEntity.setPayUserName(excelDataDTO.getPayUserName())); } return excelDataService.updateBatchById(excelDataEntityList); } @Override public boolean deleteExcel(String fileId) { return excelDataService.deleteByFileId(fileId); } @Override public boolean updateExcelUse(ExcelDataDTO excelDataDTO) { //查看数据 List excelDataEntityList = excelDataService.queryListByFileId(excelDataDTO.getFileId(), excelDataDTO.getCaseId()); if (CollUtil.isNotEmpty(excelDataEntityList)) { excelDataEntityList.forEach(excelDataEntity -> excelDataEntity.setUseFlag(excelDataDTO.getUseFlag())); } return excelDataService.updateBatchById(excelDataEntityList); } @Override public boolean mergeUser(ExcelDataDTO excelDataDTO) { //查看合并之前的人员数据 //初始化合并后的集合 List mergeList = Lists.newArrayList(); excelDataDTO.getOldPayUserName().forEach(payUserName -> { List excelDataEntityList = excelDataService.queryListByCaseIdAndPayUserName(excelDataDTO.getCaseId(), payUserName); if (CollUtil.isNotEmpty(excelDataEntityList)) { mergeList.addAll(excelDataEntityList); } }); if (CollUtil.isNotEmpty(mergeList)) { mergeList.forEach(excelDataEntity -> excelDataEntity.setPayUserName(excelDataDTO.getNewPayUserName())); excelDataService.updateBatchById(mergeList); } return Boolean.TRUE; } @Override public GraphVO draw(CaseDTO caseDTO) { // 初始化画布 GraphVO graph = new GraphVO(); // 使用Map来存储边的信息,键为payer-payee组合,值为Edge对象 Map edgeMap = new HashMap<>(); //查看案件所有的人员 List excelDataEntityList = excelDataService.queryList(caseDTO); if (CollUtil.isNotEmpty(excelDataEntityList)) { //初始化set集合 // 使用Set去重 Set uniqueNodes = new HashSet<>(); excelDataEntityList.forEach(excelDataEntity -> { if (StringUtils.isNotEmpty(excelDataEntity.getPayUserName()) && StringUtils.isNotEmpty(excelDataEntity.getPayeeName())) { uniqueNodes.add(excelDataEntity.getPayUserName()); uniqueNodes.add(excelDataEntity.getPayeeName()); // 生成边的唯一标识 String edgeKey = excelDataEntity.getPayUserName() + "-" + excelDataEntity.getPayeeName(); // 检查是否已经存在这条边 if (edgeMap.containsKey(edgeKey)) { // 如果存在,更新总笔数和总金额 EdgeVO existingEdge = edgeMap.get(edgeKey); existingEdge.setTotalTransactions(existingEdge.getTotalTransactions() + 1); existingEdge.setTotalAmount(existingEdge.getTotalAmount() + excelDataEntity.getAmount()); } else { // 如果不存在,创建新的边 String edgeId = "line-" + excelDataEntity.getPayUserName() + "-" + excelDataEntity.getPayeeName(); EdgeVO newEdge = new EdgeVO(edgeId, excelDataEntity.getPayUserName(), excelDataEntity.getPayeeName(), 1, excelDataEntity.getAmount()); edgeMap.put(edgeKey, newEdge); } } }); // 添加去重后的节点 for (String node : uniqueNodes) { if (caseDTO.getPrincipal() != null) { if (caseDTO.getPrincipal() == 1) { //过滤掉收款人名称中带有公司的数据 if (node.contains("公司") || node.length() > 4) { continue; } } else { //过滤掉收款人名称中不含公司的数据 if (!node.contains("公司") || node.length() > 4) { continue; } } graph.addNode(new NodeVO(node)); continue; } graph.addNode(new NodeVO(node)); } // 添加边 for (EdgeVO edge : edgeMap.values()) { if (caseDTO.getMinAmount() != null && caseDTO.getMaxAmount() != null) { if (edge.getTotalAmount() >= caseDTO.getMinAmount() && edge.getTotalAmount() <= caseDTO.getMaxAmount()) { graph.addEdge(edge); continue; } } if (caseDTO.getMaxTransaction() != null && caseDTO.getMinTransaction() != null) { if (edge.getTotalTransactions() >= caseDTO.getMinTransaction() && edge.getTotalTransactions() <= caseDTO.getMaxTransaction()) { graph.addEdge(edge); continue; } } if (caseDTO.getPrincipal() != null) { if (caseDTO.getPrincipal() == 1) { //过滤掉收款人名称中带有公司的数据 if (edge.getTarget().contains("公司") || edge.getTarget().length() > 4) { continue; } } else { //过滤掉收款人名称中不含公司的数据 if (!edge.getTarget().contains("公司") || edge.getTarget().length() > 4) { continue; } } graph.addEdge(edge); continue; } graph.addEdge(edge); } } return graph; } private Map> groupByPayUserName(List excelDataVOList) { Map> groupedMap = new HashMap<>(); for (ExcelDataVO obj : excelDataVOList) { String key = obj.getPayUserName(); // 获取A字段的值作为键 if (!groupedMap.containsKey(key)) { groupedMap.put(key, new ArrayList<>()); // 如果键不存在,创建一个新的List } groupedMap.get(key).add(obj); // 将对象添加到对应的List中 } return groupedMap; } @Override public List pageCase(CaseDTO caseDTO) { return baseMapper.pageCase(caseDTO); } }