package com.nantian.cloud.dbcapp.service.impl;

import com.nantian.cloud.dbcapp.bean.*;
import com.nantian.cloud.dbcapp.mapper.record.PublicInstutionNumMapper;
import com.nantian.cloud.dbcapp.service.PublicInstutionNumService;
import com.nantian.cloud.dbcapp.tool.ListComparetor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@Service
public class PublicInstutionNumServiceImpl implements PublicInstutionNumService {
    @Autowired
    PublicInstutionNumMapper publicInstutionNumMapper;

    @Override
    public PublicInstitutionDomain getPublicInstitutionDomain(String area, String instution, String time) {
        if((area.isEmpty()||instution.isEmpty()||time.isEmpty())||(("请选择地区").equals(area)||("请选择机构").equals(instution)||("请选择时间").equals(time))){
         return null;
        }else{
        String code=this.converConditionArea(area);
        String instutionCode=this.converConditionInstution(instution);
        PublicInstitutionDomain publicInstitutionDomain=new PublicInstitutionDomain();
        PublicInstitutionNumDomain publicInstitutionNumDomain=this.getPublicInstutionNum(code,instutionCode,time);
        PublicInstitutionAverage publicInstitutionAverage=this.getPublicInstitutionAverageNum(code,instutionCode,time);
        if((null==publicInstitutionNumDomain)||(null==publicInstitutionAverage)){
            publicInstitutionDomain=null;
        }else{
            publicInstitutionDomain.setPublicInstitutionNum(publicInstitutionNumDomain.getPublicInstitutionNum());
            publicInstitutionDomain.setComprehensiveEnergyConsumption(publicInstitutionNumDomain.getComprehensiveEnergyConsumption());
            publicInstitutionDomain.setF103(publicInstitutionAverage.getF103());
            publicInstitutionDomain.setF503(publicInstitutionAverage.getF503());
            publicInstitutionDomain.setF504(publicInstitutionAverage.getF504());
        }
        return publicInstitutionDomain;
        }
    }

    @Override
    public PublicInstitutionNumDomain getPublicInstutionNum(String area, String instution, String time) {
        PublicInstitutionNumDomain publicInstutionNum=publicInstutionNumMapper.getPublicInstutionNum(area,instution,time);
        return publicInstutionNum;
    }//getPublicInstutionNum end

    @Override
    public PublicInstitutionAverage getPublicInstitutionAverageNum(String area, String instution, String time) {
        PublicInstitutionAverage publicInstitutionAverage=publicInstutionNumMapper.getPublicInstitutionAverageNum(area,instution,time);
        return publicInstitutionAverage;
    }

    @Override
    public List<Object> getEveryAreaData(String institutionarg , String time ) {
        List<Object> listTotal=new ArrayList<>();

        String institution=this.converConditionInstution(institutionarg);
        //todo 取地区名称和地区编码
        List<PublicInstutionArea> listArea=this.getPublicInstutionArea();
        for(PublicInstutionArea listAreaItem:listArea){
            listAreaItem.setCode(this.converConditionArea02(listAreaItem.getCode()));
            listAreaItem.setName(this.converAreaName(listAreaItem.getName()));
        }

        //todo 根据行业和时间查询出所有地区的数据
        List<PublicInstitutionDomain> listPublicInstitutionDomain=this.getPublicInstitutionDomainSub(institution , time,listArea);
        //todo  根据行业循环将各地区的数据add到一个list子项中 然后将list子项add到最大的listTotal中
        /**综合能耗 start*/
        List<PublicInstutionAllArea> listPublicInstutionAllArea1=new ArrayList<>();
        int i=0;
        for(PublicInstitutionDomain item:listPublicInstitutionDomain){
            PublicInstutionAllArea publicInstutionAllArea1=new PublicInstutionAllArea();
            publicInstutionAllArea1.setArea(item.getName());
            publicInstutionAllArea1.setAreaValue(item.getComprehensiveEnergyConsumption());
            publicInstutionAllArea1.setPublicIns(item.getPublicInstitutionNum());
            listPublicInstutionAllArea1.add(i,publicInstutionAllArea1);
            i=i+1;
        }//for end
        Collections.sort(listPublicInstutionAllArea1,new ListComparetor());
        Collections.reverse(listPublicInstutionAllArea1);
        listTotal.add(0,listPublicInstutionAllArea1);
        /**综合能耗 end*/

        /**用能人数 start*/
        List<PublicInstutionAllArea> listPublicInstutionAllArea2=new ArrayList<>();
        int j=0;
        for(PublicInstitutionDomain item:listPublicInstitutionDomain){
            PublicInstutionAllArea publicInstutionAllArea2=new PublicInstutionAllArea();
            publicInstutionAllArea2.setArea(item.getName());
            publicInstutionAllArea2.setAreaValue(item.getF103());
            publicInstutionAllArea2.setPublicIns(item.getPublicInstitutionNum());
            listPublicInstutionAllArea2.add(j,publicInstutionAllArea2);
            j=j+1;
        }//for end
        Collections.sort(listPublicInstutionAllArea2,new ListComparetor());
        Collections.reverse(listPublicInstutionAllArea2);
        listTotal.add(1,listPublicInstutionAllArea2);
        /**用能人数 end*/

        /**人均水耗 start*/
        List<PublicInstutionAllArea> listPublicInstutionAllArea3=new ArrayList<>();
        int k=0;
        for(PublicInstitutionDomain item:listPublicInstitutionDomain){
            PublicInstutionAllArea publicInstutionAllArea3=new PublicInstutionAllArea();
            publicInstutionAllArea3.setArea(item.getName());
            publicInstutionAllArea3.setAreaValue(item.getF503());
            publicInstutionAllArea3.setPublicIns(item.getPublicInstitutionNum());
            listPublicInstutionAllArea3.add(k,publicInstutionAllArea3);
            k=k+1;
        }//for end
        Collections.sort(listPublicInstutionAllArea3,new ListComparetor());
        Collections.reverse(listPublicInstutionAllArea3);
        listTotal.add(2,listPublicInstutionAllArea3);
        /**人均水耗 end*/

        /**人均面积 start*/
        List<PublicInstutionAllArea> listPublicInstutionAllArea4=new ArrayList<>();
        int l=0;
        for(PublicInstitutionDomain item:listPublicInstitutionDomain){
            PublicInstutionAllArea publicInstutionAllArea4=new PublicInstutionAllArea();
            publicInstutionAllArea4.setArea(item.getName());
            publicInstutionAllArea4.setAreaValue(item.getF504());
            publicInstutionAllArea4.setPublicIns(item.getPublicInstitutionNum());
            listPublicInstutionAllArea4.add(l,publicInstutionAllArea4);
            l=l+1;
        }//for end
        Collections.sort(listPublicInstutionAllArea4,new ListComparetor());
        Collections.reverse(listPublicInstutionAllArea4);
        listTotal.add(3,listPublicInstutionAllArea4);
        /**人均面积end*/

        return listTotal;
    }//getEveryAreaData end

    @Override
    public List<Object> getChartData() {
        float zl_zhnh_2020=91.0f;//
        float zl_szy_2020=36952.0f;//

        float zb_rjzhnh_2020=2.16f;//
        float zb_rjsh_2020=20.46f;//
        float zb_dwjzmj_2020=3.86f;//

        PublicInstitutionChart publicInstitutionChart=new PublicInstitutionChart();
        publicInstitutionChart.setTime("2020");
        publicInstitutionChart.setType("zl_zhnh_2020");
        publicInstitutionChart.setValue(90.0f);
        //todo  两总三指标的现期数据
        List<PublicInstitutionChart> listNow=new LinkedList<>();
        listNow.add(publicInstitutionChart);

        //todo  计算变化率 封装前端需要的数据等。

        return null;
    }

    public List<PublicInstutionArea> getPublicInstutionArea(){
       List<PublicInstutionArea> listArea=new ArrayList<>();
       listArea=publicInstutionNumMapper.queryArea();
        return listArea;
   }//getPublicInstutionArea end

    public List<PublicInstitutionDomain> getPublicInstitutionDomainSub(String institution , String time,List<PublicInstutionArea> listArea){
        List<PublicInstitutionDomain> listPublicInstitutionDomain=new ArrayList<>();
        List<PublicInstitutionNumDomain> listPublicInstitutionNumDomain=publicInstutionNumMapper.getPublicInstitutionNumDomain(institution,time);
        List<PublicInstitutionAverage> listPublicInstitutionAverage=publicInstutionNumMapper.getPublicInstitutionAverage(time);
        int i=listPublicInstitutionNumDomain.size();
        int j=listPublicInstitutionAverage.size();
        if(i!=0||j!=0){
            for(int k=0;k<listArea.size();k++){
                PublicInstitutionDomain publicInstitutionDomain=new PublicInstitutionDomain();
                for(PublicInstitutionNumDomain item:listPublicInstitutionNumDomain){
                   if(item.getCode().equals(listArea.get(k).getCode())){
                       publicInstitutionDomain.setPublicInstitutionNum(item.getPublicInstitutionNum());
                       publicInstitutionDomain.setComprehensiveEnergyConsumption(item.getComprehensiveEnergyConsumption());
                       publicInstitutionDomain.setName(listArea.get(k).getName());
                       break;
                   }else {
                       publicInstitutionDomain.setName("没有数据");
                       publicInstitutionDomain.setPublicInstitutionNum("没有数据");
                       publicInstitutionDomain.setComprehensiveEnergyConsumption("没有数据");
                   }
                }
                for(PublicInstitutionAverage item:listPublicInstitutionAverage){
                    if(item.getCode().equals(listArea.get(k).getCode())){
                        publicInstitutionDomain.setF103(item.getF103());
                        publicInstitutionDomain.setF503(item.getF503());
                        publicInstitutionDomain.setF504(item.getF504());
                        break;
                    }else {
                        publicInstitutionDomain.setF103("没有数据");
                        publicInstitutionDomain.setF503("没有数据");
                        publicInstitutionDomain.setF504("没有数据");
                    }
                }
                try{
                    listPublicInstitutionDomain.add(k,publicInstitutionDomain);
                }catch (Exception e){
                    System.out.println(e);
                }

            }
            return listPublicInstitutionDomain;
        }else {
           return null;
        }

        //return listPublicInstitutionDomain;
    }//getPublicInstitutionDomain end

    public String converConditionInstution(String instution){
        String instutionCode="";
        if(instution.equals("党政机关")){
            instutionCode="01";
        }else if(instution.equals("事业单位")){
            instutionCode="02";
        }else if(instution.equals("教育类")){
            instutionCode="021";
        }else if(instution.equals("科技类")){
            instutionCode="022";
        }else if(instution.equals("文化类")){
            instutionCode="023";
        }else if(instution.equals("卫生类")){
            instutionCode="024";
        }else if(instution.equals("体育类")){
            instutionCode="025";
        }else if(instution.equals("其它类")){
            instutionCode="026";
        }else if(instution.equals("团体组织")){
            instutionCode="03";
        }else {
            instutionCode=null;
        }
        return instutionCode;
    }//converConditionInstution end

    public String converConditionArea(String area){
          String code=publicInstutionNumMapper.getPublicInstitutionArea(area);
          if(code.substring(2).equals("0000")){
              code=code.substring(0,2);
          }else if(code.substring(4).equals("00")){
              code=code.substring(0,4);
          }else{
              code=code;
          }
           return code;
    }//converConditionArea end

    public String converConditionArea02(String code){
        if(code.substring(2).equals("0000")){
            code=code.substring(0,2);
        }else if(code.substring(4).equals("00")){
            code=code.substring(0,4);
        }else{
            code=code;
        }
        return code;
    }//converConditionArea02 end

    public String converAreaName(String code){
            code=code.substring(0,2);
             return code;
    }//converAreaName end

}
