course.xml 19.6 KB
<?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.chuanqi.reading.home.dao.CourseDao">
	<sql id="queryCourseVo">
		select ci.course_id id, ci.term_id termId, ci.course_name title, date_format(audit_time, '%Y-%m-%d %H:%i') dataTime, count(cg.id) dataCount, NOT ISNULL(t.articleStatus) articleStatus
		from course_info ci
		left join (select distinct i.course_id, 1 articleStatus from  course_info i
		           join( select course_id from course_relation group by course_id having count(question_type) = 1) cr on i.course_id = cr.course_id
		           join course_read r on i.course_id =  r.course_id and r.read_flag = '1' and r.customer_id=#{userId}) t on ci.course_id = t.course_id
		left join course_progress cg on ci.course_id = cg.course_id and cg.customer_id = #{userId}
		where ci.audit_status = '1' and ci.course_status = '0'
	</sql>

	<select id="queryCourseList" resultType="com.chuanqi.reading.home.entity.DataSet"
			parameterType="com.chuanqi.reading.home.entity.DataSet">
		<!--include refid="queryCourseVo" /-->
		select ci.course_id id, ci.term_id termId, ci.course_name title, date_format(audit_time, '%Y-%m-%d %H:%i') dataTime, count(cg.id) dataCount, NOT ISNULL(t.articleStatus) articleStatus
		from course_info ci
		left join course_progress cg on ci.course_id = cg.course_id and cg.customer_id = #{userId}
		left join (select distinct i.course_id, 1 articleStatus from  course_info i
		           join( select course_id from course_relation group by course_id having count(question_type) = 1) cr on i.course_id = cr.course_id
		           join course_read r on i.course_id =  r.course_id and r.read_flag = '1' and r.customer_id=#{userId}) t on ci.course_id = t.course_id
		where ci.audit_status = '1' and ci.course_status = '0'
		and term_id = #{termId} group by ci.course_id order by sticky_status desc, dataCount, audit_time desc LIMIT #{startIndex},5
	</select>

	<select id="queryCourseById" resultType="com.chuanqi.reading.home.entity.CourseInfo"
			parameterType="long" >
		select course_id id, course_name name, term_id termId, course_num num, release_status resultStatus, ifnull(passing_Score,0) passingScore from course_info where course_id = #{id}
	</select>

	<select id="queryCourseListByRead" resultType="com.chuanqi.reading.home.entity.DataSet"
			parameterType="com.chuanqi.reading.home.entity.DataSet">
		select ci.course_id id, ci.term_id termId, ci.course_name name, audit_time dateTime, count(cg.id) dataCount, NOT ISNULL(t.articleStatus) articleStatus
		from course_info ci
		left join course_progress cg on ci.course_id = cg.course_id
		left join (select distinct i.course_id, 1 articleStatus from  course_info i
		           join( select course_id from course_relation group by course_id having count(question_type) = 1) cr on i.course_id = cr.course_id
		           join course_read r on i.course_id =  r.course_id and r.read_flag = '1' and r.customer_id=#{userId}) t on ci.course_id = t.course_id
		where ci.audit_status = '1' and ci.course_status = '0'
		  and ci.term_id in(
		    select term_id from course_term_student where user_id  = #{userId}
	  	) and cg.customer_id = #{userId}
		group by ci.course_id order by audit_time desc
	</select>

	<!--查询指定用户已读文章(包含无题目文章和已读不一定答题文章)-->
	<select id="queryCourseListByOnlyRead" resultType="com.chuanqi.reading.home.entity.CourseInfo"
			parameterType="com.chuanqi.reading.home.entity.SysUser">
		SELECT course_id id, course_name NAME FROM course_info WHERE course_id IN (
		SELECT DISTINCT course_id FROM course_read WHERE customer_id=#{userId} AND course_id IN (
		SELECT course_id FROM course_info ci WHERE ci.audit_status = '1' AND ci.course_status = '0'
		AND (ci.term_id IN (SELECT term_id FROM course_term_student WHERE user_id  = #{userId}) OR ci.term_id &lt;0)))
		ORDER BY audit_time DESC
	</select>

	<!--获取没有题目的文章阅读的记录-->
	<select id="queryByOnlyRead" resultType="com.chuanqi.reading.home.entity.CourseRead" parameterType="java.util.Map">
		SELECT id, course_id courseId, category_id categoryId,customer_id customerId, begin_time beginTime, end_time endTime,TIMEDIFF(end_time, begin_time) readFlag
		FROM course_read WHERE course_id=#{courseId} AND customer_id=#{userId} AND read_flag=1 order by endTime desc
	</select>

	<!--获得所有文章按照课程id -->
	<select id="queryArticle" resultType="com.chuanqi.reading.home.entity.Article" parameterType="java.util.Map">
		SELECT ca.article_id id, ca.article_title title, ca.article_content content, ca.article_time time,
			   ca.classify_id classifyId, ca.article_label articleLabel
		from course_article ca
		where article_id in (
			SELECT question_id FROM course_relation
			where course_id = #{id} and category_id = 1 and classify_id = #{classify}
			)LIMIT 1
	</select>

	<!--获得所有题目查询-->
	<select id="queryQuestions" resultType="com.chuanqi.reading.home.entity.Question" parameterType="java.util.Map">
		select cc.choice_id id,cr.id relationId,cc.choice_question question,cc.optionA,cc.optionB,cc.optionC,cc.optionD,
		cc.optionE, cc.optionF, cc.optionG, cc.optionH, cc.optionI, cc.choice_answer answer,
		       support_answer support,cc.choice_explain `explain`,cr.category_id categoryId,'choice' type ,cc.choice_score score
		from course_relation cr LEFT JOIN course_choice cc on cr.question_id = cc.choice_id where cr.question_type = 'choice' and cr.course_id = #{id}
		union
		select cf.fill_id id,cr.id relationId,cf.fill_question question,null optionA,null optionB,null optionC,null optionD,
		null optionE, null optionF, null optionG, null optionH, null optionI, cf.fill_answer answer,
		       support_answer support,cf.fill_explain `explain`,cr.category_id categoryId,'fill' type ,cf.fill_score score
		from course_relation cr LEFT JOIN course_fill cf on cr.question_id = cf.fill_id where cr.question_type = 'fill' and cr.course_id = #{id}
		union
		select cj.judge_id id,cr.id relationId,cj.judge_question question,null optionA,null optionB,null optionC,null optionD,
		null optionE, null optionF, null optionG, null optionH, null optionI, cj.judge_answer answer,
		       null support,cj.judge_explain `explain`,cr.category_id categoryId,'judge' type ,cj.judge_score score
		from course_relation cr LEFT JOIN course_judge cj on cr.question_id = cj.judge_id where cr.question_type = 'judge' and cr.course_id = #{id}
		ORDER BY relationId/*categoryId,type,question_sort*/
	</select>

	<insert id="saveCourseRead" parameterType="com.chuanqi.reading.home.entity.CourseRead" useGeneratedKeys="true"
			keyProperty="id">
		insert into course_read(course_id,category_id,customer_id,begin_time,end_time,read_flag)
		values (#{courseId},#{categoryId},#{customerId},#{beginTime},#{endTime},#{readFlag})
	</insert>
	<!--更新阅读记录-->
	<update id="updateCourseRead" parameterType="com.chuanqi.reading.home.entity.CourseRead">
		update course_read set end_time = #{endTime}, read_flag = #{readFlag} where id = ${id}
	</update>
	<!--保存完progress记录时,更新阅读记录-->
	<update id="updateCourseReadProgress" parameterType="java.util.Map">
		update course_read set progress_id = #{progressId}, read_flag = 1 where id in(${ids})
	</update>

	<insert id="saveCourseProgress" parameterType="com.chuanqi.reading.home.entity.CourseProgress"
			useGeneratedKeys="true" keyProperty="id">
		insert into course_progress(course_id, customer_id, lookback_num, lookback_time, read_time, answer_rate,
		                            course_status, create_time, begin_time, answer_score, exam_total, answer_right)
		values (#{courseId}, #{customerId}, #{lookbackNum}, #{lookbackTime}, #{readTime}, #{answerRate},
		        #{courseStatus}, #{createTime}, #{beginTime}, #{answerScore}, #{examTotal}, #{answerRight})
	</insert>

	<select id="queryCourseProgress" resultType="com.chuanqi.reading.home.entity.CourseProgress" parameterType="java.util.Map">
		select id, course_id, customer_id, lookback_num  lookbackNum, lookback_time lookbackTime, answer_rate answerRate,
			   read_time  readTime, course_status courseStatus, create_Time createTime
		from course_progress
		where course_id = ${courseId} and customer_id = #{userId} order by createTime
	</select>

	<insert id="saveCourseAnswer">
		insert into course_answer(progress_id, relation_id, customer_id, answer, right_num) values
		<foreach item="item" index="index" collection="list" separator=",">
			(#{item.progressId}, #{item.relationId}, #{item.customerId}, #{item.answer}, #{item.rightNum})
		</foreach>
	</insert>

	<!--查询公开试卷-->
	<select id="queryOpenCourse" resultType="com.chuanqi.reading.home.entity.DataSet"
			parameterType="com.chuanqi.reading.home.entity.DataSet">
		<include refid="queryCourseVo" />
		and term_id = -100 and course_num = #{id} <!-- course_num主题编号 -->
		group by ci.course_id order by sticky_status desc, dataCount, audit_time desc LIMIT #{startIndex},5
	</select>

	<!--查询用户所属培训班-->
	<select id="queryUserTermList" resultType="com.chuanqi.reading.home.entity.DataSet"
			parameterType="com.chuanqi.reading.home.entity.DataSet">
		SELECT id, title, content, userId FROM (
		/*##查询自己所属层级单位的所有培训班信息*/
		SELECT a.term_id id, term_title title, begin_date, CONCAT(begin_date, ' ~ ', end_date) content, user_id userId FROM course_term a
		LEFT JOIN course_term_student b ON a.term_id = b.term_id AND b.status='0' AND b.user_id=#{userId}
		WHERE dept_id IN (SELECT dept_id FROM sys_dept WHERE FIND_IN_SET(#{id}, ancestors)>0 OR dept_id IN(#{id},101))
		AND a.term_status='0' /*AND CURRENT_DATE BETWEEN begin_date AND end_date*/
		/*##查询自己所在培训班但是该班级不在所属机构下*/
		UNION
		SELECT t.term_id id, term_title title, begin_date, CONCAT(begin_date, ' ~ ', end_date) content, user_id userId
		FROM course_term_student ts ,course_term t
		WHERE ts.term_id=t.term_id AND ts.user_id=#{userId}
		AND t.term_status='0' /*AND CURRENT_DATE BETWEEN begin_date AND end_date*/
		AND t.dept_id NOT IN (SELECT dept_id FROM sys_dept WHERE FIND_IN_SET(#{id}, ancestors)>0)
		) temp ORDER BY userId DESC, begin_date LIMIT #{startIndex},5
	</select>

	<select id="queryTermById" resultType="com.chuanqi.reading.home.entity.DataSet" parameterType="long">
		SELECT term_id id, term_title title FROM course_term WHERE term_id = #{termId}
	</select>

	<sql id="queryMediaVo">
		SELECT media_id id, media_name title, media_desc content, media_link remark, media_image search,
			   date_format(ifnull(update_time, create_time), '%Y-%m-%d %H:%i') dataTime
		FROM course_media
	</sql>
	<select id="queryMediaList" resultType="com.chuanqi.reading.home.entity.DataSet"
			parameterType="com.chuanqi.reading.home.entity.DataSet">
		<include refid="queryMediaVo" />
		WHERE media_status='0' and theme_id=#{id} ORDER BY media_num LIMIT #{startIndex},5
	</select>

	<select id="queryMediaById" resultType="com.chuanqi.reading.home.entity.DataSet" parameterType="long">
		<include refid="queryMediaVo" />
		WHERE media_id = #{mediaId}
	</select>

	<select id="queryThemeList" resultType="com.chuanqi.reading.home.entity.DataSet"
			parameterType="com.chuanqi.reading.home.entity.DataSet">
		SELECT theme_id id, theme_name title FROM course_theme
		WHERE theme_status='0' and theme_type=#{remark} ORDER BY theme_num LIMIT #{startIndex},5
	</select>

	<select id="queryThemeById" resultType="com.chuanqi.reading.home.entity.DataSet" parameterType="long">
		SELECT theme_id id, theme_name title FROM course_theme WHERE theme_id = #{themeId}
	</select>
	<!-- 统计培训班个数 -->
	<select id="getClassCount" resultType="int">
		SELECT COUNT(DISTINCT term_id)  FROM course_term
	</select>
	<!-- 统计培训学员个数 -->
	<select id="getStudentCount" resultType="int">
		SELECT COUNT(DISTINCT user_id) FROM course_term_student
	</select>
	<!-- 统计培训部门下的培训班和班级人数 -->
	<select id="getTermListByDeptId" parameterType="long" resultType="com.chuanqi.reading.home.entity.Term">
		SELECT theme_id termId, theme_name termTitle, 0 userSum, '2019-01-01' begin_date FROM course_theme WHERE theme_type=1
		UNION ALL
		SELECT t.term_id termId, term_title termTitle, IFNULL(user_count, 0) userSum, begin_date FROM course_term t
		LEFT JOIN (SELECT term_id, COUNT(user_id) user_count FROM course_term_student GROUP BY term_id) ts
		ON t.term_id=ts.term_id WHERE term_status=0 ORDER BY begin_date <!-- AND t.dept_id=#{deptId} -->
	</select>
	<!-- 根据班级ID查询班级信息 -->
	<select id="getTermById" parameterType="long" resultType="com.chuanqi.reading.home.entity.Term">
		SELECT term_id termId, term_title termTitle FROM course_term WHERE term_id=#{termId}
	</select>
	<!--查询个人明细-公共培训-->
	<select id="queryPublicDetailForList" parameterType="java.util.Map" resultType="com.chuanqi.reading.home.entity.Details">
		SELECT t1.course_id courseId, ci.course_name courseName, DATE_FORMAT(t1.begin_time, '%Y-%m-%d %H:%i:%s') beginTime,
		useTime, answer_rate answerRate FROM (
			SELECT course_id,begin_time, TIMEDIFF(create_time, begin_time) useTime, answer_rate FROM course_progress
			WHERE course_id IN (SELECT course_id FROM course_info WHERE term_id &lt; 0) AND customer_id=#{userId}
			UNION ALL
			SELECT course_id, begin_time, TIMEDIFF(end_time, begin_time) useTime,'-'  AS answer_rate FROM course_read
			WHERE course_id IN (SELECT course_id FROM course_info WHERE term_id &lt; 0) AND read_flag=1 AND customer_id=#{userId}
		) t1 , course_info ci WHERE t1.course_id=#{courseId} and ci.course_id = #{courseId} ORDER BY t1.begin_time DESC LIMIT #{startIndex},5
	</select>
	<!--查询个人-专项培训-->
	<select id="queryTermSpecialForList" parameterType="java.util.Map" resultType="com.chuanqi.reading.home.entity.Term">
		select t.term_id termId, term_title termTitle
        from   course_term t
        where  exists(select * from course_read r inner join
  		                            course_info i on i.course_id=r.course_id and r.customer_id=#{userId} inner join
	    					        course_progress p on r.progress_id=p.id and r.read_flag=1 and p.customer_id = #{userId}
				                    where i.term_id = t.term_id ) OR
               exists(select * from course_read cr inner join
                                    v_course_article v on cr.course_id = v.course_id and cr.customer_id = #{userId} and cr.read_flag = 1 where t.term_id =v.term_id)
		order by t.begin_date desc LIMIT #{startIndex},5
	</select>
	<!--查询个人明细专项培训-->
	<select id="querySpecialDetailForList" parameterType="java.util.Map" resultType="com.chuanqi.reading.home.entity.Details">
		SELECT t1.course_id courseId, ci.course_name courseName, DATE_FORMAT(t1.begin_time, '%Y-%m-%d %H:%i:%s') beginTime,
		useTime, answer_rate answerRate FROM (
			SELECT course_id,begin_time, TIMEDIFF(create_time, begin_time) useTime, answer_rate FROM course_progress
			WHERE course_id = #{courseId} and course_id IN (SELECT course_id FROM course_info WHERE term_id &gt; 0 ) AND customer_id=#{userId}
			UNION ALL
			SELECT course_id, begin_time, TIMEDIFF(end_time, begin_time) useTime,'-' AS answer_rate FROM course_read
			WHERE course_id NOT IN (SELECT course_id FROM course_info WHERE term_id &lt; 0)
			AND course_id NOT IN (SELECT DISTINCT course_id FROM course_progress WHERE  customer_id=#{userId})
			AND read_flag=1 AND customer_id=#{userId} and course_id = #{courseId}
		) t1 , course_info ci WHERE t1.course_id=ci.course_id and ci.term_id= #{termId} ORDER BY t1.begin_time DESC LIMIT #{startIndex},7
	</select>
	<!--查询个人概况-时长、篇数、成绩:按照当天最高算-->
	<select id="queryPersonalLogForList" parameterType="long" resultType="com.chuanqi.reading.home.entity.DataSet">
		SELECT DATE_FORMAT(dataTime, '%m-%d') AS dataTime, termId, dataCount, search  FROM (
			SELECT DATE_FORMAT(begin_time, '%Y-%m-%d') dataTime,
			SUM(TIMESTAMPDIFF(SECOND,begin_time,create_time)) termId,
			COUNT(customer_id) dataCount,
			MAX(CONVERT(answer_rate,DECIMAL(5,2))) search
			FROM course_progress  WHERE customer_id=#{0} GROUP BY dataTime ORDER BY dataTime DESC
		) t ORDER BY dataTime  LIMIT #{startIndex}, 5
	</select>
	<select id="getSpecialTermCourseSumById" parameterType="long" resultType="com.chuanqi.reading.home.entity.TermCourse">
		SELECT i.term_id termId, count(*) courseNum,
		       CONCAT(SUM(TIMESTAMPDIFF(SECOND, r.begin_time, IFNULL(p.create_time, r.end_time))) div 60, ':', LPAD(SUM(TIMESTAMPDIFF(SECOND, r.begin_time, IFNULL(p.create_time, r.end_time))) mod 60, 2, '0')) TimeTotal
		FROM course_read r
		INNER JOIN course_info i ON r.course_id = i.course_id AND i.term_id = #{termId}
		LEFT  JOIN course_progress p on p.course_id = r.course_id and p.customer_id = #{userId} and r.progress_Id=p.Id
		LEFT JOIN v_course_article v ON v.course_id = r.course_id
	    WHERE r.customer_id = #{userId} and r.read_flag = 1 and (r.progress_id IS NOT NULL or v.course_name is not null)
		GROUP BY i.term_id
	</select>
	<!--查询个人概况- 专项培训 完成的课程列表-->
	<select id="getSpecialTermCourseList" parameterType="long" resultType="com.chuanqi.reading.home.entity.CourseInfo">
		SELECT ci.course_id id, ci.course_name name, ci.course_num num, ci.term_id termId
        from   course_info ci where  ci.term_Id=#{termId} and
              (exists(select * from v_course_article ca inner join
                      course_read cr on ca.course_id = cr.course_id and cr.read_flag = 1 and cr.customer_id = #{userId} where ci.course_id=cr.course_id) OR
               exists(select * from course_read r inner join
                      course_progress cp on r.progress_id = cp.id and r.read_flag = 1 and r.customer_id = #{userId}  where ci.course_id=r.course_id ))

	</select>
	<!--查询个人概况- 公开培训 主题列表-->
	<select id="getPubThemeList" parameterType="long" resultType="com.chuanqi.reading.home.entity.Theme">
		select ct.theme_id themeId, ct.theme_name themeName
		from   course_theme ct
		where exists(select * from course_info ci inner join course_read cr on ci.course_id = cr.course_id and cr.customer_id = #{userId} and cr.read_flag = 1
		where ci.course_num = ct.theme_id and ci.term_id &lt; 0 )
	</select>
	<select id="getPubThemeById" parameterType="long" resultType="com.chuanqi.reading.home.entity.Theme">
		select ct.theme_id themeId, ct.theme_name themeName
		from   course_theme ct where ct.theme_id = #{themeId}
	</select>
	<select id="getPubThemeCourseSumById" parameterType="long" resultType="com.chuanqi.reading.home.entity.ThemeCourse">
		SELECT ci.course_num themeId, count(*) AS courseNum,
		       CONCAT(SUM(TIMESTAMPDIFF(SECOND, cr.begin_time, IFNULL(p.create_time, cr.end_time))) div 60,':', LPAD(SUM(TIMESTAMPDIFF(SECOND, cr.begin_time, IFNULL(p.create_time, cr.end_time))) mod 60, 2, '0')) TimeTotal
		FROM course_read cr INNER JOIN course_info ci ON cr.course_id = ci.course_id  AND ci.term_id &lt; 0 LEFT JOIN
		     v_course_article va ON cr.course_id = va.course_id LEFT JOIN course_progress p ON p.course_id = cr.course_id AND p.customer_id = #{userId} AND cr.progress_Id = p.Id
		WHERE cr.customer_id = #{userId} AND cr.read_flag = 1 AND ci.course_num = #{themeId}
		AND (cr.progress_id IS NOT NULL OR va.course_name IS NOT NULL)
		GROUP BY course_num
	</select>
	<select id="getPubThemeCourseList" parameterType="long" resultType="com.chuanqi.reading.home.entity.CourseInfo">
		select ci.course_id id, ci.course_name name, ci.course_num themeId
		from   course_info ci
		where  ci.course_num = #{themeId} and ci.term_id &lt; 0 and
		exists(select * from course_read cr left join v_course_article va on cr.course_id = va.course_id
		where  cr.customer_id = #{userId} and cr.read_flag = 1 and (NOT ISNULL(cr.progress_id) or  NOT ISNULL(va.course_name)) and cr.course_id =ci.course_id)
	</select>
</mapper>