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.StatisticsScada;
import com.tofly.scada.entity.dto.ScadaQuery;
import com.tofly.scada.entity.vo.ScadaVo;
import com.tofly.scada.entity.vo.StatisticsScadaVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
* @author haiqiu
* @description 针对表【STATISTICS_SCADA】的数据库操作Mapper
* @createDate 2022-05-10 15:55:35
* @Entity com.tofly.scada.entity.StatisticsScada
*/
public interface StatisticsScadaMapper extends BaseMapper<StatisticsScada> {


    /**
     * 查询统计SCADA数据
     * @param page 分页
     * @param queryWrapper 条件
     * @return
     */
    @Select("SELECT scada.*,manage.NAME name,manage.MAKE_DATE makeDate,manage.TYPE type," +
            "manage.MANUFACTURER manufacturer,manage.LEADER leader,manage.PHONE phone," +
            "allocation.DISPLAY_NAME displayName, allocation.VARIABLE_NAME variableName, " +
            "allocation.UNIT unit" +
            " FROM ${tableName} scada " +
            "INNER JOIN TF_INDEX_ALLOCATION allocation ON allocation.VARIABLE_CODE =  scada.CODE " +
            "INNER JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.ID = allocation.DEVICE_ID " +
            "${ew.customSqlSegment} ")
    Page<StatisticsScadaVo> pageList(Page page, @Param(Constants.WRAPPER) QueryWrapper<ScadaQuery> queryWrapper,
                                     @Param("tableName") String tableName);


    @Select("SELECT scada.*,manage.NAME name,manage.MAKE_DATE makeDate,manage.TYPE type," +
            "manage.MANUFACTURER manufacturer,manage.LEADER leader,manage.PHONE phone," +
            "allocation.DISPLAY_NAME displayName, allocation.VARIABLE_NAME variableName, " +
            "allocation.UNIT unit" +
            " FROM ${tableName} scada " +
            "INNER JOIN TF_INDEX_ALLOCATION allocation ON allocation.VARIABLE_CODE =  scada.CODE " +
            "INNER JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.ID = allocation.DEVICE_ID " +
            "${ew.customSqlSegment} ")
    List<StatisticsScadaVo> statisticsList(Page page, @Param(Constants.WRAPPER) QueryWrapper<ScadaQuery> queryWrapper,
                                     @Param("tableName") String tableName);

//    /**
//     * 查询统计SCADA数据列表
//     * @param queryWrapper 条件
//     * @return
//     */
//    @Select("SELECT scada.*,manage.NAME name,manage.MAKE_DATE makeDate,manage.TYPE type," +
//            "manage.MANUFACTURER manufacturer,manage.LEADER leader,manage.PHONE phone," +
//            "allocation.DISPLAY_NAME displayName, allocation.VARIABLE_NAME variableName, " +
//            "allocation.UNIT unit" +
//            " FROM ${tableName} scada " +
//            "INNER JOIN TF_INDEX_ALLOCATION allocation ON allocation.VARIABLE_CODE =  scada.CODE " +
//            "INNER JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.ID = allocation.DEVICE_ID " +
//            "${ew.customSqlSegment} ")
//    List<StatisticsScadaVo> list(@Param(Constants.WRAPPER) QueryWrapper<ScadaQuery> queryWrapper,
//                                     @Param("tableName") String tableName);



    /**
     * 查询统计SCADA数据
     * @param queryWrapper 条件
     * @return
     */
    @Select("SELECT scada.CODE code,scada.value value,scada.SCADA_TIME scadaTime,scada.UPDATE_TIME updateTime,manage.NAME name,manage.MAKE_DATE makeDate,manage.TYPE type," +
            "manage.MANUFACTURER manufacturer,manage.LEADER leader,manage.PHONE phone," +
            "allocation.DISPLAY_NAME displayName, allocation.VARIABLE_NAME variableName, " +
            "allocation.UNIT unit" +
            " FROM ${tableName} scada " +
            "INNER JOIN TF_INDEX_ALLOCATION allocation ON allocation.VARIABLE_CODE =  scada.CODE " +
            "INNER JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.ID = allocation.DEVICE_ID " +
            "${ew.customSqlSegment} ")
    List<StatisticsScadaVo> selectListData(@Param(Constants.WRAPPER) QueryWrapper<ScadaQuery> queryWrapper,
                                           @Param("tableName") String tableName);



    /**
     * 查询统计SCADA数据
     * @param page 分页
     * @param queryWrapper 条件
     * @return
     */
    @Select("SELECT scada.CODE code,scada.value value,scada.SCADA_TIME scadaTime,scada.UPDATE_TIME updateTime,manage.NAME name,manage.MAKE_DATE makeDate,manage.TYPE type," +
            "manage.MANUFACTURER manufacturer,manage.LEADER leader,manage.PHONE phone," +
            "allocation.DISPLAY_NAME displayName, allocation.VARIABLE_NAME variableName, " +
            "allocation.UNIT unit" +
            " FROM ${tableName} scada " +
            "INNER JOIN TF_INDEX_ALLOCATION allocation ON allocation.VARIABLE_CODE =  scada.CODE " +
            "INNER JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.ID = allocation.DEVICE_ID " +
            "${ew.customSqlSegment} ")
    Page<ScadaVo> pageListNotStatistics(Page page, @Param(Constants.WRAPPER) QueryWrapper<ScadaQuery> queryWrapper,
                                     @Param("tableName") String tableName);


    /**
     * 查询统计SCADA数据
     * @param queryWrapper 条件
     * @return
     */
    @Select("SELECT scada.CODE code,scada.value value,scada.SCADA_TIME scadaTime,scada.UPDATE_TIME updateTime,manage.NAME name,manage.MAKE_DATE makeDate,manage.TYPE type," +
            "manage.MANUFACTURER manufacturer,manage.LEADER leader,manage.PHONE phone," +
            "allocation.DISPLAY_NAME displayName, allocation.VARIABLE_NAME variableName, " +
            "allocation.UNIT unit" +
            " FROM ${tableName} scada " +
            "INNER JOIN TF_INDEX_ALLOCATION allocation ON allocation.VARIABLE_CODE =  scada.CODE " +
            "INNER JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.ID = allocation.DEVICE_ID " +
            "${ew.customSqlSegment} ")
    List<ScadaVo> selectListDataNotStatistics(@Param(Constants.WRAPPER) QueryWrapper<ScadaQuery> queryWrapper,
                                              @Param("tableName") String tableName);
    Page<ScadaVo>  selectHistoryByMouthsTables(@Param("mouthTables") List<String> mouthTables,
                                                        Page page,
                                                        @Param(Constants.WRAPPER)  QueryWrapper<ScadaQuery> queryWrapper);

    List<ScadaVo>  selectHistoryByMouthsTablesNoPage(@Param("mouthTables") List<String> mouthTables,
                                               @Param(Constants.WRAPPER)  QueryWrapper<ScadaQuery> queryWrapper);



    /**
     * 删除统计数据
     * @param dateType 类型
     * @param codes 编码
     * @param start 开始时间
     * @param end 结束时间
     * @return 成功条数
     */
    int deleteByTime(Integer dateType, List<String> codes, Date start, Date end);

    void insertBacthHistoryTemp(@Param("historyTempList") List<Scada> historyTempList);
}
