package com.gyee.runeconomy.initalcache;/*
@author   谢生杰
@date   2022/11/3-9:14
*/

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.gyee.runeconomy.model.auto.*;
import com.gyee.runeconomy.service.auto.*;
import com.gyee.runeconomy.util.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CacheService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private IProBasicEquipmentService windturbineService;
    @Autowired
    private IProBasicLineService lineService;
    @Autowired
    private IProBasicProjectService projectService;
    @Resource
    private IProBasicSubStationService subStationService;
    @Autowired
    private IProBasicPowerstationService windpowerstationService;
    @Autowired
    private IProBasicEquipmentPointService windturbinetestingpointnewService;
    @Autowired
    private IProBasicPowerstationPointService windpowerstationpointnewService;
    @Autowired
    private IProBasicCompanyService companyService;
    @Autowired
    private IProBasicRegionService regionsService;
    @Resource
    private IProBasicModelPowerRdService proBasicModelPowerRdService;
    @Resource
    private IProBasicStatusPointService proBasicStatusPointService;
    @Resource
    private IProBasicWeatherStationService proBasicWeatherStationService;
    @Resource
    private IProBasicSquareService proBasicSquareService;
    @Resource
    private IProBasicBranchService proBasicBranchService;
    @Resource
    private IProBasicEnergyGroupService energyGroupService;
    //    @Resource
    //    private IProBasicSubStationTestpointService proBasicSubStationTestpointService;

    public void initRedisCache() {
        redisService.select(10);
        log.info("--------------------------redisWT");
        //        List<ProBasicEquipment> windturbineList = windturbineService.list().stream().filter(i->(
        //                i.getWindpowerstationId().equals("SXJ_KGDL_XWT_FDC_STA")
        //                        ||
        //                i.getWindpowerstationId().equals("SXJ_KGDL_GJY_FDC_STA") ||
        //                        i.getWindpowerstationId().equals("SXJ_KGDL_NJL_FDC_STA") ||
        //                        i.getWindpowerstationId().equals("SXJ_KGDL_PDL_FDC_STA")  ||
        //                        i.getWindpowerstationId().equals("SXJ_KGDL_ZK_FDC_STA")
        //        ) &&  i.getIsable().equals(1)).collect(Collectors.toList());

        List<ProBasicEquipment> windturbineList = windturbineService.list().stream().filter(i -> i.getIsable().equals(1)).collect(Collectors.toList());

        log.info("--------------------------windturbineList" + windturbineList.size());
        windturbineList.stream().forEach(i -> {
            Map<String, ProBasicEquipmentPoint> codeaimap = new HashMap<>();
            QueryWrapper<ProBasicEquipmentPoint> qw = new QueryWrapper<>();
            qw.eq("windturbine_id", i.getId());
            List<ProBasicEquipmentPoint> windturbinetestingpointai2List = windturbinetestingpointnewService.list(qw);
            log.info("--------------------------windturbinetestingpointai2List" + windturbinetestingpointai2List.size());
            windturbinetestingpointai2List.stream().forEach(x -> {
                codeaimap.put(x.getUniformCode(), x);
            });
            log.info("--------------------------codeaimap" + codeaimap.size());
            String s = JSONObject.toJSONString(codeaimap);
            redisService.set(i.getId(), s);
        });


        log.info("--------------------------redisSQ");
        List<ProBasicSquare> squareList = proBasicSquareService.list().stream().filter(i -> i.getIsAble().equals(1)).collect(Collectors.toList());
        squareList.stream().forEach(i -> {
            Map<String, ProBasicPowerstationPoint> codeaimap = new HashMap<>();
            QueryWrapper<ProBasicPowerstationPoint> qw = new QueryWrapper<>();
            qw.eq("windpowerstation_id", i.getId());
            List<ProBasicPowerstationPoint> windpowerstationtestingpoint2List = windpowerstationpointnewService.list(qw);
            windpowerstationtestingpoint2List.stream().forEach(x -> {
                codeaimap.put(x.getUniformCode(), x);
            });
            String s = JSONObject.toJSONString(codeaimap);
            redisService.set(i.getId(), s);
        });

        log.info("--------------------------redisLN");
        List<ProBasicLine> lineList = lineService.list().stream().filter(i -> i.getIsAble().equals(1)).collect(Collectors.toList());
        lineList.stream().forEach(i -> {
            Map<String, ProBasicPowerstationPoint> codeaimap = new HashMap<>();
            QueryWrapper<ProBasicPowerstationPoint> qw = new QueryWrapper<>();
            qw.eq("windpowerstation_id", i.getId());
            List<ProBasicPowerstationPoint> windpowerstationtestingpoint2List = windpowerstationpointnewService.list(qw);
            windpowerstationtestingpoint2List.stream().forEach(x -> {
                codeaimap.put(x.getUniformCode(), x);
            });
            String s = JSONObject.toJSONString(codeaimap);
            redisService.set(i.getId(), s);
        });


        log.info("--------------------------redisPJ");
        List<ProBasicProject> projectList = projectService.list().stream().filter(i -> i.getIsAble().equals(1)).collect(Collectors.toList());
        projectList.stream().forEach(i -> {
            Map<String, ProBasicPowerstationPoint> codeaimap = new HashMap<>();
            QueryWrapper<ProBasicPowerstationPoint> qw = new QueryWrapper<>();
            qw.eq("windpowerstation_id", i.getId());
            List<ProBasicPowerstationPoint> windpowerstationtestingpoint2List = windpowerstationpointnewService.list(qw);
            windpowerstationtestingpoint2List.stream().forEach(x -> {
                codeaimap.put(x.getUniformCode(), x);
            });
            String s = JSONObject.toJSONString(codeaimap);
            redisService.set(i.getId(), s);
        });

        //        log.info("--------------------------redisSubWP");
        //        redisService.select(9);
        //        List<ProBasicSubStation> subStationList = subStationService.list().stream().filter(i->i.getIsAble().equals(1)).collect(Collectors.toList());
        //        subStationList.stream().forEach(i->{
        //            Map<String, ProBasicSubStationTestpoint> codeaimap = new HashMap<>();
        //            QueryWrapper<ProBasicSubStationTestpoint> qw = new QueryWrapper<>();
        //            qw.eq("windpowerstation_id",i.getId());
        //            List<ProBasicSubStationTestpoint> windpowerstationtestingpoint2List = proBasicSubStationTestpointService.list(qw);
        //            windpowerstationtestingpoint2List.stream().forEach(x->{
        //                codeaimap.put(x.getUniformCode(),x);
        //            });
        //            String s = JSONObject.toJSONString(codeaimap);
        //            redisService.set(i.getId(),s);
        //        });

        log.info("--------------------------redisWeatherWP");
        List<ProBasicWeatherStation> weatherStationList = proBasicWeatherStationService.list().stream().filter(i -> i.getIsAble().equals(1)).collect(Collectors.toList());
        weatherStationList.stream().forEach(i -> {
            Map<String, ProBasicPowerstationPoint> codeaimap = new HashMap<>();
            QueryWrapper<ProBasicPowerstationPoint> qw = new QueryWrapper<>();
            qw.eq("windpowerstation_id", i.getId());
            List<ProBasicPowerstationPoint> windpowerstationtestingpoint2List = windpowerstationpointnewService.list(qw);
            windpowerstationtestingpoint2List.stream().forEach(x -> {
                codeaimap.put(x.getUniformCode(), x);
            });
            String s = JSONObject.toJSONString(codeaimap);
            redisService.set(i.getId(), s);
        });

        log.info("--------------------------redisWP");
        List<ProBasicPowerstation> wpList = windpowerstationService.list().stream().filter(i -> i.getIsAble().equals(1)).collect(Collectors.toList());

        wpList.stream().forEach(i -> {
            Map<String, ProBasicPowerstationPoint> codeaimap = new HashMap<>();
            QueryWrapper<ProBasicPowerstationPoint> qw = new QueryWrapper<>();
            qw.eq("windpowerstation_id", i.getId());
            List<ProBasicPowerstationPoint> windpowerstationtestingpoint2List = windpowerstationpointnewService.list(qw);
            windpowerstationtestingpoint2List.stream().forEach(x -> {
                codeaimap.put(x.getUniformCode(), x);
            });
            String s = JSONObject.toJSONString(codeaimap);
            redisService.set(i.getId(), s);
        });

        log.info("--------------------------redisCOMPANY");
        List<ProBasicCompany> coms = companyService.list().stream().filter(i -> i.getIsAble().equals(1)).collect(Collectors.toList());
        //        List<String> comList = coms.stream().map(i->i.getId()).collect(Collectors.toList());
        List<String> comList = new ArrayList<>();
        coms.stream().forEach(company -> {
            comList.add(company.getId() + "0");
            comList.add(company.getId() + "-1");
            comList.add(company.getId() + "-2");
        });
        comList.stream().forEach(i -> {
            Map<String, ProBasicPowerstationPoint> codeaimap = new HashMap<>();
            QueryWrapper<ProBasicPowerstationPoint> qw = new QueryWrapper<>();
            qw.eq("windpowerstation_id", i);
            List<ProBasicPowerstationPoint> windpowerstationtestingpoint2List = windpowerstationpointnewService.list(qw);
            windpowerstationtestingpoint2List.stream().forEach(x -> {
                codeaimap.put(x.getUniformCode(), x);
            });
            String s = JSONObject.toJSONString(codeaimap);
            redisService.set(i, s);
        });

        log.info("--------------------------redisREGION");
        List<ProBasicRegion> regs = regionsService.list().stream().filter(i -> i.getIsAble().equals(1)).collect(Collectors.toList());
        List<String> regList = new ArrayList<>();
        //        List<String> regList = regs.stream().map(i->i.getId()).collect(Collectors.toList());
        regs.stream().forEach(region -> {
            regList.add(region.getId() + "0");
            regList.add(region.getId() + "-1");
            regList.add(region.getId() + "-2");
        });
        regList.stream().forEach(i -> {
            Map<String, ProBasicPowerstationPoint> codeaimap = new HashMap<>();
            QueryWrapper<ProBasicPowerstationPoint> qw = new QueryWrapper<>();
            qw.eq("windpowerstation_id", i);
            List<ProBasicPowerstationPoint> windpowerstationtestingpoint2List = windpowerstationpointnewService.list(qw);
            windpowerstationtestingpoint2List.stream().forEach(x -> {
                codeaimap.put(x.getUniformCode(), x);
            });
            String s = JSONObject.toJSONString(codeaimap);
            redisService.set(i, s);
        });
        log.info("--------------------------redisGroup");
        List<ProBasicEnergyGroup> groups = energyGroupService.list().stream().filter(i -> i.getIsAble().equals(1) && !i.getParentid().equals("0")).collect(Collectors.toList());
        List<String> groupList = new ArrayList<>();
        //        List<String> regList = regs.stream().map(i->i.getId()).collect(Collectors.toList());
        groups.stream().forEach(group -> {
            groupList.add(group.getId() + "0");
            groupList.add(group.getId() + "-1");
            groupList.add(group.getId() + "-2");
        });
        groupList.stream().forEach(i -> {
            Map<String, ProBasicPowerstationPoint> codeaimap = new HashMap<>();
            QueryWrapper<ProBasicPowerstationPoint> qw = new QueryWrapper<>();
            qw.eq("windpowerstation_id", i);
            List<ProBasicPowerstationPoint> windpowerstationtestingpoint2List = windpowerstationpointnewService.list(qw);
            windpowerstationtestingpoint2List.stream().forEach(x -> {
                codeaimap.put(x.getUniformCode(), x);
            });
            String s = JSONObject.toJSONString(codeaimap);
            redisService.set(i, s);
        });
        redisService.select(10);
        log.info("--------------------------redis理论保证功率");
        Map<String, Map<Double, ProBasicModelPowerRd>> powerrdMap = new HashMap<>();

        List<ProBasicModelPowerRd> proBasicModelPowerRds = proBasicModelPowerRdService.list();
        proBasicModelPowerRds.stream().forEach(powerrd -> {
            if (powerrdMap.containsKey(powerrd.getModelId())) {
                powerrdMap.get(powerrd.getModelId()).put(powerrd.getSpeed(), powerrd);
            } else {
                Map<Double, ProBasicModelPowerRd> powerMap = new HashMap<>();
                powerMap.put(powerrd.getSpeed(), powerrd);
                powerrdMap.put(powerrd.getModelId(), powerMap);
            }
        });
        redisService.set("ZLLGL", JSONObject.toJSONString(powerrdMap));

        log.info("--------------------------redis十三种状态");
        Map<String, List<ProBasicStatusPoint>> sszztMap = new HashMap<>();
        List<ProBasicStatusPoint> windturbinestatusdis = proBasicStatusPointService.list();
        windturbinestatusdis.stream().forEach(w -> {
            if (sszztMap.containsKey(w.getWindturbineId())) {
                sszztMap.get(w.getWindturbineId()).add(w);
            } else {
                List<ProBasicStatusPoint> wdisList = new ArrayList<>();
                wdisList.add(w);
                sszztMap.put(w.getWindturbineId(), wdisList);
            }
        });
        redisService.set("SSZZT", JSONObject.toJSONString(sszztMap));

        log.info("--------------------------完成");
    }
}
