package com.ruoyi.system.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.constant.CacheConstants; import com.ruoyi.common.constant.ResultConstants; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.entity.SysDictData; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.exception.BusinessException; import com.ruoyi.common.utils.DictUtils; import com.ruoyi.common.utils.spring.SpringUtils; import com.ruoyi.system.domain.resp.DistrictTreeRespDTO; import com.ruoyi.system.mapper.SysDistrictMapper; import com.ruoyi.system.domain.SysDistrict; import com.ruoyi.system.service.SysDistrictService; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.PostConstruct; import java.util.*; import java.util.stream.Collectors; import static com.ruoyi.common.utils.DictUtils.clearDictCache; /** * @author hz * @since 2023-09-07 13:13:58 */ @Service("sysDistrictService") public class SysDistrictServiceImpl extends ServiceImpl implements SysDistrictService { @PostConstruct public void init() { loadingDistrictCache(); } @Override public List selectAll() { if (SpringUtils.getBean(RedisCache.class).hasKey(CacheConstants.SYS_DISTRICT_LIST_KEY)){ List sysDistricts = SpringUtils.getBean(RedisCache.class).getCacheObject(CacheConstants.SYS_DISTRICT_LIST_KEY); return sysDistricts; } List list = this.list(); SpringUtils.getBean(RedisCache.class).setCacheObject(CacheConstants.SYS_DISTRICT_LIST_KEY, list); return list; } @Override public void loadingDistrictCache() { List list = this.list(); if (!CollectionUtils.isEmpty(list)) { SpringUtils.getBean(RedisCache.class).setCacheObject(CacheConstants.SYS_DISTRICT_LIST_KEY, list); } List treeRespDTOS = this.selectAllByTree(); if (!CollectionUtils.isEmpty(treeRespDTOS)) { SpringUtils.getBean(RedisCache.class).setCacheObject(CacheConstants.SYS_DISTRICT_TREE_KEY, list); } } @Override public void clearDistrictCache() { if (SpringUtils.getBean(RedisCache.class).hasKey(CacheConstants.SYS_DISTRICT_LIST_KEY)){ SpringUtils.getBean(RedisCache.class).deleteObject(CacheConstants.SYS_DISTRICT_LIST_KEY); } if (SpringUtils.getBean(RedisCache.class).hasKey(CacheConstants.SYS_DISTRICT_TREE_KEY)){ SpringUtils.getBean(RedisCache.class).deleteObject(CacheConstants.SYS_DISTRICT_TREE_KEY); } } /** * 重置区划缓存数据 */ @Override public void resetDistrictCache() { clearDistrictCache(); loadingDistrictCache(); } @Override public List selectAllByTree() { if (SpringUtils.getBean(RedisCache.class).hasKey(CacheConstants.SYS_DISTRICT_TREE_KEY)){ List sysDistrictTree = SpringUtils.getBean(RedisCache.class).getCacheObject(CacheConstants.SYS_DISTRICT_TREE_KEY); return sysDistrictTree; } List districts = this.selectAll(); if (CollectionUtils.isEmpty(districts)){ throw new BusinessException(this.getClass(), ResultConstants.SYSTEM_ERROR_DATABASE_FAIL,"区划信息获取失败"); } //获取父节点,0表示父节点,让此次递归有个开头 List collect = districts.stream() //父节点的id = 0,根据它开始进行实现。 .filter(e -> e.getParentcode().equals("0")) .map(e ->{ DistrictTreeRespDTO dto = new DistrictTreeRespDTO(); BeanUtils.copyProperties(e,dto); //将过滤之后的数据来进行方法调用,拿到他的所有的子节点信息并进行封装 dto.setChildren(getchildrens(dto, districts)); return dto; }).collect(Collectors.toList()); SpringUtils.getBean(RedisCache.class).setCacheObject(CacheConstants.SYS_DISTRICT_TREE_KEY, collect); return collect; } private List getchildrens(DistrictTreeRespDTO root,List districts){ //root为每次最新的传递过来的数据,也就是上面过滤之后的 e ; List collect = districts.stream() //根据传递过来的 e ,拿到他的id,来查询出他的子节点id 这里有个特点 e.id = 子节点的父节点id .filter(e -> Objects.equals(e.getParentcode(), root.getCode())) .map(e -> { DistrictTreeRespDTO dto = new DistrictTreeRespDTO(); BeanUtils.copyProperties(e,dto); dto.setChildren(getchildrens(dto, districts)); //递归找到他的子节点,直到找到最后一个子节点为止,饼进行封装。 return dto; }).collect(Collectors.toList()); return collect; } }