package com.njcn.event.service.majornetwork.Impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.event.mapper.majornetwork.SpThroughMapper;
import com.njcn.event.pojo.po.SpThroughPO;
import com.njcn.event.pojo.vo.SpThroughVO;
import com.njcn.event.service.majornetwork.SpThroughService;
import com.njcn.event.pojo.param.SpThroughParam;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.api.DictTreeFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicTreeEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.system.pojo.vo.DictTreeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 高低电压穿越 服务实现类
 *
 * @author guofeihu
 * @since 2024-08-22
 */
@Service
@RequiredArgsConstructor
public class SpThroughServiceImpl extends MppServiceImpl<SpThroughMapper, SpThroughPO> implements SpThroughService {

    private final DictTreeFeignClient dictTreeFeignClient;
    private final DicDataFeignClient dicDataFeignClient;


    @Override
    public SpThroughVO getDataByEventIds(SpThroughParam spThroughParam) {
        SpThroughVO spThroughVO = new SpThroughVO();
        spThroughVO.setLowPressure("0");
        spThroughVO.setHighPressure("0");
        if(CollUtil.isNotEmpty(spThroughParam.getEventIds())){
            String treeId = dicTreeId(spThroughParam);
            DictData dip = dicDataFeignClient.getDicDataByCode(DicDataEnum.VOLTAGE_DIP.getCode()).getData();
            if (ObjectUtil.isNotNull(dip)) {
                spThroughVO.setLowPressure(eventCount(spThroughParam, treeId, dip) + "");
            }
            DictData rise = dicDataFeignClient.getDicDataByCode(DicDataEnum.VOLTAGE_RISE.getCode()).getData();
            if (ObjectUtil.isNotNull(rise)) {
                spThroughVO.setHighPressure(eventCount(spThroughParam, treeId, rise) + "");
            }
        }
        return spThroughVO;
    }

    private Integer eventCount(SpThroughParam spThroughParam, String dictTreeId, DictData rise) {
        LambdaQueryWrapper<SpThroughPO> lowLambdaQueryWrapper = new LambdaQueryWrapper();
        lowLambdaQueryWrapper.in(SpThroughPO::getEventId, spThroughParam.getEventIds())
                .eq(SpThroughPO::getIsOrNot, 1)
                .eq(SpThroughPO::getStationType, dictTreeId)
                .eq(SpThroughPO::getState, 1)
                .eq(SpThroughPO::getEventType, rise.getId());
        return this.baseMapper.selectCount(lowLambdaQueryWrapper);
    }

    /**
     * 说明:
     * 此方法是专门提供给event模块-高低压穿越模块中-根据区域获取暂态事件列表使用
     * 根据区域获取各个子区域高低电压穿越次数是基于sp_through表中记录来的 那么在根据区域获取暂态事件列表数据也应该基于sp_through表中记录来
     * 因为如果不基于sp_through表中记录来 那么根据区域获取暂态事件可能数据非常多且可能一部分的事件数据并没有被定时任务(record方法)所执行
     *
     * @param spThroughParam
     * @return
     */
    @Override
    public List<String> formatEventIds(SpThroughParam spThroughParam) {
        LambdaQueryWrapper<SpThroughPO> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.in(SpThroughPO::getEventId, spThroughParam.getEventIds())
                .eq(StrUtil.isNotBlank(spThroughParam.getFrequencyType()),SpThroughPO::getEventType,spThroughParam.getFrequencyType())
                .eq(SpThroughPO::getIsOrNot, 1)
                .eq(SpThroughPO::getStationType, dicTreeId(spThroughParam))
                .eq(SpThroughPO::getState, 1);
        List<SpThroughPO> spThroughPOS = this.baseMapper.selectList(lambdaQueryWrapper);
        return spThroughPOS.stream().map(SpThroughPO::getEventId).collect(Collectors.toList());
    }

    private String dicTreeId(SpThroughParam spThroughParam) {
        String dictTreeId = "";
        if ("1".equals(spThroughParam.getStationType())) {
            DictTreeVO windFarms = dictTreeFeignClient.queryByCode(DicTreeEnum.Wind_Farms.getCode()).getData();
            if (ObjectUtil.isNotNull(windFarms)) {
                dictTreeId = windFarms.getId();
            }
        } else {
            DictTreeVO powerStation = dictTreeFeignClient.queryByCode(DicTreeEnum.Power_Station.getCode()).getData();
            if (ObjectUtil.isNotNull(powerStation)) {
                dictTreeId = powerStation.getId();
            }
        }
        return dictTreeId;
    }
}
