package com.tofly.scada.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.scada.entity.Scada;
import com.tofly.scada.entity.dto.CustomDisplayDto;
import com.tofly.scada.entity.dto.ScadaQuery;
import com.tofly.scada.entity.dto.ScadaQueryDto;
import com.tofly.scada.entity.vo.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author HaiQiu
 * @date 2022/4/25
 */
@Mapper
public interface ScadaMapper extends BaseMapper<Scada> {

    /**
     * 临时接口，查询历史数据
     * @return
     */
    @Select("select * " +
            "  from (select t1.*, " +
            "               row_number() over(partition by t1.CODE order by SCADA_TIME DESC) rn " +
            "          from SCADA_HISTORY t1) t1 " +
            " where t1.rn = 1")
    List<Scada> getListData();


    /**
     * 临时接口，查询历史数据,支持code
     * @param code code
     * @return
     */
    @Select("select * " +
            "  from (select t1.*, " +
            "               row_number() over(partition by t1.CODE order by SCADA_TIME DESC) rn " +
            "          from SCADA_HISTORY t1 where CODE = #{cdoe} ) t1 " +
            " where t1.rn = 1")
    List<Scada> getListDataByCode(String code);


    /**
     * 创建表
     * @param tableName 表名
     */
    void createTableMonths(String tableName);

    /**
     * 判断表是否存在
     * @param tableName 表名
     * @return 条数大于1为存在
     */
    int verifyTableMonthsIsExits(String tableName);
    @Select("select  ID as id,CODE as code,VALUE as value,SCADA_TIME as scadaTime,UPDATE_TIME as updateTime from ${tableName} " +
            "where ID = #{id}")
    List<Scada> selectScadaMonitoringRealtimeById(@Param("tableName") String tableName,
                                                  @Param("id") Long id);
    /**
     * 根据时间段查询历史scada数据分组集合
     *
     * @param tableName 表名
     * @param start     开始时间
     * @param end       结束时间
     * @return 历史数据列表
     */
    List<Scada> selectHistoryMaxAndMinByTime(@Param("tableName") String tableName, @Param("codes") List<String> codes,
                                             @Param("start") String start, @Param("end") String end,
                                             @Param("dataType") Integer dataType);


    /**
     * 根据时间段查询历史scada数据分组集合
     *
     * @param tableName 表名
     * @param time     时间字符
     * @return 历史数据列表
     */
    List<Scada> selectHistoryMaxAndMinByScadaTimeToOne(@Param("tableName") String tableName, @Param("codes") List<String> codes,
                                                       @Param("time") String time,
                                                       @Param("dataType") Integer dataType);


    /**
     * 根据时间段查询历史scada数据最大值最小值
     *
     * @param start     开始时间
     * @param end       结束时间
     * @return 历史数据列表
     */
    List<StatisticsScadaCalVo> selectHistoryMaxAndMinByScadaTime(@Param("codes") List<String> codes,
                                                                 @Param("start") Date start, @Param("end") Date end,@Param("dataType")Integer dataType);

  BigDecimal getHistoryAvgValueByScadaTime(@Param("start") Date start, @Param("end") Date end,@Param("code") String code);

    List<StatisticsScadaCalVo> selectCodesComm(@Param("codes") List<String> codes,
                                                                 @Param("start") Date start, @Param("end") Date end,@Param("dataType")Integer dataType);



    /**
     * 根据时间段查询历史scada数据最大值最小值
     *
     * @param start     开始时间
     * @param end       结束时间
     * @return 历史数据列表
     */
    List<StatisticsScadaCalVo> selectHistoryRunTimeByScadaTime(@Param("codes") List<String> codes,
                                                                 @Param("start") Date start, @Param("end") Date end,@Param("dataType")Integer dataType);

    /**
     * 根据ID查询历史scada数据
     * @param tableName 表名
     * @param id ID
     * @return 历史数据列表
     */
    @Select("select  * from ${tableName} " +
            "where ID = #{id}")
    List<Scada> selectHistoryById(@Param("tableName") String tableName,
                                    @Param("id") Long id);



    @Select("select  * from ${tableName}")
    List<Scada> selectMonitoring(@Param("tableName") String tableName);


    List<ScadaVo> selectMonitoringNew(@Param("codes") List<String> codes);

    /**
     * 添加月份数据
     * @param scadas 数据
     * @param tableName 表名
     * @return 影响条数
     */
    int insertMouth(@Param("scadas") List<Scada> scadas,@Param("tableName") String tableName);


    /**
     * 添加月份数据
     * @param scada 数据
     * @param tableName 表名
     * @return 影响条数
     */
    int insertByOne(@Param("item") Scada scada,@Param("tableName") String tableName);

    /**
     * 根据表名和时间段删除表数据
     * @param tableName 表名
     * @param start 开始时间
     * @param end 结束时间
     * @return 成功条数
     */
    int deleteFromTableName(@Param("tableName") String tableName,
                            @Param("start") String start,@Param("end") String end);



    /**
     * 查询SCADA数据
     * @param queryWrapper 条件
     * @return
     */
    @Select("SELECT hours.* FROM ${tableName} hours " +
            "LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.CODE = hours.CODE " +
            "LEFT JOIN TF_YWPN_SCADASTAITEM_W staitem ON staitem.ITCD =  hours.CODE " +
            "${ew.customSqlSegment} ")
    List<ScadaVo> list(@Param(Constants.WRAPPER) QueryWrapper<ScadaQuery> queryWrapper,
                           @Param("tableName") String tableName);


    /**
     * 查询SCADA数据
     * @param page 分页
     * @param queryWrapper 条件
     * @return
     */
    @Select("SELECT hours.*,manage.NAME name,manage.MAKE_DATE makeDate,manage.TYPE type," +
            "manage.MANUFACTURER manufacturer,manage.LEADER leader,manage.PHONE phone,staitem.STATION_ID stationId " +
            " FROM ${tableName} hours " +
            "LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.CODE = hours.CODE " +
            "LEFT JOIN TF_YWPN_SCADASTAITEM_W staitem ON staitem.ITCD =  hours.CODE " +
            "${ew.customSqlSegment} ")
    Page<ScadaVo> pageList(Page page, @Param(Constants.WRAPPER) QueryWrapper<ScadaQuery> queryWrapper,
                           @Param("tableName") String tableName);

    Scada selectDataByMonitoring(String code);

    int insertMonitoring(@Param("scada") Scada scada);

    int updateMonitoring(@Param("scada") Scada scada);

    Page<StatisticsScadaVo> selectHistoryByMouthsTablePage(@Param("mouthTable") String mouthTable,
                                                        Page page,
                                                        @Param("scadaQuery")  ScadaQuery scadaQuery);

    List<StatisticsScadaVo> selectHistoryByMouthsTableList(@Param("mouthTable") String mouthTable,
                                                        @Param("scadaQuery")  ScadaQuery scadaQuery);

    /**
     * 获取时间范围内设备类型deviceType 为 2,3 的指标类型为正反的监测数据
     * @param startDate
     * @param endDate
     * @return
     */
    List<NetCumulativeFlow> getNetCumulativeFlow(@Param("startTime")String startDate, @Param("endTime")String endDate);

    List<StationIndexVo> viewScadaData(CustomDisplayDto dto);

    Page<WaterIndexInfoVo> viewScadaDataNew(Page page,CustomDisplayDto dto);

    List<WaterIndexInfoVo> viewScadaDataNew(CustomDisplayDto dto);
    List<WaterIndexValueVo> getWaterIndexInfo(@Param("statisticsType") Integer statisticsType,
                                             @Param("variableCode") String variableCode,
                                             @Param("startDate") Date startDate,
                                             @Param("endDate") Date endDate);

    Scada selectLastOne(@Param("code")String code,@Param("scadaTime")Date scadaTime);

    List<com.tofly.scada.entity.vo.ScadaIndexVo> viewScadaDataShort(ScadaQueryDto dto);
}
