package com.jtzx.crm.module.sys.service; import cc.uncarbon.framework.core.exception.BusinessException; import cc.uncarbon.framework.core.page.PageResult; import cc.uncarbon.framework.crud.entity.HelioBaseEntity; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.jtzx.crm.module.sys.entity.*; import com.jtzx.crm.module.sys.mapper.*; import com.jtzx.crm.module.sys.model.request.AdminSysUserGroupRelationDTO; import com.jtzx.crm.module.sys.model.response.*; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Comparator; import java.util.List; import java.util.Map; /** * 数据表注释 */ @RequiredArgsConstructor @Service @Slf4j public class SysUserGroupRelationService { private final SysGroupMapper sysGroupMapper; private final SysUserGroupRelationMapper sysUserGroupRelationMapper; private final DictProvinceRegionMapper dictProvinceRegionMapper; private final DictCityRegionMapper dictCityRegionMapper; private final SysUserMapper sysUserMapper; /** * 查询所有用户(左侧列表数据) * @return */ public List queryAllSysUserList(String username) { List sysUserListBOList = Lists.newArrayList(); List sysUserEntities = sysUserMapper.selectList( new LambdaQueryWrapper<>(SysUserEntity.class) .eq(StrUtil.isNotEmpty(username), SysUserEntity::getUserName, username) .eq(SysUserEntity::getStatus, 1) .orderByAsc(HelioBaseEntity::getCreatedAt) ); if (CollUtil.isNotEmpty(sysUserEntities)) { sysUserEntities.forEach(entity -> { SysUserListBO bo = SysUserListBO.builder().build(); bo.setId(entity.getId()); bo.setUsername(entity.getUserName()); bo.setAccount(entity.getPin()); sysUserListBOList.add(bo); }); } return sysUserListBOList; } /** * 分页查询表格数据 * @param dto * @return */ public PageResult queryListByPage(AdminSysUserGroupRelationDTO dto) { //查询分组 List groupEntityList = sysGroupMapper.selectList( new LambdaQueryWrapper<>(SysGroupEntity.class) .select( SysGroupEntity::getSortCode, SysGroupEntity::getId, SysGroupEntity::getTitle ) .eq(dto.getGroupId() != null, SysGroupEntity::getId, dto.getGroupId()) .orderByAsc(SysGroupEntity::getSortCode) ); if (CollUtil.isEmpty(groupEntityList)) { return new PageResult() .setCurrent(1) .setSize(dto.getPageSize()) .setTotal(0) .setRecords(Lists.newArrayList()); } //查询已开通使用的省份数据 List provinceRegionEntityList = dictProvinceRegionMapper.selectList( new LambdaQueryWrapper<>(DictProvinceRegionEntity.class) .select( DictProvinceRegionEntity::getId, DictProvinceRegionEntity::getCode, DictProvinceRegionEntity::getName, DictProvinceRegionEntity::getSortCode ) .eq(dto.getProvinceId() != null, DictProvinceRegionEntity::getId, dto.getProvinceId()) .eq(DictProvinceRegionEntity::getUseStatus, 1) ); if (CollUtil.isEmpty(provinceRegionEntityList)) { return new PageResult() .setCurrent(1) .setSize(dto.getPageSize()) .setTotal(0) .setRecords(Lists.newArrayList()); } //查询城市数据 List cityRegionEntityList = dictCityRegionMapper.selectList( new LambdaQueryWrapper<>(DictCityRegionEntity.class) .select( DictCityRegionEntity::getParentCode, DictCityRegionEntity::getId, DictCityRegionEntity::getName ) .eq(dto.getCityId() != null, DictCityRegionEntity::getId, dto.getCityId()) .in(DictCityRegionEntity::getParentCode, provinceRegionEntityList.stream().map(DictProvinceRegionEntity::getCode).toList()) ); if (CollUtil.isEmpty(cityRegionEntityList)) { return new PageResult() .setCurrent(1) .setSize(dto.getPageSize()) .setTotal(0) .setRecords(Lists.newArrayList()); } //查询用户数据 List sysUserEntityList = sysUserMapper.selectList( new LambdaQueryWrapper<>(SysUserEntity.class) .select( SysUserEntity::getUserName, SysUserEntity::getId, SysUserEntity::getPin ) .eq(SysUserEntity::getStatus, 1) .eq(dto.getUserId() != null, SysUserEntity::getId, dto.getUserId()) .and(StrUtil.isNotEmpty(dto.getQueryStr()), z -> z.like(SysUserEntity::getUserName, dto.getQueryStr()) .or().like(SysUserEntity::getPin, dto.getQueryStr()) ) ); if (CollUtil.isEmpty(sysUserEntityList)) { return new PageResult() .setCurrent(1) .setSize(dto.getPageSize()) .setTotal(0) .setRecords(Lists.newArrayList()); } List userIds = sysUserEntityList.stream() .sorted(Comparator.comparing(SysUserEntity::getId)) .map(SysUserEntity::getId) .toList(); Page pageData = sysUserGroupRelationMapper.selectPage( new Page<>(dto.getPageNum(), dto.getPageSize()), new LambdaQueryWrapper<>(SysUserGroupRelationEntity.class) .eq(dto.getProvinceId() != null, SysUserGroupRelationEntity::getProvinceId, dto.getProvinceId()) .eq(dto.getCityId() != null, SysUserGroupRelationEntity::getCityId, dto.getCityId()) .eq(dto.getGroupId() != null, SysUserGroupRelationEntity::getGroupId, dto.getGroupId()) .eq(dto.getUserId() != null && StrUtil.isEmpty(dto.getQueryStr()), SysUserGroupRelationEntity::getUserId, dto.getUserId()) .in(StrUtil.isNotEmpty(dto.getQueryStr()), SysUserGroupRelationEntity::getUserId, userIds) .orderByDesc(SysUserGroupRelationEntity::getCreatedAt) ); if (CollUtil.isEmpty(pageData.getRecords())) { return new PageResult() .setCurrent(1) .setSize(dto.getPageSize()) .setTotal(0) .setRecords(Lists.newArrayList()); } List result = Lists.newArrayList(); PageResult pageResult = new PageResult() .setCurrent(pageData.getCurrent()) .setSize(pageData.getSize()) .setTotal(pageData.getTotal()); pageData.getRecords().forEach(entity -> { SysUserGroupRelationPageListBO bo = SysUserGroupRelationPageListBO.builder().build(); bo.setId(entity.getId()); groupEntityList.stream() .filter(x -> x.getId().equals(entity.getGroupId())) .findFirst() .ifPresent(x -> { bo.setGroupId(x.getId()); bo.setGroupName(x.getTitle()); }); sysUserEntityList.stream() .filter(x -> x.getId().equals(entity.getUserId())) .findFirst() .ifPresent(x -> { bo.setUserId(x.getId()); bo.setUserName(x.getUserName()); bo.setAccount(x.getPin()); }); provinceRegionEntityList.stream() .filter(x -> x.getId().equals(entity.getProvinceId())) .findFirst() .ifPresent(x -> { bo.setProvinceId(x.getId()); bo.setProvinceName(x.getName()); }); cityRegionEntityList.stream() .filter(x -> x.getId().equals(entity.getCityId())) .findFirst() .ifPresent(x -> { bo.setCityId(x.getId()); bo.setCityName(x.getName()); }); bo.setDelStatus(0); result.add(bo); }); pageResult.setRecords(result); return pageResult; } /** * 分组管理,查询表格数据(第一种表格形式的数据) * @return */ public List queryTableData() { List result = Lists.newArrayList(); //查询分组 List groupEntityList = Lists.newArrayList(); //查询用户和分组数据的关系数据 List groupRelationEntityList = Lists.newArrayList(); //查询用户数据 List sysUserEntityList = Lists.newArrayList(); //查询已开通使用的省份数据 List provinceRegionEntityList = Lists.newArrayList(); //查询城市数据 List cityRegionEntityList = Lists.newArrayList(); //公共查询数据 this.commonQueryData(groupEntityList, groupRelationEntityList, sysUserEntityList, provinceRegionEntityList, cityRegionEntityList); //按省份处理 if (CollUtil.isNotEmpty(provinceRegionEntityList)) { //循环省份 provinceRegionEntityList.forEach(provinceRegionEntity -> { //过滤省份下的城市 List cityRegionEntities = cityRegionEntityList.stream() .filter(x -> x.getParentCode().equals(provinceRegionEntity.getCode())) .sorted(Comparator.comparing(DictCityRegionEntity::getCode)).toList(); List userGroupRelationEntityList = Lists.newArrayList(); //过滤省份下的用户关系 if (CollUtil.isNotEmpty(groupRelationEntityList)) { userGroupRelationEntityList.addAll(groupRelationEntityList.stream() .filter(x -> x.getProvinceId().equals(provinceRegionEntity.getId())) .toList()); } //过滤每个省份下的用户数据信息 List sysUserEntities = Lists.newArrayList(); if (CollUtil.isNotEmpty(userGroupRelationEntityList) && CollUtil.isNotEmpty(sysUserEntityList)) { List userIds = userGroupRelationEntityList.stream() .map(SysUserGroupRelationEntity::getUserId) .distinct().toList(); sysUserEntities.addAll(sysUserEntityList.stream() .filter(x -> userIds.contains(x.getId())) .toList()); } //处理返回数据 result.add( this.handleTableData(provinceRegionEntity, cityRegionEntities, groupEntityList, userGroupRelationEntityList, sysUserEntities)); }); } return result; } public GroupUserProvinceBO handleTableData(DictProvinceRegionEntity dictProvinceRegionEntity, List cityRegionEntities, List groupEntityList, List userGroupRelationEntityList, List sysUserEntities) { GroupUserProvinceBO provinceBO = GroupUserProvinceBO.builder().build(); provinceBO.setProvinceId(dictProvinceRegionEntity.getId()); provinceBO.setProvinceName(dictProvinceRegionEntity.getName()); provinceBO.setSortCode(dictProvinceRegionEntity.getSortCode()); List groupData = Lists.newArrayList(); if (CollUtil.isNotEmpty(groupEntityList)) { groupEntityList.stream() .sorted(Comparator.comparing(SysGroupEntity::getSortCode)) .forEach(groupEntity -> { SysGroupBO sysGroupBO = SysGroupBO.builder().build(); BeanUtils.copyProperties(groupEntity, sysGroupBO); groupData.add(sysGroupBO); }); } provinceBO.setGroupData(groupData); //处理表格数据 List cityData = Lists.newArrayList(); cityRegionEntities.stream() .sorted(Comparator.comparing(DictCityRegionEntity::getCode)) .forEach(cityRegionEntity -> { GroupUserCityBO groupUserCityBO = GroupUserCityBO.builder().build(); groupUserCityBO.setCityId(cityRegionEntity.getId()); groupUserCityBO.setCityName(cityRegionEntity.getName()); Map userData = Maps.newHashMap(); if (CollUtil.isNotEmpty(groupEntityList)) { groupEntityList.stream() .sorted(Comparator.comparing(SysGroupEntity::getSortCode)) .forEach(groupEntity -> { //设置默认 userData.put(groupEntity.getId(), "-"); if (CollUtil.isNotEmpty(userGroupRelationEntityList)) { List userBOS = Lists.newArrayList(); userGroupRelationEntityList.stream() .filter(x -> x.getCityId().equals(cityRegionEntity.getId())) .filter(x -> x.getGroupId().equals(groupEntity.getId())) .sorted(Comparator.comparing(HelioBaseEntity::getCreatedAt)) .forEach(groupRelationEntity -> { if (CollUtil.isNotEmpty(sysUserEntities)) { sysUserEntities.stream() .filter(x -> x.getId().equals(groupRelationEntity.getUserId())) .findFirst() .ifPresent(d -> { if (CollUtil.isEmpty(userBOS) || userBOS.stream().map(GroupUserBO::getUserId).noneMatch(groupRelationEntity.getUserId()::equals)) { GroupUserBO bo = GroupUserBO.builder().build(); bo.setId(groupRelationEntity.getId()); bo.setUserId(groupRelationEntity.getUserId()); bo.setUserName(d.getUserName()); bo.setAccount(d.getPin()); bo.setGroupId(groupEntity.getId()); bo.setProvinceId(dictProvinceRegionEntity.getId()); bo.setCityId(cityRegionEntity.getId()); userBOS.add(bo); } }); } }); userData.put(groupEntity.getId(), userBOS); } }); } groupUserCityBO.setUserData(userData); cityData.add(groupUserCityBO); }); provinceBO.setCityData(cityData); return provinceBO; } /** * 后台管理-新增 */ @Transactional(rollbackFor = Exception.class) public Long adminInsert(AdminSysUserGroupRelationDTO dto) { log.info("[后台管理-数据表注释-新增] >> DTO={}", dto); //先判断有没有同个人有没有重复 long count = sysUserGroupRelationMapper.selectCount( new LambdaQueryWrapper<>(SysUserGroupRelationEntity.class) .select( SysUserGroupRelationEntity::getId ) .eq(SysUserGroupRelationEntity::getProvinceId, dto.getProvinceId()) .eq(SysUserGroupRelationEntity::getCityId, dto.getCityId()) .eq(SysUserGroupRelationEntity::getGroupId, dto.getGroupId()) .eq(SysUserGroupRelationEntity::getUserId, dto.getUserId()) ); if (count > 0) { throw new BusinessException(400, "请勿重复设置"); } SysUserGroupRelationEntity entity = new SysUserGroupRelationEntity(); BeanUtil.copyProperties(dto, entity); sysUserGroupRelationMapper.insert(entity); return entity.getId(); } /** * 后台管理-删除 */ @Transactional(rollbackFor = Exception.class) public void adminDelete(Long id) { log.info("[后台管理-数据表注释-删除] >> id={}", id); sysUserGroupRelationMapper.deleteById(id); } public void commonQueryData(List groupEntityList, List groupRelationEntityList, List sysUserEntityList, List provinceRegionEntityList, List cityRegionEntityList) { List groupEntityList1 = sysGroupMapper.selectList( new LambdaQueryWrapper<>(SysGroupEntity.class) .select( SysGroupEntity::getId, SysGroupEntity::getSortCode, SysGroupEntity::getTitle ) .orderByAsc(SysGroupEntity::getSortCode) ); if (CollUtil.isNotEmpty(groupEntityList1)) { groupEntityList.addAll(groupEntityList1); } //查询用户和分组数据的关系数据 List groupRelationEntityList1 = sysUserGroupRelationMapper.selectList( new LambdaQueryWrapper<>(SysUserGroupRelationEntity.class) .select( SysUserGroupRelationEntity::getId, SysUserGroupRelationEntity::getCityId, SysUserGroupRelationEntity::getProvinceId, SysUserGroupRelationEntity::getGroupId, SysUserGroupRelationEntity::getUserId ) .orderByAsc(SysUserGroupRelationEntity::getCreatedAt) ); if (CollUtil.isNotEmpty(groupRelationEntityList1)) { groupRelationEntityList.addAll(groupRelationEntityList1); List sysUserEntities = sysUserMapper.selectList( new LambdaQueryWrapper<>(SysUserEntity.class) .eq(SysUserEntity::getStatus, 1) .in(SysUserEntity::getId, groupRelationEntityList1.stream() .map(SysUserGroupRelationEntity::getUserId).distinct().toList()) ); if (CollUtil.isNotEmpty(sysUserEntities)) { sysUserEntityList.addAll(sysUserEntities); } } List regionEntityList = dictProvinceRegionMapper.selectList( new LambdaQueryWrapper<>(DictProvinceRegionEntity.class) .select( DictProvinceRegionEntity::getId, DictProvinceRegionEntity::getSortCode, DictProvinceRegionEntity::getName, DictProvinceRegionEntity::getCode ) .eq(DictProvinceRegionEntity::getUseStatus, 1) .orderByAsc(DictProvinceRegionEntity::getSortCode) ); if (CollUtil.isNotEmpty(regionEntityList)) { provinceRegionEntityList.addAll(regionEntityList); List provinceCode = regionEntityList.stream() .map(DictProvinceRegionEntity::getCode) .distinct() .toList(); List dictCityRegionEntities = dictCityRegionMapper.selectList( new LambdaQueryWrapper<>(DictCityRegionEntity.class) .select( DictCityRegionEntity::getId, DictCityRegionEntity::getCode, DictCityRegionEntity::getName, DictCityRegionEntity::getParentCode ) .in(DictCityRegionEntity::getParentCode, provinceCode) ); if (CollUtil.isNotEmpty(dictCityRegionEntities)) { cityRegionEntityList.addAll(dictCityRegionEntities); } } } }