Commit ba7ba16e authored by wujipeng's avatar wujipeng
Browse files

Merge remote-tracking branch 'origin/dev' into dev

parents 1546ef3e b13cd184
package com.platform.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.platform.model.entity.GroupDetailEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface GroupDetailMapper extends BaseMapper<GroupDetailEntity> {
}
package com.platform.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.platform.model.entity.GroupEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface GroupMapper extends BaseMapper<GroupEntity> {
}
package com.platform.model.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("group_detail")
public class GroupDetailEntity {
@TableId(value = "id")
private int id;
@TableField("case_id")
private int caseId;
private String userName;
private int transactionCount; // 总交易次数
private double totalAmount; // 总交易金额(转出+转入)
private double outgoingAmount; // 转出金额
private double incomingAmount; // 转入金额
public GroupDetailEntity(String userName) {
this.userName = userName;
}
public void addOutgoingTransaction(double amount) {
this.outgoingAmount += amount;
this.totalAmount += amount;
}
public void addIncomingTransaction(double amount) {
this.incomingAmount += amount;
this.totalAmount += amount;
}
public void incrementTransactionCount(int count) {
this.transactionCount += count;
}
}
package com.platform.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("group_info")
public class GroupEntity {
@TableId(type = IdType.AUTO)
private int id;
private int caseId;
private String groupName;
private int groupSize;
private Double totalAmount;
private int totalTransactions;
}
package com.platform.model.vo;
import com.platform.model.entity.GroupDetailEntity;
import lombok.Data;
import java.util.List;
@Data
public class GroupVO {
private int id;
private String groupName; // 团体名
private int groupSize; // 团体人数
private double totalAmount; // 团体资金总量
private int totalTransactions; // 交易总次数
private List<GroupDetailEntity> groupDetailList; // 成员明细列表
public GroupVO(String groupName, int groupSize, double totalAmount, int totalTransactions) {
public GroupVO(String groupName, int groupSize, double totalAmount, int totalTransactions, List<GroupDetailEntity> groupDetailList) {
this.groupName = groupName;
this.groupSize = groupSize;
this.totalAmount = totalAmount;
this.totalTransactions = totalTransactions;
this.groupDetailList = groupDetailList;
}
}
package com.platform.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.platform.model.entity.GroupDetailEntity;
public interface GroupDetailService extends IService<GroupDetailEntity> {
}
package com.platform.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.platform.model.entity.GroupEntity;
import com.platform.model.vo.GroupVO;
import java.util.List;
public interface GroupService extends IService<GroupEntity> {
void saveGroup(int id, List<GroupVO> groups);
}
......@@ -7,9 +7,11 @@ 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.entity.GroupDetailEntity;
import com.platform.model.vo.*;
import com.platform.service.CaseService;
import com.platform.service.ExcelDataService;
import com.platform.service.GroupService;
import com.platform.utils.StringUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
......@@ -22,6 +24,8 @@ public class CaseServiceImpl extends ServiceImpl<CaseMapper, CaseEntity> impleme
@Resource
private ExcelDataService excelDataService;
@Resource
private GroupService groupService;
@Override
public List<CaseVO> queryDataList(String id) {
......@@ -91,66 +95,65 @@ public class CaseServiceImpl extends ServiceImpl<CaseMapper, CaseEntity> impleme
@Override
public GraphVO draw(CaseDTO caseDTO) {
// 初始化画布
// 初始化返回对象
GraphVO graph = new GraphVO();
// 使用并查集来管理团体
// 使用并查集管理团体关系
UnionFindVO uf = new UnionFindVO();
// 使用Map来存储边的信息,键为payer-payee组合,值为Edge对象
// 存储边信息,键为payer-payee组合
Map<String, EdgeVO> edgeMap = new HashMap<>();
// 查看案件所有的人员
// 查询案件数据
List<ExcelDataEntity> excelDataEntityList = excelDataService.queryList(caseDTO);
if (CollUtil.isNotEmpty(excelDataEntityList)) {
excelDataEntityList.forEach(excelDataEntity -> {
if (StringUtils.isNotEmpty(excelDataEntity.getPayUserName())
&& StringUtils.isNotEmpty(excelDataEntity.getPayeeName())) {
if (CollUtil.isEmpty(excelDataEntityList)) {
return graph; // 无数据直接返回空图
}
// 第一步:处理原始数据
for (ExcelDataEntity excelDataEntity : excelDataEntityList) {
if (StringUtils.isEmpty(excelDataEntity.getPayUserName()) ||
StringUtils.isEmpty(excelDataEntity.getPayeeName())) {
continue; // 跳过无效记录
}
String payer = excelDataEntity.getPayUserName();
String payee = excelDataEntity.getPayeeName();
double amount = excelDataEntity.getAmount();
// 添加节点到并查集
// 添加到并查集
uf.makeSet(payer);
uf.makeSet(payee);
// 合并团体
uf.union(payer, payee);
// 生成边的唯一标识
// 处理边信息
String edgeKey = payer + "-" + payee;
// 检查是否已经存在这条边
if (edgeMap.containsKey(edgeKey)) {
// 如果存在,更新总笔数和总金额
EdgeVO existingEdge = edgeMap.get(edgeKey);
if (existingEdge != null) {
// 更新现有边
existingEdge.setTotalTransactions(existingEdge.getTotalTransactions() + 1);
existingEdge.setTotalAmount(existingEdge.getTotalAmount() + amount);
} else {
// 如果不存在,创建新的边
// 创建新边
String edgeId = "line-" + payer + "-" + payee;
EdgeVO newEdge = new EdgeVO(edgeId, payer, payee, 1, amount, uf.find(payer));
edgeMap.put(edgeKey, newEdge);
}
}
});
// === 修复1:先完成所有过滤 ===
Set<String> validNodes = new HashSet<>(); // 有效节点集合
// 节点过滤
// 第二步:过滤节点
Set<String> validNodes = new HashSet<>();
for (String node : uf.getParent().keySet()) {
if (caseDTO.getPrincipal() != null) {
if (caseDTO.getPrincipal() == 1) {
if (node.contains("公司") || node.length() > 4) continue;
} else {
if (!node.contains("公司") || node.length() > 4) continue;
}
}
if (shouldIncludeNode(node, caseDTO)) {
validNodes.add(node);
graph.addNode(new NodeVO(node));
}
}
// 边过滤(同时检查两端节点有效性)
// 第三步:过滤边
List<EdgeVO> validEdges = new ArrayList<>();
for (EdgeVO edge : edgeMap.values()) {
// 检查两端节点是否有效
if (!validNodes.contains(edge.getSource()) ||
......@@ -158,83 +161,158 @@ public class CaseServiceImpl extends ServiceImpl<CaseMapper, CaseEntity> impleme
continue;
}
// 金额/笔数过滤
boolean amountValid = true;
// 检查金额过滤条件
if (caseDTO.getMinAmount() != null && caseDTO.getMaxAmount() != null) {
amountValid = edge.getTotalAmount() >= caseDTO.getMinAmount()
&& edge.getTotalAmount() <= caseDTO.getMaxAmount();
if (edge.getTotalAmount() < caseDTO.getMinAmount() ||
edge.getTotalAmount() > caseDTO.getMaxAmount()) {
continue;
}
}
boolean transValid = true;
// 检查交易次数过滤条件
if (caseDTO.getMinTransaction() != null && caseDTO.getMaxTransaction() != null) {
transValid = edge.getTotalTransactions() >= caseDTO.getMinTransaction()
&& edge.getTotalTransactions() <= caseDTO.getMaxTransaction();
if (edge.getTotalTransactions() < caseDTO.getMinTransaction() ||
edge.getTotalTransactions() > caseDTO.getMaxTransaction()) {
continue;
}
}
if (amountValid || transValid) {
// 添加到有效边集合
validEdges.add(edge);
graph.addEdge(edge);
}
}
// === 修复2:基于过滤后数据重建团体 ===
// 第四步:重建过滤后的团体关系
// 创建新的并查集用于过滤后的数据
UnionFindVO filteredUf = new UnionFindVO();
validNodes.forEach(filteredUf::makeSet);
// 用于按团体分组统计
Map<String, Set<String>> groupMembers = new HashMap<>();
Map<String, Double> groupTotalAmount = new HashMap<>();
Map<String, Integer> groupTotalTransactions = new HashMap<>();
// 步骤1:为每个有效节点创建独立团体
// 初始化每个节点的团体
for (String node : validNodes) {
String root = uf.find(node);
groupMembers.computeIfAbsent(root, k -> new HashSet<>()).add(node);
}
// 步骤2:仅用有效边更新团体关系
UnionFindVO filteredUf = new UnionFindVO(); // 新建过滤后的并查集
validNodes.forEach(filteredUf::makeSet);
for (EdgeVO edge : graph.getEdges()) {
// 处理有效边,更新团体关系
for (EdgeVO edge : validEdges) {
// 更新过滤后的并查集
filteredUf.union(edge.getSource(), edge.getTarget());
// 更新团体统计
String root = filteredUf.find(edge.getSource());
groupTotalAmount.merge(root, edge.getTotalAmount(), Double::sum);
groupTotalTransactions.merge(root, edge.getTotalTransactions(), Integer::sum);
String originalRoot = uf.find(edge.getSource());
groupTotalAmount.merge(originalRoot, edge.getTotalAmount(), Double::sum);
groupTotalTransactions.merge(originalRoot, edge.getTotalTransactions(), Integer::sum);
}
// 第五步:计算成员明细
Map<String, GroupDetailEntity> memberDetailMap = new HashMap<>();
// 初始化所有有效节点的成员明细
for (String node : validNodes) {
memberDetailMap.put(node, new GroupDetailEntity(node));
}
// 更新成员明细
for (EdgeVO edge : validEdges) {
String payer = edge.getSource();
String payee = edge.getTarget();
double amount = edge.getTotalAmount();
// 更新付款人(转出)
GroupDetailEntity payerDetail = memberDetailMap.get(payer);
if (payerDetail != null) {
payerDetail.addOutgoingTransaction(amount);
payerDetail.incrementTransactionCount(edge.getTotalTransactions());
}
// 步骤3:合并最终团体
// 更新收款人(转入)
GroupDetailEntity payeeDetail = memberDetailMap.get(payee);
if (payeeDetail != null) {
payeeDetail.addIncomingTransaction(amount);
payeeDetail.incrementTransactionCount(edge.getTotalTransactions());
}
}
// 第六步:处理团体合并(因过滤可能改变团体结构)
for (String node : validNodes) {
String originalRoot = uf.find(node);
String newRoot = filteredUf.find(node);
String oldRoot = uf.find(node);
if (!newRoot.equals(oldRoot)) {
if (!originalRoot.equals(newRoot)) {
// 合并团体
Set<String> members = groupMembers.remove(oldRoot);
Set<String> members = groupMembers.remove(originalRoot);
if (members != null) {
groupMembers.computeIfAbsent(newRoot, k -> new HashSet<>()).addAll(members);
// 合并统计信息
if (groupTotalAmount.containsKey(originalRoot)) {
groupTotalAmount.merge(newRoot, groupTotalAmount.get(originalRoot), Double::sum);
groupTotalAmount.remove(originalRoot);
}
if (groupTotalTransactions.containsKey(originalRoot)) {
groupTotalTransactions.merge(newRoot, groupTotalTransactions.get(originalRoot), Integer::sum);
groupTotalTransactions.remove(originalRoot);
}
}
}
}
// === 修复3:生成最终团体对象 ===
// 第七步:生成团体对象
for (Map.Entry<String, Set<String>> entry : groupMembers.entrySet()) {
String groupId = entry.getKey();
Set<String> members = entry.getValue();
// 跳过孤立节点(可选)
// 跳过孤立节点
if (members.size() == 1 && !hasConnections(members, graph)) {
continue;
}
// 收集成员明细
List<GroupDetailEntity> memberDetails = new ArrayList<>();
for (String member : members) {
GroupDetailEntity detail = memberDetailMap.get(member);
if (detail != null) {
memberDetails.add(detail);
}
}
// 按用户名排序
memberDetails.sort(Comparator.comparing(GroupDetailEntity::getUserName));
// 添加团体对象
graph.addGroup(new GroupVO(
groupId,
members.size(),
groupTotalAmount.getOrDefault(groupId, 0.0),
groupTotalTransactions.getOrDefault(groupId, 0)
groupTotalTransactions.getOrDefault(groupId, 0),
memberDetails
));
}
}
//保存团体数据
groupService.saveGroup(caseDTO.getId(), graph.getGroups());
return graph;
}
// 节点过滤逻辑
private boolean shouldIncludeNode(String node, CaseDTO caseDTO) {
if (caseDTO.getPrincipal() == null) {
return true; // 无过滤条件时包含所有节点
}
if (caseDTO.getPrincipal() == 1) {
// 过滤掉收款人名称中带有公司的数据
return !(node.contains("公司") || node.length() > 4);
} else {
// 过滤掉收款人名称中不含公司的数据
return node.contains("公司") || node.length() > 4;
}
}
// 检查团体是否有实际连接(非孤立节点)
private boolean hasConnections(Set<String> members, GraphVO graph) {
return graph.getEdges().stream()
......
package com.platform.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.mapper.GroupDetailMapper;
import com.platform.model.entity.GroupDetailEntity;
import com.platform.service.GroupDetailService;
import org.springframework.stereotype.Service;
@Service
public class GroupDetailServiceImpl extends ServiceImpl<GroupDetailMapper, GroupDetailEntity> implements GroupDetailService {
}
package com.platform.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.mapper.GroupMapper;
import com.platform.model.entity.GroupDetailEntity;
import com.platform.model.entity.GroupEntity;
import com.platform.model.vo.GroupVO;
import com.platform.service.GroupDetailService;
import com.platform.service.GroupService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, GroupEntity> implements GroupService {
@Resource
private GroupDetailService groupDetailService;
@Override
public void saveGroup(int id, List<GroupVO> groups) {
//先删除每个案例的团体数据
baseMapper.delete(new LambdaQueryWrapper<GroupEntity>()
.eq(GroupEntity::getCaseId, id));
groupDetailService.remove(new LambdaQueryWrapper<GroupDetailEntity>()
.eq(GroupDetailEntity::getCaseId, id));
//保存新的案例团体和
groups.forEach(groupVO -> {
//初始化团体实体
GroupEntity groupEntity = new GroupEntity();
groupEntity.setGroupSize(groupVO.getGroupSize());
groupEntity.setGroupName(groupVO.getGroupName());
groupEntity.setCaseId(id);
groupEntity.setTotalAmount(groupVO.getTotalAmount());
groupEntity.setTotalTransactions(groupVO.getTotalTransactions());
//入库
save(groupEntity);
System.out.println(groupEntity.getId());
groupVO.setId(groupEntity.getId());
List<GroupDetailEntity> groupDetailList = groupVO.getGroupDetailList();
if (CollUtil.isNotEmpty(groupDetailList)) {
groupDetailList.forEach(groupDetailEntity -> groupDetailEntity.setCaseId(id));
groupDetailService.saveBatch(groupDetailList);
}
groupVO.setGroupDetailList(null);
});
}
}
......@@ -29,6 +29,8 @@
excel_data
where
use_flag = 1
and
case_id = #{id}
<if test="startTime != null and endTime != ''">
and time betwenn #{startTime} and #{endTime}
</if>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment