SysUserGroupRelationService.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. package com.jtzx.crm.module.sys.service;
  2. import cc.uncarbon.framework.core.exception.BusinessException;
  3. import cc.uncarbon.framework.core.page.PageResult;
  4. import cc.uncarbon.framework.crud.entity.HelioBaseEntity;
  5. import cn.hutool.core.bean.BeanUtil;
  6. import cn.hutool.core.collection.CollUtil;
  7. import cn.hutool.core.util.StrUtil;
  8. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  9. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  10. import com.google.common.collect.Lists;
  11. import com.google.common.collect.Maps;
  12. import com.jtzx.crm.module.sys.entity.*;
  13. import com.jtzx.crm.module.sys.mapper.*;
  14. import com.jtzx.crm.module.sys.model.request.AdminSysUserGroupRelationDTO;
  15. import com.jtzx.crm.module.sys.model.response.*;
  16. import lombok.RequiredArgsConstructor;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import java.util.Comparator;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * 数据表注释
  26. */
  27. @RequiredArgsConstructor
  28. @Service
  29. @Slf4j
  30. public class SysUserGroupRelationService {
  31. private final SysGroupMapper sysGroupMapper;
  32. private final SysUserGroupRelationMapper sysUserGroupRelationMapper;
  33. private final DictProvinceRegionMapper dictProvinceRegionMapper;
  34. private final DictCityRegionMapper dictCityRegionMapper;
  35. private final SysUserMapper sysUserMapper;
  36. /**
  37. * 查询所有用户(左侧列表数据)
  38. * @return
  39. */
  40. public List<SysUserListBO> queryAllSysUserList(String username) {
  41. List<SysUserListBO> sysUserListBOList = Lists.newArrayList();
  42. List<SysUserEntity> sysUserEntities =
  43. sysUserMapper.selectList(
  44. new LambdaQueryWrapper<>(SysUserEntity.class)
  45. .eq(StrUtil.isNotEmpty(username), SysUserEntity::getUserName, username)
  46. .eq(SysUserEntity::getStatus, 1)
  47. .orderByAsc(HelioBaseEntity::getCreatedAt)
  48. );
  49. if (CollUtil.isNotEmpty(sysUserEntities)) {
  50. sysUserEntities.forEach(entity -> {
  51. SysUserListBO bo = SysUserListBO.builder().build();
  52. bo.setId(entity.getId());
  53. bo.setUsername(entity.getUserName());
  54. bo.setAccount(entity.getPin());
  55. sysUserListBOList.add(bo);
  56. });
  57. }
  58. return sysUserListBOList;
  59. }
  60. /**
  61. * 分页查询表格数据
  62. * @param dto
  63. * @return
  64. */
  65. public PageResult<SysUserGroupRelationPageListBO> queryListByPage(AdminSysUserGroupRelationDTO dto) {
  66. //查询分组
  67. List<SysGroupEntity> groupEntityList =
  68. sysGroupMapper.selectList(
  69. new LambdaQueryWrapper<>(SysGroupEntity.class)
  70. .select(
  71. SysGroupEntity::getSortCode,
  72. SysGroupEntity::getId,
  73. SysGroupEntity::getTitle
  74. )
  75. .eq(dto.getGroupId() != null, SysGroupEntity::getId, dto.getGroupId())
  76. .orderByAsc(SysGroupEntity::getSortCode)
  77. );
  78. if (CollUtil.isEmpty(groupEntityList)) {
  79. return new PageResult<SysUserGroupRelationPageListBO>()
  80. .setCurrent(1)
  81. .setSize(dto.getPageSize())
  82. .setTotal(0)
  83. .setRecords(Lists.newArrayList());
  84. }
  85. //查询已开通使用的省份数据
  86. List<DictProvinceRegionEntity> provinceRegionEntityList = dictProvinceRegionMapper.selectList(
  87. new LambdaQueryWrapper<>(DictProvinceRegionEntity.class)
  88. .select(
  89. DictProvinceRegionEntity::getId,
  90. DictProvinceRegionEntity::getCode,
  91. DictProvinceRegionEntity::getName,
  92. DictProvinceRegionEntity::getSortCode
  93. )
  94. .eq(dto.getProvinceId() != null, DictProvinceRegionEntity::getId, dto.getProvinceId())
  95. .eq(DictProvinceRegionEntity::getUseStatus, 1)
  96. );
  97. if (CollUtil.isEmpty(provinceRegionEntityList)) {
  98. return new PageResult<SysUserGroupRelationPageListBO>()
  99. .setCurrent(1)
  100. .setSize(dto.getPageSize())
  101. .setTotal(0)
  102. .setRecords(Lists.newArrayList());
  103. }
  104. //查询城市数据
  105. List<DictCityRegionEntity> cityRegionEntityList = dictCityRegionMapper.selectList(
  106. new LambdaQueryWrapper<>(DictCityRegionEntity.class)
  107. .select(
  108. DictCityRegionEntity::getParentCode,
  109. DictCityRegionEntity::getId,
  110. DictCityRegionEntity::getName
  111. )
  112. .eq(dto.getCityId() != null, DictCityRegionEntity::getId, dto.getCityId())
  113. .in(DictCityRegionEntity::getParentCode, provinceRegionEntityList.stream().map(DictProvinceRegionEntity::getCode).toList())
  114. );
  115. if (CollUtil.isEmpty(cityRegionEntityList)) {
  116. return new PageResult<SysUserGroupRelationPageListBO>()
  117. .setCurrent(1)
  118. .setSize(dto.getPageSize())
  119. .setTotal(0)
  120. .setRecords(Lists.newArrayList());
  121. }
  122. //查询用户数据
  123. List<SysUserEntity> sysUserEntityList =
  124. sysUserMapper.selectList(
  125. new LambdaQueryWrapper<>(SysUserEntity.class)
  126. .select(
  127. SysUserEntity::getUserName,
  128. SysUserEntity::getId,
  129. SysUserEntity::getPin
  130. )
  131. .eq(SysUserEntity::getStatus, 1)
  132. .eq(dto.getUserId() != null, SysUserEntity::getId, dto.getUserId())
  133. .and(StrUtil.isNotEmpty(dto.getQueryStr()), z -> z.like(SysUserEntity::getUserName, dto.getQueryStr())
  134. .or().like(SysUserEntity::getPin, dto.getQueryStr())
  135. )
  136. );
  137. if (CollUtil.isEmpty(sysUserEntityList)) {
  138. return new PageResult<SysUserGroupRelationPageListBO>()
  139. .setCurrent(1)
  140. .setSize(dto.getPageSize())
  141. .setTotal(0)
  142. .setRecords(Lists.newArrayList());
  143. }
  144. List<Long> userIds = sysUserEntityList.stream()
  145. .sorted(Comparator.comparing(SysUserEntity::getId))
  146. .map(SysUserEntity::getId)
  147. .toList();
  148. Page<SysUserGroupRelationEntity> pageData = sysUserGroupRelationMapper.selectPage(
  149. new Page<>(dto.getPageNum(), dto.getPageSize()),
  150. new LambdaQueryWrapper<>(SysUserGroupRelationEntity.class)
  151. .eq(dto.getProvinceId() != null, SysUserGroupRelationEntity::getProvinceId, dto.getProvinceId())
  152. .eq(dto.getCityId() != null, SysUserGroupRelationEntity::getCityId, dto.getCityId())
  153. .eq(dto.getGroupId() != null, SysUserGroupRelationEntity::getGroupId, dto.getGroupId())
  154. .eq(dto.getUserId() != null && StrUtil.isEmpty(dto.getQueryStr()), SysUserGroupRelationEntity::getUserId, dto.getUserId())
  155. .in(StrUtil.isNotEmpty(dto.getQueryStr()), SysUserGroupRelationEntity::getUserId, userIds)
  156. .orderByDesc(SysUserGroupRelationEntity::getCreatedAt)
  157. );
  158. if (CollUtil.isEmpty(pageData.getRecords())) {
  159. return new PageResult<SysUserGroupRelationPageListBO>()
  160. .setCurrent(1)
  161. .setSize(dto.getPageSize())
  162. .setTotal(0)
  163. .setRecords(Lists.newArrayList());
  164. }
  165. List<SysUserGroupRelationPageListBO> result = Lists.newArrayList();
  166. PageResult<SysUserGroupRelationPageListBO> pageResult = new PageResult<SysUserGroupRelationPageListBO>()
  167. .setCurrent(pageData.getCurrent())
  168. .setSize(pageData.getSize())
  169. .setTotal(pageData.getTotal());
  170. pageData.getRecords().forEach(entity -> {
  171. SysUserGroupRelationPageListBO bo = SysUserGroupRelationPageListBO.builder().build();
  172. bo.setId(entity.getId());
  173. groupEntityList.stream()
  174. .filter(x -> x.getId().equals(entity.getGroupId()))
  175. .findFirst()
  176. .ifPresent(x -> {
  177. bo.setGroupId(x.getId());
  178. bo.setGroupName(x.getTitle());
  179. });
  180. sysUserEntityList.stream()
  181. .filter(x -> x.getId().equals(entity.getUserId()))
  182. .findFirst()
  183. .ifPresent(x -> {
  184. bo.setUserId(x.getId());
  185. bo.setUserName(x.getUserName());
  186. bo.setAccount(x.getPin());
  187. });
  188. provinceRegionEntityList.stream()
  189. .filter(x -> x.getId().equals(entity.getProvinceId()))
  190. .findFirst()
  191. .ifPresent(x -> {
  192. bo.setProvinceId(x.getId());
  193. bo.setProvinceName(x.getName());
  194. });
  195. cityRegionEntityList.stream()
  196. .filter(x -> x.getId().equals(entity.getCityId()))
  197. .findFirst()
  198. .ifPresent(x -> {
  199. bo.setCityId(x.getId());
  200. bo.setCityName(x.getName());
  201. });
  202. bo.setDelStatus(0);
  203. result.add(bo);
  204. });
  205. pageResult.setRecords(result);
  206. return pageResult;
  207. }
  208. /**
  209. * 分组管理,查询表格数据(第一种表格形式的数据)
  210. * @return
  211. */
  212. public List<GroupUserProvinceBO> queryTableData() {
  213. List<GroupUserProvinceBO> result = Lists.newArrayList();
  214. //查询分组
  215. List<SysGroupEntity> groupEntityList = Lists.newArrayList();
  216. //查询用户和分组数据的关系数据
  217. List<SysUserGroupRelationEntity> groupRelationEntityList = Lists.newArrayList();
  218. //查询用户数据
  219. List<SysUserEntity> sysUserEntityList = Lists.newArrayList();
  220. //查询已开通使用的省份数据
  221. List<DictProvinceRegionEntity> provinceRegionEntityList = Lists.newArrayList();
  222. //查询城市数据
  223. List<DictCityRegionEntity> cityRegionEntityList = Lists.newArrayList();
  224. //公共查询数据
  225. this.commonQueryData(groupEntityList, groupRelationEntityList, sysUserEntityList, provinceRegionEntityList, cityRegionEntityList);
  226. //按省份处理
  227. if (CollUtil.isNotEmpty(provinceRegionEntityList)) {
  228. //循环省份
  229. provinceRegionEntityList.forEach(provinceRegionEntity -> {
  230. //过滤省份下的城市
  231. List<DictCityRegionEntity> cityRegionEntities =
  232. cityRegionEntityList.stream()
  233. .filter(x -> x.getParentCode().equals(provinceRegionEntity.getCode()))
  234. .sorted(Comparator.comparing(DictCityRegionEntity::getCode)).toList();
  235. List<SysUserGroupRelationEntity> userGroupRelationEntityList = Lists.newArrayList();
  236. //过滤省份下的用户关系
  237. if (CollUtil.isNotEmpty(groupRelationEntityList)) {
  238. userGroupRelationEntityList.addAll(groupRelationEntityList.stream()
  239. .filter(x -> x.getProvinceId().equals(provinceRegionEntity.getId()))
  240. .toList());
  241. }
  242. //过滤每个省份下的用户数据信息
  243. List<SysUserEntity> sysUserEntities = Lists.newArrayList();
  244. if (CollUtil.isNotEmpty(userGroupRelationEntityList) && CollUtil.isNotEmpty(sysUserEntityList)) {
  245. List<Long> userIds =
  246. userGroupRelationEntityList.stream()
  247. .map(SysUserGroupRelationEntity::getUserId)
  248. .distinct().toList();
  249. sysUserEntities.addAll(sysUserEntityList.stream()
  250. .filter(x -> userIds.contains(x.getId()))
  251. .toList());
  252. }
  253. //处理返回数据
  254. result.add(
  255. this.handleTableData(provinceRegionEntity, cityRegionEntities,
  256. groupEntityList, userGroupRelationEntityList, sysUserEntities));
  257. });
  258. }
  259. return result;
  260. }
  261. public GroupUserProvinceBO handleTableData(DictProvinceRegionEntity dictProvinceRegionEntity,
  262. List<DictCityRegionEntity> cityRegionEntities,
  263. List<SysGroupEntity> groupEntityList,
  264. List<SysUserGroupRelationEntity> userGroupRelationEntityList,
  265. List<SysUserEntity> sysUserEntities) {
  266. GroupUserProvinceBO provinceBO = GroupUserProvinceBO.builder().build();
  267. provinceBO.setProvinceId(dictProvinceRegionEntity.getId());
  268. provinceBO.setProvinceName(dictProvinceRegionEntity.getName());
  269. provinceBO.setSortCode(dictProvinceRegionEntity.getSortCode());
  270. List<SysGroupBO> groupData = Lists.newArrayList();
  271. if (CollUtil.isNotEmpty(groupEntityList)) {
  272. groupEntityList.stream()
  273. .sorted(Comparator.comparing(SysGroupEntity::getSortCode))
  274. .forEach(groupEntity -> {
  275. SysGroupBO sysGroupBO = SysGroupBO.builder().build();
  276. BeanUtils.copyProperties(groupEntity, sysGroupBO);
  277. groupData.add(sysGroupBO);
  278. });
  279. }
  280. provinceBO.setGroupData(groupData);
  281. //处理表格数据
  282. List<GroupUserCityBO> cityData = Lists.newArrayList();
  283. cityRegionEntities.stream()
  284. .sorted(Comparator.comparing(DictCityRegionEntity::getCode))
  285. .forEach(cityRegionEntity -> {
  286. GroupUserCityBO groupUserCityBO = GroupUserCityBO.builder().build();
  287. groupUserCityBO.setCityId(cityRegionEntity.getId());
  288. groupUserCityBO.setCityName(cityRegionEntity.getName());
  289. Map<Long, Object> userData = Maps.newHashMap();
  290. if (CollUtil.isNotEmpty(groupEntityList)) {
  291. groupEntityList.stream()
  292. .sorted(Comparator.comparing(SysGroupEntity::getSortCode))
  293. .forEach(groupEntity -> {
  294. //设置默认
  295. userData.put(groupEntity.getId(), "-");
  296. if (CollUtil.isNotEmpty(userGroupRelationEntityList)) {
  297. List<GroupUserBO> userBOS = Lists.newArrayList();
  298. userGroupRelationEntityList.stream()
  299. .filter(x -> x.getCityId().equals(cityRegionEntity.getId()))
  300. .filter(x -> x.getGroupId().equals(groupEntity.getId()))
  301. .sorted(Comparator.comparing(HelioBaseEntity::getCreatedAt))
  302. .forEach(groupRelationEntity -> {
  303. if (CollUtil.isNotEmpty(sysUserEntities)) {
  304. sysUserEntities.stream()
  305. .filter(x -> x.getId().equals(groupRelationEntity.getUserId()))
  306. .findFirst()
  307. .ifPresent(d -> {
  308. if (CollUtil.isEmpty(userBOS) || userBOS.stream().map(GroupUserBO::getUserId).noneMatch(groupRelationEntity.getUserId()::equals)) {
  309. GroupUserBO bo = GroupUserBO.builder().build();
  310. bo.setId(groupRelationEntity.getId());
  311. bo.setUserId(groupRelationEntity.getUserId());
  312. bo.setUserName(d.getUserName());
  313. bo.setAccount(d.getPin());
  314. bo.setGroupId(groupEntity.getId());
  315. bo.setProvinceId(dictProvinceRegionEntity.getId());
  316. bo.setCityId(cityRegionEntity.getId());
  317. userBOS.add(bo);
  318. }
  319. });
  320. }
  321. });
  322. userData.put(groupEntity.getId(), userBOS);
  323. }
  324. });
  325. }
  326. groupUserCityBO.setUserData(userData);
  327. cityData.add(groupUserCityBO);
  328. });
  329. provinceBO.setCityData(cityData);
  330. return provinceBO;
  331. }
  332. /**
  333. * 后台管理-新增
  334. */
  335. @Transactional(rollbackFor = Exception.class)
  336. public Long adminInsert(AdminSysUserGroupRelationDTO dto) {
  337. log.info("[后台管理-数据表注释-新增] >> DTO={}", dto);
  338. //先判断有没有同个人有没有重复
  339. long count =
  340. sysUserGroupRelationMapper.selectCount(
  341. new LambdaQueryWrapper<>(SysUserGroupRelationEntity.class)
  342. .select(
  343. SysUserGroupRelationEntity::getId
  344. )
  345. .eq(SysUserGroupRelationEntity::getProvinceId, dto.getProvinceId())
  346. .eq(SysUserGroupRelationEntity::getCityId, dto.getCityId())
  347. .eq(SysUserGroupRelationEntity::getGroupId, dto.getGroupId())
  348. .eq(SysUserGroupRelationEntity::getUserId, dto.getUserId())
  349. );
  350. if (count > 0) {
  351. throw new BusinessException(400, "请勿重复设置");
  352. }
  353. SysUserGroupRelationEntity entity = new SysUserGroupRelationEntity();
  354. BeanUtil.copyProperties(dto, entity);
  355. sysUserGroupRelationMapper.insert(entity);
  356. return entity.getId();
  357. }
  358. /**
  359. * 后台管理-删除
  360. */
  361. @Transactional(rollbackFor = Exception.class)
  362. public void adminDelete(Long id) {
  363. log.info("[后台管理-数据表注释-删除] >> id={}", id);
  364. sysUserGroupRelationMapper.deleteById(id);
  365. }
  366. public void commonQueryData(List<SysGroupEntity> groupEntityList,
  367. List<SysUserGroupRelationEntity> groupRelationEntityList,
  368. List<SysUserEntity> sysUserEntityList,
  369. List<DictProvinceRegionEntity> provinceRegionEntityList,
  370. List<DictCityRegionEntity> cityRegionEntityList) {
  371. List<SysGroupEntity> groupEntityList1 =
  372. sysGroupMapper.selectList(
  373. new LambdaQueryWrapper<>(SysGroupEntity.class)
  374. .select(
  375. SysGroupEntity::getId,
  376. SysGroupEntity::getSortCode,
  377. SysGroupEntity::getTitle
  378. )
  379. .orderByAsc(SysGroupEntity::getSortCode)
  380. );
  381. if (CollUtil.isNotEmpty(groupEntityList1)) {
  382. groupEntityList.addAll(groupEntityList1);
  383. }
  384. //查询用户和分组数据的关系数据
  385. List<SysUserGroupRelationEntity> groupRelationEntityList1 = sysUserGroupRelationMapper.selectList(
  386. new LambdaQueryWrapper<>(SysUserGroupRelationEntity.class)
  387. .select(
  388. SysUserGroupRelationEntity::getId,
  389. SysUserGroupRelationEntity::getCityId,
  390. SysUserGroupRelationEntity::getProvinceId,
  391. SysUserGroupRelationEntity::getGroupId,
  392. SysUserGroupRelationEntity::getUserId
  393. )
  394. .orderByAsc(SysUserGroupRelationEntity::getCreatedAt)
  395. );
  396. if (CollUtil.isNotEmpty(groupRelationEntityList1)) {
  397. groupRelationEntityList.addAll(groupRelationEntityList1);
  398. List<SysUserEntity> sysUserEntities = sysUserMapper.selectList(
  399. new LambdaQueryWrapper<>(SysUserEntity.class)
  400. .eq(SysUserEntity::getStatus, 1)
  401. .in(SysUserEntity::getId, groupRelationEntityList1.stream()
  402. .map(SysUserGroupRelationEntity::getUserId).distinct().toList())
  403. );
  404. if (CollUtil.isNotEmpty(sysUserEntities)) {
  405. sysUserEntityList.addAll(sysUserEntities);
  406. }
  407. }
  408. List<DictProvinceRegionEntity> regionEntityList = dictProvinceRegionMapper.selectList(
  409. new LambdaQueryWrapper<>(DictProvinceRegionEntity.class)
  410. .select(
  411. DictProvinceRegionEntity::getId,
  412. DictProvinceRegionEntity::getSortCode,
  413. DictProvinceRegionEntity::getName,
  414. DictProvinceRegionEntity::getCode
  415. )
  416. .eq(DictProvinceRegionEntity::getUseStatus, 1)
  417. .orderByAsc(DictProvinceRegionEntity::getSortCode)
  418. );
  419. if (CollUtil.isNotEmpty(regionEntityList)) {
  420. provinceRegionEntityList.addAll(regionEntityList);
  421. List<String> provinceCode =
  422. regionEntityList.stream()
  423. .map(DictProvinceRegionEntity::getCode)
  424. .distinct()
  425. .toList();
  426. List<DictCityRegionEntity> dictCityRegionEntities = dictCityRegionMapper.selectList(
  427. new LambdaQueryWrapper<>(DictCityRegionEntity.class)
  428. .select(
  429. DictCityRegionEntity::getId,
  430. DictCityRegionEntity::getCode,
  431. DictCityRegionEntity::getName,
  432. DictCityRegionEntity::getParentCode
  433. )
  434. .in(DictCityRegionEntity::getParentCode, provinceCode)
  435. );
  436. if (CollUtil.isNotEmpty(dictCityRegionEntities)) {
  437. cityRegionEntityList.addAll(dictCityRegionEntities);
  438. }
  439. }
  440. }
  441. }