package com.nantian.cloud.dbcapp.service.impl;
import com.nantian.cloud.dbcapp.mapper.record.MonitorOnlineMapper;
import com.nantian.cloud.dbcapp.service.EnergyService;
import com.nantian.cloud.dbcapp.service.MonitorOnlineService;
import com.nantian.cloud.dbcapp.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * <p>
 * 综合能耗 服务实现类
 * </p>
 *
 * @author zfw
 * @since 2020-08-05
 */
@Service("MonitorOnlineService")
public class MonitorOnlineServiceImpl implements MonitorOnlineService {

    @Autowired
    private MonitorOnlineMapper monitorOnlineMapper;

    @Autowired
    private EnergyService energyService;

    /**
    * @Description: 根据不同的条件查询综合能耗
    * @Param: [area, industry, time]
    * @return: java.lang.Float
    * @Author: zfw
    * @Date: 2020/12/26
    * @Date: 15:00
    */
    @Override
    public  List<MonitorOnlineVO> getZHNHByCompany(String area, String industry, String time) {
        //截取时间
        String startTime = null;
        String endTime = null;
        if (time.contains("-")) { //跨年查询
             startTime = StringUtils.isNotBlank(time.split("-")[0]) ? time.split("-")[0] : time.split("-")[1] +"-01";
             endTime = time.split("-")[1] +"-12";
        }else{ //单个年份
            startTime = time+"-01";
            endTime = time+"-12";
        }

        List<String> companyList = companyList2( area, industry, startTime, endTime);
        //查询能耗
        List<MonitorOnlineVO> list = new ArrayList<>();
        if(companyList!=null && companyList.size()>0){
            list = monitorOnlineMapper.foundZhnhByList( companyList, startTime, endTime);
        }
        return list;
    }

    @Override
    public List<MonitorOnlineTotalVO> getTotalZHNH(String[] area, String[] industry, String time) {
        List<MonitorOnlineTotalVO> list = new ArrayList<>();
        String code = "";
        //截取时间
        String startTime = null;
        String endTime = null;
        String[] timeTre = new String[2];
        List<String> companyList = new ArrayList<>();
        if (time.contains("-")) {
            timeTre[0] = time.split("-")[0];
            timeTre[1] = time.split("-")[1];
            startTime = timeTre[0] + "-01";
            endTime = timeTre[1] + "-12";

        } else {//2019
            timeTre[0] = time;
            startTime = timeTre[0] + "-01";
            endTime = timeTre[0] + "-12";
        }

        for (int y = 0; y < timeTre.length; y++) { //循环年
            startTime = timeTre[y] + "-01";
            endTime = timeTre[y] + "-12";
            for (int i = 0; i < area.length; i++) {
                String areaTmp = area[i].replaceAll("0000", "").replaceAll("00", "");
                HashMap<String, String> areaAndIndustryCode = energyService.getAreaAndIndustryCode(areaTmp, null);
                code = areaAndIndustryCode.get("地区");
                //通过code查询企业
                for (int j = 0; j < industry.length; j++) {
                    companyList = companyList1(area[i], industry[j], startTime, endTime);
                    //得到行业名称和行业code
                    CoalVo ca = monitorOnlineMapper.getCodeAndName(industry[j]);
                    //查询能耗
                    if (companyList != null && companyList.size() > 0) {
                        MonitorOnlineTotalVO totalVO = monitorOnlineMapper.foundTotalZhnh(companyList, startTime, endTime);
                        totalVO.setArea(area[i]);
                        totalVO.setAreaName(code);
                        totalVO.setYear(timeTre[y]);
                        if (ca != null) {
                            totalVO.setTrade(ca.getValue());
                            totalVO.setTradeName(ca.getMonth());
                        } else {
                            totalVO.setTrade("");
                            totalVO.setTradeName("全部");
                        }

                        list.add(totalVO);
                    }

                }
            }
        }

        //匹配地区 昆明市、曲靖市、玉溪市、昭通市、丽江市、普洱市、保山市、临沧市、楚雄州、红河州、迪庆州、文山州、西双版纳州、大理州、德宏州、怒江州
        //查询能耗
        //MonitorOnlineTotalVO totalVO =monitorOnlineMapper.foundTotalZhnh(companyList,startTime,endTime);

        //totalVO.setContent(area+"/"+industry);
        return list;
    }


    public  List<String> companyList1(String area, String industry, String startTime,String endTime){

/*        //截取时间
        String startTime=null;
        String endTime=null;
        if (time.contains("-")) {
            startTime = time.split("-")[0] +"-01";
            endTime = time.split("-")[1] +"-12";

        }else{//2019
            startTime= time+"-01";
            endTime= time+"-12";
        }*/

        if (area != null && area.length()>0){
            if (area.contains("530000")){
                area=null;
            }else {
                area = area.substring(0,4);
            }
        }

        if (industry != null && industry.length()>0){
            industry=industry;
            if (industry.contains("全部")){
                industry=null;
            }else{

            }
        }
         List<String> companyList = monitorOnlineMapper.getAllCompanyList(area,industry,startTime,endTime);


        return companyList;

    }
    public  List<String> companyList2( String area, String industry, String startTime, String endTime){

        if (StringUtils.isNotBlank(area)){
            area = area.contains("530000") ? null : area.trim().substring(0,4);
        }

        if (StringUtils.isNotBlank(industry)){
            industry = industry.contains("全部") ? null : industry;
        }

        List<String> companyList = monitorOnlineMapper.getAllCompanyList( area, industry, startTime, endTime);
        return companyList;

    }

}

