<?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.CompanyUserRoleMapper">
    
    <resultMap type="CompanyUserRole" id="CompanyUserRoleResult">
        <result property="userId"    column="user_id"    />
        <result property="roleId"    column="role_id"    />
    </resultMap>

    <sql id="selectCompanyUserRoleVo">
        select user_id, role_id from company_user_role
    </sql>

    <select id="selectCompanyUserRoleList" parameterType="CompanyUserRole" resultMap="CompanyUserRoleResult">
        <include refid="selectCompanyUserRoleVo"/>
        <where>  
        </where>
    </select>
    
    <select id="selectCompanyUserRoleById" parameterType="Long" resultMap="CompanyUserRoleResult">
        <include refid="selectCompanyUserRoleVo"/>
        where user_id = #{userId}
    </select>
        
    <insert id="insertCompanyUserRole" parameterType="CompanyUserRole">
        insert into company_user_role
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="roleId != null">role_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="roleId != null">#{roleId},</if>
         </trim>
    </insert>

    <update id="updateCompanyUserRole" parameterType="CompanyUserRole">
        update company_user_role
        <trim prefix="SET" suffixOverrides=",">
            <if test="roleId != null">role_id = #{roleId},</if>
        </trim>
        where user_id = #{userId}
    </update>

    <delete id="deleteCompanyUserRoleById" parameterType="Long">
        delete from company_user_role where user_id = #{userId}
    </delete>

    <delete id="deleteCompanyUserRoleByIds" parameterType="String">
        delete from company_user_role where user_id in 
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <select id="countUserRoleByRoleId" resultType="Integer">
	    select count(1) from company_user_role where role_id=#{roleId}
	</select>

    <insert id="batchUserRole">
        insert into company_user_role(user_id, role_id) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.userId},#{item.roleId})
        </foreach>
    </insert>

    <delete id="deleteUserRoleByUserId" parameterType="Long">
		delete from company_user_role where user_id=#{userId}
	</delete>
</mapper>