package com.pqs9000.serviceimpl.responsibility;

import com.alibaba.fastjson.JSON;
import com.njcn.pojo.commons.AreaGeneralData;
import com.njcn.pojo.commons.RedisDB;
import com.njcn.utils.UserUtil;
import com.njcn.utils.redis.JedisManager;
import com.pqs9000.mapper.responsibility.AreaOverNormalMapper;
import com.pqs9000.mapper.responsibility.WarningSubstationMapper;
import com.pqs9000.pojo.common.Sub;
import com.pqs9000.pojo.responsibility.WarningSubstation;
import com.pqs9000.service.responsibility.WarningSubstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author xuyang
 * @date 2020/10/9 20:54
 */
@Service
public class WarningSubstationServiceImpl implements WarningSubstationService {

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private WarningSubstationMapper warningSubstationMapper;

    @Autowired
    private AreaOverNormalMapper areaOverNormalMapper;

    @Autowired
    private JedisManager jedisManager;

    @Override
    public List<WarningSubstation> getWarningSubstationData(String startTime, String endTime, String type, String area, String vollevel,boolean statFlag) {
        List<WarningSubstation> subAllList = new ArrayList<>();
        List<AreaGeneralData> ag = new ArrayList<>();
        List<Integer> mlist = new ArrayList<>();
       // ag = userUtil.getAreaDataByInfoRun(area);
        //新增统计类型判断
        if(statFlag){
            ag = userUtil.getAreaDataByInfoRun(area);
        }else {
            //排除不需要计算的监测点
            ag =userUtil.getAreaDataByStatFlagAll(area);
        }
        //将电压等级参数传入sql
        if ("null".equals(vollevel)){
            vollevel = "";
        }
        //判断是不是国网类型监测点
        ag = judgeMonitor(type,ag);
        for (AreaGeneralData areaDate:ag) {
            mlist.addAll(areaDate.getLineIndexs());
        }
        if (!mlist.isEmpty()){
            subAllList = getWarningParam(mlist,startTime,endTime,vollevel);
        }
        //处理数据存入redis，用于导出报表
        String json = JSON.toJSONString(subAllList);
        jedisManager.saveValueByKey(RedisDB.REPORTID,RedisDB.SUBREPORTNAME,json,3600);
        return subAllList;
    }

    //获取具体告警参数
    public List<WarningSubstation> getWarningParam(List<Integer> monitor,String startTime, String endTime,String vollevel){
        List<WarningSubstation> result = new ArrayList<>();
        List<WarningSubstation> list = warningSubstationMapper.getEachIndex(monitor,startTime,endTime,vollevel);
        return list;
    }

    //判断是不是国网监测点
    public List<AreaGeneralData> judgeMonitor(String type,List<AreaGeneralData> ag) {
        if ("南网".equals(type) || "国网".equals(type) || "灿能".equals(type)){
            List<Integer> countryList = new ArrayList<>();
            for (AreaGeneralData typeDate:ag) {
                if (typeDate.getMonitors() !=0 ){
                    countryList = countryMonitor(typeDate.getLineIndexs());
                }
                if (countryList.isEmpty()){
                    typeDate.setMonitors(0);
                } else {
                    typeDate.setMonitors(countryList.size());
                }
                typeDate.setLineIndexs(countryList);
            }
        }
        return ag;
    }

    //获取国网监测点
    public List<Integer> countryMonitor(List<Integer> list){
        return areaOverNormalMapper.getCountryMonitor(list);
    }
}
