<?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.xinyilu.system.mapper.sysuserclassrelation.SysUserClassRelationMapper">
    <resultMap type="com.xinyilu.system.domain.sysuserclassrelation.SysUserClassRelation"
               id="SysUserClassRelationResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="classId" column="class_id"/>
        <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"/>
    </resultMap>

    <sql id="selectSysUserClassRelationVo">
        select id,
               user_id,
               class_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from sys_user_class_relation
    </sql>

    <select id="selectSysUserClassRelationList"
            parameterType="com.xinyilu.system.domain.sysuserclassrelation.SysUserClassRelation"
            resultMap="SysUserClassRelationResult">
        <include refid="selectSysUserClassRelationVo"/>
        <where>
            <if test="userId != null ">
                and user_id = #{userId}
            </if>
            <if test="classId != null ">
                and class_id = #{classId}
            </if>
        </where>
    </select>

    <select id="selectSysUserClassRelationById" parameterType="Long"
            resultMap="SysUserClassRelationResult">
        <include refid="selectSysUserClassRelationVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysUserClassRelation"
            parameterType="com.xinyilu.system.domain.sysuserclassrelation.SysUserClassRelation" useGeneratedKeys="true"
            keyProperty="id">
        insert into sys_user_class_relation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">
                user_id,
            </if>
            <if test="classId != null">
                class_id,
            </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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">
                #{userId},
            </if>
            <if test="classId != null">
                #{classId},
            </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>
        </trim>
    </insert>

    <update id="updateSysUserClassRelation"
            parameterType="com.xinyilu.system.domain.sysuserclassrelation.SysUserClassRelation">
        update sys_user_class_relation
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">
                user_id =
                #{userId},
            </if>
            <if test="classId != null">
                class_id =
                #{classId},
            </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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysUserClassRelationById" parameterType="Long">
        delete
        from sys_user_class_relation
        where id = #{id}
    </delete>

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

    <select id="getSysUserClassRelationList"
            parameterType="com.xinyilu.system.domain.sysuserclassrelation.SysUserClassRelation"
            resultType="com.xinyilu.system.domain.sysuserclassrelation.SysUserClassRelation">
        select id, user_id, class_id, del_flag, create_by, create_time, update_by, update_time from
        sys_user_class_relation
        <where>
            AND del_flag = 0
            <if test="userId != null ">
                and user_id = #{userId}
            </if>
        </where>
    </select>

    <select id="getSysUserClassInfoList"
            parameterType="com.xinyilu.system.domain.sysuserclassrelation.SysUserClassRelation"
            resultType="com.xinyilu.system.domain.vo.ClassInfoVO">
        SELECT
        ci.id,
        ci.class_name
        FROM
        sys_user_class_relation sucr
        LEFT JOIN class_info ci ON ci.id=sucr.class_id
        <where>
            AND sucr.del_flag = 0
            <if test="userId != null ">
                and sucr.user_id = #{userId}
            </if>
        </where>
    </select>

    <insert id="insertSysUserClassRelationList" parameterType="java.util.List">
        insert into sys_user_class_relation(
        user_id,
        class_id,
        del_flag,
        create_by,
        create_time,
        update_by,
        update_time
        ) values
        <foreach item="sysUserClassRelation" index="index" collection="list" separator=",">
            (
            #{sysUserClassRelation.userId},
            #{sysUserClassRelation.classId},
            #{sysUserClassRelation.delFlag},
            #{sysUserClassRelation.createBy},
            #{sysUserClassRelation.createTime},
            #{sysUserClassRelation.updateBy},
            #{sysUserClassRelation.updateTime}
            )
        </foreach>
    </insert>

    <update id="updateSysUserClassRelationByUserId" parameterType="Long">
        update sys_user_class_relation
        set del_flag = 1
        where user_id = #{userId}
    </update>

    <update id="updateSysUserClassRelationByUserIdList" parameterType="Long">
        update sys_user_class_relation set del_flag = 1 where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </update>

    <delete id="deleteSysUserClassById" parameterType="Long">
        delete
        from sys_user_class_relation
        where user_id = #{userId}
          and class_id = #{classId}
    </delete>
</mapper>
