<?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.ho.flow.mapper.AlarmConfigMapper">

    <resultMap id="BaseResultMap" type="com.ho.flow.vo.AlarmConfig">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="groupId" column="group_id" jdbcType="INTEGER"/>
        <result property="stationId" column="station_id" jdbcType="INTEGER"/>
        <result property="colDefineId" column="col_define_id" jdbcType="INTEGER"/>
        <result property="deviceTypeId" column="device_type_id" jdbcType="INTEGER"/>
        <result property="deviceType" column="device_type" jdbcType="VARCHAR"/>
        <result property="col" column="col" jdbcType="VARCHAR"/>
        <result property="colName" column="col_name" jdbcType="VARCHAR"/>
        <result property="sensType" column="sens_type" jdbcType="VARCHAR"/>
        <result property="info0" column="info0" jdbcType="VARCHAR"/>
        <result property="info1" column="info1" jdbcType="VARCHAR"/>
        <result property="upperLimit" column="upper_limit" jdbcType="DECIMAL"/>
        <result property="lowerLimit" column="lower_limit" jdbcType="DECIMAL"/>
        <result property="alarmLevel" column="alarm_level" jdbcType="DECIMAL"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="deleted" column="deleted" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        ,group_id,station_id,`scope`,
        col_define_id,device_type_id,device_name,device_type,
        col,col_name,sens_type,info0,info1,
        upper_limit,lower_limit,alarm_level,
        create_time,update_time,deleted
    </sql>

    <sql id="Insert_Batch_Column_List">
        group_id
        ,station_id,`scope`,
        col_define_id,device_type_id,device_name,device_type,
        col,col_name,sens_type,info0,info1,upper_limit,lower_limit,alarm_level,
        create_time,deleted
    </sql>


    <insert id="insert" keyColumn="id" keyProperty="id">
        insert into alarm_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="record.groupId != null">group_id,</if>
            <if test="record.stationId != null">station_id,</if>
            <if test="record.scope != null">scope,</if>
            <if test="record.colDefineId != null">col_define_id,</if>
            <if test="record.deviceTypeId != null">device_type_id ,</if>
            <if test="record.deviceName != null">device_name ,</if>
            <if test="record.deviceType != null">device_type,</if>
            <if test="record.col != null">col,</if>
            <if test="record.colName != null">col_name,</if>
            <if test="record.sensType != null">sens_type,</if>
            <if test="record.info0 != null">info0,</if>
            <if test="record.info1 != null">info1,</if>
            <if test="record.upperLimit != null">upper_limit,</if>
            <if test="record.lowerLimit != null">lower_limit,</if>
            <if test="record.alarmLevel != null">alarm_level,</if>
            <if test="record.createTime != null">create_time,</if>
            <if test="record.updateTime != null">update_time,</if>
            <if test="record.deleted != null">deleted,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="record.groupId != null">#{record.groupId,jdbcType=INTEGER},</if>
            <if test="record.stationId!= null">#{record.stationId,jdbcType=INTEGER},</if>
            <if test="record.scope!= null">#{record.scope,jdbcType=INTEGER},</if>
            <if test="record.colDefineId != null">#{record.colDefineId},</if>
            <if test="record.deviceTypeId != null">#{record.deviceTypeId},</if>
            <if test="record.deviceName != null">#{record.deviceName},</if>
            <if test="record.deviceType != null">#{record.deviceType},</if>
            <if test="record.col != null">#{record.col},</if>
            <if test="record.colName != null">#{record.colName},</if>
            <if test="record.sensType != null">#{record.sensType},</if>
            <if test="record.info0 != null">#{record.info0},</if>
            <if test="record.info1 != null">#{record.info1},</if>
            <if test="record.upperLimit != null">#{record.upperLimit},</if>
            <if test="record.lowerLimit != null">#{record.lowerLimit},</if>
            <if test="record.alarmLevel != null">#{record.alarmLevel},</if>
            <if test="record.createTime != null">#{record.createTime},</if>
            <if test="record.updateTime != null">#{record.updateTime},</if>
            <if test="record.deleted != null">#{record.deleted},</if>

        </trim>
    </insert>

    <insert id="insertBatch" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into alarm_config (<include refid="Insert_Batch_Column_List"/>)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.groupId},#{item.stationId},#{item.scope},
            #{item.colDefineId},#{item.deviceTypeId},#{item.deviceName},#{item.deviceType},
            #{item.col},#{item.colName},#{item.sensType},#{item.info0},#{item.info1},#{item.upperLimit},#{item.lowerLimit},
            #{item.alarmLevel},now(),1
            )
        </foreach>
    </insert>

    <update id="updateBatch" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        <foreach collection="list" item="item" index="index" separator=";">
            update alarm_config set device_name = #{item.deviceName},col_name = #{item.colName},sens_type = #{item.sensType},
            info0 = #{item.info0},info1 = #{item.info1},upper_limit = #{item.upperLimit},lower_limit = #{item.lowerLimit},
            alarm_level = #{item.alarmLevel}, update_time = #{item.updateTime}
            where id = #{item.id}
        </foreach>
    </update>

    <update id="updateByPrimaryKeySelective">
        update alarm_config
        <set>
            <if test="record.groupId != null">
                group_id = #{record.groupId,jdbcType=INTEGER},
            </if>
            <if test="record.stationId != null">
                station_id = #{record.stationId,jdbcType=INTEGER},
            </if>
            <if test="record.colDefineId != null">
                col_define_id = #{record.colDefineId},
            </if>
            <if test="record.deviceTypeId != null">
                device_type_id = #{record.deviceTypeId},
            </if>

            <if test="record.deviceType != null">
                device_type = #{record.deviceType},
            </if>
            <if test="record.deviceName != null">
                device_name = #{record.deviceName},
            </if>
            <if test="record.col != null">
                col = #{record.col},
            </if>
            <if test="record.colName != null">
                col_name = #{record.colName},
            </if>
            <if test="record.sensType != null">
                sens_type = #{record.sensType},
            </if>
            <if test="record.upperLimit != null">
                upper_limit = #{record.upperLimit},
            </if>
            <if test="record.lowerLimit != null">
                lower_limit = #{record.lowerLimit},
            </if>
            <if test="record.alarmLevel != null">
                alarm_level = #{record.alarmLevel},
            </if>
            <if test="record.createTime != null">
                create_time = #{record.createTime},
            </if>
            <if test="record.updateTime != null">
                update_time = #{record.updateTime},
            </if>
            <if test="record.deleted != null">
                deleted = #{record.deleted},
            </if>
        </set>
        where id = #{record.id,jdbcType=INTEGER}
    </update>

    <select id="selectById" resultType="com.ho.flow.vo.AlarmConfig">
        select
        <include refid="Base_Column_List"/>
        from alarm_config
        where id = #{id,jdbcType=INTEGER}
    </select>

    <select id="selectList" resultType="com.ho.flow.vo.AlarmConfig">
        select
        <include refid="Base_Column_List"/>
        from alarm_config
        <where>
            <if test="record.groupId != null">
                and group_id = #{record.groupId}
            </if>
            <if test="record.stationId != null">
                and station_id = #{record.stationId}
            </if>
            <if test="record.deviceType != null">
                and device_type = #{record.deviceType}
            </if>
            <if test="record.sensType != null">
                and sens_type = #{record.sensType}
            </if>
            <if test="record.alarmLevel != null">
                and alarm_level = #{record.alarmLevel}
            </if>
            <if test="record.otherAlarmLevel != null">
                and alarm_level != #{record.otherAlarmLevel}
            </if>
            <if test="record.scope != null">
                and `scope` = #{record.scope}
            </if>
            <if test="record.colIds!=null and record.colIds.size()!=0">
                and col_define_id in
                <foreach collection="record.colIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="record.colName != null and record.colName !=''">
                and `col_name` LIKE CONCAT('%',#{record.colName},'%')
            </if>
            <if test="record.colNameList != null and record.colNameList.size >0 ">
                <foreach collection="record.colNameList" item="item" open=" " close=" " separator=" ">
                    and `col_name` LIKE CONCAT('%',#{item},'%')
                </foreach>
            </if>
            <if test="record.deviceTypeList != null and record.deviceTypeList.size > 0">
                and device_type in
                <foreach collection="record.deviceTypeList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="record.deviceTypeSuffix != null">
                and device_type like concat('%',#{record.deviceTypeSuffix})
            </if>
            and deleted = 1
        </where>
        order by group_id,station_id, col, scope
    </select>

    <select id="selectRepeatValue" resultType="com.ho.flow.vo.AlarmConfig">
        select
        <include refid="Base_Column_List"/>
        from alarm_config
        <where>
            <if test="groupId != null">
                and group_id = #{groupId}
            </if>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="scope != null">
                and scope = #{scope}
            </if>
            <if test="colDefineId != null">
                and col_define_id = #{colDefineId}
            </if>
            and deleted = 1
        </where>
    </select>

    <select id="selectListByParams" resultType="com.ho.flow.vo.AlarmConfig">
        select
        <include refid="Base_Column_List"/>
        from alarm_config
        <where>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="deviceType != null">
                and device_type = #{deviceType}
            </if>
        </where>
    </select>

    <delete id="deleteByPrimaryKey">
        delete
        from alarm_config
        where id = #{id,jdbcType=INTEGER}
    </delete>

    <delete id="deleteByParam">
        delete
        from alarm_config
        where station_id = #{stationId} or device_type like concat('%',#{deviceType})
    </delete>

    <delete id="deleteBatch">
        delete
        from alarm_config
        <where>
            and id in
            <foreach collection="list" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </where>
    </delete>

    <update id="updateByParams">
        update alarm_config
        <set>
            <if test="record.upperLimit != null">
                upper_limit = #{record.upperLimit},
            </if>
            <if test="record.lowerLimit != null">
                lower_limit = #{record.lowerLimit},
            </if>
            <if test="record.alarmLevel != null">
                alarm_level = #{record.alarmLevel},
            </if>
            <if test="record.updateTime != null">
                update_time = #{record.updateTime},
            </if>
            <if test="record.deleted != null">
                deleted = #{record.deleted},
            </if>
        </set>
        <where>
            <if test="record.scope != null">
                and `scope` = #{record.scope}
            </if>
            <if test="record.stationId != null">
                and station_id = #{record.stationId,jdbcType=INTEGER}
            </if>
            <if test="record.id != null">
                and col_define_id = #{record.id}
            </if>
            <if test="record.deviceType != null">
                and device_type = #{record.deviceType}
            </if>
        </where>
    </update>
</mapper>
