需求分析
在现有地点中解析其地点优先级,最小到区县,当前区县可指定作业优先级,当绑定优先级数据后的数据应在选择时优先展示。
行政优先级维护
采用百度地图数据行政区域编码维护,考虑到现有数据是人工维护,获取其省市区县可能不准确,所以先根据其经纬度定位后可准确获取其省市区县,然后再进行编码维护。
示例
地点北京市东城区天安门,百度坐标116.403963,39.915119
API:
https://lbsyun.baidu.com/index.php?title=webapi/district-search
https://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad
Map<String, String> map = IbsBMapUtils.reverseGeocoding("116.403963", "39.915119");
String province = map.get("province");
List<IbsBMapUtils.Districts> districts = IbsBMapUtils.regionSearch(province.substring(0, 2));
for (IbsBMapUtils.Districts district : districts) {
DivisionDo divisionDo = new DivisionDo();
divisionDo.setProvinceCode(district.getCode());
divisionDo.setProvinceName(district.getName());
divisionDo.setWarehouseCode(locationConfigDO.getWarehouseCode());
List<IbsBMapUtils.Districts> districtsList = district.getDistricts();
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(districtsList)) {
for (IbsBMapUtils.Districts districts2 : districtsList) {
divisionDo.setCityCode(districts2.getCode());
divisionDo.setCityName(districts2.getName());
List<IbsBMapUtils.Districts> districts3 = districts2.getDistricts();
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(districts3)) {
for (IbsBMapUtils.Districts district3 : districts3) {
divisionDo.setRegionName(district3.getName());
divisionDo.setRegionCode(district3.getCode());
divisionRepository.insert(divisionDo);
}
} else {
divisionRepository.insert(divisionDo);
}
}
} else {
divisionRepository.insert(divisionDo);
}
}
当前区域指定优先级作业后数据优先展示
例如北京市有N个区,指定了N个规划路线,当东城区指定了由最近的骑手配送时,则点击东城区商家的配送线路优先展示
List<MunicipalLevel> list = MunicipalLevelMapper.selectMunicipalLevelList("北京市-东城区");
if(CollectionUtils.isEmpty(list)){
return Collections.emptyList();
} else {
List<String> riderList = divisionMapper.getRiderByProvince("11001", "北京市","东城区");
//错误实例
if (CollectionUtils.isNotEmpty(riderList)) {
for (String riderName : riderList) {
List<RirderListResponse> listResponses = list.stream().filter(vo -> StringUtils.isNotEmpty(vo.getRirderName()) && vo.getRiderName().equals(riderName)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(listResponses)) {
for (int i = 0; i < listResponses.size(); i++) {
RirderListResponse response = listResponses.get(i);
final List l = list;
l.set(i, l.set(j, l.get(i)));
}
}
}
}
}
log.warn("响应{}", JSONObject.toJSONString(list));
bug修复
当前查询出东城区绑定优先级为1.2.3.4时,查询出来的顺序更新索引位置会变为4.3.2.1,且考虑到list会多个且可重复重新更改代码
List<MunicipalLevel> list = MunicipalLevelMapper.selectMunicipalLevelList("北京市-东城区");
if(CollectionUtils.isEmpty(list)){
return Collections.emptyList();
} else {
List<String> riderList = divisionMapper.getRiderByProvince("11001", "北京市","东城区");
riderList = riderList.stream().filter(item -> responseList.stream().map(RouteListResponse::getName).collect(Collectors.toList()).contains(item)).collect(Collectors.toList());
Collections.reverse(riderList);
Collections.reverse(riderList);
for (int i = 0; i < riderList.size(); i++) {
for (MunicipalLevel res : list) {
if(res.getName().equals(riderList.get(i))){
Collections.swap(list, list.indexOf(res), i);
}
}
}
}
}
bug再次修复终极版
当存在多个优先级相同的区域路线时,会因为本身在list的位置不同,导致最后只有第一个正确,其他元素位置不正确
if(CollectionUtils.isEmpty(list)){
return Collections.emptyList();
} else {
// List<String> riderList = divisionMapper.getRiderByProvince("11001", "北京市","东城区");
//riderList = riderList.stream().filter(item -> responseList.stream().map(RouteListResponse::getName).collect(Collectors.toList()).contains(item)).collect(Collectors.toList());
//Collections.reverse(riderList);
//借助外部元素记录交换元素位置
if (CollectionUtils.isNotEmpty(supplierList)) {
int a = 0 ;
for (int i = 0; i < riderList.size(); i++) {
for (MunicipalLevel res : list) {
if(res.getName().equals(riderList.get(i))){
Collections.swap(list, list.indexOf(res), i);
a++;
}
}
}
}