<?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.xhkjedu.sstudy.mapper.subjectbook.PointMapper">

    <!-- 根据科目获取知识点 -->
    <select id="listPoints" resultType="com.xhkjedu.sstudy.model.subjectbook.TPoint">
        SELECT p.pointid,p.pointname,p.pointorder,p.pointpid,p.subjectid,p.pointlevel,p.createid,
        p.createtime,u.username AS createname,p.belong
        FROM t_point p LEFT JOIN t_user u ON p.createid=u.userid
        WHERE p.subjectid=#{point.subjectid} and (p.belong=1
        <if test="point.schoolid!=null">
            or p.schoolid=#{point.schoolid}
        </if>)
        ORDER BY p.pointorder asc,p.createtime asc
    </select>

    <!-- 根据知识点名称搜索知识点，用户选择 -->
    <select id="listPointsForSearch" resultType="com.xhkjedu.sstudy.vo.subjectbook.PointVo">
        select p.pointid,p.pointname from t_point p left join t_point p1 on p1.pointpid=p.pointid
        where p.subjectid=#{point.subjectid} and p1.pointid is null
        and (p.belong=1
        <if test="point.schoolid!=null">
            or p.schoolid=#{point.schoolid}
        </if>)
         <if test="point.pointname!='' and point.pointname!=null">
             and p.pointname like '%${point.pointname}%'
         </if>
    </select>

    <!--查找使用知识点的模块-->
    <select id="findUseModule" resultType="java.lang.String">
        SELECT CONCAT(
        (select (case when count(*)>0 then '子知识点' else '' end) from t_point where pointpid=#{pointid})
        ,(select (case when count(*)>0 then ',试题' else '' end) from t_question_point where pointid=#{pointid})
        )
    </select>

    <!--根据知识点ID获取知识点信息-->
    <select id="getPointInfoById" resultType="com.xhkjedu.sstudy.model.subjectbook.TPoint">
        select pointid,pointname,pointall,pointlevel,
         (select count(*) from t_point p where p.pointpid=#{pointid}) sonnum
        from t_point where pointid=#{pointid}
    </select>

    <!--获取知识点全拼接字段-->
    <select id="getPointallByPointid" resultType="java.lang.String">
        select pointall from t_point where pointid=#{pointid}
    </select>

    <!--修改层级知识点-->
    <update id="updatePointAll">
        UPDATE t_point d1
        <if test ="4 > pointlevel">LEFT JOIN t_point d2 ON d2.pointpid=d1.pointid</if>
        <if test="3 > pointlevel">
            LEFT JOIN t_point d3 ON d3.pointpid=d2.pointid
        </if>
        <if test="2 > pointlevel">
            LEFT JOIN t_point d4 ON d4.pointpid=d3.pointid
        </if>
        SET d1.pointall=CONCAT(#{pointname},';',d1.pointname)
        <if test="4 > pointlevel">
            ,d2.pointall=CONCAT(#{pointname},';',d1.pointname,';',d2.pointname)
        </if>
        <if test="3 > pointlevel">
            ,d3.pointall=CONCAT(#{pointname},';',d1.pointname,';',d2.pointname,';',d3.pointname)
        </if>
        <if test="2 > pointlevel">
            ,d4.pointall=CONCAT(#{pointname},';',d1.pointname,';',d2.pointname,';',d3.pointname,';',d4.pointname)
        </if>
        WHERE d1.pointpid=#{pointid}
    </update>

</mapper>
