<?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.branch.mapper.TBranchDemeanorMapper">
    
    <resultMap type="TBranchDemeanor" id="TBranchDemeanorResult">
        <result property="demeanorId"    column="demeanor_id"    />
        <result property="demeanorTitle"    column="demeanor_title"    />
        <result property="demeanorContent"    column="demeanor_content"    />
        <result property="isPinned"    column="is_pinned"    />
        <result property="deptId"    column="dept_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectTBranchDemeanorVo">
        select u.demeanor_id, u.demeanor_title, u.demeanor_content, u.is_pinned, u.dept_id, u.del_flag, u.create_by, u.create_time, u.update_by, u.update_time from t_branch_demeanor u left join sys_dept d on u.dept_id = d.dept_id
    </sql>

    <select id="selectTBranchDemeanorList" parameterType="TBranchDemeanor" resultMap="TBranchDemeanorResult">
        <include refid="selectTBranchDemeanorVo"/>
        <where>  
            <if test="demeanorId != null "> and u.demeanor_id = #{demeanorId}</if>
            <if test="demeanorTitle != null  and demeanorTitle != ''"> and u.demeanor_title like concat(concat('%', #{demeanorTitle}), '%')</if>
            <if test="demeanorContent != null  and demeanorContent != ''"> and u.demeanor_content like concat(concat('%', #{demeanorContent}), '%')</if>
            <if test="isPinned != null  and isPinned != ''"> and u.is_pinned = #{isPinned}</if>
            <if test="deptId != null "> and u.dept_id = #{deptId}</if>
            and u.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by IS_PINNED desc
    </select>
    
    <select id="selectTBranchDemeanorByDemeanorId" parameterType="Long" resultMap="TBranchDemeanorResult">
        <include refid="selectTBranchDemeanorVo"/>
        where u.demeanor_id = #{demeanorId}
        and u.del_flag = 0
    </select>
        
    <insert id="insertTBranchDemeanor" parameterType="TBranchDemeanor">
        <selectKey keyProperty="demeanorId" resultType="long" order="BEFORE">
            SELECT seq_t_branch_demeanor.NEXTVAL as demeanorId FROM DUAL
        </selectKey>
        insert into t_branch_demeanor
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="demeanorId != null">demeanor_id,</if>
            <if test="demeanorTitle != null and demeanorTitle != ''">demeanor_title,</if>
            <if test="demeanorContent != null">demeanor_content,</if>
            <if test="isPinned != null">is_pinned,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="demeanorId != null">#{demeanorId},</if>
            <if test="demeanorTitle != null and demeanorTitle != ''">#{demeanorTitle},</if>
            <if test="demeanorContent != null">#{demeanorContent},</if>
            <if test="isPinned != null">#{isPinned},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateTBranchDemeanor" parameterType="TBranchDemeanor">
        update t_branch_demeanor
        <trim prefix="SET" suffixOverrides=",">
            <if test="demeanorTitle != null and demeanorTitle != ''">demeanor_title = #{demeanorTitle},</if>
            <if test="demeanorContent != null">demeanor_content = #{demeanorContent},</if>
            <if test="isPinned != null">is_pinned = #{isPinned},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where demeanor_id = #{demeanorId}
    </update>

    <update id="deleteTBranchDemeanorByDemeanorId" parameterType="Long">
        update t_branch_demeanor set del_flag = 2 where demeanor_id = #{demeanorId}
    </update>

    <update id="deleteTBranchDemeanorByDemeanorIds" parameterType="String">
        update t_branch_demeanor set del_flag = 2 where demeanor_id in
        <foreach item="demeanorId" collection="array" open="(" separator="," close=")">
            #{demeanorId}
        </foreach>
    </update>
</mapper>