| package com.xzx.gc.user.service;  | 
|   | 
| import cn.hutool.core.collection.CollUtil;  | 
| import cn.hutool.core.convert.Convert;  | 
| import cn.hutool.core.util.StrUtil;  | 
| import com.xzx.gc.common.Result;  | 
| import com.xzx.gc.common.dto.gdmap.GdReverseGEODto;  | 
| import com.xzx.gc.common.exception.RestException;  | 
| import com.xzx.gc.common.utils.BusinessUtil;  | 
| import com.xzx.gc.common.utils.gdmap.GdMapUtil;  | 
| import com.xzx.gc.entity.AddressInfo;  | 
| import com.xzx.gc.entity.AddressLevelInfo;  | 
| import com.xzx.gc.user.mapper.AddressLevelMapper;  | 
| import org.springframework.beans.factory.annotation.Autowired;  | 
| import org.springframework.stereotype.Service;  | 
| import org.springframework.transaction.annotation.Transactional;  | 
| import tk.mybatis.mapper.entity.Example;  | 
|   | 
| import java.util.ArrayList;  | 
| import java.util.List;  | 
| import java.util.Map;  | 
|   | 
| @Service  | 
| @Transactional  | 
| public class AddressLevelService {  | 
|   | 
|     @Autowired  | 
|     private AddressLevelMapper addressLevelMapper;  | 
|   | 
|     @Autowired  | 
|     private BusinessUtil businessUtil;  | 
|   | 
|     @Autowired  | 
|     private CityPartnerService cityPartnerService;  | 
|   | 
|     @Autowired  | 
|     private PartnerGaodeService partnerGaodeService;  | 
|   | 
|   | 
|     public AddressLevelInfo findByLocation(String lon,String lat){  | 
|         GdReverseGEODto gdReverseGEODto = new GdReverseGEODto();  | 
|         gdReverseGEODto.setLocation(lon+","+lat);  | 
|         Result areaInfo = GdMapUtil.getAreaInfo(gdReverseGEODto, 0);  | 
|         if(areaInfo.getCode()==0){  | 
|             AddressInfo addressInfo1= (AddressInfo) areaInfo.getData();  | 
|             AddressLevelInfo byAdcode = findByAdcode(addressInfo1.getAdcode());  | 
|             if(byAdcode!=null){  | 
|                return byAdcode;  | 
|             }else {  | 
|                 throw new RestException("定位异常:"+addressInfo1.getAdcode());  | 
|             }  | 
|         }else {  | 
|             throw new RestException("定位异常:"+areaInfo.getMsg());  | 
|         }  | 
|     }  | 
|   | 
|     public List<AddressLevelInfo> findByTownIds(String townIds){  | 
|         if(StrUtil.isNotBlank(townIds)) {  | 
|             Example example = new Example(AddressLevelInfo.class);  | 
|             Example.Criteria criteria = example.createCriteria();  | 
|             criteria.andIn("level3Id", CollUtil.newArrayList(townIds.split(",")));  | 
|             List<AddressLevelInfo> addressLevelInfos = addressLevelMapper.selectByExample(example);  | 
|             return addressLevelInfos;  | 
|         }else {  | 
|             return new ArrayList<>();  | 
|         }  | 
|     }  | 
|   | 
|     public AddressLevelInfo find(String level1Id,String level2Id,String level3Id){  | 
|         /**  | 
|          *   private String level1Id;  | 
|          *     @Column(name = "level_1_name")  | 
|          *     private String level1Name;  | 
|          *     @Column(name = "level_2_id")  | 
|          *     private String level2Id;  | 
|          *     @Column(name = "level_2_name")  | 
|          *     private String level2Name;  | 
|          *     @Column(name = "level_3_id")  | 
|          *     private String level3Id;  | 
|          */  | 
|         Example example = new Example(AddressLevelInfo.class);  | 
|         Example.Criteria criteria = example.createCriteria();  | 
|         criteria.andEqualTo("level3Id", level3Id);  | 
|         criteria.andEqualTo("level2Id", level2Id);  | 
|         criteria.andEqualTo("level1Id", level1Id);  | 
|         List<AddressLevelInfo> addressLevelInfos = addressLevelMapper.selectByExample(example);  | 
|         if (CollUtil.isNotEmpty(addressLevelInfos)) {  | 
|             return addressLevelInfos.get(0);  | 
|         }  | 
|         return null;  | 
|     }  | 
|   | 
|     /**  | 
|      * 根据任意条件查询  | 
|      * @param addressLevelInfo  | 
|      * @return  | 
|      */  | 
|     public List<AddressLevelInfo> findByAny(AddressLevelInfo addressLevelInfo){  | 
|         if(StrUtil.isNotBlank(addressLevelInfo.getLevel3Name())){  | 
|             addressLevelInfo.setLevel3Name(businessUtil.changeArea(addressLevelInfo.getLevel3Name()));  | 
|         }  | 
|   | 
|         List<AddressLevelInfo> addressLevelInfos = addressLevelMapper.select(addressLevelInfo);  | 
|         if(CollUtil.isNotEmpty(addressLevelInfos)){  | 
|             for (AddressLevelInfo levelInfo : addressLevelInfos) {  | 
|                 levelInfo.setLevel3Name(businessUtil.changeArea(levelInfo.getLevel3Name()));  | 
|             }  | 
|         }  | 
|         return addressLevelInfos;  | 
|     }  | 
|   | 
|     public AddressLevelInfo findByAdcode(String adcode){  | 
|         Example example = new Example(AddressLevelInfo.class);  | 
|         Example.Criteria criteria = example.createCriteria();  | 
|         criteria.andEqualTo("adcode", adcode);  | 
|         AddressLevelInfo addressLevelInfos = addressLevelMapper.selectOneByExample(example);  | 
|         return addressLevelInfos;  | 
|     }  | 
|   | 
|     public List<Map<String, Object>>  queryAllProvince(String level){  | 
|         List<Map<String, Object>> result;  | 
|         result = addressLevelMapper.queryAllProvince();  | 
|         for (Map<String, Object> pMap:result) {  | 
|             List<Map<String, Object>> cList  = addressLevelMapper.queryAllCity(pMap.get("code").toString());  | 
|             for (Map<String, Object> cMap:cList) {  | 
|                 if(null!=cMap){  | 
|                     if(null!=cMap.get("code")){  | 
|                         List<Map<String, Object>> tList  = addressLevelMapper.queryAllArea(cMap.get("code").toString());  | 
|                         cMap.put("children",tList);  | 
|                     }  | 
|                 }  | 
|   | 
|             }  | 
|             pMap.put("children",cList);  | 
|         }  | 
|         return result;  | 
|     }  | 
|   | 
|     public List<Map<String,Object>> queryApiArea(){  | 
|         List<String> partnerIds = cityPartnerService.queryPartnerByCurrent();  | 
|         List<String> townModel = new ArrayList<>();  | 
|         if(null!=partnerIds&&partnerIds.size()>0){  | 
|             String partnerId= partnerIds.get(0);  | 
|             townModel =partnerGaodeService.queryAreaByParentner(partnerId);  | 
|         }else{  | 
|             townModel =partnerGaodeService.queryAreaByParentner(null);  | 
|   | 
|         }  | 
|         List<Map<String,Object>> areaList = addressLevelMapper.queryApiArea(townModel);  | 
|         return areaList;  | 
|     }  | 
| }  |