|
|
@@ -1,79 +1,121 @@
|
|
|
package com.izouma.awesomeAdmin.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.github.kevinsawicki.http.HttpRequest;
|
|
|
import com.izouma.awesomeAdmin.domain.District;
|
|
|
-import com.izouma.awesomeAdmin.dto.PageQuery;
|
|
|
+import com.izouma.awesomeAdmin.enums.DistrictLevel;
|
|
|
import com.izouma.awesomeAdmin.repo.DistrictRepo;
|
|
|
-import com.izouma.awesomeAdmin.utils.JpaUtils;
|
|
|
-import com.izouma.awesomeAdmin.utils.qqmap.DistrictResponse;
|
|
|
-import com.izouma.awesomeAdmin.utils.qqmap.QQMapUtil;
|
|
|
+import com.izouma.awesomeAdmin.utils.amap.DistrictsItem;
|
|
|
+import com.izouma.awesomeAdmin.utils.amap.QueryDistrictResponse;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
|
public class DistrictService {
|
|
|
|
|
|
- private DistrictRepo districtRepo;
|
|
|
+ private final DistrictRepo districtRepo;
|
|
|
|
|
|
- public Page<District> all(PageQuery pageQuery) {
|
|
|
- return districtRepo.findAll(JpaUtils.toSpecification(pageQuery, District.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
+ public List<District> get(DistrictLevel level, DistrictLevel maxLevel, Long parent) {
|
|
|
+ return districtRepo.findAll((Specification<District>) (root, criteriaQuery, criteriaBuilder) -> {
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
+ if (level != null) {
|
|
|
+ predicates.add(criteriaBuilder.equal(root.get("level"), level));
|
|
|
+ }
|
|
|
+ if (parent != null) {
|
|
|
+ predicates.add(criteriaBuilder.equal(root.get("parent"), parent));
|
|
|
+ }
|
|
|
+ if (maxLevel != null) {
|
|
|
+ List<DistrictLevel> list = Arrays.stream(DistrictLevel.values())
|
|
|
+ .filter(l -> l.getValue() <= maxLevel.getValue())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ predicates.add(criteriaBuilder.equal(root.get("level"), DistrictLevel.NONE));
|
|
|
+ } else {
|
|
|
+ predicates.add(root.get("level").in(list));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
|
|
|
+ }).stream().peek(district -> {
|
|
|
+ if (maxLevel != null) {
|
|
|
+ int childCount = 0;
|
|
|
+ switch (maxLevel) {
|
|
|
+ case PROVINCE:
|
|
|
+ break;
|
|
|
+ case CITY:
|
|
|
+ childCount = district.getCityCount();
|
|
|
+ break;
|
|
|
+ case DISTRICT:
|
|
|
+ childCount = district.getCityCount() + district.getDistrictCount();
|
|
|
+ break;
|
|
|
+ case STREET:
|
|
|
+ childCount = district.getCityCount() + district.getDistrictCount() + district.getStreetCount();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ district.setLeaf(childCount == 0);
|
|
|
+ } else {
|
|
|
+ district.setLeaf(district.getChildCount() == 0);
|
|
|
+ }
|
|
|
+ }).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
public void sync() {
|
|
|
- List<List<DistrictResponse.District>> res = QQMapUtil.queryDistrict().getResult();
|
|
|
- for (int i = 0; i < 3; i++) {
|
|
|
- for (DistrictResponse.District district : res.get(i)) {
|
|
|
- District d = convert(district);
|
|
|
- d.setLevel(i);
|
|
|
- if (i > 0) {
|
|
|
- int finalI = i;
|
|
|
- DistrictResponse.District parent = res.get(i - 1)
|
|
|
- .stream()
|
|
|
- .filter(ii -> String.valueOf(ii.getId())
|
|
|
- .startsWith(String.valueOf(d.getId()).substring(0, finalI == 1 ? 2 : 4)))
|
|
|
- .findFirst()
|
|
|
- .orElse(null);
|
|
|
-
|
|
|
- if (parent == null) {
|
|
|
- parent = res.get(0)
|
|
|
- .stream()
|
|
|
- .filter(ii -> String.valueOf(ii.getId())
|
|
|
- .startsWith(String.valueOf(d.getId()).substring(0, 2)))
|
|
|
- .findFirst()
|
|
|
- .orElse(null);
|
|
|
- District p = convert(parent);
|
|
|
- p.setLevel(1);
|
|
|
- p.setParent(Long.parseLong(String.valueOf(parent.getId()).substring(0, 2) + "0100"));
|
|
|
- districtRepo.save(p);
|
|
|
- d.setParent(p.getId());
|
|
|
- } else {
|
|
|
- d.setParent((long) parent.getId());
|
|
|
- }
|
|
|
+ QueryDistrictResponse response = JSON.parseObject(HttpRequest.get("https://restapi.amap.com/v3/config/district?key=3d59fb422c5c13af59bf82a8b6f3ad54&subdistrict=4")
|
|
|
+ .body(), QueryDistrictResponse.class);
|
|
|
+ response.getDistricts().get(0).getDistricts().stream().parallel().forEach(item -> saveDistrict(item, null));
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
- if (i == 2) {
|
|
|
- d.setLeaf(true);
|
|
|
- }
|
|
|
- if (i == 1 && !String.valueOf(d.getId()).substring(4).equals("00")) {
|
|
|
- d.setLeaf(true);
|
|
|
- }
|
|
|
- districtRepo.save(d);
|
|
|
- }
|
|
|
+ private void saveDistrict(DistrictsItem item, Long parentId) {
|
|
|
+ District district = District.builder()
|
|
|
+ .id(Long.parseLong(item.getAdcode()))
|
|
|
+ .name(item.getName())
|
|
|
+ .parent(parentId)
|
|
|
+ .level(DistrictLevel.valueOf(item.getLevel().toUpperCase()))
|
|
|
+ .build();
|
|
|
+ if (district.getLevel() == DistrictLevel.STREET) {
|
|
|
+ district.setId(Long.parseLong(district.getId() + String.format("%02d", item.getIdx())));
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(item.getCenter())) {
|
|
|
+ String[] arr = item.getCenter().split(",");
|
|
|
+ district.setLng(Double.parseDouble(arr[0]));
|
|
|
+ district.setLat(Double.parseDouble(arr[1]));
|
|
|
+ }
|
|
|
+ if (item.getCitycode() != null && item.getCitycode() instanceof String) {
|
|
|
+ district.setCityCode((String) item.getCitycode());
|
|
|
+ }
|
|
|
+ if (item.getDistricts() != null) {
|
|
|
+ district.setChildCount(item.getDistricts().size());
|
|
|
+ district.setCityCount((int) item.getDistricts()
|
|
|
+ .stream()
|
|
|
+ .filter(d -> DistrictLevel.valueOf(d.getLevel().toUpperCase()) == DistrictLevel.CITY)
|
|
|
+ .count());
|
|
|
+ district.setDistrictCount((int) item.getDistricts()
|
|
|
+ .stream()
|
|
|
+ .filter(d -> DistrictLevel.valueOf(d.getLevel().toUpperCase()) == DistrictLevel.DISTRICT)
|
|
|
+ .count());
|
|
|
+ district.setStreetCount((int) item.getDistricts()
|
|
|
+ .stream()
|
|
|
+ .filter(d -> DistrictLevel.valueOf(d.getLevel().toUpperCase()) == DistrictLevel.STREET)
|
|
|
+ .count());
|
|
|
+ } else {
|
|
|
+ district.setChildCount(0);
|
|
|
+ }
|
|
|
+ districtRepo.save(district);
|
|
|
+ if (item.getDistricts() != null) {
|
|
|
+ int[] idx = {1};
|
|
|
+ item.getDistricts().stream().parallel().forEach(child -> {
|
|
|
+ child.setIdx(idx[0]++);
|
|
|
+ saveDistrict(child, district.getId());
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private District convert(DistrictResponse.District district) {
|
|
|
- District res = new District();
|
|
|
- res.setId((long) district.getId());
|
|
|
- res.setName(district.getName());
|
|
|
- res.setFullName(district.getFullname());
|
|
|
- res.setLat(district.getLocation().getLat());
|
|
|
- res.setLng(district.getLocation().getLng());
|
|
|
- res.setPinyin(StringUtils.join(district.getPinyin(), " "));
|
|
|
- return res;
|
|
|
- }
|
|
|
}
|