<?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.vctgo.system.mapper.SysReClassUserMapper">

    <resultMap type="com.vctgo.system.domain.SysReClassUser" id="SysReClassUserResult">
        <result property="id"    column="id"    />
        <result property="classId"    column="class_id"    />
        <result property="bindUserId"    column="bind_user_id"    />
        <result property="bindUserName"    column="bind_user_name"    />
        <result property="bindUserPosition"    column="bind_user_position"    />
        <result property="isDeleted"    column="is_deleted"    />
        <result property="createById"    column="create_by_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateById"    column="update_by_id"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="tenantId"    column="tenant_id"    />
    </resultMap>

    <sql id="selectSysReClassUserVo">
        select id, class_id, bind_user_id, bind_user_name, bind_user_position, is_deleted, create_by_id, create_by, create_time, update_by_id, update_by, update_time, tenant_id from sys_re_class_user
    </sql>

    <sql id="defaultDeleteCondition">
        is_deleted = 0
    </sql>

    <select id="selectSysReClassUserList"   resultMap="SysReClassUserResult">
        <include refid="selectSysReClassUserVo"/>
        <where>
            <include refid="defaultDeleteCondition"/>
            <if test="query.classId != null "> and class_id = #{query.classId}</if>
            <if test="query.bindUserId != null "> and bind_user_id = #{query.bindUserId}</if>
            <if test="query.bindUserName != null  and query.bindUserName != ''">
                and bind_user_name like concat('%',#{query.bindUserName}, '%')
            </if>
            <if test="query.bindUserPosition != null  and query.bindUserPosition != ''"> and bind_user_position = #{query.bindUserPosition}</if>
            <if test="query.params.classIdList != null  and query.params.classIdList.size >0 ">
                and class_id in
                <foreach collection="query.params.classIdList" item="classId" index="i" open="(" close=")" separator=",">
                    #{classId}
                </foreach>
            </if>
            <if test="query.params.bindUserIdList != null  and query.params.bindUserIdList.size >0 ">
                and bind_user_id in
                <foreach collection="query.params.bindUserIdList" item="bindUserId" index="i" open="(" close=")" separator=",">
                    #{bindUserId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="staticsBindUserCountByClassId" resultType="java.lang.Long">
        select count(*) from sys_re_class_user
        where class_id = #{classId} and is_deleted = '0'
        <if test="bindUserPosition != null and bindUserPosition != ''">
          and bind_user_position = #{bindUserPosition}</if>
    </select>

    <update id="logicalDeleteSysReClassUserByIds" parameterType="String">
        update sys_re_class_user set is_deleted = #{query.isDeleted},
        update_by_id = #{query.updateById}, update_by = #{query.updateBy},
        update_time = #{query.updateTime} where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="logicalDeleteSysReClassUserByClassId">
        update sys_re_class_user set is_deleted = '1' where class_id = #{classId}
        <choose>
            <when test="bindType == '1'.toString()">
                and bind_user_position in ('1','2','3')
            </when>
            <when test="bindType == '4'.toString()">
                and bind_user_position = '4'
            </when>
            <otherwise >
            </otherwise>
        </choose>
    </update>

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

    <delete id="deleteSysReClassUserByClassId">
        delete from sys_re_class_user
        where class_id = #{classId} and is_deleted = '1'
        <choose>
            <when test="bindType == '1'.toString()">
                and bind_user_position in ('1','2','3')
            </when>
            <when test="bindType == '4'.toString()">
                and bind_user_position = '4'
            </when>
            <otherwise >
            </otherwise>
        </choose>
    </delete>

    <delete id="deleteUserClass" parameterType="Long">
        delete from sys_re_class_user where bind_user_id in
        <foreach collection="ids" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <delete id="deleteSysReClassUserByTenantId">
        delete from sys_re_class_user
        where tenant_id in
        <foreach collection="tenantIds" item="tenantId" open="(" separator="," close=")">
            #{tenantId}
        </foreach>
        <choose>
            <when test="bindType == '1'.toString()">
                and bind_user_position in ('1','2','3')
            </when>
            <when test="bindType == '4'.toString()">
                and bind_user_position = '4'
            </when>
            <otherwise >
            </otherwise>
        </choose>
    </delete>
</mapper>
