package com.platform.service.impl; import cn.hutool.core.collection.CollUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcelFactory; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.platform.mapper.GroupDetailMapper; import com.platform.model.dto.CaseDTO; import com.platform.model.dto.GroupDetailDTO; import com.platform.model.entity.CaseRuleConfigEntity; import com.platform.model.entity.ExcelDataEntity; import com.platform.model.entity.GroupDetailEntity; import com.platform.model.entity.GroupEntity; import com.platform.model.vo.*; import com.platform.service.*; import com.platform.utils.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.*; @Service public class GroupDetailServiceImpl extends ServiceImpl implements GroupDetailService { @Resource private UserService userService; @Resource private WhiteService whiteService; @Resource private GroupService groupService; @Resource private ExcelDataService excelDataService; @Resource private CaseRuleConfigService caseRuleConfigService; @Override public List queryList(GroupDetailDTO groupDetailDTO) { return baseMapper.selectList(new LambdaQueryWrapper() .eq(GroupDetailEntity::getGroupId, groupDetailDTO.getGroupId()) .notLike(GroupDetailEntity::getUserName, "商户")); } @Override public List queryIp(GroupDetailDTO groupDetailDTO) { return baseMapper.queryIp(groupDetailDTO); } @Override public List queryIpDetail(String ip) { return baseMapper.selectList(new LambdaQueryWrapper() .eq(GroupDetailEntity::getIp, ip)); } @Override public List queryMac(GroupDetailDTO groupDetailDTO) { return baseMapper.queryMac(groupDetailDTO); } @Override public List queryMacDetail(String mac) { return baseMapper.selectList(new LambdaQueryWrapper() .eq(GroupDetailEntity::getMac, mac)); } @Override public GraphVO draw(String groupId) { //查看group信息 GroupEntity groupEntity = groupService.getById(groupId); if (groupEntity == null) { return null; } // 创建图 GraphVO graph = new GraphVO(); // 使用Map来存储边的信息,键为payer-payee组合,值为Edge对象 Map edgeMap = new HashMap<>(); // 使用Set去重 Set uniqueNodes = new HashSet<>(); //根据团体id查看团体明细 List userList = baseMapper.queryUserList(groupId); if (CollUtil.isNotEmpty(userList)) { CaseDTO caseDTO = new CaseDTO(); CaseRuleConfigEntity caseRuleConfigEntity = caseRuleConfigService.getByCaseId(groupEntity.getCaseId()); if (caseRuleConfigEntity != null) { caseDTO.setMinTransaction(caseRuleConfigEntity.getMinTransaction()); caseDTO.setMaxTransaction(caseRuleConfigEntity.getMaxTransaction()); caseDTO.setMinAmount(caseRuleConfigEntity.getMinAmount()); caseDTO.setMaxAmount(caseRuleConfigEntity.getMaxAmount()); caseDTO.setStartTime(caseRuleConfigEntity.getStartTime()); caseDTO.setEndTime(caseRuleConfigEntity.getEndTime()); caseDTO.setPrincipal(caseRuleConfigEntity.getPrincipal()); caseDTO.setWhiteFlag(caseRuleConfigEntity.isWhiteFlag()); caseDTO.setId(groupEntity.getCaseId()); } List excelDataEntityList = filterExcelData(userList, caseDTO); // 遍历结果集 for (ExcelDataVO excelDataVO : excelDataEntityList) { String payer = excelDataVO.getPayUserName(); String payee = excelDataVO.getPayeeName(); double amount = excelDataVO.getAmount(); if (StringUtils.isEmpty(payee) || StringUtils.isEmpty(payer)) { continue; } // 添加节点 uniqueNodes.add(payer); uniqueNodes.add(payee); // 生成边的唯一标识 String edgeKey = payer + "-" + payee; // 检查是否已经存在这条边 if (edgeMap.containsKey(edgeKey)) { // 如果存在,更新总笔数和总金额 EdgeVO existingEdge = edgeMap.get(edgeKey); 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); edgeMap.put(edgeKey, newEdge); } } List userNameList = new ArrayList<>(uniqueNodes); List excelDataEntityListNew = userService.queryUserLabel(userNameList, groupEntity.getCaseId()); Map userAndLabel = new HashMap<>(); excelDataEntityListNew.forEach(excelDataVO -> userAndLabel.put(excelDataVO.getPayUserName(), excelDataVO.getLabel())); // 添加去重后的节点 for (String node : uniqueNodes) { graph.addNode(new NodeVO(node, userAndLabel.get(node), false, false)); } // 添加边 for (EdgeVO edge : edgeMap.values()) { graph.addEdge(edge); } return graph; } return null; } private List filterExcelData(List userList, CaseDTO caseDTO) { List excelDataEntityList = excelDataService.queryListByPayUserList(userList, caseDTO); if (CollUtil.isEmpty(excelDataEntityList)) { return excelDataEntityList; } // 使用迭代器安全删除 Iterator iterator = excelDataEntityList.iterator(); while (iterator.hasNext()) { ExcelDataEntity excelDataEntity = iterator.next(); // 过滤付款人类型 if (caseDTO.getPrincipal() != null) { if (caseDTO.getPrincipal() == 1 && excelDataEntity.getPayeeName().contains("公司")) { iterator.remove(); continue; } if (caseDTO.getPrincipal() == 2 && !excelDataEntity.getPayeeName().contains("公司")) { iterator.remove(); continue; } } // 过滤金额范围 if (!isWithinAmountRange(excelDataEntity, caseDTO.getMinAmount(), caseDTO.getMaxAmount())) { iterator.remove(); continue; } // 过滤交易次数 if (!isWithinTransactionRange(excelDataEntity, caseDTO.getMinTransaction(), caseDTO.getMaxTransaction())) { iterator.remove(); } } // 白名单过滤 if (caseDTO.getWhiteFlag() != null && caseDTO.getWhiteFlag()) { filterWhite(excelDataEntityList); } return excelDataEntityList; } private boolean isWithinAmountRange(ExcelDataEntity entity, Double min, Double max) { double amount = entity.getAmount(); if (min != null && amount < min) return false; if (max != null && amount > max) return false; return true; } private boolean isWithinTransactionRange(ExcelDataEntity entity, Integer min, Integer max) { int count = entity.getTransactionCount(); if (min != null && count < min) return false; if (max != null && count > max) return false; return true; } private void filterWhite(List excelDataEntityList) { //查看白名单 List whiteUser = whiteService.queryList(); // 如果 name 属性值存在于白名单中,则移除该元素 excelDataEntityList.removeIf(entity -> whiteUser.contains(entity.getPayUserName()) || whiteUser.contains(entity.getPayeeName())); } @Override public void personExport(GroupDetailDTO groupDetailDTO, HttpServletResponse response) { // 设置响应头 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName; try { fileName = URLEncoder.encode("团体-人员列表", "UTF-8").replaceAll("\\+", "%20"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); List groupDetailEntityList = queryList(groupDetailDTO); // 写入Excel try { EasyExcel.write(response.getOutputStream(), WhiteVO.class) .sheet("团体-人员列表") .doWrite(groupDetailEntityList); } catch (IOException e) { throw new RuntimeException(e); } } @Override public void ipExport(GroupDetailDTO groupDetailDTO, HttpServletResponse response) { // 设置响应头 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName; try { fileName = URLEncoder.encode("团体-ip列表", "UTF-8").replaceAll("\\+", "%20"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); List groupDetailIpVOList = baseMapper.queryIp(groupDetailDTO); // 写入Excel try { EasyExcelFactory.write(response.getOutputStream(), WhiteVO.class) .sheet("团体-IP列表") .doWrite(groupDetailIpVOList); } catch (IOException e) { throw new RuntimeException(e); } } @Override public void macExport(GroupDetailDTO groupDetailDTO, HttpServletResponse response) { // 设置响应头 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName; try { fileName = URLEncoder.encode("团体-ip列表", "UTF-8").replaceAll("\\+", "%20"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); List groupDetailMacVOList = baseMapper.queryMac(groupDetailDTO); // 写入Excel try { EasyExcelFactory.write(response.getOutputStream(), WhiteVO.class) .sheet("团体-IP列表") .doWrite(groupDetailMacVOList); } catch (IOException e) { throw new RuntimeException(e); } } }