/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.wechat.service;

import java.io.InputStream;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimaps;
import com.wechat.dao.AreaDao;
import com.wechat.global.CustomException;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.Area;
import com.wechat.model.dto.AreaDto;
import com.wechat.model.dto.City;
import com.wechat.model.dto.Province;
import com.wechat.model.responseDto.LVList;

/**
 * 区域Service
 * 
 * @author ThinkGem
 * @version 2014-05-16
 */
@Service
public class AreaService {

	@Autowired
	private AreaDao areaDao;

	/**
	 * 省市区
	 * @param lang 
	 * 
	 * @return
	 */
	public List<Province> getAllArea(String lang) {

		// 查询所有的省份
		List<Area> provinceList = areaDao.getAllArea("1", "2",lang);
		List<String> provinceIds = Lists.transform(provinceList, new Function<Area, String>() {
			@Override
			public String apply(Area arg0) {
				return arg0.getId();
			}
		});
		// 查询所有市
		List<Area> cityList = areaDao.getAllAreaByParentIdList(provinceIds, "3",lang);
		List<String> cityIds = Lists.transform(cityList, new Function<Area, String>() {
			@Override
			public String apply(Area arg0) {
				return arg0.getId();
			}
		});
		// 获取城市并根据省份分组
		ImmutableListMultimap<String, Area> groupCityList = Multimaps.index(cityList, new Function<Area, String>() {
			@Override
			public String apply(Area arg0) {
				return arg0.getParentId();
			}
		});
		// 查询所有区县
		List<Area> areaList = areaDao.getAllAreaByParentIdList(cityIds, "4",lang);

		ImmutableListMultimap<String, Area> groupAreaList = Multimaps.index(areaList, new Function<Area, String>() {
			@Override
			public String apply(Area arg0) {
				return arg0.getParentId();
			}
		});
		// 组织List<Province>
		List<Province> listPro = Lists.newArrayList();
		for (Area province : provinceList) {
			Province pro = new Province();
			pro.setValue(province.getCode());
			pro.setName(province.getName());
			if (groupCityList.containsKey(province.getId())) {
				List<Area> groupCity = groupCityList.get(province.getId());
				List<City> cityLists = Lists.newArrayList();
				for (Area city : groupCity) {
					City newCity = new City();
					newCity.setValue(city.getCode());
					newCity.setName(city.getName());
					List<AreaDto> areaLists = Lists.newArrayList();
					if (groupAreaList.containsKey(city.getId())) {
						List<Area> groupArea = groupAreaList.get(city.getId());
						for (Area area : groupArea) {
							AreaDto newArea = new AreaDto();
							newArea.setValue(area.getCode());
							newArea.setName(area.getName());
							areaLists.add(newArea);
						}
						newCity.setChildren(areaLists);
					}
					cityLists.add(newCity);
				}
				pro.setChildren(cityLists);
			}
			listPro.add(pro);
		}
		return listPro;
	}

	/**
	 * 获取所有的省市
	 * @param lang 
	 * 
	 * @return
	 */
	public List<Province> getAllProvinceAndCity(String lang) {

		// 查询所有的省份
		List<Area> provinceList = areaDao.getAllArea("1", "2",lang);
		List<String> provinceIds = Lists.transform(provinceList, new Function<Area, String>() {
			@Override
			public String apply(Area arg0) {
				return arg0.getId();
			}
		});
		// 查询所有市
		List<Area> cityList = areaDao.getAllAreaByParentIdList(provinceIds, "3",lang);
		// 获取城市并根据省份分组
		ImmutableListMultimap<String, Area> groupCityList = Multimaps.index(cityList, new Function<Area, String>() {
			@Override
			public String apply(Area arg0) {
				return arg0.getParentId();
			}
		});
		// 组织List<Province>
		List<Province> listPro = Lists.newArrayList();
		for (Area province : provinceList) {
			Province pro = new Province();
			pro.setValue(province.getCode());
			pro.setName(province.getName());
			if (groupCityList.containsKey(province.getId())) {
				List<Area> groupCity = groupCityList.get(province.getId());
				List<City> cityLists = Lists.newArrayList();
				for (Area city : groupCity) {
					City newCity = new City();
					newCity.setValue(city.getCode());
					newCity.setName(city.getName());
					cityLists.add(newCity);
				}
				pro.setChildren(cityLists);
			}
			listPro.add(pro);
		}
		return listPro;
	}
	
	public void changeCn2En() {
		List<Area> list = get();
		for(Area a: list) {
			String id = a.getId();
			String name = a.getName();
			String nameEn;
			try {
				nameEn = PinyinHelper.convertToPinyinString(name, "", PinyinFormat.WITHOUT_TONE);
				int i = update(id,nameEn);
				if(i != 1) {
					throw new CustomException(InfoMsg.ERROR_SAVE_FIELD);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}
	public List<Area> get(){
		return areaDao.getall();
	}
	@Transactional
	public int update(String id, String nameEn) {
		Area area = new Area();
		area.setId(id);
		area.setNameEn(nameEn);
		return areaDao.updateAreaEnName(area);
	}
	@Transactional
	public String importEn() {
		InputStream input = this.getClass().getResourceAsStream("/template/provinceCityArea.txt");
		byte[] strBuffer = null;
		try {
			strBuffer = new byte[input.available()];
			input.read(strBuffer, 0, input.available());
		} catch (Exception e) {
			e.printStackTrace();
		}
		String json = new String(strBuffer);
		List<LVList> list = JSONObject.parseArray(json, LVList.class);
		for(LVList d : list) {
//			Area area = new Area();
//			area.setCode(d.getValue());
//			area.setNameEn(d.getName());
//			areaDao.updateAreaEnNameByCode(area);
			if(d.getChildren() != null && d.getChildren().size()>0) {
				for(LVList d1 : d.getChildren()) {
//					Area area1 = new Area();
//					area1.setCode(d1.getValue());
//					area1.setNameEn(d1.getName());
//					areaDao.updateAreaEnNameByCode(area1);
					if(d1.getChildren() != null && d1.getChildren().size()>0) {
						for(LVList d2 : d1.getChildren()) {
							Area area1 = new Area();
							area1.setCode(d2.getValue());
							area1.setNameEn(d2.getName());
							areaDao.updateAreaEnNameByCode(area1);
						}
					}
				}
			}
		}
		return null;
	}

}
