<?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.zhiqiyun.open.mapper.RoleInfoMapper">
    <resultMap id="BaseResultMap" type="com.zhiqiyun.open.models.RoleInfo">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column">
        `id`
        , `name`, `remark`
    </sql>

    <insert id="insert" parameterType="com.zhiqiyun.open.models.RoleInfo">
        insert into `role_info` (`name`, remark)
        values (#{name,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR})
    </insert>

    <update id="updateById" parameterType="com.zhiqiyun.open.models.RoleInfo">
        update `role_info`
        <set>
            <if test="name != null and name !=''">
                `name` = #{name,jdbcType=VARCHAR},
            </if>
            <if test="remark != null and remark !=''">
                `remark` = #{remark,jdbcType=VARCHAR},
            </if>
        </set>
        <where>
            `id` = #{id, jdbcType=BIGINT}
        </where>
    </update>

    <insert id="insertAuthority">
        insert into role_authority (authority_id, role_id)
        values
        <foreach collection="authorities" item="item" index="index" separator=",">
            (#{item,jdbcType=BIGINT}, #{roleId,jdbcType=BIGINT})
        </foreach>
    </insert>

    <select id="findAuthorities" resultType="java.lang.Long">
        select authority_id
        from role_authority
        WHERE role_id = #{roleId,jdbcType=BIGINT}
    </select>
    <select id="findPage" resultMap="BaseResultMap">
        select
        <include refid="Base_Column"/>
        from `role_info`
        <where>
            <if test="name != null and name != ''">
                `name` like CONCAT('%',#{name,jdbcType=BIGINT},'%')
            </if>
        </where>
        order by id desc
    </select>
    <delete id="deleteAuthorities" parameterType="java.lang.Long">
        delete FROM role_authority
        <where>
            role_id in
            <foreach collection="roleIds" item="roleId" separator="," open="(" close=")">
                #{roleId,jdbcType=BIGINT}
            </foreach>
        </where>
    </delete>

    <delete id="deleteBatchIds">
        delete FROM role_info
        <where>
            id in
            <foreach collection="roleIds" item="roleId" separator="," open="(" close=")">
                #{roleId,jdbcType=BIGINT}
            </foreach>
        </where>
    </delete>
</mapper>