| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- 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<SysUserListBO> queryAllSysUserList(String username) {
- List<SysUserListBO> sysUserListBOList = Lists.newArrayList();
- List<SysUserEntity> 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<SysUserGroupRelationPageListBO> queryListByPage(AdminSysUserGroupRelationDTO dto) {
- //查询分组
- List<SysGroupEntity> 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<SysUserGroupRelationPageListBO>()
- .setCurrent(1)
- .setSize(dto.getPageSize())
- .setTotal(0)
- .setRecords(Lists.newArrayList());
- }
- //查询已开通使用的省份数据
- List<DictProvinceRegionEntity> 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<SysUserGroupRelationPageListBO>()
- .setCurrent(1)
- .setSize(dto.getPageSize())
- .setTotal(0)
- .setRecords(Lists.newArrayList());
- }
- //查询城市数据
- List<DictCityRegionEntity> 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<SysUserGroupRelationPageListBO>()
- .setCurrent(1)
- .setSize(dto.getPageSize())
- .setTotal(0)
- .setRecords(Lists.newArrayList());
- }
- //查询用户数据
- List<SysUserEntity> 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<SysUserGroupRelationPageListBO>()
- .setCurrent(1)
- .setSize(dto.getPageSize())
- .setTotal(0)
- .setRecords(Lists.newArrayList());
- }
- List<Long> userIds = sysUserEntityList.stream()
- .sorted(Comparator.comparing(SysUserEntity::getId))
- .map(SysUserEntity::getId)
- .toList();
- Page<SysUserGroupRelationEntity> 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<SysUserGroupRelationPageListBO>()
- .setCurrent(1)
- .setSize(dto.getPageSize())
- .setTotal(0)
- .setRecords(Lists.newArrayList());
- }
- List<SysUserGroupRelationPageListBO> result = Lists.newArrayList();
- PageResult<SysUserGroupRelationPageListBO> pageResult = new PageResult<SysUserGroupRelationPageListBO>()
- .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<GroupUserProvinceBO> queryTableData() {
- List<GroupUserProvinceBO> result = Lists.newArrayList();
- //查询分组
- List<SysGroupEntity> groupEntityList = Lists.newArrayList();
- //查询用户和分组数据的关系数据
- List<SysUserGroupRelationEntity> groupRelationEntityList = Lists.newArrayList();
- //查询用户数据
- List<SysUserEntity> sysUserEntityList = Lists.newArrayList();
- //查询已开通使用的省份数据
- List<DictProvinceRegionEntity> provinceRegionEntityList = Lists.newArrayList();
- //查询城市数据
- List<DictCityRegionEntity> cityRegionEntityList = Lists.newArrayList();
- //公共查询数据
- this.commonQueryData(groupEntityList, groupRelationEntityList, sysUserEntityList, provinceRegionEntityList, cityRegionEntityList);
- //按省份处理
- if (CollUtil.isNotEmpty(provinceRegionEntityList)) {
- //循环省份
- provinceRegionEntityList.forEach(provinceRegionEntity -> {
- //过滤省份下的城市
- List<DictCityRegionEntity> cityRegionEntities =
- cityRegionEntityList.stream()
- .filter(x -> x.getParentCode().equals(provinceRegionEntity.getCode()))
- .sorted(Comparator.comparing(DictCityRegionEntity::getCode)).toList();
- List<SysUserGroupRelationEntity> userGroupRelationEntityList = Lists.newArrayList();
- //过滤省份下的用户关系
- if (CollUtil.isNotEmpty(groupRelationEntityList)) {
- userGroupRelationEntityList.addAll(groupRelationEntityList.stream()
- .filter(x -> x.getProvinceId().equals(provinceRegionEntity.getId()))
- .toList());
- }
- //过滤每个省份下的用户数据信息
- List<SysUserEntity> sysUserEntities = Lists.newArrayList();
- if (CollUtil.isNotEmpty(userGroupRelationEntityList) && CollUtil.isNotEmpty(sysUserEntityList)) {
- List<Long> 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<DictCityRegionEntity> cityRegionEntities,
- List<SysGroupEntity> groupEntityList,
- List<SysUserGroupRelationEntity> userGroupRelationEntityList,
- List<SysUserEntity> sysUserEntities) {
- GroupUserProvinceBO provinceBO = GroupUserProvinceBO.builder().build();
- provinceBO.setProvinceId(dictProvinceRegionEntity.getId());
- provinceBO.setProvinceName(dictProvinceRegionEntity.getName());
- provinceBO.setSortCode(dictProvinceRegionEntity.getSortCode());
- List<SysGroupBO> 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<GroupUserCityBO> 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<Long, Object> 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<GroupUserBO> 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<SysGroupEntity> groupEntityList,
- List<SysUserGroupRelationEntity> groupRelationEntityList,
- List<SysUserEntity> sysUserEntityList,
- List<DictProvinceRegionEntity> provinceRegionEntityList,
- List<DictCityRegionEntity> cityRegionEntityList) {
- List<SysGroupEntity> 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<SysUserGroupRelationEntity> 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<SysUserEntity> 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<DictProvinceRegionEntity> 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<String> provinceCode =
- regionEntityList.stream()
- .map(DictProvinceRegionEntity::getCode)
- .distinct()
- .toList();
- List<DictCityRegionEntity> 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);
- }
- }
- }
- }
|