package com.zfire.jiasm.syncdata.download.basedata;

import lombok.extern.slf4j.Slf4j;


/** 线程太多，合并线程
 * 省、市、区、镇、数据同步
 */
@Slf4j
// @Component
public class ProvinceCityAreaTownbak {
    /*
    @Value("${baseurl}")
    private String baseurl = "";
    @Value("${sxc}")
    private String sxc = "";
    @Value("${ssxc}")
    private String ssxc = "";
    @Value("${qxc}")
    private String qxc = "";
    @Value("${zxc}")
    private String zxc = "";

    @Resource
    TokenCenter tokenCenter;
    @Resource
    ProvinceCityAreaTownService provinceCityAreaTownService;
    @Resource
    CityService cityService;
    @Resource
    ProvinceService provinceService;



    // 省份基础资料信息同步
    @Scheduled(fixedDelayString = "${sx}")
    public void processProvinceTask() {
        try{


            if (sxc.equals(TaskConfigEnum.FALSE.getCode())){
                return;
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            log.info("开始下载省份基础资料信息: \uF0B7{}",sdf.format(new Date()));

            Map<String, String> paramMap = new HashMap<String, String>();
            Token token = tokenCenter.getCurrentToken();
            String url = baseurl + InterFaceTypeEnum.TBL_PROVINCE.getCode() + "/querylist";
            String paramJson= JSON.toJSONString(paramMap);

            JSONResult jsonResult = HttpUtil.httpRequestWithToken(url, "POST", paramJson, token);
            if (jsonResult.getStatus() == null || !jsonResult.getStatus().toString().equals("200")) {
                log.info("下载省份基础资料信息失败！: {}",jsonResult.getMessage());
                return;
            }
            JSONArray jsonArray = (JSONArray) jsonResult.getData();

            if (jsonArray != null) {
                log.info("下载了{}条数据",jsonArray.size());

                provinceCityAreaTownService.extractedProvince(jsonArray);

                log.info("省份基础资料信息处理完成: \uF0B7{}",sdf.format(new Date()));
            }else {
                log.info("["+url+"]无数据");
            }
        } catch (Exception ex){
            log.error("下载省份基础资料信息失败！", ex);
        }
    }

    // 市基础资料信息同步
    @Scheduled(fixedDelayString = "${ssx}")
    public void processCityTask() {
        try{


            if (ssxc.equals(TaskConfigEnum.FALSE.getCode())){
                return;
            }

            // 提取省份信息
            List<Province> provinceRec =  provinceService.lambdaQuery().list();
            // 省份信息未同步时先不同步
            if (CollectionUtils.isEmpty(provinceRec)){
                return;
            }
            provinceRec.clear();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dt = sdf.format(new Date());

            log.info("开始下载市基础资料信息: {}",dt);

            Map<String, String> paramMap = new HashMap<String, String>();
            Token token = tokenCenter.getCurrentToken();
            String url = baseurl + InterFaceTypeEnum.TBL_CITY.getCode() + "/querylist";
            String paramJson= JSON.toJSONString(paramMap);

            JSONResult jsonResult = HttpUtil.httpRequestWithToken(url, "POST", paramJson, token);
            if (jsonResult.getStatus() == null || !jsonResult.getStatus().toString().equals("200")) {
                log.info("下载市基础资料信息失败！: {}",jsonResult.getMessage());
                return;
            }
            JSONArray jsonArray = (JSONArray) jsonResult.getData();

            if (jsonArray != null) {
                log.info("下载了{}条数据"+jsonArray.size());

                provinceCityAreaTownService.extractedCity(jsonArray);

                log.info("市基础资料信息处理完成: \uF0B7{}",sdf.format(new Date()));

            }else {
                log.info("["+url+"]无数据");
            }
        } catch (Exception ex){
            log.error("下载市基础资料信息失败！", ex);
        }
    }

    // 区基础资料信息同步
    @Scheduled(fixedDelayString = "${qx}")
    public void processCountyTask() {
        try{


            if (qxc.equals(TaskConfigEnum.FALSE.getCode())){
                return;
            }


            // 提取市信息
            List<City> cityRec =  cityService.lambdaQuery().list();
            // 市信息未同步时先不同步
            if (CollectionUtils.isEmpty(cityRec)){
                return;
            }
            cityRec.clear();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dt = sdf.format(new Date());

            log.info("开始下载区基础资料信息: {}",dt);

            Map<String, String> paramMap = new HashMap<String, String>();
            Token token = tokenCenter.getCurrentToken();
            String url = baseurl + InterFaceTypeEnum.TBL_COUNTY.getCode() + "/querylist";
            String paramJson= JSON.toJSONString(paramMap);

            JSONResult jsonResult = HttpUtil.httpRequestWithToken(url, "POST", paramJson, token);
            if (jsonResult.getStatus() == null || !jsonResult.getStatus().toString().equals("200")) {
                log.info("下载区基础资料信息失败！: {}",jsonResult.getMessage());
                return;
            }
            JSONArray jsonArray = (JSONArray) jsonResult.getData();

            if (jsonArray != null) {
                log.info("下载了{}条数据"+jsonArray.size());

                provinceCityAreaTownService.extractedCounty(jsonArray);

                log.info("区基础资料信息处理完成: \uF0B7{}",sdf.format(new Date()));

            }else {
                log.info("["+url+"]无数据");
            }
        } catch (Exception ex){
            log.error("下载区基础资料信息失败！", ex);
        }
    }

    // 乡镇基础资料信息同步
    @Scheduled(fixedDelayString = "${zx}")
    public void processCityJDTask() {
        try{


            if (zxc.equals(TaskConfigEnum.FALSE.getCode())){
                return;
            }


            // 提取市信息
            List<City> cityRec =  cityService.lambdaQuery().list();
            // 市信息未同步时先不同步
            if (CollectionUtils.isEmpty(cityRec)){
                return;
            }
            cityRec.clear();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dt = sdf.format(new Date());

            log.info("开始下载乡镇基础资料信息: {}",dt);

            Map<String, String> paramMap = new HashMap<String, String>();
            Token token = tokenCenter.getCurrentToken();
            String url = baseurl + InterFaceTypeEnum.TBL_CITY_JDXZ.getCode() + "/querylist";
            String paramJson= JSON.toJSONString(paramMap);

            JSONResult jsonResult = HttpUtil.httpRequestWithToken(url, "POST", paramJson, token);
            if (jsonResult.getStatus() == null || !jsonResult.getStatus().toString().equals("200")) {
                log.info("下载乡镇基础资料信息失败！: {}",jsonResult.getMessage());
                return;
            }
            JSONArray jsonArray = (JSONArray) jsonResult.getData();

            if (jsonArray != null) {
                log.info("下载了{}条数据"+jsonArray.size());

                provinceCityAreaTownService.extractedStreet(jsonArray);

                log.info("乡镇基础资料信息处理完成: \uF0B7{}",sdf.format(new Date()));
            }else {
                log.info("["+url+"]无数据");
            }
        } catch (Exception ex){
            log.error("下载乡镇基础资料信息失败！", ex);
        }
    }
    */

}
