<?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.cool.store.mapper.EnterpriseUserRoleMapper">
    <resultMap id="BaseResultMap" type="com.cool.store.entity.EnterpriseUserRoleDO">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="role_id" jdbcType="VARCHAR" property="roleId"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="deleted" jdbcType="BIT" property="deleted"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, role_id, user_id, type, deleted, create_time, update_time
    </sql>
    <insert id="batchInsertOrUpdate">
        <foreach collection="recordList" separator=";" item="record">
            insert into enterprise_user_role
            <trim prefix="(" suffix=")" suffixOverrides=",">
              <if test="record.roleId != null">
                role_id,
              </if>
              <if test="record.userId != null">
                user_id,
              </if>
                <if test="record.type != null">
                    type,
                </if>
              <if test="record.deleted != null">
                deleted,
              </if>
              <if test="record.createTime != null">
                create_time,
              </if>
              <if test="record.updateTime != null">
                update_time,
              </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
              <if test="record.roleId != null">
                #{record.roleId},
              </if>
              <if test="record.userId != null">
                #{record.userId},
              </if>
                <if test="record.type != null">
                    #{record.type},
                </if>
              <if test="record.deleted != null">
                #{record.deleted},
              </if>
              <if test="record.createTime != null">
                #{record.createTime},
              </if>
              <if test="record.updateTime != null">
                #{record.updateTime},
              </if>
            </trim>
          ON DUPLICATE KEY UPDATE deleted = values(deleted)
        </foreach>
    </insert>
    <update id="updateByPrimaryKeySelective">
        update enterprise_user_role
        <set>
            <if test="roleId != null">
                role_id = #{roleId},
            </if>
            <if test="userId != null">
                user_id = #{userId},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        where id = #{id}
    </update>
    
    <update id="deleteRoleInUser">
        update enterprise_user_role set deleted = 1 where role_id = #{roleId} and type = #{type} and user_id not in <foreach collection="excludeUserIds" item="userId" separator="," open="(" close=")">#{userId}</foreach>
    </update>

    <update id="deleteUserInRole">
        update enterprise_user_role set deleted = 1 where role_id != #{excludeRoleId} and user_id = #{userId} and type = #{type}
    </update>

    <update id="deleteUserRole">
        update enterprise_user_role set deleted = 1 where user_id = #{userId}
    </update>
</mapper>