package com.njcn.prepare.harmonic.service.mysql.Impl.area;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.harmonic.pojo.po.RMpIntegrityDPO;
import com.njcn.prepare.harmonic.mapper.mysql.area.RMpIntegrityDPOMapper;
import com.njcn.prepare.harmonic.pojo.dto.MonitorDataIntegrityDTO;
import com.njcn.prepare.harmonic.service.mysql.area.RMpIntegrityDPOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/11/29  15:38【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class RMpIntegrityDPOServiceImpl extends ServiceImpl<RMpIntegrityDPOMapper, RMpIntegrityDPO> implements RMpIntegrityDPOService{

    private @Autowired
    RMpIntegrityDPOMapper rMpIntegrityDPOMapper;
    /**
     * @Description: 获取监测点的数据完整性（日）
     * @Param: 〖数据完整率〗_日统计=(频率+相电压+线电压+畸变率+不平衡度)/(有效值数据个数*5)*100%
     * @return: com.njcn.prepare.harmonic.pojo.dto.MonitorDataIntegrityDTO
     * @Author: clam
     * @Date: 2022/11/28
     */
    @Override
    public List<MonitorDataIntegrityDTO> calculateDataIntegrity(List<String> indexlist, String dataDate){

        List<MonitorDataIntegrityDTO> monitorDataIntegrityDTOList = new ArrayList<> ();
        QueryWrapper<RMpIntegrityDPO> wrapper = new QueryWrapper ( );
        if(CollectionUtils.isEmpty (indexlist)){
            return monitorDataIntegrityDTOList;
        }
        wrapper.in ("measurement_point_id",indexlist).
                eq("DATE_FORMAT( data_date ,'%Y-%m-%d')",dataDate);

        List<RMpIntegrityDPO> rMpIntegrityDPOS = rMpIntegrityDPOMapper.selectList (wrapper);
        monitorDataIntegrityDTOList = rMpIntegrityDPOS.stream ( ).
                map (temp -> {
                    MonitorDataIntegrityDTO monitorDataIntegrityDTO = new MonitorDataIntegrityDTO();

                    Integer molecular = temp.getFreqCount ( ) + temp.getPhaseVoltageCount ( ) +
                            temp.getLineVoltageCount ( ) + temp.getVThdCount ( )
                            + temp.getUnbalanceCount ( );
                    Integer denominator = 5 * temp.getEffectiveMinuteCount ( );
                    BigDecimal b = new BigDecimal ((float) molecular / denominator);
                    BigDecimal dataIntegrity = b.setScale (2, BigDecimal.ROUND_HALF_UP);
                    monitorDataIntegrityDTO.setId (temp.getMeasurementPointId ());
                    monitorDataIntegrityDTO.setDataIntegrity (dataIntegrity);
                    return monitorDataIntegrityDTO;
                }).collect (Collectors.toList ());

        return monitorDataIntegrityDTOList;

    }
    /** 
    * @Description: 获取监测点的数据完整性（月平均值）
    * @Param: [indexlist, dataDate] 
    * @return: java.util.List<com.njcn.prepare.harmonic.pojo.dto.MonitorDataIntegrityDTO> 
    * @Author: clam
    * @Date: 2022/12/1 
    */ 
    @Override
    public List<MonitorDataIntegrityDTO> calculateMouthDataIntegrity(List<String> indexlist, String dataDate) {
        List<MonitorDataIntegrityDTO> monitorDataIntegrityDTOList = new ArrayList<> ();
        QueryWrapper<RMpIntegrityDPO> wrapper = new QueryWrapper ( );
        if(CollectionUtils.isEmpty (indexlist)){
            return monitorDataIntegrityDTOList;
        }
        wrapper.in ("measurement_point_id",indexlist).
                eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring (0, 7));

        List<RMpIntegrityDPO> rMpIntegrityDPOS = rMpIntegrityDPOMapper.selectList (wrapper);
        monitorDataIntegrityDTOList = rMpIntegrityDPOS.stream ( ).
                map (temp -> {
                    MonitorDataIntegrityDTO monitorDataIntegrityDTO = new MonitorDataIntegrityDTO();

                    Integer molecular = temp.getFreqCount ( ) + temp.getPhaseVoltageCount ( ) +
                            temp.getLineVoltageCount ( ) + temp.getVThdCount ( )
                            + temp.getUnbalanceCount ( );
                    Integer denominator = 5 * temp.getEffectiveMinuteCount ( );
                    BigDecimal b = new BigDecimal ((float) molecular / denominator);
                    BigDecimal dataIntegrity = b.setScale (2, BigDecimal.ROUND_HALF_UP);
                    monitorDataIntegrityDTO.setId (temp.getMeasurementPointId ());
                    monitorDataIntegrityDTO.setDataIntegrity (dataIntegrity);
                    return monitorDataIntegrityDTO;
                }).collect (Collectors.toList ());

        return monitorDataIntegrityDTOList;
    }

}
