package com.softwarebr.nunu.service.oper.impl;

import com.brframework.commondb.core.AbstractEntityService;
import com.brframework.commondb.core.CommonRepository;
import com.brframework.commondb.core.ControllerPageHandle;
import com.brframework.commonweb.core.SpringContext;
import com.softwarebr.nunu.dao.oper.AreaDao;
import com.softwarebr.nunu.entity.oper.Area;
import com.softwarebr.nunu.json.AreaResult;
import com.softwarebr.nunu.service.oper.AreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @date: 2019-10-21 17:33:17
 * @author: lilin
 * @description: 省市区数据
 */

@Slf4j
@Service
@CacheConfig(cacheNames = "default")
public class AreaServiceImpl extends AbstractEntityService<Area, Long, Object> implements AreaService {

	@Autowired
	private AreaDao areaDao;

	@Override
	public CommonRepository<Long, Area> getRepository() {
		return areaDao;
	}

	@Override
	public List<Area> findProvince() {
		return SpringContext.getBean(AreaServiceImpl.class).findByParentId(Long.parseLong("0"));
	}

	@Override
	@Cacheable(key = "targetClass + '-key-all'")
	public List<AreaResult> all() {
		List<AreaResult> results = ControllerPageHandle.convertListTo(findProvince(), AreaResult.class);
		for (AreaResult areaResult : results) {
			List<AreaResult> citys =  ControllerPageHandle.convertListTo(findCity(areaResult.getId()), AreaResult.class);
			for (AreaResult city : citys) {
				city.setCitys(ControllerPageHandle.convertListTo(findCity(city.getId()), AreaResult.class));
			}
			areaResult.setCitys(citys);
		}
		return results;
	}

	@Override
	public List<Area> findCity(Long id) {
		return SpringContext.getBean(AreaServiceImpl.class).findByParentId(id);
	}

	@Override
	public Area findByParentIdAndCityName(Long parentId, String cityName) {
		return areaDao.findByParentIdAndCityName(parentId, cityName);
	}

	@Cacheable(key = "targetClass + '-key-' + #parentId")
	public List<Area> findByParentId(Long parentId) {
		return areaDao.findByParentId(parentId);
	}


	@Override
	public String addressSplicing(Long provinceId, Long cityId, Long areaId){
		StringBuilder address = new StringBuilder();
		if(null != provinceId){
			Area area = accessObject(provinceId);
			address.append(area.getCityName());
		}
		if(null != cityId){
			Area area = accessObject(cityId);
			address.append(area.getCityName());
		}
		if(null != areaId){
			Area area = accessObject(areaId);
				address.append(area.getCityName());
		}
		return address.toString();
	}

	@Override
	public String addressSplicing(Long provinceId, Long cityId, Long areaId, Long streetId){
		StringBuilder address = new StringBuilder();
		if(null != provinceId){
			Area area = accessObject(provinceId);
			address.append(area.getCityName());
		}
		if(null != cityId){
			Area area = accessObject(cityId);
			address.append(area.getCityName());
		}
		if(null != areaId){
			Area area = accessObject(areaId);
			address.append(area.getCityName());
		}
		if(null != streetId){
			Area area = accessObject(streetId);
			address.append(area.getCityName());
		}
		return address.toString();
	}

}