package com.njcn.device.pms.service.majornetwork.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.device.pms.mapper.majornetwork.RStatBusbarHarmonicMapper;
import com.njcn.device.pms.pojo.dto.GeneratrixAndPowerStationDTO;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.param.PmsGeneratrixParam;
import com.njcn.device.pms.pojo.param.UniversalFrontEndParam;
import com.njcn.device.pms.pojo.po.RStatBusbarHarmonicYPO;
import com.njcn.device.pms.pojo.vo.RStatBusbarHarmonicYVO;
import com.njcn.device.pms.service.majornetwork.IGeneratrixWireService;
import com.njcn.device.pms.service.majornetwork.IPmsGeneralDeviceService;
import com.njcn.device.pms.service.majornetwork.RStatBusbarHarmonicService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 变电站母线电压指标年报业务层实现类
 *
 * @author yzh
 * @date 2022/10/8
 */

@Service
@RequiredArgsConstructor
@Slf4j
public class RStatBusbarHarmonicServiceImpl extends ServiceImpl<RStatBusbarHarmonicMapper, RStatBusbarHarmonicYVO> implements RStatBusbarHarmonicService {

    private final RStatBusbarHarmonicMapper rStatBusbarHarmonicMapper;

    private final IGeneratrixWireService generatrixWireService;

    private final IPmsGeneralDeviceService iPmsGeneralDeviceService;

    /**
     * 获取变电站母线电压指标年报
     *
     * @param param 前端参数
     * @return 变电站母线电压指标年报返回前端实体类
     */
    @Override
    public List<RStatBusbarHarmonicYVO> getRStatBusbarHarmonic(UniversalFrontEndParam param) {
        // 获取当前用户的部门的子部门信息
        PmsDeviceInfoParam pmsDeviceInfoParam = new PmsDeviceInfoParam();
        pmsDeviceInfoParam.setDeptIndex(param.getId());
        pmsDeviceInfoParam.setStatisticalType(new SimpleDTO());
        List<PmsGeneralDeviceDTO> data = iPmsGeneralDeviceService.getPmsDeviceInfoWithInOrg(pmsDeviceInfoParam);
        if (CollUtil.isEmpty(data)) {
            return Collections.emptyList();
        }

        // 创建集合封装返回数据
        List<RStatBusbarHarmonicYVO> result = new ArrayList<>();

        for (PmsGeneralDeviceDTO dto : data) {
            if (CollUtil.isEmpty(dto.getGeneratrixIdList())) {
                continue;
            }
            PmsGeneratrixParam pmsGeneratrixParam = new PmsGeneratrixParam();
            pmsGeneratrixParam.setGeneratrixIds(dto.getGeneratrixIdList());
            pmsGeneratrixParam.setPowerStationVoltageLevel(param.getVoltageLevel());
            List<GeneratrixAndPowerStationDTO> dtoS = generatrixWireService.getGeneratrixAndPowerStationInfo(pmsGeneratrixParam);

            // 通过母线id查询变电站背景谐波年表
            List<RStatBusbarHarmonicYPO> info = rStatBusbarHarmonicMapper.getRStatBusbarHarmonic(param.getStartTime(), param.getEndTime(), dto.getGeneratrixIdList());


            for (GeneratrixAndPowerStationDTO generatrixAndPowerStationDTO : dtoS) {
                boolean flag = false;
                RStatBusbarHarmonicYVO vo = new RStatBusbarHarmonicYVO();
                vo.setBusbarName(generatrixAndPowerStationDTO.getGeneratrixName());
                vo.setSubId(generatrixAndPowerStationDTO.getPowerId());
                vo.setSubName(generatrixAndPowerStationDTO.getPowerName());
                vo.setSubVoltageLevel(generatrixAndPowerStationDTO.getPowerVoltageLevel());
                vo.setDeptName(generatrixAndPowerStationDTO.getOrgName());
                for (RStatBusbarHarmonicYPO po : info) {
                    if (generatrixAndPowerStationDTO.getGeneratrixId().equals(po.getBusbarId())){
                        vo.setBusbarId(po.getBusbarId());
                        vo.setDataDate(po.getDataDate());
                        vo.setVEffectiveMax(po.getVEffectiveMax());
                        vo.setVEffectiveMin(po.getVEffectiveMin());
                        vo.setVEffectiveAvg(po.getVEffectiveAvg());
                        vo.setVEffective95(po.getVEffective95());
                        vo.setVThdMax(po.getVThdMax());
                        vo.setVThdMin(po.getVThdMin());
                        vo.setVThdAvg(po.getVThdAvg());
                        vo.setVThd95(po.getVThd95());
                        vo.setUnbalanceMax(po.getUnbalanceMax());
                        vo.setUnbalanceMin(po.getUnbalanceMin());
                        vo.setUnbalanceAvg(po.getUnbalanceAvg());
                        vo.setUnbalance95(po.getUnbalance95());
                        vo.setFlickerMax(po.getFlickerMax());
                        vo.setFlickerMin(po.getFlickerMin());
                        vo.setFlickerAvg(po.getFlickerAvg());
                        vo.setFlicker95(po.getFlicker95());
                        break;
                    }
                }
                result.add(vo);
            }
        }

        result = result.stream().sorted(Comparator.comparing(RStatBusbarHarmonicYVO::getDeptName)).collect(Collectors.toList());
        return result;
    }

}
