<?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.qryord.project.master.roleField.mapper.RoleFieldMapper">
    
    <resultMap type="RoleField" id="RoleFieldResult">
        <result property="id"    column="id"    />
        <result property="roleKey"    column="role_key"    />
        <result property="fieldKey"    column="field_key"    />
        <result property="showFlag"    column="show_flag"    />
        <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="selectRoleFieldVo">
        select id, role_key, field_key, show_flag, del_flag, create_by, create_time, update_by, update_time
        from m_role_field
    </sql>

    <select id="selectRoleList" parameterType="RoleField" resultMap="RoleFieldResult">
        select role_key
        from sys_role sr
        where sr.del_flag = 0
    </select>

    <select id="selectFieldList" parameterType="RoleField" resultMap="RoleFieldResult">
        select mrf.id, mf.field_key, ifnull(mrf.show_flag,0) show_flag
        from m_field mf
        left join m_role_field mrf on mrf.field_key = mf.field_key and mrf.role_key = #{roleKey}
        where mf.del_flag = 0
        order by mf.sort
    </select>

    <select id="selectRoleFieldList" parameterType="RoleField" resultMap="RoleFieldResult">
        <include refid="selectRoleFieldVo"/>
        <where>  
            <if test="roleKey != null  and roleKey != ''"> and role_key = #{roleKey}</if>
            <if test="fieldKey != null and fieldKey != ''"> and field_key like concat('%', #{fieldKey}, '%')</if>
            <if test="showFlag != null  and showFlag != ''"> and show_flag = #{showFlag}</if>
            <if test="delFlag != null and delFlag != ''"> and del_flag = #{delFlag}</if>
        </where>
    </select>
    
    <select id="selectRoleFieldById" parameterType="Long" resultMap="RoleFieldResult">
        <include refid="selectRoleFieldVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertRoleField" parameterType="RoleField" useGeneratedKeys="true" keyProperty="id">
        insert into m_role_field
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="roleKey != null">role_key,</if>
            <if test="fieldKey != null">field_key,</if>
            <if test="showFlag != null">show_flag,</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="roleKey != null">#{roleKey},</if>
            <if test="fieldKey != null">#{fieldKey},</if>
            <if test="showFlag != null">#{showFlag},</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="updateRoleField" parameterType="RoleField">
        update m_role_field
        <trim prefix="SET" suffixOverrides=",">
            <if test="roleKey != null">role_key = #{roleKey},</if>
            <if test="fieldKey != null">field_key = #{fieldKey},</if>
            <if test="showFlag != null">show_flag = #{showFlag},</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 id = #{id}
    </update>

    <update id="deleteRoleFieldById" parameterType="Long">
        update m_role_field
        set del_flag = '2'
        where id = #{id}
    </update>

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

</mapper>