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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.device.biz.commApi.CommLineClient;
import com.njcn.device.biz.pojo.dto.LineDTO;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpMonitorEvaluateDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.ROperatingMonitorDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.RStatAbnormalDMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.harmonic.pojo.po.algorithm.line.RMpMonitorEvaluateD;
import com.njcn.prepare.harmonic.pojo.mysql.ROperatingMonitorDPO;
import com.njcn.prepare.harmonic.pojo.mysql.RStatAbnormalDPO;
import com.njcn.prepare.harmonic.service.mysql.line.ROperatingMonitorService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2023/4/21  10:17【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class ROperatingMonitorDServiceImpl extends MppServiceImpl<ROperatingMonitorDMapper, ROperatingMonitorDPO> implements ROperatingMonitorService {

    private  final RMpMonitorEvaluateDMapper rMpMonitorEvaluateDMapper;
    private  final RStatAbnormalDMapper rStatAbnormalDMapper;
    private final CommLineClient commLineClient;

    /** 
    * @Description: 采用以下公式计算数据完整率：
     * 〖数据完整率〗_日统计=有效值数据个数/(1440/interval)*100%
     * 	采用以下公式计算数据准确率
     * 〖数据准确率〗_日统计=(频率+相电压+线电压+畸变率+不平衡度)/(有效值数据个数*5)*100%
     * 	采用以下公式计算指标完整率
     * 〖指标完整率〗_日统计=(频率+相电压+线电压+畸变率+不平衡度)/(1440/interval*5)*100%	采用以下公式计算数据完整率：
     * 〖数据完整率〗_日统计=有效值数据个数/(1440/interval)*100%
     * 	采用以下公式计算数据准确率
     * 〖数据准确率〗_日统计=(频率+相电压+线电压+畸变率+不平衡度)/(有效值数据个数*5)*100%
     * 	采用以下公式计算指标完整率
     * 〖指标完整率〗_日统计=(频率+相电压+线电压+畸变率+不平衡度)/(1440/interval*5)*100% 
    * @Author: clam
    * @Date: 2023/4/21 
    */ 
    @Override
    public void handleDay(CalculatedParam calculatedParam) {
        LocalDate localDate = LocalDateTimeUtil.parseDate(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN);
        List<ROperatingMonitorDPO> rOperatingMonitorDPOList = new ArrayList<> ();
        List<List<String>> pendingIds = ListUtils.partition(calculatedParam.getIdList(), 100);
        for (List<String> pendingId : pendingIds) {
            List<LineDTO> lineDTOList = commLineClient.getLineDetailBatch(pendingId).getData();
            for (LineDTO lineDTO : lineDTOList) {
                ROperatingMonitorDPO rOperatingMonitorDPO = new ROperatingMonitorDPO();

                Integer statisticalInterval = lineDTO.getTimeInterval ();
                RMpMonitorEvaluateD rMpMonitorEvaluateD = new RMpMonitorEvaluateD();
                rMpMonitorEvaluateD.setMeasurementPointId (lineDTO.getLineId());
                rMpMonitorEvaluateD.setDataDate (localDate);
                rMpMonitorEvaluateD = rMpMonitorEvaluateDMapper.selectByMultiId (rMpMonitorEvaluateD);
                if (Objects.isNull(rMpMonitorEvaluateD)) {
                    continue;

                }
                RStatAbnormalDPO rStatAbnormalDPO = new RStatAbnormalDPO();
                rStatAbnormalDPO.setDataDate (localDate);
                rStatAbnormalDPO.setMeasurementPointId (lineDTO.getLineId());
                rStatAbnormalDPO = rStatAbnormalDMapper.selectByMultiId (rStatAbnormalDPO);
                if (Objects.isNull(rStatAbnormalDPO)) {
                    continue;

                }
                rOperatingMonitorDPO.setMeasurementPointId (lineDTO.getLineId());
                rOperatingMonitorDPO.setDataDate (localDate);
                rOperatingMonitorDPO.setIsUnusual (rStatAbnormalDPO.getValueAlarm ());
                rOperatingMonitorDPO.setEffectiveMinuteCount (rMpMonitorEvaluateD.getEffectiveMinuteCount ());
                rOperatingMonitorDPO.setDataIntegrityRate (BigDecimal.valueOf (Double.valueOf(rMpMonitorEvaluateD.getEffectiveMinuteCount ()*statisticalInterval)/1440).setScale(5, BigDecimal.ROUND_HALF_UP));
                if(rMpMonitorEvaluateD.getEffectiveMinuteCount ()==0){
                    rOperatingMonitorDPO.setDataRightRate(BigDecimal.valueOf(0.00));
                }else{
                    rOperatingMonitorDPO.setDataRightRate (BigDecimal.valueOf(
                            (Double.valueOf(rMpMonitorEvaluateD.getFreqCount ()+ rMpMonitorEvaluateD.getLineVoltageCount ()+
                                    rMpMonitorEvaluateD.getPhaseVoltageCount ()+ rMpMonitorEvaluateD.getUnbalanceCount ()+
                                    rMpMonitorEvaluateD.getVThdCount ()))*statisticalInterval/(5*1440)
                    ).setScale(5, BigDecimal.ROUND_HALF_UP));
                }

                rOperatingMonitorDPO.setIndexIntegrityRate (
                        BigDecimal.valueOf(
                                (Double.valueOf(rMpMonitorEvaluateD.getFreqCount ()+ rMpMonitorEvaluateD.getLineVoltageCount ()+
                                        rMpMonitorEvaluateD.getPhaseVoltageCount ()+ rMpMonitorEvaluateD.getUnbalanceCount ()+
                                        rMpMonitorEvaluateD.getVThdCount ())*statisticalInterval)/(5*1440)
                        ).setScale(5, BigDecimal.ROUND_HALF_UP)
                );
                rOperatingMonitorDPO.setDevId (lineDTO.getDevId());
                rOperatingMonitorDPO.setMonitorState("0");
                rOperatingMonitorDPOList.add (rOperatingMonitorDPO);
            }
        }

        this.saveOrUpdateBatchByMultiId (rOperatingMonitorDPOList,500);

    }
}
