<?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.ruoyi.project.system.mapper.TAlarmPrincipalMapper">

    <resultMap type="TAlarmPrincipal" id="TAlarmPrincipalResult">
        <result property="id"    column="id"    />
        <result property="typeId"    column="type_id"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="staffid"    column="staffid"    />
        <result property="principalName"    column="principal_name"    />
        <result property="principalEmail"    column="mail"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createrCode"    column="creater_code"    />
        <result property="createdate"    column="createdate"    />
        <result property="updaterCode"    column="updater_code"    />
        <result property="updatedate"    column="updatedate"    />
        <result property="deptId"    column="dept_id"    />
        <result property="remarks"    column="remarks"    />
        <result property="deptName" column="dept_name" />
    </resultMap>

    <sql id="selectTAlarmPrincipalVo">
        select d.id, d.type_id, d.plant_code, d.staffid, d.principal_name, t.email as mail, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks ,s.dept_name from t_alarm_principal d
      left join sys_dept s on s.dept_id = d.dept_id
      left join sys_user t on t.staffid = d.staffid
    </sql>

    <select id="selectTAlarmPrincipalList" parameterType="TAlarmPrincipal" resultMap="TAlarmPrincipalResult">
        <include refid="selectTAlarmPrincipalVo"/>
        <where>
            <if test="typeId != null  and typeId != ''"> and type_id = #{typeId}</if>
            <if test="principalName != null  and principalName != ''"> and principal_name like concat(concat('%', #{principalName}), '%')</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectList" parameterType="TAlarmPrincipal" resultMap="TAlarmPrincipalResult">
        <include refid="selectTAlarmPrincipalVo"/>
        <where>
            <if test="typeId != null  and typeId != ''"> and type_id = #{typeId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectTAlarmPrincipalById" parameterType="Long" resultMap="TAlarmPrincipalResult">
        <include refid="selectTAlarmPrincipalVo"/>
        where d.id = #{id}
    </select>

    <insert id="insertTAlarmPrincipal" parameterType="TAlarmPrincipal">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_alarm_principal.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_alarm_principal
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="plantCode != null and plantCode != ''">plant_code,</if>
            <if test="staffid != null and staffid != ''">staffid,</if>
            <if test="principalName != null and principalName != ''">principal_name,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="plantCode != null and plantCode != ''">#{plantCode},</if>
            <if test="staffid != null and staffid != ''">#{staffid},</if>
            <if test="principalName != null and principalName != ''">#{principalName},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateTAlarmPrincipal" parameterType="TAlarmPrincipal">
        update t_alarm_principal
        <trim prefix="SET" suffixOverrides=",">
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
            <if test="staffid != null and staffid != ''">staffid = #{staffid},</if>
            <if test="principalName != null and principalName != ''">principal_name = #{principalName},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTAlarmPrincipalById" parameterType="Long">
        update t_alarm_principal set del_flag = 2 where id = #{id}
    </update>

    <update id="deleteTAlarmPrincipalByIds" parameterType="String">
        update t_alarm_principal set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
