|
|
@@ -1,13 +1,21 @@
|
|
|
package com.zhongshu.card.server.core.service.org;
|
|
|
|
|
|
-import com.github.microservice.auth.client.content.ResultContent;
|
|
|
import com.zhongshu.card.client.model.org.RegionModel;
|
|
|
+import com.zhongshu.card.client.ret.ResultContent;
|
|
|
+import com.zhongshu.card.client.utils.ITree;
|
|
|
+import com.zhongshu.card.client.utils.TreeUtil;
|
|
|
import com.zhongshu.card.server.core.dao.org.RegionDao;
|
|
|
+import com.zhongshu.card.server.core.domain.org.Region;
|
|
|
+import com.zhongshu.card.server.core.util.BeanUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
@@ -16,9 +24,54 @@ public class RegionService {
|
|
|
@Autowired
|
|
|
RegionDao regionDao;
|
|
|
|
|
|
- public ResultContent<List<RegionModel>> getRegion(RegionModel regionModel){
|
|
|
+ public ResultContent<List<RegionModel>> getRegion(RegionModel regionModel) {
|
|
|
List<RegionModel> byCodeStartingWithAndTypeAndName = regionDao.findByCodeStartingWithAndTypeAndName(regionModel);
|
|
|
return ResultContent.buildContent(byCodeStartingWithAndTypeAndName);
|
|
|
}
|
|
|
|
|
|
+ public ResultContent<List<ITree>> getAllTree() {
|
|
|
+ fixData();
|
|
|
+ List<Region> list = regionDao.findAll(Sort.by(Sort.Order.asc("index")));
|
|
|
+ List<ITree> models = list.parallelStream().map(this::toModel).collect(Collectors.toList());
|
|
|
+ List<ITree> iTrees = TreeUtil.buildTree(models, ITree.ROOT_ID);
|
|
|
+ return ResultContent.buildSuccess(iTrees);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void fixData() {
|
|
|
+ Region region = regionDao.findByCode("11");
|
|
|
+ if (region != null) {
|
|
|
+ log.info("修复数据");
|
|
|
+ if (StringUtils.isEmpty(region.getParentId())) {
|
|
|
+ List<Region> list = regionDao.findAll();
|
|
|
+ list.stream().forEach(it -> {
|
|
|
+ String code = it.getCode();
|
|
|
+ String parentId = ITree.ROOT_ID;
|
|
|
+ if (code != null && code.length() > 2) {
|
|
|
+ parentId = code.substring(0, code.length() - 2);
|
|
|
+ }
|
|
|
+ it.setIndex(Long.parseLong(code));
|
|
|
+ it.setParentId(parentId);
|
|
|
+ });
|
|
|
+ regionDao.saveAll(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (region.getIndex() == null || region.getIndex() == 0) {
|
|
|
+ List<Region> list = regionDao.findAll();
|
|
|
+ list.stream().forEach(it -> {
|
|
|
+ String code = it.getCode();
|
|
|
+ it.setIndex(Long.parseLong(code));
|
|
|
+ });
|
|
|
+ regionDao.saveAll(list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public RegionModel toModel(Region entity) {
|
|
|
+ RegionModel model = new RegionModel();
|
|
|
+ if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
+ BeanUtils.copyProperties(entity, model);
|
|
|
+ model.setId(entity.getCode());
|
|
|
+ }
|
|
|
+ return model;
|
|
|
+ }
|
|
|
}
|