<?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.zhongzheng.modules.goods.mapper.GoodsAttachedMapper">

    <resultMap type="com.zhongzheng.modules.goods.domain.GoodsAttached" id="GoodsAttachedResult">
        <result property="id" column="id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="majorId" column="major_id"/>
        <result property="sort" column="sort"/>
        <result property="type" column="type"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.goods.vo.GoodsAttachedVo" id="GoodsAttachedResultVo">
        <result property="id" column="id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="majorId" column="major_id"/>
        <result property="sort" column="sort"/>
        <result property="type" column="type"/>
        <result property="name" column="name"/>
        <result property="subjectName" column="subject_name"/>
        <result property="questionNum" column="question_num"/>
		<result property="recordId" column="record_id"/>
		<result property="recordStatus" column="record_status"/>
		<result property="totalScore" column="total_score"/>
		<result property="totalQuestionNum" column="total_question_num"/>
		<result property="rightQuestionNum" column="right_question_num"/>
		<result property="doQuestionNum" column="do_question_num"/>
		<result property="recordStatus" column="record_status"/>
		<result property="recordId" column="record_id"/>
		<result property="doType" column="do_type"/>
    </resultMap>

    <select id="selectList" parameterType="com.zhongzheng.modules.goods.bo.GoodsAttachedQueryBo" resultMap="GoodsAttachedResultVo">
		SELECT
			ga.*,
			CASE

			WHEN ga.type = 1 THEN
			0
			WHEN ga.type = 2 THEN
			0
			WHEN ga.type = 3 THEN
			e.do_type
			END do_type,
			CASE

				WHEN ga.type = 1 THEN
					qm.module_name
				WHEN ga.type = 2 THEN
					qc.`name`
				WHEN ga.type = 3 THEN
					e.exam_name
				END NAME,
			CASE

				WHEN ga.type = 1 THEN
					cs.subject_name
				WHEN ga.type = 2 THEN
					cs1.subject_name
				WHEN ga.type = 3 THEN
					cs2.subject_name
				END subject_name,
			CASE

				WHEN ga.type = 1 THEN
					0
				WHEN ga.type = 2 THEN
					0
				WHEN ga.type = 3 THEN
					e.question_num
				END question_num,
			CASE

				WHEN ga.type = 1 THEN
					qm.publish_status
				WHEN ga.type = 2 THEN
					qc.publish_status
				WHEN ga.type = 3 THEN
					e.publish_status
				END publish_status
			<if test="userId != null ">
				,
				CASE
				WHEN ga.type = 1 THEN
				-1
				WHEN ga.type = 2 THEN
				-1
				WHEN ga.type = 3 THEN
				ifnull( (select `status` from user_exam_record uer where uer.exam_id = ga.major_id and uer.user_id = #{userId} ORDER BY uer.record_id desc limit 1),-1)
				END record_status
				,
				CASE
				WHEN ga.type = 1 THEN
				-1
				WHEN ga.type = 2 THEN
				-1
				WHEN ga.type = 3 THEN
				ifnull( (select record_id from user_exam_record uer where uer.exam_id = ga.major_id and uer.user_id = #{userId} ORDER BY uer.record_id desc limit 1),-1)
				END record_id
			</if>
		FROM
			goods_attached ga
				LEFT JOIN question_module qm ON ga.major_id = qm.module_exam_id
				AND ga.type = 1
				LEFT JOIN question_business qb ON qb.major_id = qm.module_exam_id
				AND qb.type = 4
				LEFT JOIN course_subject cs ON qb.subject_id = cs.id
				LEFT JOIN question_chapter qc ON ga.major_id = qc.chapter_exam_id
				AND ga.type = 2
				LEFT JOIN question_business qb1 ON qb1.major_id = qc.chapter_exam_id
				AND qb1.type = 3
				LEFT JOIN course_subject cs1 ON qb1.subject_id = cs1.id
				LEFT JOIN (
				SELECT
					e.*,
					count( eq.question_id ) question_num
				FROM
					exam e
						LEFT JOIN exam_question eq ON e.exam_id = eq.exam_id
				GROUP BY
					e.exam_id
			) e ON ga.major_id = e.exam_id
				AND ga.type = 3
				AND e.STATUS = 1
				LEFT JOIN question_business qb2 ON qb2.major_id = e.exam_id
				AND qb2.type = 2
				LEFT JOIN course_subject cs2 ON qb2.subject_id = cs2.id
		WHERE
			ga.goods_id = #{goodsId}
		order by ga.sort asc
    </select>

	<select id="selectDoList" parameterType="com.zhongzheng.modules.goods.bo.GoodsAttachedQueryBo" resultMap="GoodsAttachedResultVo">
		SELECT
			ga.*,
			CASE

				WHEN ga.type = 1 THEN
					qm.module_name
				WHEN ga.type = 2 THEN
					qc.`name`
				WHEN ga.type = 3 THEN
					e.exam_name
				END NAME,
			CASE

				WHEN ga.type = 1 THEN
					cs.subject_name
				WHEN ga.type = 2 THEN
					cs1.subject_name
				WHEN ga.type = 3 THEN
					cs2.subject_name
				END subject_name,
			CASE

				WHEN ga.type = 1 THEN
					0
				WHEN ga.type = 2 THEN
					0
				WHEN ga.type = 3 THEN
					e.question_num
				END question_num,
			CASE

				WHEN ga.type = 1 THEN
					qm.publish_status
				WHEN ga.type = 2 THEN
					qc.publish_status
				WHEN ga.type = 3 THEN
					e.publish_status
				END publish_status,
			CASE

				WHEN ga.type = 1 THEN
					0
				WHEN ga.type = 2 THEN
					0
				WHEN ga.type = 3 THEN
					uer.record_id
				END record_id,
			CASE
				WHEN ga.type = 1 THEN
					0
				WHEN ga.type = 2 THEN
					0
				WHEN ga.type = 3 THEN
					uer.status
				END record_status,
			uer.total_question_num,
			uer.right_question_num,
			uer.do_question_num
		FROM
			goods_attached ga
				LEFT JOIN question_module qm ON ga.major_id = qm.module_exam_id
				AND ga.type = 1
				LEFT JOIN question_business qb ON qb.major_id = qm.module_exam_id
				AND qb.type = 4
				LEFT JOIN course_subject cs ON qb.subject_id = cs.id
				LEFT JOIN question_chapter qc ON ga.major_id = qc.chapter_exam_id
				AND ga.type = 2
				LEFT JOIN question_business qb1 ON qb1.major_id = qc.chapter_exam_id
				AND qb1.type = 3
				LEFT JOIN course_subject cs1 ON qb1.subject_id = cs1.id
				LEFT JOIN (
				SELECT
					e.*,
					count( eq.question_id ) question_num
				FROM
					exam e
						LEFT JOIN exam_question eq ON e.exam_id = eq.exam_id
				GROUP BY
					e.exam_id
			) e ON ga.major_id = e.exam_id
				AND ga.type = 3
				AND e.STATUS = 1
				LEFT JOIN question_business qb2 ON qb2.major_id = e.exam_id
				AND qb2.type = 2
				LEFT JOIN course_subject cs2 ON qb2.subject_id = cs2.id
				LEFT JOIN (SELECT
							   record_id,
							   `status`,
							   exam_id,
							   total_question_num,
							   right_question_num,
							   do_question_num
						   FROM
							   user_exam_record
						   WHERE
								   record_id IN (
								   SELECT
									   MAX( record_id ) record_id
								   FROM
									   user_exam_record
								   WHERE
									   user_id = #{userId}
									 AND goods_id = #{goodsId}
								   GROUP BY
									   exam_id)) uer on uer.exam_id = e.exam_id
		WHERE
			ga.goods_id = #{goodsId}
		  AND ((
					   ga.type = 3
					   AND ga.major_id IN ( SELECT exam_id FROM user_exam_record WHERE user_id = #{userId} AND goods_id = #{goodsId} AND module_exam_id = 0 AND chapter_exam_id = 0 GROUP BY exam_id )
					   AND e.STATUS = 1
				   )
			OR (
					   ga.type = 2
					   AND ga.major_id IN ( SELECT chapter_exam_id FROM user_exam_record WHERE user_id = #{userId} AND goods_id = #{goodsId} AND module_exam_id = 0 AND chapter_exam_id > 0 GROUP BY chapter_exam_id ))
			OR (
					   ga.type = 1
					   AND ga.major_id IN ( SELECT module_exam_id FROM user_exam_record WHERE user_id = #{userId} AND goods_id = #{goodsId} AND module_exam_id > 0 GROUP BY module_exam_id )))
	</select>
</mapper>
