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

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.pq.pojo.vo.LineDetailDataVO;
import com.njcn.harmonic.pojo.po.RMpPartHarmonicDetailD;
import com.njcn.harmonic.pojo.po.RMpSurplusHarmonicDetailD;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpPartHarmonicDetailDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpPassRateDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpSurplusHarmonicDetailDMapper;
import com.njcn.prepare.harmonic.pojo.mysql.RMpMonitorEvaluateDPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.line.RMpPassRateDPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.line.RMpPassRateMPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.line.RMpPassRateQPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.line.RMpPassRateYPO;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.line.*;
import com.njcn.prepare.harmonic.utils.PublicUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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/2/28  15:39【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class RMpPassRateDServiceImpl extends MppServiceImpl<RMpPassRateDMapper, RMpPassRateDPO> implements RMpPassRateDService {

    private final LineFeignClient lineFeignClient;
    private final RMpMonitorEvaluateDService rMpMonitorEvaluateDService;
    private final RMpPartHarmonicDetailDMapper rMpPartHarmonicDetailDMapper;
    private final RMpSurplusHarmonicDetailDMapper rMpSurplusHarmonicDetailDMapper;

    private final RMpPassRateMService rMpPassRateMService;
    private final RMpPassRateQService rMpPassRateQService;
    private final RMpPassRateYService rMpPassRateYService;



    @DS("process")
    @Override
    public List<RMpPassRateDPO> getAll() {
        return this.list ( );
    }


    public void passRateHandlerD(LineParam lineParam) {
        List<String> lineIds = lineParam.getLineIds ( );
        String dataDate = lineParam.getDataDate ( );
        Date date = DateUtil.parse (dataDate);
        List<RMpPassRateDPO> rMpPassRateDPOList = new ArrayList<> ( );
        QueryWrapper<RMpMonitorEvaluateDPO> rMpMonitorEvaluateDPOQueryWrapper = new QueryWrapper ( );

        QueryWrapper<RMpPartHarmonicDetailD> queryWrapper = new QueryWrapper ( );
        QueryWrapper<RMpSurplusHarmonicDetailD> surplusHarmonicDetailDQueryWrapper = new QueryWrapper ( );

        lineIds.forEach (temp -> {
            LineDetailDataVO data = lineFeignClient.getLineDetailData (temp).getData ( );
            Integer statisticalInterval = data.getTimeInterval ( );
            rMpMonitorEvaluateDPOQueryWrapper.clear ();
            rMpMonitorEvaluateDPOQueryWrapper.select ("effective_minute_count").
                    eq ("measurement_point_id", temp).
                    eq ("DATE_FORMAT( data_date ,'%Y-%m-%d')", lineParam.getDataDate ( ));
            RMpMonitorEvaluateDPO one = rMpMonitorEvaluateDService.getOne (rMpMonitorEvaluateDPOQueryWrapper);
            if (Objects.isNull(one)) {
                return;
            }
            queryWrapper.clear ( );
            queryWrapper.select ("freq_times", "v_dev_times").
                    eq ("measurement_point_id", temp).
                    eq ("DATE_FORMAT( data_date ,'%Y-%m-%d')", lineParam.getDataDate ( ));
            RMpPartHarmonicDetailD rMpPartHarmonicDetailD = rMpPartHarmonicDetailDMapper.selectOne (queryWrapper);
            if (Objects.isNull(rMpPartHarmonicDetailD)) {
                return;
            }
            surplusHarmonicDetailDQueryWrapper.clear ( );
            surplusHarmonicDetailDQueryWrapper.select ("flicker_times").
                    eq ("measurement_point_id", temp).
                    eq ("DATE_FORMAT( data_date ,'%Y-%m-%d')", lineParam.getDataDate ( ));
            RMpSurplusHarmonicDetailD rMpSurplusHarmonicDetailD = rMpSurplusHarmonicDetailDMapper.selectOne (surplusHarmonicDetailDQueryWrapper);
            if (Objects.isNull(rMpSurplusHarmonicDetailD)) {
                return;
            }
            RMpPassRateDPO rMpPassRateDPO = new RMpPassRateDPO ( );
            rMpPassRateDPO.setMeasurementPointId (temp);
            rMpPassRateDPO.setDataDate (date);
            rMpPassRateDPO.setEffectiveTime (statisticalInterval *one.getEffectiveMinuteCount ());
            rMpPassRateDPO.setFreqTime (statisticalInterval * rMpPartHarmonicDetailD.getFreqTimes ( ));
            rMpPassRateDPO.setvDevTime (statisticalInterval * rMpPartHarmonicDetailD.getVDevTimes ( ));
            rMpPassRateDPO.setFlickerTime (statisticalInterval * rMpSurplusHarmonicDetailD.getFlickerTimes ( ));

            rMpPassRateDPOList.add (rMpPassRateDPO);

        });
        this.saveOrUpdateBatchByMultiId(rMpPassRateDPOList, 500);
    }
    @Override
    public void passRateHandler(LineParam lineParam) {
        Integer type = lineParam.getType ( );
        switch (type) {
            case 1:
                this.passRateHandlerY (lineParam);
                break;
            case 2:
                this.passRateHandlerQ (lineParam);
                break;
            case 3:
                this.passRateHandlerM (lineParam);
                break;
            case 5:
                this.passRateHandlerD (lineParam);
                break;
            default:
                break;
        }
    }

    private void passRateHandlerY(LineParam lineParam) {
        List<String> lineIds = lineParam.getLineIds ( );
        String dataDate = lineParam.getDataDate ( );
        Date date = DateUtil.parse (dataDate);
        List<RMpPassRateYPO> rMpPassRateYPOList = new ArrayList<> ( );

        lineIds.forEach (temp -> {
            QueryWrapper<RMpPassRateDPO> queryWrapper = new QueryWrapper<> ();
            queryWrapper.eq ("measurement_point_id", temp).
                    eq("YEAR( data_date )",dataDate.substring (0, 4));
            List<RMpPassRateDPO> rMpPassRateDPOList = this.getBaseMapper ( ).selectList (queryWrapper);
            RMpPassRateYPO rMpPassRateYPO = new RMpPassRateYPO();
            rMpPassRateYPO.setMeasurementPointId (temp);
            rMpPassRateYPO.setDataDate (date);
            int sumEffectiveTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getEffectiveTime).sum ( );
            int sumFreqTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getFreqTime).sum ( );
            int sumDevTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getvDevTime).sum ( );
            int sumFlickerTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getFlickerTime).sum ( );

            rMpPassRateYPO.setFreqPassRate (calculatePassRate(sumEffectiveTime,sumFreqTime));
            rMpPassRateYPO.setvDevPassRate (calculatePassRate(sumEffectiveTime,sumDevTime));
            rMpPassRateYPO.setFlickerPassRate (calculatePassRate(sumEffectiveTime,sumFlickerTime));
            rMpPassRateYPOList.add (rMpPassRateYPO);
        });
        rMpPassRateYService.saveOrUpdateBatchByMultiId (rMpPassRateYPOList, 500);
    }

    private void passRateHandlerQ(LineParam lineParam) {

        List<String> lineIds = lineParam.getLineIds ( );
        String dataDate = lineParam.getDataDate ( );
        Date date = DateUtil.parse (dataDate);
        List<RMpPassRateQPO> rMpPassRateQPOList = new ArrayList<> ( );

        lineIds.forEach (temp -> {
            QueryWrapper<RMpPassRateDPO> queryWrapper = new QueryWrapper<> ();
            queryWrapper.eq ("measurement_point_id", temp).
                    eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
            List<RMpPassRateDPO> rMpPassRateDPOList = this.getBaseMapper ( ).selectList (queryWrapper);
            RMpPassRateQPO rMpPassRateQPO = new RMpPassRateQPO();
            rMpPassRateQPO.setMeasurementPointId (temp);
            rMpPassRateQPO.setDataDate (date);
            int sumEffectiveTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getEffectiveTime).sum ( );
            int sumFreqTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getFreqTime).sum ( );
            int sumDevTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getvDevTime).sum ( );
            int sumFlickerTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getFlickerTime).sum ( );

            rMpPassRateQPO.setFreqPassRate (calculatePassRate(sumEffectiveTime,sumFreqTime));
            rMpPassRateQPO.setvDevPassRate (calculatePassRate(sumEffectiveTime,sumDevTime));
            rMpPassRateQPO.setFlickerPassRate (calculatePassRate(sumEffectiveTime,sumFlickerTime));
            rMpPassRateQPOList.add (rMpPassRateQPO);
        });
        rMpPassRateQService.saveOrUpdateBatchByMultiId (rMpPassRateQPOList, 500);
    }

    private void passRateHandlerM(LineParam lineParam) {

        List<String> lineIds = lineParam.getLineIds ( );
        String dataDate = lineParam.getDataDate ( );
        Date date = DateUtil.parse (dataDate);
        List<RMpPassRateMPO> rMpPassRateMPOList = new ArrayList<> ( );

        lineIds.forEach (temp -> {
            QueryWrapper<RMpPassRateDPO> queryWrapper = new QueryWrapper<> ();
            queryWrapper.eq ("measurement_point_id", temp).
                    eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring (0, 7));
            List<RMpPassRateDPO> rMpPassRateDPOList = this.getBaseMapper ( ).selectList (queryWrapper);
            RMpPassRateMPO rMpPassRateMPO = new RMpPassRateMPO();
            rMpPassRateMPO.setMeasurementPointId (temp);
            rMpPassRateMPO.setDataDate (date);
            int sumEffectiveTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getEffectiveTime).sum ( );
            int sumFreqTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getFreqTime).sum ( );
            int sumDevTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getvDevTime).sum ( );
            int sumFlickerTime = rMpPassRateDPOList.stream ( ).mapToInt (RMpPassRateDPO::getFlickerTime).sum ( );

            rMpPassRateMPO.setFreqPassRate (calculatePassRate(sumEffectiveTime,sumFreqTime));
            rMpPassRateMPO.setvDevPassRate (calculatePassRate(sumEffectiveTime,sumDevTime));
            rMpPassRateMPO.setFlickerPassRate (calculatePassRate(sumEffectiveTime,sumFlickerTime));
            rMpPassRateMPOList.add (rMpPassRateMPO);
        });
        rMpPassRateMService.saveOrUpdateBatchByMultiId (rMpPassRateMPOList, 500);
    }

    private Double calculatePassRate(int sumEffectiveTime, int sumFreqTime) {
        Double result =0.00;
        if(sumEffectiveTime==0||sumEffectiveTime<=sumFreqTime){
            return result;
        }
        result = (double) ((sumEffectiveTime-sumFreqTime)/sumEffectiveTime);
        return result;
    }
}
