package cn.wise.sc.energy.power.plant.business;

import cn.wise.sc.energy.power.plant.business.constant.RealTimeDataChartType;
import cn.wise.sc.energy.power.plant.business.domain.vo.DeviceInfoVo;
import cn.wise.sc.energy.power.plant.business.domain.vo.EntityVo;
import cn.wise.sc.energy.power.plant.business.domain.vo.PlantInfoVo;
import cn.wise.sc.energy.power.plant.business.domain.vo.UnitInfoVo;
import cn.wise.sc.energy.power.plant.business.service.IDeviceInfoService;
import cn.wise.sc.energy.power.plant.business.service.IPlantInfoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 初始化数据类
 * @author: qh
 * @create: 2020-06-01 10:21
 **/
@Component
public class PowerPlantRunner implements ApplicationRunner {

    final
    RedissonClient redissonClient;
    @Autowired
    IDeviceInfoService iDeviceInfoService;
    @Autowired
    IPlantInfoService iPlantInfoService;

    public PowerPlantRunner(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //初始化redis
        initRedis();

    }

    /**
     * 初始化 电厂配置文件信息
     */
    private void initRedis() {

        //将配置首页实时数据的kkscode编码存入到redis  根据机组存
        //key   plantCode:deviceName:图形名字

        //将2号机组的kks存到redis
        List<EntityVo> list = iPlantInfoService.list();
        List<PlantInfoVo> plantInfoVos = new ArrayList<>(list.size());
        list.forEach(arg -> {
            plantInfoVos.add((PlantInfoVo) arg);
        });
        PlantInfoVo plantInfoVo = plantInfoVos.stream()
                .filter(arg -> "TEST".equals(arg.getPlantCode()))
                .findFirst().orElse(null);

        assert plantInfoVo != null;

        //找到一号机组
        List<DeviceInfoVo> deviceInfoVos = iDeviceInfoService.getDeviceUnitAndCharacterPlantCode(plantInfoVo.getPlantId());

        DeviceInfoVo deviceInfoVo1 = deviceInfoVos.stream()
                .filter(arg -> "1号机组".equals(arg.getDeviceName()))
                .findFirst()
                .orElse(null);

        //一号机组的汇总统计
        String electricityKey1 = plantInfoVo.getPlantId() + ":" + deviceInfoVo1.getDeviceName().substring(0, 1) + ":"
                + RealTimeDataChartType.ELECTRICITY_VOLTAGE_CHART.getName();
        List<String> electricityKKS1 = new ArrayList<>();
        deviceInfoVo1.getUnitInfoVos().forEach(units -> {
            units.getCharacters().stream()
                    .filter(characters ->
                            characters.getCpName().contains("F发电机定子A相电流")
                                    || characters.getCpName().contains("F发电机定子AB线电压")
                                    || characters.getCpName().contains("F发电机定子BC线电压")
                                    || characters.getCpName().contains("F发电机定子CA线电压")
                                    || characters.getCpName().contains("F发电机定子B相电流")
                                    || characters.getCpName().contains("F发电机定子C相电流")
                    ).forEach(characters -> {
                electricityKKS1.add(characters.getKksCode());
            });
        });

        //一号机组的振动统计
        String vibrationKey1 = plantInfoVo.getPlantId() + ":" + deviceInfoVo1.getDeviceName().substring(0, 1) + ":"
                + RealTimeDataChartType.VIBRATION_CHART.getName();
        List<String> vibrationKKS1 = new ArrayList<>();
        deviceInfoVo1.getUnitInfoVos().forEach(units -> {
            units.getCharacters().stream()
                    .filter(characters ->
                            "1F汽端座振X".equals(characters.getCpName())
                                    || "1F汽端轴振X".equals(characters.getCpName())
                                    || "1F励端座振X".equals(characters.getCpName())
                                    || "1F励端轴振X".equals(characters.getCpName())
                    ).forEach(characters -> {
                vibrationKKS1.add(characters.getKksCode());
            });
        });

        //一号机组的温度统计
        String temperatureKey1 = plantInfoVo.getPlantId() + ":" + deviceInfoVo1.getDeviceName().substring(0, 1) + ":"
                + RealTimeDataChartType.TEMPERATURE_CHART.getName();
        List<String> temperatureKKS1 = new ArrayList<>();
        deviceInfoVo1.getUnitInfoVos().forEach(units -> {
            units.getCharacters().stream()
                    .filter(characters ->
                            characters.getCpName().contains("F汽端轴瓦温度4")
                                    || characters.getCpName().contains("F汽端轴瓦温度5")
                                    || characters.getCpName().contains("F励端轴瓦温度1")
                                    || characters.getCpName().contains("F励端轴瓦温度2")
                    ).forEach(characters -> {
                temperatureKKS1.add(characters.getKksCode());
            });
        });


        //找到二号机组
        DeviceInfoVo device2 = deviceInfoVos.stream()
                .filter(arg -> "2号机组".equals(arg.getDeviceName()))
                .findFirst()
                .orElse(null);

        //二号机组的汇总统计
        String electricityKey2 = plantInfoVo.getPlantId() + ":" + device2.getDeviceName().substring(0, 1) + ":"
                + RealTimeDataChartType.ELECTRICITY_VOLTAGE_CHART.getName();
        List<String> electricityKKS2 = new ArrayList<>();
        device2.getUnitInfoVos().forEach(units -> {
            units.getCharacters().stream()
                    .filter(characters ->
                            "2F功角".equals(characters.getCpName())
                                    || characters.getCpName().endsWith("运行中H2纯度")
                                    || characters.getCpName().endsWith("发电机机内氢压")
                                    || characters.getCpName().endsWith("漏氢含量")
                    ).forEach(characters -> {
                electricityKKS2.add(characters.getKksCode());
            });
        });

        //二号机组的振动统计
        String vibrationKey2 = plantInfoVo.getPlantId() + ":" + device2.getDeviceName().substring(0, 1) + ":"
                + RealTimeDataChartType.VIBRATION_CHART.getName();
        List<String> vibrationKKS2 = new ArrayList<>();
        device2.getUnitInfoVos().forEach(units -> {
            units.getCharacters().stream()
                    .filter(characters ->
                            "2F汽端座振X".equals(characters.getCpName())
                                    || "2F汽端轴振X".equals(characters.getCpName())
                                    || "2F励端座振X".equals(characters.getCpName())
                                    || "2F励端轴振X".equals(characters.getCpName())
                    ).forEach(characters -> {
                vibrationKKS2.add(characters.getKksCode());
            });
        });


        //二号机组的温度统计
        String temperatureKey2 = plantInfoVo.getPlantId() + ":" + device2.getDeviceName().substring(0, 1) + ":"
                + RealTimeDataChartType.TEMPERATURE_CHART.getName();
        List<String> temperatureKKS2 = new ArrayList<>();
        device2.getUnitInfoVos().forEach(units -> {
            units.getCharacters().stream()
                    .filter(characters ->
                            "2F汽端轴承排油温度3".equals(characters.getCpName())
                                    || "2F汽端轴承排油温度4".equals(characters.getCpName())
                                    || "2F励端轴承排油温度1".equals(characters.getCpName())
                                    || "2F励端轴承排油温度2".equals(characters.getCpName())
                    ).forEach(characters -> {
                temperatureKKS2.add(characters.getKksCode());
            });
        });
        redissonClient.getBucket(electricityKey2).set(JSON.toJSONString(electricityKKS2));
        redissonClient.getBucket(vibrationKey2).set(JSON.toJSONString(vibrationKKS2));
        redissonClient.getBucket(temperatureKey2).set(JSON.toJSONString(temperatureKKS2));
        redissonClient.getBucket(electricityKey1).set(JSON.toJSONString(electricityKKS1));
        redissonClient.getBucket(vibrationKey1).set(JSON.toJSONString(vibrationKKS1));
        redissonClient.getBucket(temperatureKey1).set(JSON.toJSONString(temperatureKKS1));

    }

    /**
     * 根据实时数据图形类型赛选测点kkscode
     *
     * @param units     待赛选的部件集合
     * @param chartType 图类型
     * @return kkscode集合
     */
    private List<String> getUnitInfoVosByChartType(List<UnitInfoVo> units, RealTimeDataChartType chartType) {

        List<String> rts = new ArrayList<>();

        //各机组功率趋势图
        if (chartType.getCode() == 1) {
            for (UnitInfoVo unit : units) {
                unit.getCharacters().stream()
                        .filter(arg -> arg.getCpName().contains("F功率"))
                        .findFirst()
                        .ifPresent(character -> rts.add(character.getKksCode()));
            }
        } else if (chartType.getCode() == 4) {
            for (UnitInfoVo unit : units) {
                unit.getCharacters().stream()
                        .filter(characters ->
                                characters.getCpName().contains("F发电机定子A相电流")
                                        || characters.getCpName().contains("F发电机定子AB线电压")
                                        || characters.getCpName().contains("F发电机定子BC线电压")
                                        || characters.getCpName().contains("F发电机定子CA线电压")
                                        || characters.getCpName().contains("F发电机定子B相电流")
                                        || characters.getCpName().contains("F发电机定子C相电流"))
                        .findFirst()
                        .ifPresent(character -> rts.add(character.getKksCode()));
            }
        } else if (chartType.getCode() == 5) {
            for (UnitInfoVo unit : units) {
                unit.getCharacters().stream()
                        .filter(characters ->
                                "F汽端座振X".equals(characters.getCpName())
                                        || "F汽端轴振X".equals(characters.getCpName())
                                        || "F励端座振X".equals(characters.getCpName())
                                        || "F励端轴振X".equals(characters.getCpName()))
                        .findFirst()
                        .ifPresent(character -> rts.add(character.getKksCode()));
            }
        } else if (chartType.getCode() == 6) {
            for (UnitInfoVo unit : units) {
                unit.getCharacters().stream()
                        .filter(characters ->
                                characters.getCpName().endsWith("F汽端轴瓦温度4")
                                        || characters.getCpName().contains("F汽端轴瓦温度5")
                                        || characters.getCpName().contains("F励端轴瓦温度1")
                                        || characters.getCpName().contains("F励端轴瓦温度2"))
                        .findFirst()
                        .ifPresent(character -> rts.add(character.getKksCode()));
            }
        }
        return rts;
    }

}
