import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
/**
* @Title:区域服务类
* @Description
* @Version
*/
@Service
@Slf4j
public class AreaServiceImpl implements IAreaService {
@Resource
private AreaMapper areaMapper;
@Resource
private ISysDictService sysDictService;
@Override
public List<Area> selectAllProvince() {
return areaMapper.selectAllProvince();
}
@Override
public List<Area> selectProvinceCityByParams(AreaSearchVo areaSearchVo) {
return areaMapper.selectProvincecityByParams(areaSearchVo);
}
@Override
public List<Area> selectAllProvinceByCityId() {
List<SysDict> allProvinceByCity = sysDictService.getAllProvinceByCity();
log.info("通过字典表获取的数据:{}", JSON.toJSONString(allProvinceByCity));
List<Area> areaList = new ArrayList<Area>();
if(allProvinceByCity != null){
allProvinceByCity.stream().forEach(c->{
//System.out.println("城市id是:"+c.getValue());
//根据城市id获取省信息 直辖市
if("110000".equals(c.getValue()) || "120000".equals(c.getValue())
|| "500000".equals(c.getValue()) || "310000".equals(c.getValue())
) {
Area centralInfo = areaMapper.selectProvinceNameById(c.getValue());
//c.setId(centralInfo.getId());
areaList.add(centralInfo);
}else{ //非直辖市根据城市id获取省信息
//获取pid
Area cityInfo = areaMapper.selectCityNameById(c.getValue());
Area provinceInfo= areaMapper.selectProvinceNameById(cityInfo.getPid() + "");
areaList.add(provinceInfo);
}
});
}//if end
//list中去掉重复
List endList = removeDuplicate(areaList);
return endList;
}
public static List removeDuplicate(List<Area> list) {
Map<Integer,String> singleMap = new HashMap<>();
List<Area> reList = new ArrayList<>();
for(Area a: list){
if(!singleMap.containsKey(a.getId())){
singleMap.put(a.getId(),"");
reList.add(a);
}
}
list.clear();
return reList;
}
}