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

import com.njcn.harmonic.pojo.po.RMpEventDetailDPO;
import com.njcn.harmonic.pojo.po.RMpEventDetailMPO;
import com.njcn.harmonic.pojo.po.RMpEventDetailQPO;
import com.njcn.harmonic.pojo.po.RMpEventDetailYPO;
import com.njcn.prepare.harmonic.constant.AlgorithmParam;
import com.njcn.prepare.harmonic.pojo.oracle.PqLinedetailPO;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.line.*;
import com.njcn.prepare.harmonic.service.oracle.PqLinedetailPOService;
import com.njcn.prepare.harmonic.service.oracle.PqsEventdetailService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/12/28  14:11【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@Service
@AllArgsConstructor
public class PqsEventdetailHandlerServiceImpl implements PqsEventdetailHandlerService {

    private final  PqLinedetailPOService pqLinedetailPOService;


    private final PqsEventdetailService pqsEventdetailService;

    private final RMpEventDetailDService rMpEventDetailDService;

    private final RMpEventDetailMService rMpEventDetailMService;

    private final RMpEventDetailQService rMpEventDetailQService;

    private final RMpEventDetailYService rMpEventDetailYService;



    @Override
    @Async("asyncExecutor")
    public void eventdetailHandler(LineParam lineParam) {
        if (Objects.equals (lineParam.getDataSource ( ), AlgorithmParam.ORACLE_DATA)) {
            hanlderOracleData (lineParam);
        } else if (Objects.equals (lineParam.getDataSource ( ), AlgorithmParam.INFLUX_DATA)) {
            hanlderInfludxbData (lineParam);
        }
    }
    /**
    * @Description: hanlderInfludxbData
    * @Param: [lineParam]
    * @return: void
    * @Author: clam
    * @Date: 2022/12/28
    */
    private void hanlderInfludxbData(LineParam lineParam) {
    }
    /**
    * @Description: hanlderOracleData
    * @Param: [lineParam]
    * @return: void
    * @Author: clam
    * @Date: 2022/12/28
    */
    private void hanlderOracleData(LineParam lineParam) {
        Integer type = lineParam.getType ( );
        switch (type) {
            case 1:
                this.hanlderOracleYear (lineParam);
                break;
            case 2:
                this.hanlderOracleQtr (lineParam);
                break;
            case 3:
                this.hanlderOracleMonth (lineParam);
                break;
            case 5:
                this.hanlderOracleDay (lineParam);
                break;
            default:
                break;
        }
    }
    @SneakyThrows
    private void hanlderOracleYear(LineParam lineParam) {
        List<String> lineIds = lineParam.getLineIds ( );
        String dataDate = lineParam.getDataDate ( );
        final Date tempDate = new SimpleDateFormat ("yyyy-MM-dd").parse (dataDate);
        List<RMpEventDetailYPO> rMpEventDetailYPOList = new ArrayList<> ( );

        lineIds.forEach (lineId -> {
            /*查询月表数据生成年表数据*/
            List<RMpEventDetailMPO> rMpEventDetailMPOList = rMpEventDetailMService.queryOneYearData(lineId,dataDate);
            int sagCount = rMpEventDetailMPOList.stream ( ).mapToInt (RMpEventDetailMPO::getSagTimes).sum ( );
            int riseCount = rMpEventDetailMPOList.stream ( ).mapToInt (RMpEventDetailMPO::getSwellTimes).sum ( );
            int interruptCount = rMpEventDetailMPOList.stream ( ).mapToInt (RMpEventDetailMPO::getInterruptTimes).sum ( );

            RMpEventDetailYPO rMpEventDetailYPO = new RMpEventDetailYPO();
            rMpEventDetailYPO.setMeasurementPointId (lineId);
            rMpEventDetailYPO.setDataDate (tempDate);
            rMpEventDetailYPO.setSagTimes (sagCount);
            rMpEventDetailYPO.setSwellTimes (riseCount);
            rMpEventDetailYPO.setInterruptTimes (interruptCount);
            rMpEventDetailYPOList.add (rMpEventDetailYPO);

        });

        rMpEventDetailYService.saveOrUpdateBatchByMultiId (rMpEventDetailYPOList,500);


    }
    @SneakyThrows
    private void hanlderOracleQtr(LineParam lineParam) {
        List<String> lineIds = lineParam.getLineIds ( );
        String dataDate = lineParam.getDataDate ( );
        final Date tempDate = new SimpleDateFormat ("yyyy-MM-dd").parse (dataDate);
        List<RMpEventDetailQPO> rMpEventDetailQPOList = new ArrayList<> ( );

        lineIds.forEach (lineId -> {
            /*查询月表数据生成季表数据*/
            List<RMpEventDetailMPO> rMpEventDetailMPOList = rMpEventDetailMService.queryOneQtrData(lineId,dataDate);
            int sagCount = rMpEventDetailMPOList.stream ( ).mapToInt (RMpEventDetailMPO::getSagTimes).sum ( );
            int riseCount = rMpEventDetailMPOList.stream ( ).mapToInt (RMpEventDetailMPO::getSwellTimes).sum ( );
            int interruptCount = rMpEventDetailMPOList.stream ( ).mapToInt (RMpEventDetailMPO::getInterruptTimes).sum ( );

            RMpEventDetailQPO rMpEventDetailQPO = new RMpEventDetailQPO();
            rMpEventDetailQPO.setMeasurementPointId (lineId);
            rMpEventDetailQPO.setDataDate (tempDate);
            rMpEventDetailQPO.setSagTimes (sagCount);
            rMpEventDetailQPO.setSwellTimes (riseCount);
            rMpEventDetailQPO.setInterruptTimes (interruptCount);
            rMpEventDetailQPOList.add (rMpEventDetailQPO);

        });

        rMpEventDetailQService.saveOrUpdateBatchByMultiId (rMpEventDetailQPOList,500);
    }

    @SneakyThrows
    private void hanlderOracleMonth(LineParam lineParam) {
        List<String> lineIds = lineParam.getLineIds ( );
        String dataDate = lineParam.getDataDate ( );
        final Date tempDate = new SimpleDateFormat ("yyyy-MM-dd").parse (dataDate);
        List<RMpEventDetailMPO> rMpEventDetailMPOList = new ArrayList<> ( );

        lineIds.forEach (lineId -> {
            /*查询day表数据生成月表数据*/
            List<RMpEventDetailDPO> rMpEventDetailDPOList = rMpEventDetailDService.queryOneMothData(lineId,dataDate);
            int sagCount = rMpEventDetailDPOList.stream ( ).mapToInt (RMpEventDetailDPO::getSagTimes).sum ( );
            int riseCount = rMpEventDetailDPOList.stream ( ).mapToInt (RMpEventDetailDPO::getSwellTimes).sum ( );
            int interruptCount = rMpEventDetailDPOList.stream ( ).mapToInt (RMpEventDetailDPO::getInterruptTimes).sum ( );

            RMpEventDetailMPO rMpEventDetailMPO = new RMpEventDetailMPO();
            rMpEventDetailMPO.setMeasurementPointId (lineId);
            rMpEventDetailMPO.setDataDate (tempDate);
            rMpEventDetailMPO.setSagTimes (sagCount);
            rMpEventDetailMPO.setSwellTimes (riseCount);
            rMpEventDetailMPO.setInterruptTimes (interruptCount);
            rMpEventDetailMPOList.add (rMpEventDetailMPO);

        });

        rMpEventDetailMService.saveOrUpdateBatchByMultiId (rMpEventDetailMPOList,500);

    }
    @SneakyThrows
    private void hanlderOracleDay(LineParam lineParam) {

        List<String> lineIds = lineParam.getLineIds ( );
        String dataDate = lineParam.getDataDate ( );
        final Date tempDate = new SimpleDateFormat ("yyyy-MM-dd").parse (dataDate);
        List<RMpEventDetailDPO> rMpEventDetailDPOList = new ArrayList<> ( );


        lineIds.forEach (lineId -> {

            /*获取mysql与Oracle监测点id对应关系*/

            PqLinedetailPO oracleIndexIdByMonitorId = pqLinedetailPOService.getOracleIndexIdByMonitorId (lineId);

            Integer sagCount = pqsEventdetailService.queryByCondition (oracleIndexIdByMonitorId.getLineIndex(),dataDate,AlgorithmParam.SAG);
            Integer riseCount = pqsEventdetailService.queryByCondition (oracleIndexIdByMonitorId.getLineIndex(),dataDate,AlgorithmParam.RISE);
            Integer interruptCount = pqsEventdetailService.queryByCondition (oracleIndexIdByMonitorId.getLineIndex(),dataDate,AlgorithmParam.INTERRUPT);
            RMpEventDetailDPO rMpEventDetailDPO = new RMpEventDetailDPO();
            rMpEventDetailDPO.setMeasurementPointId (lineId);
            rMpEventDetailDPO.setDataDate (tempDate);
            rMpEventDetailDPO.setSagTimes (sagCount);
            rMpEventDetailDPO.setSwellTimes (riseCount);
            rMpEventDetailDPO.setInterruptTimes (interruptCount);

            rMpEventDetailDPOList.add (rMpEventDetailDPO);
        });

        rMpEventDetailDService.saveOrUpdateBatchByMultiId (rMpEventDetailDPOList,500);
    }
}
