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.math.BigDecimal;
import java.math.RoundingMode;
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) {
                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() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getVEffectiveMax()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setVEffectiveMin(po.getVEffectiveMin() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getVEffectiveMin()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setVEffectiveAvg(po.getVEffectiveAvg() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getVEffectiveAvg()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setVEffective95(po.getVEffective95() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getVEffective95()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setVThdMax(po.getVThdMax() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getVThdMax()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setVThdMin(po.getVThdMin() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getVThdMin()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setVThdAvg(po.getVThdAvg() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getVThdAvg()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setVThd95(po.getVThd95() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getVThd95()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setUnbalanceMax(po.getUnbalanceMax() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getUnbalanceMax()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setUnbalanceMin(po.getUnbalanceMin() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getUnbalanceMin()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setUnbalanceAvg(po.getUnbalanceAvg() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getUnbalanceAvg()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setUnbalance95(po.getUnbalance95() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getUnbalance95()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setFlickerMax(po.getFlickerMax() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getFlickerMax()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setFlickerMin(po.getFlickerMin() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getFlickerMin()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setFlickerAvg(po.getFlickerAvg() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getFlickerAvg()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        vo.setFlicker95(po.getFlicker95() ==3.14159 ? 3.14159: BigDecimal.valueOf(po.getFlicker95()).setScale(2, RoundingMode.HALF_UP).doubleValue());
                        break;
                    }
                }
                result.add(vo);
            }
        }

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

}
