package com.goafanti.common.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.goafanti.common.bo.AreaBo;
import com.goafanti.common.bo.CityBo;
import com.goafanti.common.bo.ProvinceBo;
import com.goafanti.common.dao.DistrictGlossoryMapper;
import com.goafanti.common.model.DistrictGlossory;
import com.goafanti.common.utils.LoggerUtils;
@Service
public class DistrictGlossoryService {
	private static final Logger	logger	= LoggerFactory.getLogger(DistrictGlossoryService.class);
	
	@Autowired
	private DistrictGlossoryMapper districtGlossoryMapper;
	
	public DistrictGlossory selectByPrimaryKey(Integer id) {
		return districtGlossoryMapper.selectByPrimaryKey(id);
	}
	
	@Cacheable(value = "DistrictGlossoryCache", key = "'DistrictGlossory:'+#pid")
	public List<DistrictGlossory> list(Integer pid) {
		LoggerUtils.debug(logger, "缓存地区列表:[%s]", pid);
		return districtGlossoryMapper.findByPid(pid);
	}

	@CacheEvict(value = "DistrictGlossoryCache", key = "'DistrictGlossory:'+#pid")
	public void clear(Integer pid) {
		LoggerUtils.debug(logger, "清除地区列表缓存:[%s]", pid);
	}

	public String selectNameById(Integer id) {
		return districtGlossoryMapper.selectNameById(id);
	}
	
	public List<ProvinceBo> getProvince(){
		List<ProvinceBo> provinces=districtGlossoryMapper.getProvince();
		List<CityBo> city=districtGlossoryMapper.getCity();
		List<AreaBo> Area=districtGlossoryMapper.getArea();		
		for(ProvinceBo one:provinces){
			List <CityBo> cc=new ArrayList<CityBo>();
			int pbid = one.getId();
			for(CityBo two:city){
				List <AreaBo> aa=new ArrayList<AreaBo>();
				int cbid=two.getId();
				int cbpid=two.getPid();
				if(cbpid==pbid){					
					for(AreaBo three:Area){
						int abpid=three.getPid();
						if(abpid==cbid){
							aa.add(three);
							two.setAreaList(aa);
						}
					}
					cc.add(two);
					one.setCityList(cc);
				}
			}
		}
		return provinces;
	}
}
