|
@@ -1,13 +1,21 @@
|
|
|
package com.citu.module.menduner.system.service.industry;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import com.citu.framework.common.pojo.PageResult;
|
|
|
import com.citu.framework.common.util.object.BeanUtils;
|
|
|
+
|
|
|
+import com.citu.module.menduner.system.controller.admin.industry.vo.IndustryChildrenRespVO;
|
|
|
import com.citu.module.menduner.system.controller.admin.industry.vo.IndustryListReqVO;
|
|
|
import com.citu.module.menduner.system.controller.admin.industry.vo.IndustryPageReqVO;
|
|
|
import com.citu.module.menduner.system.controller.admin.industry.vo.IndustrySaveReqVO;
|
|
|
+import com.citu.module.menduner.system.convert.IndustryConvert;
|
|
|
+
|
|
|
import com.citu.module.menduner.system.dal.dataobject.industry.IndustryDO;
|
|
|
import com.citu.module.menduner.system.dal.mysql.industry.IndustryMapper;
|
|
|
+import com.citu.module.menduner.system.dal.redis.RedisKeyConstants;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.cache.annotation.CacheEvict;
|
|
|
+import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
@@ -15,10 +23,11 @@ import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static com.citu.framework.common.util.collection.CollectionUtils.convertSet;
|
|
|
import static com.citu.module.menduner.system.enums.ErrorCodeConstants.MDE_INDUSTRY_NOT_EXISTS;
|
|
|
|
|
|
|
|
@@ -40,6 +49,8 @@ public class IndustryServiceImpl implements IndustryService {
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
@Override
|
|
|
+ @CacheEvict(cacheNames = RedisKeyConstants.MDE_INDUSTRY_CHILDREN_id_LIST,
|
|
|
+ allEntries = true)
|
|
|
public Long createIndustry(IndustrySaveReqVO createReqVO) {
|
|
|
// 插入
|
|
|
IndustryDO industry = BeanUtils.toBean(createReqVO, IndustryDO.class);
|
|
@@ -49,6 +60,8 @@ public class IndustryServiceImpl implements IndustryService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @CacheEvict(cacheNames = RedisKeyConstants.MDE_INDUSTRY_CHILDREN_id_LIST,
|
|
|
+ allEntries = true)
|
|
|
public void updateIndustry(IndustrySaveReqVO updateReqVO) {
|
|
|
// 校验存在
|
|
|
validateIndustryExists(updateReqVO.getId());
|
|
@@ -58,6 +71,8 @@ public class IndustryServiceImpl implements IndustryService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @CacheEvict(cacheNames = RedisKeyConstants.MDE_INDUSTRY_CHILDREN_id_LIST,
|
|
|
+ allEntries = true)
|
|
|
public void deleteIndustry(Long id) {
|
|
|
// 校验存在
|
|
|
validateIndustryExists(id);
|
|
@@ -88,6 +103,8 @@ public class IndustryServiceImpl implements IndustryService {
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
+ @CacheEvict(cacheNames = RedisKeyConstants.MDE_INDUSTRY_CHILDREN_id_LIST,
|
|
|
+ allEntries = true)
|
|
|
public void syncBossIndustry() {
|
|
|
log.info(" ========== 开始解析同步BOSS行业信息到数据库 ========== ");
|
|
|
String url = "https://www.zhipin.com/wapi/zpCommon/data/industry.json";
|
|
@@ -136,4 +153,59 @@ public class IndustryServiceImpl implements IndustryService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<IndustryChildrenRespVO> getIndustry(IndustryListReqVO reqVO) {
|
|
|
+ List<IndustryDO> areaList = industryMapper.selectList(reqVO);
|
|
|
+ if (null == areaList) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<IndustryChildrenRespVO> convertList = IndustryConvert.INSTANCE.convertList(areaList);
|
|
|
+ //获取父节点
|
|
|
+ List<IndustryChildrenRespVO> collect = convertList.stream().filter(m -> m.getParentId() == 0).map(
|
|
|
+ (m) -> {
|
|
|
+ m.setChildren(getChildren(m, convertList));
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ ).collect(Collectors.toList());
|
|
|
+ return collect;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Cacheable(cacheNames = RedisKeyConstants.MDE_INDUSTRY_CHILDREN_id_LIST, key = "#id", unless = "#result.size==0")
|
|
|
+ public List<IndustryChildrenRespVO> getIndustryIdChildren(Long id) {
|
|
|
+ List<IndustryChildrenRespVO> children = new LinkedList<>();
|
|
|
+ // 遍历每一层
|
|
|
+ Collection<Long> parentIds = Collections.singleton(id);
|
|
|
+ for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环
|
|
|
+ // 查询当前层,所有的子节点
|
|
|
+ List<IndustryDO> areaDOList = industryMapper.selectListByParentId(parentIds);
|
|
|
+ // 1. 如果没有子节点,则结束遍历
|
|
|
+ if (CollUtil.isEmpty(areaDOList)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ // 2. 如果有子节点,继续遍历
|
|
|
+ children.addAll(IndustryConvert.INSTANCE.convertList(areaDOList));
|
|
|
+ parentIds = convertSet(areaDOList, IndustryDO::getId);
|
|
|
+ }
|
|
|
+ return children;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归查询子节点
|
|
|
+ * @param root 根节点
|
|
|
+ * @param all 所有节点
|
|
|
+ * @return 根节点信息
|
|
|
+ */
|
|
|
+ private List<IndustryChildrenRespVO> getChildren(IndustryChildrenRespVO root, List<IndustryChildrenRespVO> all) {
|
|
|
+ List<IndustryChildrenRespVO> children = all.stream().filter(m -> {
|
|
|
+ return Objects.equals(m.getParentId(), root.getId());
|
|
|
+ }).map(
|
|
|
+ (m) -> {
|
|
|
+ m.setChildren(getChildren(m, all));
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ ).collect(Collectors.toList());
|
|
|
+ return children;
|
|
|
+ }
|
|
|
}
|