<?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.fs.company.mapper.CompanyRoleMapper">
    
    <resultMap type="CompanyRole" id="CompanyRoleResult">
        <result property="roleId"    column="role_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="roleName"    column="role_name"    />
        <result property="roleKey"    column="role_key"    />
        <result property="roleSort"    column="role_sort"    />
        <result property="dataScope"    column="data_scope"    />
        <result property="menuCheckStrictly"    column="menu_check_strictly"    />
        <result property="deptCheckStrictly"    column="dept_check_strictly"    />
        <result property="status"    column="status"    />
        <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"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectCompanyRoleVo">
        select role_id, company_id, role_name, role_key, role_sort, data_scope, menu_check_strictly, dept_check_strictly, status, del_flag, create_by, create_time, update_by, update_time, remark from company_role
    </sql>

    <select id="selectCompanyRoleList" parameterType="CompanyRole" resultMap="CompanyRoleResult">
        <include refid="selectCompanyRoleVo"/>
        <where>  
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="roleName != null  and roleName != ''"> and role_name like concat('%', #{roleName}, '%')</if>
            <if test="roleKey != null  and roleKey != ''"> and role_key = #{roleKey}</if>
            <if test="roleSort != null "> and role_sort = #{roleSort}</if>
            <if test="dataScope != null  and dataScope != ''"> and data_scope = #{dataScope}</if>
            <if test="menuCheckStrictly != null "> and menu_check_strictly = #{menuCheckStrictly}</if>
            <if test="deptCheckStrictly != null "> and dept_check_strictly = #{deptCheckStrictly}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="delFlag != null  and delFlag != ''"> and del_flag = #{delFlag}</if>
        </where>
    </select>
    
    <select id="selectCompanyRoleById" parameterType="Long" resultMap="CompanyRoleResult">
        <include refid="selectCompanyRoleVo"/>
        where role_id = #{roleId}
    </select>
        
    <insert id="insertCompanyRole" parameterType="CompanyRole" useGeneratedKeys="true" keyProperty="roleId">
        insert into company_role
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="roleName != null and roleName != ''">role_name,</if>
            <if test="roleKey != null and roleKey != ''">role_key,</if>
            <if test="roleSort != null">role_sort,</if>
            <if test="dataScope != null">data_scope,</if>
            <if test="menuCheckStrictly != null">menu_check_strictly,</if>
            <if test="deptCheckStrictly != null">dept_check_strictly,</if>
            <if test="status != null and status != ''">status,</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>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="roleName != null and roleName != ''">#{roleName},</if>
            <if test="roleKey != null and roleKey != ''">#{roleKey},</if>
            <if test="roleSort != null">#{roleSort},</if>
            <if test="dataScope != null">#{dataScope},</if>
            <if test="menuCheckStrictly != null">#{menuCheckStrictly},</if>
            <if test="deptCheckStrictly != null">#{deptCheckStrictly},</if>
            <if test="status != null and status != ''">#{status},</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>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCompanyRole" parameterType="CompanyRole">
        update company_role
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="roleName != null and roleName != ''">role_name = #{roleName},</if>
            <if test="roleKey != null and roleKey != ''">role_key = #{roleKey},</if>
            <if test="roleSort != null">role_sort = #{roleSort},</if>
            <if test="dataScope != null">data_scope = #{dataScope},</if>
            <if test="menuCheckStrictly != null">menu_check_strictly = #{menuCheckStrictly},</if>
            <if test="deptCheckStrictly != null">dept_check_strictly = #{deptCheckStrictly},</if>
            <if test="status != null and status != ''">status = #{status},</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>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where role_id = #{roleId}
    </update>

    <delete id="deleteCompanyRoleById" parameterType="Long">
        delete from company_role where role_id = #{roleId}
    </delete>

    <delete id="deleteCompanyRoleByIds" parameterType="String">
        delete from company_role where role_id in 
        <foreach item="roleId" collection="array" open="(" separator="," close=")">
            #{roleId}
        </foreach>
    </delete>



    <select id="selectRolePermissionByUserId" parameterType="Long" resultMap="CompanyRoleResult">
        <include refid="selectRoleVo"/>
        WHERE r.del_flag = '0' and ur.user_id = #{userId}
    </select>

    <sql id="selectRoleVo">
	    select distinct r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.menu_check_strictly, r.dept_check_strictly,
            r.status, r.del_flag, r.create_time, r.remark, r.company_id
        from company_role r
	        left join company_user_role ur on ur.role_id = r.role_id
	        left join company_user u on u.user_id = ur.user_id
	        left join company_dept d on u.dept_id = d.dept_id
    </sql>

    <select id="selectRolesByUserName" parameterType="String" resultMap="CompanyRoleResult">
        <include refid="selectRoleVo"/>
        WHERE r.del_flag = '0' and u.user_name = #{userName}
    </select>


    <select id="checkRoleNameUnique" parameterType="String" resultMap="CompanyRoleResult">
        <include refid="selectRoleVo"/>
        where r.role_name=#{roleName} limit 1
    </select>

    <select id="checkRoleKeyUnique" parameterType="String" resultMap="CompanyRoleResult">
        <include refid="selectRoleVo"/>
        where r.role_key=#{roleKey} limit 1
    </select>

    <select id="selectRoleListByUserId" parameterType="Long" resultType="Integer">
		select r.role_id
        from company_role r
	        left join company_user_role ur on ur.role_id = r.role_id
	        left join company_user u on u.user_id = ur.user_id
	    where u.user_id = #{userId}
	</select>

    <select id="selectRoleListByIds" resultType="com.fs.company.domain.CompanyRole">
        select * from company_role
        where del_flag = '0' and status = '0'
        and role_id in
        <foreach collection="roleIds" item="roleId" separator="," open="(" close=")">
            #{roleId}
        </foreach>
    </select>
</mapper>