package com.sysu.admin.api.city;

import com.sysu.admin.controller.city.*;
import com.sysu.admin.controller.crop.CropLandService;
import com.sysu.admin.controller.crop.range.LandRangeIndexService;
import com.sysu.admin.site.CommonVo;
import com.sysu.admin.utils.shape.GeoCastUtil;
import com.xiesx.fastboot.base.result.BaseResult;
import com.xiesx.fastboot.base.result.R;
import org.apache.commons.lang3.StringUtils;
import org.locationtech.jts.geom.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 镇区域
 */
@RequestMapping("/api/city")
@Controller("ApiCityController2")
public class ApiCityController {

    @Autowired
    ProvinceRepository provinceRepository;
    @Autowired
    CityRepository cityRepository;
    @Autowired
    DistrictRepository districtRepository;
    @Autowired
    TownRepository townRepository;



    @RequestMapping(value = "/data")
    @ResponseBody
    public BaseResult data(){
        Province province = provinceRepository.find("440000");
        List<City> cityList = cityRepository.findListByCode(province.getCode().substring(0, 2) + "%");
        province.setCityList(cityList);
        cityList.stream().forEach(city -> {
            List<District> districts =districtRepository.findList(city.getCode().substring(0,4) + "%");
            city.setDistricts(districts);
            districts.stream().forEach(district -> {
                List<Town> towns = townRepository.findInfoList(district.getCode());
                district.setTowns(towns);
            });
        });
        return R.succ(province);
    }

    @RequestMapping(value = "/subStat")
    @ResponseBody
    public BaseResult subStat(@RequestBody CommonVo commonVo){
        if(StringUtils.isNotBlank(commonVo.getDistrict())){
            List<Town> towns = townRepository.findList(commonVo.getDistrict());
            towns.stream().forEach(town -> town.castGeojson());
            return R.succ(towns);
        }
        if(StringUtils.isNotBlank(commonVo.getCity())){
            List<District> districts = districtRepository.findList(commonVo.getCity().substring(0,4) + "%");
            districts.stream().forEach(district -> district.castGeojson());
            return R.succ(districts);
        }
        List<City> cityList = cityRepository.findListByCode("44%");
        cityList.stream().forEach(city -> city.castGeojson());
        return R.succ(cityList);
    }




}
