package com.nantian.cloud.dbcapp.tool;

import com.nantian.cloud.dbcapp.vo.AccessUnionQuotaVo;
import com.nantian.cloud.dbcapp.vo.AccessUnionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;

/**
 * 统计延期企业的填报情况
 */
@Component
public class StatisticalDetails {

    private static AreaCode code = new AreaCode();

    //月份数据
    private static final String[] MONTHS = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};

    //月份数据英文缩写
    private static final String[] MONTHS_EN = {"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};

    //州市数据
    private static final String[] ALL_CITY_NAMES = {"昆明", "曲靖", "玉溪", "保山", "昭通", "丽江", "普洱", "临沧", "楚雄",
            "红河", "文山", "版纳", "大理", "德宏", "怒江", "迪庆"};

    /**
     * 各个州市的各个月份的能源消费结构表填报情况
     * @param list
     * @param allShouldWrite
     * @return
     */
    public static List<Map<String,Object>> statisticalData(List<AccessUnionVo> list,Integer[] allShouldWrite) {


        List<Map<String,Object>> citylist = new ArrayList<>();//16个州市每个对应一个map
        for (int m = 0; m < ALL_CITY_NAMES.length; m++) {
            Map<String, Object> map = new LinkedHashMap<>();//存储每个州市的各个月份的数据
            map.put("index",m+1);
            map.put("city",ALL_CITY_NAMES[m]);
            map.put("num",allShouldWrite[m]);
            for (int j = 0; j < 12; j++) {
                int total = 0;
                String month = MONTHS[j];
                String montn_en = MONTHS_EN[j];
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getRecordDate() == null) {
                        continue;
                    }

                    if (list.get(i).getTname().equals(ALL_CITY_NAMES[m]) &&
                            MONTHS[j].equals(list.get(i).getRecordDate().substring(5, 7))) {
                        total++;
                    }

                }
                map.put(montn_en, total);

            }
            citylist.add(map);

        }
        return citylist;
    }

    /**
     * 产品指标表的填报情况
     * @param list
     * @param allShouldWrite
     * @return
     */
    public static List<Map<String, Object>> statisticalDataQuota(List<AccessUnionQuotaVo> list,Integer[] allShouldWrite) {

        List<Map<String,Object>> citylist = new ArrayList<>();
        for (int m = 0; m < ALL_CITY_NAMES.length; m++) {
            Map<String, Object> map = new LinkedHashMap<>();//存储每个州市的各个月份的数据
            map.put("index",m+1);
            map.put("city",ALL_CITY_NAMES[m]);
            map.put("num",allShouldWrite[m]);
            for (int j = 0; j < 12; j++) {
                int total = 0;
                String month = MONTHS[j];
                String montn_en = MONTHS_EN[j];
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getRecordDate() == null) {
                        continue;
                    }

                    if (list.get(i).getTname().equals(ALL_CITY_NAMES[m]) &&
                            MONTHS[j].equals(list.get(i).getRecordDate().substring(5, 7))) {
                        total++;
                    }


                }
                map.put(montn_en, total);



            }
            citylist.add(map);

        }
        return citylist;

    }

    /**
     * 得到每个地州应填的企业数量
     * @param list
     * @return
     */
    public static Integer[] getInfodetial(List<AccessUnionVo> list){
        //存放每个地州该上报的企业的数量
        Integer[] infodetial = new Integer[ALL_CITY_NAMES.length];
        for(int i = 0;i< ALL_CITY_NAMES.length;i++){
            int total=0;
            for(int j = 0;j<list.size();j++){

                if(ALL_CITY_NAMES[i].equals(list.get(j).getTname())){
                    total++;

                }
                infodetial[i]=total;
            }
        }
        return infodetial;
    }


    /**
     * 按照企业统计能源消费结构情况表的填报情况
     * @param list
     * @param allList
     * @return
     */
    public static List<Map<String,Object>> getTructure(List<AccessUnionVo> list,List<AccessUnionVo> allList){

        List<Map<String,Object>> volist = new ArrayList<>();
        for(int i = 0;i< allList.size();i++){
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("index",i+1);
            map.put("tname",allList.get(i).getTname());
            map.put("cmpName",allList.get(i).getCmpName());
            map.put("enterpriseCode",allList.get(i).getEnterpriseCode());
            for (int j = 0 ;j< list.size();j++){
                for(int k = 0;k< MONTHS.length;k++){
                    if(list.get(j).getEnterpriseCode().equals(allList.get(i).getEnterpriseCode())
                            && list.get(j).getRecordDate().substring(5,7).equals(MONTHS[k])){
                        map.put(MONTHS_EN[k],"已填写" );

                    }
                }

            }
            volist.add(map);
        }

        return volist;
    }

    /**
     *按照企业统计能源消费结构情况表的填报情况，加上地区参数
     * @param list
     * @param allList
     * @param dictid
     * @return
     */
    public static List<Map<String,Object>> getTructure(List<AccessUnionVo> list,List<AccessUnionVo> allList,String dictid){

        List<Map<String,Object>> volist = new ArrayList<>();
        int m = 0;
        for(int i = 0;i< allList.size();i++){
            System.out.println(code.getAreaMapB().get(dictid));
            if(code.getAreaMapB().get(dictid).equals(allList.get(i).getTname())){
                m++;
                System.out.println(code.getAreaMapB().get(dictid));
                Map<String,Object> map = new LinkedHashMap<>();
                map.put("index",m);
                map.put("tname",allList.get(i).getTname());
                map.put("cmpName",allList.get(i).getCmpName());
                map.put("enterpriseCode",allList.get(i).getEnterpriseCode());
                for (int j = 0 ;j< list.size();j++){
                    for(int k = 0;k< MONTHS.length;k++){
                        if(list.get(j).getEnterpriseCode().equals(allList.get(i).getEnterpriseCode())
                                && list.get(j).getRecordDate().substring(5,7).equals(MONTHS[k])){
                            map.put(MONTHS_EN[k],"已填写" );

                        }
                    }

                }
                volist.add(map);
            }

        }

        return volist;
    }

    /**
     *按照企业统计产品指标表的填报情况
     * @param list
     * @param allList
     * @return
     */
    public static List<Map<String,Object>> getQuota(List<AccessUnionQuotaVo> list,List<AccessUnionVo> allList){
        List<Map<String,Object>> volist = new ArrayList<>();
        for(int i = 0;i< allList.size();i++){
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("index",i+1);
            map.put("tname",allList.get(i).getTname());
            map.put("cmpName",allList.get(i).getCmpName());
            map.put("enterpriseCode",allList.get(i).getEnterpriseCode());
            for (int j = 0 ;j< list.size();j++){
                for(int k = 0;k< MONTHS.length;k++){
                    if(list.get(j).getEnterpriseCode().equals(allList.get(i).getEnterpriseCode())
                            && list.get(j).getRecordDate().substring(5,7).equals(MONTHS[k])){
                        map.put(MONTHS_EN[k],"已填写" );

                    }
                }

            }
            volist.add(map);
        }

        return volist;
    }

    /**
     *按照企业统计产品指标表的填报情况，加上地区参数
     * @param list
     * @param allList
     * @param dictid
     * @return
     */
    public static List<Map<String,Object>> getQuota(List<AccessUnionQuotaVo> list,List<AccessUnionVo> allList,String dictid){
        List<Map<String,Object>> volist = new ArrayList<>();
        int m = 0;
        for(int i = 0;i< allList.size();i++){
            if(code.getAreaMapB().get(dictid).equals(allList.get(i).getTname())){
                m++;
                Map<String,Object> map = new LinkedHashMap<>();
                map.put("index",m);
                map.put("tname",allList.get(i).getTname());
                map.put("cmpName",allList.get(i).getCmpName());
                map.put("enterpriseCode",allList.get(i).getEnterpriseCode());
                for (int j = 0 ;j< list.size();j++){
                    for(int k = 0;k< MONTHS.length;k++){
                        if(list.get(j).getEnterpriseCode().equals(allList.get(i).getEnterpriseCode())
                                && list.get(j).getRecordDate().substring(5,7).equals(MONTHS[k])){
                            map.put(MONTHS_EN[k],"已填写" );

                        }
                    }

                }
                volist.add(map);
            }

        }

        return volist;
    }
}
