<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.casic.missiles.modular.system.dao.AlarmLevelMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.modular.system.model.AlarmLevel">
        <id column="ID" property="id"/>
        <result column="NAME" property="name"/>
        <result column="DEVICE_TYPE" property="deviceType"/>
        <result column="LEVEL" property="level"/>
        <result column="LOW_VALUE" property="lowValue"/>
        <result column="HIGH_VALUE" property="highValue"/>
        <result column="OPEN_ALARM" property="openAlarm"/>
        <result column="OPEN_JOB" property="openJob"/>
        <result column="TENANT_ID" property="tenantId"/>
        <result column="TS" property="ts"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID AS id, NAME AS name, DEVICE_TYPE AS "deviceType", LEVEL AS level,
        LOW_VALUE AS "lowValue", HIGH_VALUE AS "highValue", OPEN_ALARM AS "openAlarm",
        OPEN_JOB AS "openJob", TENANT_ID AS "tenantId", TS AS ts
    </sql>

    <select id="getLevelList" resultType="map">
        SELECT
            level AS "level",
            name  "name",
            OPEN_ALARM AS "openAlarm",
            OPEN_JOB AS "openJob",
            TS AS "ts"
        FROM  alarm_level
        WHERE TENANT_ID = #{tenantId}
        <if test="deviceType!=-1">
            AND   DEVICE_TYPE = #{deviceType}
        </if>
    </select>

    <select id="selectByNameAndTenList" resultType="com.casic.missiles.modular.system.model.AlarmLevel">
        SELECT
        <include refid="Base_Column_List"/>
        FROM alarm_level
        WHERE TENANT_ID = #{tenantId} AND NAME = #{name}
    </select>

    <select id="selectByNameAndTenAndLevel" resultType="com.casic.missiles.modular.system.model.AlarmLevel">
        SELECT
        <include refid="Base_Column_List"/>
        FROM alarm_level
        WHERE TENANT_ID = #{tenantId} AND NAME = #{name} AND LEVEL = #{level}
    </select>

    <select id="selectByID" resultType="com.casic.missiles.modular.system.model.AlarmLevel">
        SELECT
        <include refid="Base_Column_List"/>
        FROM alarm_level
        WHERE ID = #{id}
    </select>

    <delete id="deleteByCondition">
        DELETE
        FROM alarm_level
        WHERE
        <choose>
            <when test="(deviceType != null and deviceType != '')  or (tenantId != null and tenantId != '')
                   or (name != null and name != '')">
                1=1
                <if test="deviceType!='' and deviceType!=null">
                    AND DEVICE_TYPE = #{deviceType}
                </if>
                <if test="name!='' and name!=null">
                    AND NAME= #{name}
                </if>
                <if test="tenantId!='' and tenantId!=null">
                    AND TENANT_ID = #{tenantId}
                </if>
            </when>
            <otherwise>
                1=0
            </otherwise>
        </choose>
    </delete>

</mapper>
