package com.dcit.authority.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dcit.common.enums.ResultEnum;
import com.dcit.common.utils.ResultVOUtil;
import com.dcit.common.vo.ResultVO;
import com.dcit.authority.dto.DistrictInfoTreeDTO;
import com.dcit.authority.dto.vo.CityInfoVO;
import com.dcit.authority.dto.vo.CountyInfoVO;
import com.dcit.authority.dto.vo.DistrictInfoVO;
import com.dcit.authority.exception.AuthException;
import com.dcit.authority.model.City;
import com.dcit.authority.model.County;
import com.dcit.authority.model.Province;
import com.dcit.authority.service.CityService;
import com.dcit.authority.service.CountyService;
import com.dcit.authority.service.ProvinceService;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("district")
@Slf4j
public class DistrictController {

	@Autowired
	private ProvinceService provinceService;
	
	@Autowired
	private CityService cityService;
	
	@Autowired
	private CountyService countyService;
	
	@ApiOperation(value = "省市区县树")
	@GetMapping("/getDistrictData")
	public ResultVO<Object> getDistrictData(){
		
		List<DistrictInfoVO> treeVoList = new ArrayList<DistrictInfoVO>();
		
		try {
			List<Province> provinceList = provinceService.selectByExample(null);
			List<City> cityList = cityService.selectByExample(null);
			List<County> countyList = countyService.selectByExample(null);
			
			
			DistrictInfoVO districtInfoVO = null;
			
			for(Province province : provinceList){
				
				districtInfoVO = new DistrictInfoVO();
				
				districtInfoVO.setId(province.getId());
				districtInfoVO.setFatherId("0");
				districtInfoVO.setLabel(province.getName());
				districtInfoVO.setTreeLevel("1");
				
				treeVoList.add(districtInfoVO);
			}
			
			List<CityInfoVO> cityInfoVoList = new ArrayList<>();
			
			CityInfoVO cityInfoVO = null;
			for(City city : cityList){
				
				cityInfoVO = new CityInfoVO();
				
				cityInfoVO.setId(city.getId());
				cityInfoVO.setFatherId(city.getProvinceid());
				cityInfoVO.setLabel(city.getName());
				cityInfoVO.setTreeLevel("2");
				
				cityInfoVoList.add(cityInfoVO);
			}
			
			for(CityInfoVO cityVO : cityInfoVoList){
				
				List<CountyInfoVO> countyInfoVoList = new ArrayList<>();
				for(int i=0; i<countyList.size(); i++){
					if(cityVO.getId().equals(countyList.get(i).getCityid())){
						
						CountyInfoVO countyInfoVO = new CountyInfoVO();
						
						
						countyInfoVO.setId(countyList.get(i).getId());
						countyInfoVO.setFatherId(countyList.get(i).getCityid());
						countyInfoVO.setLabel(countyList.get(i).getName());
						countyInfoVO.setTreeLevel("3");
						
						countyInfoVoList.add(countyInfoVO);
						
						countyList.remove(i);
						--i;
					}
				}
				
				cityVO.setChildren(countyInfoVoList);
			}
			
			for(DistrictInfoVO treeVO : treeVoList){
				for(int i=0; i<cityInfoVoList.size(); i++){
					if(treeVO.getId().equals(cityInfoVoList.get(i).getFatherId())){
						
						treeVO.getChildren().add(cityInfoVoList.get(i));
						
						cityInfoVoList.remove(i);
						--i;
					}
					
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("【省市区县树】操作出错,error={}",e.getMessage());
			throw new AuthException(ResultEnum.OPERATION_FAIL);
		}
		
		
		
		return ResultVOUtil.success(treeVoList);
	}
	
	
}
