<?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.xunmei.core.edu.mapper.SysLearningMaterialsMapper">

    <resultMap type="com.xunmei.common.core.domain.edu.domain.SysLearningMaterials" id="SysLearningMaterialsResult">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="content" column="content"/>
        <result property="knowledgeId" column="knowledge_id"/>
        <result property="orgId" column="org_id"/>
        <result property="orgName" column="org_name"/>
        <result property="orgPath" column="org_path"/>
        <result property="isOpen" column="is_open"/>
        <result property="file" column="file"/>
        <result property="deleted" column="deleted"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="updateTime"/>
    </resultMap>

    <sql id="selectSysLearningMaterialsVo">
        select id,
               title,
               content,
               knowledge_id,
               org_id,
               org_name,
               org_path,
               is_open,
               file,
               remark,
               deleted,
               create_by,
               create_time,
               update_by,
               update_time
        from sys_learning_materials
    </sql>

    <select id="selectSysLearningMaterialsList"
            parameterType="com.xunmei.common.core.domain.edu.domain.SysLearningMaterials"
            resultMap="SysLearningMaterialsResult">
        <include refid="selectSysLearningMaterialsVo"/>
        <where>
            <if test="title != null  and title != ''">
                and title like concat('%', #{title}, '%')
            </if>
            <if test="content != null  and content != ''">
                and title like concat('%', #{title}, '%')
            </if>
            <if test="knowledgeId != null ">
                and knowledge_id = #{knowledgeId}
            </if>
            <if test="orgId != null ">
                and org_id = #{orgId}
            </if>
            <if test="orgName != null  and orgName != ''">
                and org_name like concat('%', #{orgName}, '%')
            </if>
            <if test="orgPath != null  and orgPath != ''">
                and org_path = #{orgPath}
            </if>
            <if test="isOpen != null ">
                and is_open = #{isOpen}
            </if>
            <if test="file != null  and file != ''">
                and file = #{file}
            </if>
            <if test="deleted != null ">
                and deleted = #{deleted}
            </if>
            <if test="updateTime != null ">
                and updateTime = #{updateTime}
            </if>
        </where>
    </select>

    <select id="selectSysLearningMaterialsById" parameterType="Long"
            resultMap="SysLearningMaterialsResult">
        <include refid="selectSysLearningMaterialsVo"/>
        where id = #{id}
    </select>
    <sql id="pageRequest">
        <if test="request.knowledgeId != null">
            and m.knowledge_id = #{request.knowledgeId}
        </if>
        <if test="request.title != null ">
            and m.title = #{request.title}
        </if>
    </sql>
    <select id="selectPageData" resultType="com.xunmei.common.core.domain.edu.vo.SysLearningMaterialsPageVo">
        select m.id,
        title,
        k.name as knowledgeName,
        m.org_id,
        m.org_name,
        is_open,
        file,
        remark as remark,
        u.name as create_by,
        m.create_time,
        m.update_time as updateTime
        from sys_learning_materials m
        inner join sys_knowledge k on m.knowledge_id = k.id
        inner join sys_user u on u.id=m.create_by
        where m.deleted = 0
        and k.deleted = 0
        <choose>
            <!-- 查询公开的,需要判断是否关联上级机构,类型与标题 -->
            <when test="request.isOpen != null and request.isOpen ==1">
                <include refid="pageRequest"/>
                <if test="request.checkSub==false">
                    and m.org_id = #{request.orgId}
                </if>
                <if test="request.checkSub==true">
                    or ( m.org_id in
                    <foreach collection="request.orgIdList" item="item" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                    and m.is_open = 1
                    <include refid="pageRequest"/>)
                </if>
            </when>
            <!-- 查询未公开的,需要判断类型与标题 且查询当前机构自己创建的, -->
            <when test="request.isOpen != null">
                <include refid="pageRequest"/>
                and m.org_id = #{request.orgId}
                and m.create_by = #{request.userId}
                and m.is_open = 0
            </when>
            <!-- 公开条件等于null时,需要判断类型与标题 且查询当前机构自己创建的, -->
            <otherwise>
                <include refid="pageRequest"/>
                <if test="request.checkSub==false">
                    and m.org_id = #{request.orgId}
                </if>
                <if test="request.checkSub==true">
                    or ( m.org_id in
                    <foreach collection="request.orgIdList" item="item" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                    and m.is_open = 1
                    <include refid="pageRequest"/>)
                </if>
            </otherwise>
        </choose>
        order by m.org_id, m.create_time desc
    </select>

    <insert id="insertSysLearningMaterials"
            parameterType="com.xunmei.common.core.domain.edu.domain.SysLearningMaterials">
        insert into sys_learning_materials
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,
            </if>
            <if test="title != null">title,
            </if>
            <if test="content != null">content,
            </if>
            <if test="knowledgeId != null">knowledge_id,
            </if>
            <if test="orgId != null">org_id,
            </if>
            <if test="orgName != null">org_name,
            </if>
            <if test="orgPath != null">org_path,
            </if>
            <if test="isOpen != null">is_open,
            </if>
            <if test="file != null">file,
            </if>
            <if test="remark != null">remark,
            </if>
            <if test="deleted != null">deleted,
            </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">updateTime,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},
            </if>
            <if test="title != null">#{title},
            </if>
            <if test="content != null">#{content},
            </if>
            <if test="knowledgeId != null">#{knowledgeId},
            </if>
            <if test="orgId != null">#{orgId},
            </if>
            <if test="orgName != null">#{orgName},
            </if>
            <if test="orgPath != null">#{orgPath},
            </if>
            <if test="isOpen != null">#{isOpen},
            </if>
            <if test="file != null">#{file},
            </if>
            <if test="remark != null">#{remark},
            </if>
            <if test="deleted != null">#{deleted},
            </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="updateSysLearningMaterials"
            parameterType="com.xunmei.common.core.domain.edu.domain.SysLearningMaterials">
        update sys_learning_materials
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title =
                #{title},
            </if>
            <if test="content != null">content =
                #{content},
            </if>
            <if test="knowledgeId != null">knowledge_id =
                #{knowledgeId},
            </if>
            <if test="orgId != null">org_id =
                #{orgId},
            </if>
            <if test="orgName != null">org_name =
                #{orgName},
            </if>
            <if test="orgPath != null">org_path =
                #{orgPath},
            </if>
            <if test="isOpen != null">is_open =
                #{isOpen},
            </if>
            <if test="file != null">file =
                #{file},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
            <if test="deleted != null">deleted =
                #{deleted},
            </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">updateTime =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysLearningMaterialsById" parameterType="Long">
        delete
        from sys_learning_materials
        where id = #{id}
    </delete>

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