<?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.yice.webadmin.app.dao.KnowledgeUserMapper">
    <resultMap id="BaseResultMap" type="com.yice.webadmin.upms.model.SysUser">
        <id column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="login_name" jdbcType="VARCHAR" property="loginName"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
        <result column="dept_id" jdbcType="BIGINT" property="deptId"/>
        <result column="show_name" jdbcType="VARCHAR" property="showName"/>
        <result column="user_type" jdbcType="INTEGER" property="userType"/>
        <result column="head_image_url" jdbcType="VARCHAR" property="headImageUrl"/>
        <result column="user_status" jdbcType="INTEGER" property="userStatus"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="deleted_flag" jdbcType="INTEGER" property="deletedFlag"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO lmp_knowledge_user(user_id,knowledge_id)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.userId},#{item.knowledgeId})
        </foreach>
    </insert>

    <!-- 如果有逻辑删除字段过滤，请写到这里 -->
    <sql id="filterRef">
        <!-- 这里必须加上全包名，否则当filterRef被其他Mapper.xml包含引用的时候，就会调用Mapper.xml中的该SQL片段 -->
        <include refid="com.yice.webadmin.upms.dao.SysUserMapper.inputFilterRef"/>
        AND sys_user.deleted_flag = ${@com.yice.common.core.constant.GlobalDeletedFlag@NORMAL}
    </sql>

    <select id="getSysUserListByKnowledgeId" resultMap="BaseResultMap">
        SELECT
            sys_user.*
        FROM
            lmp_knowledge_user,
            sys_user
        <where>
            AND lmp_knowledge_user.knowledge_id = #{knowledgeId}
            AND lmp_knowledge_user.user_id = sys_user.user_id
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getNotInSysUserListByKnowledgeId" resultMap="BaseResultMap">
        SELECT * FROM sys_user
        <where>
            NOT EXISTS (SELECT * FROM lmp_knowledge_user
            WHERE lmp_knowledge_user.knowledge_id = #{knowledgeId} AND lmp_knowledge_user.user_id = sys_user.user_id)
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>
</mapper>
