<?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.KnowledgeManageMapper">
    <resultMap id="BaseResultMap" type="com.yice.webadmin.app.model.KnowledgeManage">
        <id column="knowledge_id" jdbcType="BIGINT" property="knowledgeId"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="knowledge_name" jdbcType="VARCHAR" property="knowledgeName"/>
        <result column="knowledge_code" jdbcType="VARCHAR" property="knowledgeCode"/>
        <result column="knowledge_describe" jdbcType="VARCHAR" property="knowledgeDescribe"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO lmp_knowledge_manage
        (knowledge_id,
        create_user_id,
        create_time,
        update_user_id,
        update_time,
        knowledge_name,
        knowledge_code,
        knowledge_describe)
        VALUES
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.knowledgeId},
            #{item.createUserId},
            #{item.createTime},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.knowledgeName},
            #{item.knowledgeCode},
            #{item.knowledgeDescribe})
        </foreach>
    </insert>

    <!-- 如果有逻辑删除字段过滤，请写到这里 -->
    <sql id="filterRef">
        <!-- 这里必须加上全包名，否则当filterRef被其他Mapper.xml包含引用的时候，就会调用Mapper.xml中的该SQL片段 -->
        <include refid="com.yice.webadmin.app.dao.KnowledgeManageMapper.inputFilterRef"/>
    </sql>

    <select id="loadKnowLedgeList" resultMap="BaseResultMap" parameterType="com.yice.webadmin.app.model.KnowledgeManage">
        SELECT DISTINCT lmp_knowledge_manage.*
        FROM lmp_knowledge_manage
        LEFT JOIN lmp_knowledge_user ON lmp_knowledge_user.knowledge_id = lmp_knowledge_manage.knowledge_id
        <if test="knowledgeManageFilter.createUserId != null">
            WHERE lmp_knowledge_user.user_id = #{knowledgeManageFilter.createUserId}
        </if>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="knowledgeManageFilter != null">
            <if test="knowledgeManageFilter.knowledgeId != null">
                AND lmp_knowledge_manage.knowledge_id = #{knowledgeManageFilter.knowledgeId}
            </if>
            <if test="knowledgeManageFilter.createUserId != null">
                AND lmp_knowledge_manage.create_user_id = #{knowledgeManageFilter.createUserId}
            </if>
            <if test="knowledgeManageFilter.knowledgeName != null">
                AND lmp_knowledge_manage.knowledge_name = #{knowledgeManageFilter.knowledgeName}
            </if>
            <if test="knowledgeManageFilter.searchString != null and knowledgeManageFilter.searchString != ''">
                <bind name="safeKnowledgeManageSearchString" value="'%' + knowledgeManageFilter.searchString + '%'"/>
                AND IFNULL(lmp_knowledge_manage.knowledge_name,'') LIKE #{safeKnowledgeManageSearchString}
            </if>
        </if>
    </sql>

    <select id="getKnowledgeManageList" resultMap="BaseResultMap"
            parameterType="com.yice.webadmin.app.model.KnowledgeManage">
        SELECT * FROM lmp_knowledge_manage
        <where>
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>
</mapper>
