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

import cn.hutool.core.date.DateUtil;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.common.utils.PubUtils;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.pq.pojo.vo.LineDetailDataVO;
import com.njcn.prepare.harmonic.mapper.mysql.line.ROperatingMonitorDMapper;
import com.njcn.prepare.harmonic.pojo.mysql.RMpMonitorEvaluateDPO;
import com.njcn.prepare.harmonic.pojo.mysql.ROperatingMonitorDPO;
import com.njcn.prepare.harmonic.pojo.mysql.RStatAbnormalDPO;
import com.njcn.prepare.harmonic.service.mysql.line.RMpMonitorEvaluateDService;
import com.njcn.prepare.harmonic.service.mysql.line.ROperatingMonitorService;
import com.njcn.prepare.harmonic.service.mysql.line.RStatAbnormalDService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
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 RMpMonitorEvaluateDService rMpMonitorEvaluateDService;
    private  final RStatAbnormalDService rStatAbnormalDService;
    private final LineFeignClient lineFeignClient;

    /** 
    * @Description: 采用以下公式计算数据完整率：
     * 〖数据完整率〗_日统计=有效值数据个数/(1440/interval)*100%
     * 	采用以下公式计算数据准确率
     * 〖数据准确率〗_日统计=(频率+相电压+线电压+畸变率+不平衡度)/(有效值数据个数*5)*100%
     * 	采用以下公式计算指标完整率
     * 〖指标完整率〗_日统计=(频率+相电压+线电压+畸变率+不平衡度)/(1440/interval*5)*100%	采用以下公式计算数据完整率：
     * 〖数据完整率〗_日统计=有效值数据个数/(1440/interval)*100%
     * 	采用以下公式计算数据准确率
     * 〖数据准确率〗_日统计=(频率+相电压+线电压+畸变率+不平衡度)/(有效值数据个数*5)*100%
     * 	采用以下公式计算指标完整率
     * 〖指标完整率〗_日统计=(频率+相电压+线电压+畸变率+不平衡度)/(1440/interval*5)*100% 
    * @Param: [indexLists, dataDate] 
    * @return: void 
    * @Author: clam
    * @Date: 2023/4/21 
    */ 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    public void handlerMonitorOperatingD(List<String> indexLists, String dataDate) {
        Date date = DateUtil.parse(dataDate);
        List<ROperatingMonitorDPO> rOperatingMonitorDPOList = new ArrayList<> ();
        indexLists.forEach (temp -> {
            ROperatingMonitorDPO rOperatingMonitorDPO = new ROperatingMonitorDPO();

            LineDetailDataVO data = lineFeignClient.getLineDetailData (temp).getData ( );
            /*todo 统计间隔，电压等级取值方式,devid后期提供融合版本接口获取不然pms，pqs系统取值方式不一样*/
            /*统计间隔*/
            Integer statisticalInterval = data.getTimeInterval ();
            String devid = lineFeignClient.getLineIdByDevId(temp).getData();

            RMpMonitorEvaluateDPO rMpMonitorEvaluateDPO = new RMpMonitorEvaluateDPO();
            rMpMonitorEvaluateDPO.setMeasurementPointId (temp);
            rMpMonitorEvaluateDPO.setDataDate (date);
            rMpMonitorEvaluateDPO = rMpMonitorEvaluateDService.selectByMultiId (rMpMonitorEvaluateDPO);
            if (Objects.isNull(rMpMonitorEvaluateDPO)) {
                return;
            }
            RStatAbnormalDPO rStatAbnormalDPO = new RStatAbnormalDPO();
            rStatAbnormalDPO.setDataDate (date);
            rStatAbnormalDPO.setMeasurementPointId (temp);
            rStatAbnormalDPO = rStatAbnormalDService.selectByMultiId (rStatAbnormalDPO);
            if (Objects.isNull(rStatAbnormalDPO)) {
                return;
            }
            rOperatingMonitorDPO.setMeasurementPointId (temp);
            rOperatingMonitorDPO.setDataDate (date);
            rOperatingMonitorDPO.setIsUnusual (rStatAbnormalDPO.getValueAlarm ());
            rOperatingMonitorDPO.setEffectiveMinuteCount (rMpMonitorEvaluateDPO.getEffectiveMinuteCount ());
            rOperatingMonitorDPO.setDataIntegrityRate (BigDecimal.valueOf (rMpMonitorEvaluateDPO.getEffectiveMinuteCount ()*statisticalInterval/1440));
            if(rMpMonitorEvaluateDPO.getEffectiveMinuteCount ()==0){
                rOperatingMonitorDPO.setDataRightRate(BigDecimal.valueOf(0.00));
            }else{
                rOperatingMonitorDPO.setDataRightRate (BigDecimal.valueOf(
                        (rMpMonitorEvaluateDPO.getFreqCount ()+ rMpMonitorEvaluateDPO.getLineVoltageCount ()+
                                rMpMonitorEvaluateDPO.getPhaseVoltageCount ()+rMpMonitorEvaluateDPO.getUnbalanceCount ()+
                                rMpMonitorEvaluateDPO.getVThdCount ())/(5*rMpMonitorEvaluateDPO.getEffectiveMinuteCount ())
                ));
            }

            rOperatingMonitorDPO.setIndexIntegrityRate (
                    BigDecimal.valueOf(
                            (rMpMonitorEvaluateDPO.getFreqCount ()+ rMpMonitorEvaluateDPO.getLineVoltageCount ()+
                                    rMpMonitorEvaluateDPO.getPhaseVoltageCount ()+rMpMonitorEvaluateDPO.getUnbalanceCount ()+
                                    rMpMonitorEvaluateDPO.getVThdCount ())*statisticalInterval/(5*1440)
                    )
            );
            rOperatingMonitorDPO.setDevId (devid);
            rOperatingMonitorDPO.setMonitorState (PubUtils.getRunFlag(data.getRunFlag())+"");
            rOperatingMonitorDPOList.add (rOperatingMonitorDPO);
        });
        this.saveOrUpdateBatchByMultiId (rOperatingMonitorDPOList,500);
    }
}
