package com.gyee.alarm.mapper.auto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.alarm.model.auto.ProEconShutdownEvent2;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gyee.alarm.model.vo.AlarmTsVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

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

/**
 * <p>
 * 停机事件2 Mapper 接口
 * </p>
 *
 * @author shilin
 * @since 2022-10-21
 */
public interface ProEconShutdownEvent2Mapper extends BaseMapper<ProEconShutdownEvent2> {


    @Select({"<script>",
            "SELECT UPPER(t.name),count(*) FROM pro_econ_shutdown_event2 as s,pro_econ_alarm_configuration as a," +
                    "pro_econ_alarm_type as t where s.warning_id=a.id and UPPER(a.components)=UPPER(t.nem_code)  " ,
            "<where>" ,

            "<if test='wpId != null'>",
            "and a.station_id like #{wpId}",
            "</if>",
            "<if test='modelId != null'>",
            "and  a.model_id=#{modelId}",
            "</if>",
            "and s.stop_time &gt;= #{begin}",
            "and s.stop_time &lt;= #{end}",

            "</where>",
            " GROUP BY t.name ",
            "</script>"})
    List<ProEconShutdownEvent2> queryShutdownEvent2ByType(@Param("wpId") String wpId, @Param("modelId") String modelId,@Param("begin")Date begin, @Param("end") Date end);

    @Select({"<script>",
            "SELECT  DATE(stop_time) AS stop_day, count(*) as times,COALESCE(SUM(s.stop_hours), 0)  hours  FROM pro_econ_shutdown_event2 as s,pro_econ_alarm_configuration as a," +
                    "pro_econ_alarm_type as t where s.warning_id=a.id  " ,
            "<where>" ,

            "<if test='wpId != null'>",
            "and a.station_id like #{wpId}",
            "</if>",
            "<if test='modelId != null'>",
            "and  a.model_id=#{modelId}",
            "</if>",
            "and s.stop_time &gt;= #{begin}",
            "and s.stop_time &lt;= #{end}",

            "</where>",
            " GROUP BY stop_day ",
            "</script>"})
    List<ProEconShutdownEvent2> queryShutdownEvent2ByDay(@Param("wpId") String wpId, @Param("modelId") String modelId,@Param("begin")Date begin, @Param("end") Date end);


    @Select({"<script>",
            "SELECT TO_CHAR(s.stop_time, 'YYYY-MM') AS year_month, count(*) as times,COALESCE(SUM(s.stop_hours), 0)  hours  FROM pro_econ_shutdown_event2 as s," +
                    "pro_econ_alarm_configuration as a,pro_econ_alarm_type as t " +
                    "where s.warning_id=a.id and s.warning_id is not null  " +

            "<where>" ,

            "<if test='wpId != null'>",
            "and a.station_id like #{wpId}",
            "</if>",
            "<if test='modelId != null'>",
            "and  a.model_id=#{modelId}",
            "</if>",
            "and s.stop_time &gt;= #{begin}",
            "and s.stop_time &lt;= #{end}",

            "</where>",
            " GROUP BY year_month ",
            "</script>"})
    List<ProEconShutdownEvent2> queryShutdownEvent2ByMonth(@Param("wpId") String wpId, @Param("modelId") String modelId,@Param("begin")Date begin, @Param("end") Date end);

    @Select({"<script>",
            "SELECT '机械故障' AS 故障类型  ,count(*) as times,COALESCE(SUM(s.stop_hours), 0)  hours  FROM pro_econ_shutdown_event2 as s,pro_econ_alarm_configuration as a," +
                    "pro_econ_alarm_type as t where s.warning_id=a.id and UPPER(a.components)=UPPER(t.nem_code) and " +
                    "s.warning_id is not null  " ,
            "<where>" ,

            "<if test='wpId != null'>",
            "and a.station_id like #{wpId}",
            "</if>",
            "<if test='modelId != null'>",
            "and  a.model_id=#{modelId}",
            "</if>",
            "and s.stop_time &gt;= #{begin}",
            "and s.stop_time &lt;= #{end}",
            "and (t.nem_code='YP' or t.nem_code='LG' or t.nem_code='PHXT' or t.nem_code='BJXT' )",
            "</where>",

            "</script>"})
    List<ProEconShutdownEvent2> queryShutdownEvent2ByJX(@Param("wpId") String wpId, @Param("modelId") String modelId,@Param("begin")Date begin, @Param("end") Date end);

    @Select({"<script>",
            "SELECT '电气故障' AS 故障类型  ,count(*) as times,COALESCE(SUM(s.stop_hours), 0)  hours  FROM pro_econ_shutdown_event2 as s,pro_econ_alarm_configuration as a," +
                    "pro_econ_alarm_type as t where s.warning_id=a.id and UPPER(a.components)=UPPER(t.nem_code) and " +
                    "s.warning_id is not null  " ,
            "<where>" ,

            "<if test='wpId != null'>",
            "and a.station_id like #{wpId}",
            "</if>",
            "<if test='modelId != null'>",
            "and  a.model_id=#{modelId}",
            "</if>",
            "and s.stop_time &gt;= #{begin}",
            "and s.stop_time &lt;= #{end}",
            "and (t.nem_code='FDJ' or t.nem_code='BPQ' or t.nem_code='KZXT'  )",
            "</where>",

            "</script>"})
    List<ProEconShutdownEvent2> queryShutdownEvent2ByDQ(@Param("wpId") String wpId, @Param("modelId") String modelId,@Param("begin")Date begin, @Param("end") Date end);


    @Select({"<script>",
            "SELECT '其它故障' AS 故障类型 ,count(*) as times,COALESCE(SUM(s.stop_hours), 0)  hours FROM pro_econ_shutdown_event2 as s,pro_econ_alarm_configuration as a," +
                    "pro_econ_alarm_type as t where s.warning_id=a.id and UPPER(a.components)=UPPER(t.nem_code) and " +
                    "s.warning_id is not null  " ,
            "<where>" ,

            "<if test='wpId != null'>",
            "and a.station_id like #{wpId}",
            "</if>",
            "<if test='modelId != null'>",
            "and  a.model_id=#{modelId}",
            "</if>",
            "and s.stop_time &gt;= #{begin}",
            "and s.stop_time &lt;= #{end}",
            "and (t.nem_code=!='FDJ' and t.nem_code!='BPQ' and t.nem_code!='KZXT' and t.nem_code!='YP' and t.nem_code!='LG' and t.nem_code!='PHXT' and t.nem_code!='BJXT' )",
            "</where>",

            "</script>"})
    List<ProEconShutdownEvent2> queryShutdownEvent2ByOther(@Param("wpId") String wpId, @Param("modelId") String modelId,@Param("begin")Date begin, @Param("end") Date end);

}
