package com.platform.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.map.MapUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.github.pagehelper.PageHelper; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.platform.exception.BusinessException; import com.platform.mapper.UserMapper; import com.platform.model.dto.UserDTO; import com.platform.model.entity.ExcelDataEntity; import com.platform.model.entity.UserAccountEntity; import com.platform.model.entity.UserEntity; import com.platform.model.entity.UserRelationEntity; import com.platform.model.vo.AssociationVO; import com.platform.model.vo.EveryoneVO; import com.platform.model.vo.UserVO; import com.platform.service.ExcelDataService; import com.platform.service.UserAccountService; import com.platform.service.UserRelationService; import com.platform.service.UserService; import com.platform.utils.DozerUtils; import com.platform.utils.IdCardUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.ObjectUtils; import javax.annotation.Resource; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Map; @Service public class UserServiceImpl extends ServiceImpl implements UserService { @Resource private UserAccountService userAccountService; @Resource private UserRelationService userRelationService; @Resource private ExcelDataService excelDataService; @Override public UserVO getDetail(String userName,String caseId) { UserEntity userEntity = baseMapper.selectOne(new LambdaQueryWrapper() .eq(UserEntity::getUserName, userName)); UserVO userVO = DozerUtils.convertBean(userEntity, UserVO.class); if (userVO != null) { //查看用户账户列表 List userAccountEntityList = userAccountService.queryListByUserName(userName); userVO.setUserAccountList(userAccountEntityList); } List relationShipList = userRelationService.list(new LambdaQueryWrapper() .eq(UserRelationEntity::getUserName, userEntity.getUserName()).eq(UserRelationEntity::getCaseId, caseId)); if(!ObjectUtils.isEmpty(relationShipList)){ userVO.setRelationShipList(relationShipList); } //本人账号数量 HashSet account = Sets.newHashSet(); //查询关联资金 List financialTransactions = excelDataService.selectFinancialTransactions(userName, caseId); Map> detailsFunds = Maps.newHashMap(); for (AssociationVO financialTransaction : financialTransactions) { if(financialTransaction.getDirection().equals("出账")){ if(detailsFunds.containsKey(financialTransaction.getPayee())){ financialTransaction.setRelationDesc(detailsFunds.get(financialTransaction.getPayee()).size() + "、"+ financialTransaction.getStartTime() + "-" + financialTransaction.getEndTime() + financialTransaction.getPayee() + "->" + financialTransaction.getPayment() + "," + financialTransaction.getTotalAmount() + ",交易" + financialTransaction.getNum() + "次"); detailsFunds.get(financialTransaction.getPayee()).add(financialTransaction); account.add(financialTransaction.getPayment()); }else { List list = new ArrayList<>(); financialTransaction.setRelationDesc("1、"+ financialTransaction.getStartTime() + "-" + financialTransaction.getEndTime() + financialTransaction.getPayee() + "->" + financialTransaction.getPayment() + "," + financialTransaction.getTotalAmount() + ",交易" + financialTransaction.getNum() + "次"); list.add(financialTransaction); detailsFunds.put(financialTransaction.getPayee(),list); account.add(financialTransaction.getPayment()); } }else if(financialTransaction.getDirection().equals("入账")){ if(detailsFunds.containsKey(financialTransaction.getPayment())){ financialTransaction.setRelationDesc(detailsFunds.get(financialTransaction.getPayment()).size() + "、"+ financialTransaction.getStartTime() + "-" + financialTransaction.getEndTime() + financialTransaction.getPayee() + "->" + financialTransaction.getPayment() + "," + financialTransaction.getTotalAmount() + ",交易" + financialTransaction.getNum() + "次"); detailsFunds.get(financialTransaction.getPayment()).add(financialTransaction); account.add(financialTransaction.getPayee()); }else { List list = new ArrayList<>(); financialTransaction.setRelationDesc("1、"+ financialTransaction.getStartTime() + "-" + financialTransaction.getEndTime() + financialTransaction.getPayee() + "->" + financialTransaction.getPayment() + "," + financialTransaction.getTotalAmount() + ",交易" + financialTransaction.getNum() + "次"); list.add(financialTransaction); detailsFunds.put(financialTransaction.getPayment(),list); account.add(financialTransaction.getPayee()); } } } //关联人员数量 userVO.setHeadcount(detailsFunds.size()); //关联交易次数 int quantity = 0; for (AssociationVO financialTransaction : financialTransactions) { quantity += financialTransaction.getNum(); } userVO.setQuantity(quantity); //本人账户数量 userVO.setAccountNum(account.size()); //明细 userVO.setDetailsFunds(detailsFunds); return userVO; } @Override public boolean updateUser(UserDTO userDTO) { List relationShipList = userDTO.getRelationShipList(); if(!ObjectUtils.isEmpty(relationShipList)){ userRelationService.saveOrUpdateBatch(relationShipList); } updateById(userDTO); //先删除账户信息 userAccountService.deleteByUserName(userDTO.getUserName()); //保存新的账户信息 List userAccountEntityList = userDTO.getUserAccountList(); if (CollUtil.isNotEmpty(userAccountEntityList)) { userAccountEntityList.forEach(userAccountEntity -> userAccountEntity.setUserName(userDTO.getUserName())); userAccountService.updateBatchById(userAccountEntityList); } return Boolean.TRUE; } @Override @Transactional public Boolean add(UserDTO userDTO) { //解析身份证号 Map idCardInfo = IdCardUtils.parseIdCardInfo(userDTO.getIdCard()); if(MapUtil.isNotEmpty(idCardInfo)){ userDTO.setAge(Integer.parseInt(idCardInfo.get("age"))); userDTO.setSex(idCardInfo.get("sex")); userDTO.setIdType("身份证"); } save(userDTO); List relationShipList = userDTO.getRelationShipList(); for (UserRelationEntity userRelationEntity : relationShipList) { userRelationEntity.setOtherName(userDTO.getUserName()); userRelationEntity.setCaseId(userDTO.getCaseId()); } userRelationService.saveBatch(relationShipList); for (UserRelationEntity userRelationEntity : relationShipList) { userRelationEntity.setOtherName(userRelationEntity.getUserName()); userRelationEntity.setUserName(userDTO.getUserName()); userRelationEntity.setCaseId(userDTO.getCaseId()); userRelationEntity.setId(null); } userRelationService.saveBatch(relationShipList); return Boolean.TRUE; } @Override public List cashFlow(UserDTO userDTO) { List excelDataList = excelDataService.cashFlow(userDTO); for (ExcelDataEntity excelDataEntity : excelDataList) { if("出账".equals(excelDataEntity.getDirection())){ excelDataEntity.setSelfAccount(excelDataEntity.getPayAccount()); excelDataEntity.setAssociationAccount(excelDataEntity.getPayeeAccount()); }else if("入账".equals(excelDataEntity.getDirection())){ excelDataEntity.setSelfAccount(excelDataEntity.getPayeeAccount()); excelDataEntity.setAssociationAccount(excelDataEntity.getPayAccount()); } } return excelDataList; } @Override public EveryoneVO relationPage(UserDTO userDTO) { EveryoneVO everyoneVO = new EveryoneVO(); UserEntity oneself = getOne(new LambdaQueryWrapper() .eq(UserEntity::getUserName, userDTO.getUserName()).eq(UserEntity::getCaseId, userDTO.getCaseId()),false); UserEntity other = getOne(new LambdaQueryWrapper() .eq(UserEntity::getUserName, userDTO.getOtherName()).eq(UserEntity::getCaseId, userDTO.getCaseId()),false); if(ObjectUtils.isEmpty(oneself) || ObjectUtils.isEmpty(other)){ throw new BusinessException("用户不存在"); } //标题 everyoneVO.setTitle(oneself.getUserName() + "," + other.getUserName()); //查询关系 List userRelation = userRelationService.list(new LambdaQueryWrapper() .eq(UserRelationEntity::getUserName, userDTO.getUserName()) .eq(UserRelationEntity::getOtherName, userDTO.getOtherName())); //指认关系 if(ObjectUtils.isEmpty(userRelation)){ everyoneVO.setRelation(userRelation); } //查询账户 List oneselfAccountList = userAccountService.queryUserAccountList(oneself.getUserName()); List otherAccountList = userAccountService.queryUserAccountList(oneself.getUserName()); PageHelper.startPage(userDTO.getPageNum(),userDTO.getPageSize()); List details = excelDataService.relationPage(oneselfAccountList,otherAccountList); everyoneVO.setDetails(details); PageHelper.clearPage(); return everyoneVO; } @Override @Transactional public Boolean delete(String userName) { excelDataService.remove(new LambdaQueryWrapper().eq(ExcelDataEntity::getPayUserName, userName)); //删除关系 userRelationService.remove(new LambdaQueryWrapper().eq(UserRelationEntity::getUserName, userName)); userRelationService.remove(new LambdaQueryWrapper().eq(UserRelationEntity::getOtherName, userName)); //删除账户 userAccountService.deleteByUserName(userName); return remove(new LambdaQueryWrapper().eq(UserEntity::getUserName, userName)); } }