<?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.user.mapper.UserCourseAnswerMapper">

    <resultMap type="com.zhongzheng.modules.user.domain.UserCourseAnswer" id="UserCourseAnswerResult">
        <result property="answerId" column="answer_id"/>
        <result property="userId" column="user_id"/>
        <result property="assignUserId" column="assign_user_id"/>
        <result property="answerText" column="answer_text"/>
        <result property="courseId" column="course_id"/>
        <result property="sectionId" column="section_id"/>
        <result property="moduleId" column="module_id"/>
        <result property="chapterId" column="chapter_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="goodsId" column="goods_id"/>
        <result property="assignAnswerId" column="assign_answer_id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.user.vo.UserCourseAnswerVo" id="UserCourseAnswerVo">
        <result property="answerId" column="answer_id"/>
        <result property="userId" column="user_id"/>
        <result property="assignUserId" column="assign_user_id"/>
        <result property="answerText" column="answer_text"/>
        <result property="courseId" column="course_id"/>
        <result property="sectionId" column="section_id"/>
        <result property="moduleId" column="module_id"/>
        <result property="chapterId" column="chapter_id"/>
        <result property="realname" column="realname"/>
        <result property="avatar" column="avatar"/>
        <result property="assignRealname" column="assign_realname"/>
        <result property="assignAvatar" column="assign_avatar"/>
        <result property="createTime" column="create_time"/>
        <result property="goodsId" column="goods_id"/>
        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="goodsName" column="goods_name"/>
        <result property="courseName" column="course_name"/>
        <result property="assignAnswerId" column="assign_answer_id"/>
    </resultMap>

    <select id="selectAnswer" parameterType="com.zhongzheng.modules.user.bo.UserCourseAnswerQueryBo"  resultMap="UserCourseAnswerVo">
        SELECT
        uca.*,
        u.realname,
        u.avatar,
        u2.realname assign_realname,
        u2.avatar assign_avatar
        <if test="msgType != null">
        ,g.goods_name,
        c.course_name,
        cet.education_name,
        cpt.project_name,
        cb.business_name
        </if>
        FROM
        user_course_answer uca
        LEFT JOIN `user` u ON uca.user_id = u.user_id
        LEFT JOIN `user` u2 ON uca.assign_user_id = u2.user_id
        <if test="msgType != null">
        LEFT JOIN goods g on uca.goods_id = g.goods_id
        LEFT JOIN course c on c.course_id = uca.course_id
        LEFT JOIN course_education_type cet ON g.education_type_id = cet.id
        LEFT JOIN course_project_type cpt ON g.project_id = cpt.id
        LEFT JOIN course_business cb ON g.business_id = cb.id
        </if>
        WHERE
        1 = 1
        AND uca.STATUS =1
        <if test="courseId != null and courseId != ''">
          AND uca.course_id = #{courseId}
        </if>
        <if test="moduleId != null and moduleId != ''">
          AND uca.module_id = #{moduleId}
        </if>
        <if test="chapterId != null and chapterId != ''">
          AND uca.chapter_id = #{chapterId}
        </if>
        <if test="sectionId != null and sectionId != ''">
          AND uca.section_id = #{sectionId}
        </if>
        <if test="goodsId != null and goodsId != ''">
            AND uca.goods_id = #{goodsId}
        </if>
        <if test="msgType != null and msgType == 1">
            AND ISNULL(uca.assign_user_id )
        </if>
        <if test="msgType != null and msgType == 2">
            AND uca.assign_user_id>0
        </if>
        <if test="msgType != null">
            <if test="educationTypeId != null and educationTypeId != ''">
                AND g.education_type_id = #{educationTypeId}
            </if>
            <if test="businessIds != null and businessIds.size()!=0 ">
                AND g.business_id in
                <foreach collection="businessIds" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="businessId != null and businessId != ''">
                AND g.business_id = #{businessId}
            </if>
        </if>
    </select>
</mapper>
