package com.njcn.prepare.harmonic.service.oracle.Impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.prepare.harmonic.mapper.oracle.DataVOraclePOMapper;
import com.njcn.prepare.harmonic.pojo.dto.DataVFiveItemDTO;
import com.njcn.prepare.harmonic.pojo.oracle.DataVOraclePO;
import com.njcn.prepare.harmonic.pojo.vo.DataVOracleVO;
import com.njcn.prepare.harmonic.service.oracle.DataVOraclePOService;
import com.njcn.prepare.harmonic.utils.PublicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/11/29  11:45【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@DS("oracle")
public class DataVOraclePOServiceImpl extends ServiceImpl<DataVOraclePOMapper, DataVOraclePO> implements DataVOraclePOService{

    private @Autowired
    DataVOraclePOMapper dataVOraclePOMapper;
    /**
     * @Description: 主网单位在线监测点数 :3、在Oracle库的data_v表中查询当天RMS数据数量，数量大于1则该监测点为在线监测点
     * line_id	监测点编号
     * timeid	时间
     * phasic_type = A	相别
     * RMS	电压有效值数据个数
     * @Param: [indexlist]
     * @return: java.lang.Integer
     * @Author: clam
     * @Date: 2022/11/25
     */
    @Override
    public Integer queryTransitMeasurementPoints(List<Integer> indexlist, String datadate) {
        Integer temp =0;
        if(CollectionUtils.isEmpty (indexlist)){
            return temp;
        }
        QueryWrapper<DataVOraclePO> monitorPOLambdaQueryWrapper = new QueryWrapper ( );
        monitorPOLambdaQueryWrapper.select ("distinct lineid").
                in ("LINEID",indexlist).
                eq ("PHASIC_TYPE","A").
                gt ("RMS", 1).
                eq("TO_CHAR(timeid, 'yyyy-mm-dd')",datadate);

        temp = dataVOraclePOMapper.selectCount (monitorPOLambdaQueryWrapper);
        return temp;
    }

    /**
     * 取五项指标（数据异常）
     * @author qijian
     * @date 2022/11/30
     */
    @Override
    public DataVOracleVO getFiveItemsForDataException(Integer lineId, Date date, Integer type) {
        //根据类型获取日期的开始和结束
        Map<String, DateTime> beginAndEndByDate = PublicUtil.getBeginAndEndByDate(type, date);
        DateTime startTime = beginAndEndByDate.get("startTime");
        DateTime endTime = beginAndEndByDate.get("endTime");
        DataVOracleVO dataVOracleVO = dataVOraclePOMapper.getFiveItemsForDataException(lineId,startTime,endTime);
        return dataVOracleVO;
    }

    /**
     * 取五项指标数量（数据完整性）
     * @author qijian
     * @date 2022/12/01
     */
    @Override
    public DataVOracleVO getFiveCountForDataIntegrityRate(Integer lineId, Date date, Integer type) {
        //根据类型获取日期的开始和结束
        Map<String, DateTime> beginAndEndByDate = PublicUtil.getBeginAndEndByDate(type, date);
        DateTime startTime = beginAndEndByDate.get("startTime");
        DateTime endTime = beginAndEndByDate.get("endTime");
        DataVOracleVO dataVOracleVO = dataVOraclePOMapper.getFiveCountForDataIntegrityRate(lineId,startTime,endTime);
        return dataVOracleVO;
    }

    /**
     * 电压总谐波畸变率最大值求最大值
     *
     * @param lineId
     * @return
     */
    @Override
    public Double queryVThdMax(Integer lineId,String datadate) {
        QueryWrapper<DataVOraclePO> dataVOraclePOQueryWrapper = new QueryWrapper ( );
        dataVOraclePOQueryWrapper.select ("max(V_THD_MAX) as V_THD_MAX").
                eq ("LINEID", lineId).
                in ("PHASIC_TYPE", Stream.of ("A","B","C" ).collect(Collectors.toList())).
                eq("TO_CHAR(TIMEID, 'yyyy-mm-dd')",datadate);
        DataVOraclePO dataVOraclePO = dataVOraclePOMapper.selectOne (dataVOraclePOQueryWrapper);
        Double vThdMax = dataVOraclePO.getVThdMax ( );
        return vThdMax;
    }

    /**
     * @param lineId
     * @param date
     * @Description: 获取监测点当天数据
     * @Param: [lineId, date]
     * @return: java.util.List<com.njcn.prepare.harmonic.pojo.vo.DataVOracleVO>
     * @Author: clam
     * @Date: 2023/2/23
     */
    @Override
    public List<DataVFiveItemDTO> getFiveItems(Integer lineId, String date) {

        List<DataVFiveItemDTO> collect = dataVOraclePOMapper.getFiveItems(lineId,date);

        return collect;
    }
}
