package com.gyee.frame.service.redar;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.common.feign.RemoteServiceBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class RedarService {
    private static final Logger log = LoggerFactory.getLogger(RedarService.class);

    @Resource
    private RemoteServiceBuilder remoteService;

    public List<List<Map>> hitch(String stId, String Date) {

        Map<String, String> TimeMap = new LinkedHashMap<>();

        //年份
        TimeMap.put("yearstarttime", DateUtil.format(DateUtil.beginOfYear(DateUtil.parseDate(Date)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        TimeMap.put("yearendtime", DateUtil.format(DateUtil.endOfYear(DateUtil.parseDate(Date)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        //月份
        TimeMap.put("monthstarttime", DateUtil.format(DateUtil.beginOfMonth(DateUtil.parseDate(Date)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        TimeMap.put("monthendtime", DateUtil.format(DateUtil.endOfMonth(DateUtil.parseDate(Date)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        //一天
        TimeMap.put("daystarttime", DateUtil.format(DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.parseDate(Date),-1)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        TimeMap.put("dayendtime", DateUtil.format(DateUtil.endOfDay(DateUtil.parseDate(Date)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));

        AjaxResult year = remoteService.earlyService().getHith(stId,TimeMap.get("yearstarttime"),TimeMap.get("yearendtime"));

        AjaxResult month = remoteService.earlyService().getHith(stId,TimeMap.get("monthstarttime"),TimeMap.get("monthendtime"));

        AjaxResult day = remoteService.earlyService().getHith(stId,TimeMap.get("daystarttime"),TimeMap.get("dayendtime"));


        String jsonStr = JSON.toJSONString(year);
        List<Map> year1 = parseJSON(jsonStr);

        String jsonStr1 = JSON.toJSONString(month);
        List<Map> month1 = parseJSON(jsonStr1);

        String jsonStr2 = JSON.toJSONString(day);
        List<Map> day1 = parseJSON(jsonStr2);

        List<List<Map>> ui = new ArrayList<>();
        ui.add(year1);
        ui.add(month1);
        ui.add(day1);
        return ui;
    }


    public List<Map> parseJSON(String jsonData) {
        try {
            JSONObject object = JSON.parseObject(jsonData);
            JSONArray data1 = object.getJSONArray("data");

            double count = 0;
            int time = 0;

            int bj = 0;
            int fjqt = 0;
            int fdj = 0;
            int clx = 0;
            int bpq = 0;
            int zz = 0;
            int hh = 0;
            int ph = 0;
            int yy = 0;
            int lq = 0;
            int rh = 0;
            int kz = 0;
            int jcjr = 0;

            for (int i = 0; i < data1.size(); i++) {
                JSONObject objecti = data1.getJSONObject(i);
                Set<String> keys = objecti.keySet();
                for (String key : keys) {
                    JSONArray array = objecti.getJSONArray(key);
                    for (int j = 0; j < array.size(); j++) {
                        JSONObject jsonObject = array.getJSONObject(j);

                        count += jsonObject.getInteger("count");
                        time += jsonObject.getInteger("time");

                        String c = jsonObject.getString("typeID");

                        if (c.equals("bj")) {
                            bj += jsonObject.getInteger("count");
                        } else if (c.equals("fjqt")) {
                            fjqt += jsonObject.getInteger("count");
                        } else if (c.equals("fdj")) {
                            fdj += jsonObject.getInteger("count");
                        } else if (c.equals("clx")) {
                            clx += jsonObject.getInteger("count");
                        } else if (c.equals("bpq")) {
                            bpq += jsonObject.getInteger("count");
                        } else if (c.equals("zz")) {
                            zz += jsonObject.getInteger("count");
                        } else if (c.equals("hh")) {
                            hh += jsonObject.getInteger("count");
                        } else if (c.equals("ph")) {
                            ph += jsonObject.getInteger("count");
                        } else if (c.equals("yy")) {
                            yy += jsonObject.getInteger("count");
                        } else if (c.equals("lq")) {
                            lq += jsonObject.getInteger("count");
                        } else if (c.equals("rh")) {
                            rh += jsonObject.getInteger("count");
                        } else if (c.equals("kz")) {
                            kz += jsonObject.getInteger("count");
                        } else if (c.equals("jcjr")) {
                            jcjr += jsonObject.getInteger("count");
                        }
                    }
                }
            }

            List<Map> conver = new ArrayList<>();

            DecimalFormat df = new DecimalFormat("0.00");

            Map<String, String> year = new LinkedHashMap<>();
            year.put("bj", count!=0 ?df.format(bj / count):0.0+"");
            year.put("fjqt", count!=0 ?df.format(fjqt / count):0.0+"");
            year.put("fdj", count!=0 ?df.format(fdj / count):0.0+"");
            year.put("clx", count!=0 ?df.format(clx / count):0.0+"");
            year.put("bpq", count!=0 ?df.format(bpq / count):0.0+"");
            year.put("zz", count!=0 ?df.format(zz / count):0.0+"");
            year.put("hh", count!=0 ?df.format(hh / count):0.0+"");
            year.put("ph", count!=0 ?df.format(ph / count):0.0+"");
            year.put("yy", count!=0 ?df.format(yy / count):0.0+"");
            year.put("lq", count!=0 ?df.format(lq / count):0.0+"");
            year.put("rh", count!=0 ?df.format(rh / count):0.0+"");
            year.put("kz", count!=0 ?df.format(kz / count):0.0+"");
            year.put("jcjr", count!=0 ?df.format(jcjr / count):0.0+"");

            conver.add(year);
            return  conver;

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return null;
    }


    public List<List<Map>> warning(String stationid, String date) {

        Map<String, String> TimeMap = new LinkedHashMap<>();
        //年份
        TimeMap.put("yearstarttime", DateUtil.format(DateUtil.beginOfYear(DateUtil.parseDate(date)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        TimeMap.put("yearendtime", DateUtil.format(DateUtil.endOfYear(DateUtil.parseDate(date)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        //月份
        TimeMap.put("monthstarttime", DateUtil.format(DateUtil.beginOfMonth(DateUtil.parseDate(date)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        TimeMap.put("monthendtime", DateUtil.format(DateUtil.endOfMonth(DateUtil.parseDate(date)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        //一天
        TimeMap.put("daystarttime", DateUtil.format(DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.parseDate(date),-1)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        TimeMap.put("dayendtime", DateUtil.format(DateUtil.endOfDay(DateUtil.parseDate(date)), DatePattern.NORM_DATETIME_MINUTE_PATTERN));

        AjaxResult year = remoteService.earlyService().getEarlyWarning(stationid,TimeMap.get("yearstarttime"),TimeMap.get("yearendtime"));

        AjaxResult month = remoteService.earlyService().getEarlyWarning(stationid,TimeMap.get("monthstarttime"),TimeMap.get("monthendtime"));

        AjaxResult day = remoteService.earlyService().getEarlyWarning(stationid,TimeMap.get("daystarttime"),TimeMap.get("dayendtime"));

        List<List<Map>> fh = new ArrayList<>();
        String jsonStr1 = JSON.toJSONString(year);
        List<Map> year1 = pron(jsonStr1);

        String jsonStr2 = JSON.toJSONString(month);
        List<Map> month1 = pron(jsonStr2);

        String jsonStr3 = JSON.toJSONString(day);
        List<Map> day1 = pron(jsonStr3);

        fh.add(year1);
        fh.add(month1);
        fh.add(day1);
        return fh;
    }

    public List<Map> pron(String jsonData1) {
        JSONObject object = JSON.parseObject(jsonData1);
        JSONArray data2 = object.getJSONArray("data");

        double count = 0;
        int time = 0;

        int CFXT = 0;
        int CDL = 0;
        int FDJ = 0;
        int BJXT = 0;
        int CLX = 0;
        int YYXT = 0;
        int PHXT = 0;
        int JC = 0;
        int QT = 0;
        int TDG = 0;
        for (int i = 0; i < data2.size(); i++) {
            JSONObject objecti = data2.getJSONObject(i);
            Set<String> keys = objecti.keySet();
            for (String key : keys) {
                JSONArray array = objecti.getJSONArray(key);
                for (int j = 0; j < array.size(); j++) {
                    JSONObject jsonObject = array.getJSONObject(j);

                    count += jsonObject.getInteger("count");
                    time += jsonObject.getInteger("time");

                    String c = jsonObject.getString("relateParts");
                    if (StringUtils.isEmpty(c))
                        continue;

                    if (c.equals("CFXT")) {
                        CFXT += jsonObject.getInteger("count");
                    } else if (c.equals("CDL")) {
                        CDL += jsonObject.getInteger("count");
                    } else if (c.equals("FDJ")) {
                        FDJ += jsonObject.getInteger("count");
                    } else if (c.equals("BJXT")) {
                        BJXT += jsonObject.getInteger("count");
                    } else if (c.equals("CLX")) {
                        CLX += jsonObject.getInteger("count");
                    } else if (c.equals("YYXT")) {
                        YYXT += jsonObject.getInteger("count");
                    } else if (c.equals("PHXT")) {
                        PHXT += jsonObject.getInteger("count");
                    } else if (c.equals("JC")) {
                        JC += jsonObject.getInteger("count");
                    } else if (c.equals("QT")) {
                        QT += jsonObject.getInteger("count");
                    } else if (c.equals("TDG")) {
                        TDG += jsonObject.getInteger("count");
                    }
                }
            }
        }

        List<Map> conver = new ArrayList<>();

        DecimalFormat df = new DecimalFormat("0.00");

        Map<String, String> year = new LinkedHashMap<>();
        year.put("CFXT",count!=0 ? df.format(CFXT / count):0.0+"");
        year.put("CDL",count!=0 ? df.format(CDL / count):0.0+"");
        year.put("FDJ",count!=0 ? df.format(FDJ / count):0.0+"");
        year.put("BJXT",count!=0 ? df.format(BJXT / count):0.0+"");
        year.put("CLX",count!=0 ? df.format(CLX / count):0.0+"");
        year.put("YYXT",count!=0 ? df.format(YYXT / count):0.0+"");
        year.put("PHXT",count!=0 ? df.format(PHXT / count):0.0+"");
        year.put("JC",count!=0 ? df.format(JC / count):0.0+"");
        year.put("QT",count!=0 ? df.format(QT / count):0.0+"");
        year.put("TDG",count!=0 ? df.format(TDG / count):0.0+"");

        conver.add(year);

        return conver;
    }

}
