package com.gyee.alarm.mapper.auto;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.alarm.model.auto.ProEconAlarmPlan;
import com.gyee.alarm.model.auto.ProEconAlarmRule;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gyee.alarm.model.vo.AlarmRuleVo;
import com.gyee.common.model.Alertrule2;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 自定义报警配置 Mapper 接口
 * </p>
 *
 * @author shilin
 * @since 2023-08-09
 */
public interface ProEconAlarmRuleMapper extends BaseMapper<ProEconAlarmRule> {



    @Update({"<script>",
            "update pro_econ_alarm_rule ",
            "<set>",
            "name = #{name},",
            "description = #{description},",
            "expression = #{expression},",
            "tag = #{tag}," ,
            "rank = #{rank},",
            "enable = #{enable},",
            "model_id = #{modelId},",
            "category = #{category}," ,
            "range =#{range}," ,
            "station_id =#{stationId}," ,
            "device_id = #{deviceId}," ,
            "line_id = #{lineId}," ,
            "project_id = #{projectId}," ,
            "electrical_id = #{electricalId}," ,
            "related_parts = #{relatedParts}," ,
            "create_time = #{createTime}," ,
            "uniform_code = #{uniformCode}" ,
            "</set>",
            "where id = #{id}",

            "</script>"})
    Integer updateByAlertruleId(ProEconAlarmRule alertrule);

    @Select({"<script>",
            "select a.* from pro_econ_alarm_rule a",
            "<where>",
            " 1=1 ",

            "<if test='wpId !=null  '>",
            "and a.station_id  = #{wpId} ",
            "</if>",
            "<if test='name !=null  '>",
            "and a.description like #{name} ",
            "</if>",
            "<if test='modelId !=null  '>",
            "and a.model_id = #{modelId} ",
            "</if>",
            "<if test='rank !=null  '>",
            "and a.rank = #{rank} ",
            "</if>",
            "<if test='category !=null  '>",
            "and a.category = #{category} ",
            "</if>",
            "<if test='enable !=null  '>",
            "and a.enable = #{enable} ",
            "</if>",
            "<if test='relatedparts !=null  '>",
            "and a.related_parts = #{relatedparts} ",
            "</if>",
            "</where>",

            "</script>"})
    IPage<ProEconAlarmRule> pageQueryAll(Page page,
                                         @Param("name") String name,
                                         @Param("wpId") String wpId,
                                         @Param("modelId") String modelId,
                                         @Param("rank") Integer rank,
                                         @Param("category") String category,
                                         @Param("enable") Boolean enable,
                                         @Param("relatedparts") String relatedparts);

    @Select("select distinct a.related_parts as relatedParts,a.name as alarmName,d.name  as typeName from pro_econ_alarm_rule a " +
            "left join pro_econ_alarm_type d on d.nem_code = a.related_parts " +
            "where related_parts is not null")
    List<AlarmRuleVo> queryTree();


    @Select({"<script>",
            "select a.* from pro_econ_alarm_rule a",
            "<where>",
            " 1=1 ",

            "<if test='wpId !=null  '>",
            "and a.station_id  = #{wpId} ",
            "</if>",
            "<if test='name !=null  '>",
            "and a.name =  #{name} ",
            "</if>",
            "<if test='modelId !=null  '>",
            "and a.model_id = #{modelId} ",
            "</if>",
            "</where>",

            "</script>"})
    List<ProEconAlarmRule> queryObject( @Param("name") String name,
                                 @Param("wpId") String wpId,
                                 @Param("modelId") String modelId);
    @Select(" select a.name from  pro_econ_alarm_rule a where a.related_parts = #{relatedParts}   group by a.name ")
    List<ProEconAlarmRule> quertByrelatedParts(@Param("relatedParts") String relatedParts);

    @Select({"<script>",
            "select a.* from pro_econ_alarm_rule a",
            "<where>",
            "a.station like '%#{station}%'",
            "<if test='modelId !=null '>",
            "and a.model_id like '%#{modelId}%'",
            "</if>",

            "</where>",

            "</script>"})
    List<ProEconAlarmRule> getAllByStationIdAndModelId(@Param("station") String station,
                                                       @Param("modelId") String modelId
    );


    @Select({"<script>",
            "select a.* from pro_econ_alarm_rule a",
            "<where>",
            "a.station like '%#{station}%'",

            "<if test='name !=null  '>",
            "and a.name like '%#{name}%'",
            "</if>",
            "<if test='modelid !=null  '>",
            "and a.model_id like '%#{modelid}%'",
            "</if>",
            "</where>",

            "</script>"})
    List<ProEconAlarmRule> getUniformCodeByNameAndStation(@Param("name") String name, @Param("station") String station, @Param("modelid") String modelid);
}
