<?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.bank.mapper.QuestionMapper">

    <resultMap type="com.zhongzheng.modules.bank.domain.Question" id="QuestionResult">
        <result property="questionId" column="question_id"/>
        <result property="content" column="content"/>
        <result property="type" column="type"/>
        <result property="answerQuestion" column="answer_question"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="analysisContent" column="analysis_content"/>
        <result property="imgUrl" column="img_url"/>
        <result property="jsonStr" column="json_str"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="knowledgeIds" column="knowledge_ids"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="code" column="code"/>
        <result property="importNo" column="import_no"/>
        <result property="createBy" column="create_by"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.bank.vo.QuestionVo" id="QuestionResultVo">
        <result property="questionId" column="question_id"/>
        <result property="content" column="content"/>
        <result property="type" column="type"/>
        <result property="answerQuestion" column="answer_question"/>
        <result property="status" column="status"/>
        <result property="analysisContent" column="analysis_content"/>
        <result property="imgUrl" column="img_url"/>
        <result property="jsonStr" column="json_str"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="knowledgeIds" column="knowledge_ids"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="code" column="code"/>
        <collection property="businessList" column="question_id" select="findBusinessList" />
        <collection property="knowledgeList" column="knowledge_ids" select="findKnowledge"/>
        <collection property="examList" column="question_id" select="findExamList" />
    </resultMap>

    <resultMap type="com.zhongzheng.modules.bank.vo.ExamVo" id="ExamVoResult">
        <result property="examId" column="exam_id"/>
        <result property="examName" column="exam_name"/>
    </resultMap>

    <select id="findExamList" resultMap="ExamVoResult">
        SELECT
            e.exam_id,
            e.exam_name
        FROM
            exam_question eq
                LEFT JOIN exam e ON eq.exam_id = e.exam_id
        WHERE
            eq.question_id = #{question_id} AND e.status !=-1
    </select>

    <select id="findKnowledge" resultMap="ExamKnowledge">
        SELECT
            ek.knowledge_name,
            ek.knowledge_id
        FROM
            exam_knowledge ek
        WHERE
            FIND_IN_SET( ek.knowledge_id, #{knowledge_ids} ) AND ek.status !=-1
    </select>
    <select id="findBusinessList" resultMap="QuestionBusiness">
        SELECT
            qb.education_type_id,
            qb.business_id,
            qb.project_id,
            qb.subject_id,
            qb.type as business_type,
            cet.education_name,
            cpt.project_name,
            cb.business_name,
            cs.subject_name
        FROM
             question_business qb
                 LEFT JOIN course_education_type cet ON qb.education_type_id = cet.id AND cet.status =1
                 LEFT JOIN course_project_type cpt ON qb.project_id = cpt.id AND cpt.status =1
                 LEFT JOIN course_business cb ON qb.business_id = cb.id AND cb.status =1
                 LEFT JOIN course_subject cs ON cs.id = qb.subject_id AND cs.status =1
        WHERE
            qb.major_id=#{question_id} and qb.type = 1
    </select>

    <resultMap type="com.zhongzheng.modules.bank.domain.QuestionBusiness" id="QuestionBusiness">
        <result property="id" column="id"/>
        <result property="subjectId" column="subject_id"/>
        <result property="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="projectId" column="project_id"/>

        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="subjectName" column="subject_name"/>
        <result property="type" column="business_type"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.exam.domain.ExamKnowledge" id="ExamKnowledge">
        <result property="knowledgeId" column="knowledge_id"/>
        <result property="knowledgeName" column="knowledge_name"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.goods.vo.GoodsUserQuestionVo" id="GoodsUserQuestionVo">
        <result property="goodsId" column="goods_id"/>
        <result property="year" column="year"/>
        <result property="supplyId" column="supply_id"/>
        <result property="goodsType" column="goods_type"/>
        <result property="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="schoolId" column="school_id"/>
        <result property="majorId" column="major_id"/>
        <result property="goodsName" column="goods_name"/>
        <result property="standPrice" column="stand_price"/>
        <result property="lowestPrice" column="lowest_price"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="validityStartTime" column="validity_start_time"/>
        <result property="validityEndTime" column="validity_end_time"/>
        <result property="studyStartTime" column="study_start_time"/>
        <result property="studyEndTime" column="study_end_time"/>
        <result property="certificateIds" column="certificate_ids"/>
        <result property="introduce" column="introduce"/>
        <result property="suitableObject" column="suitable_object"/>
        <result property="buyNote" column="buy_note"/>
        <result property="pcDetailHtml" column="pc_detail_html"/>
        <result property="mobileDetailHtml" column="mobile_detail_html"/>
        <result property="goodsStatus" column="goods_status"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="classHours" column="class_hours"/>
        <result property="standPriceJson" column="stand_price_json"/>
        <result property="code" column="code"/>
        <result property="projectId" column="project_id"/>
        <result property="goodsAuditionConfig" column="goods_audition_config"/>
        <result property="goodsPhotographConfig" column="goods_photograph_config"/>
        <result property="goodsPlayConfig" column="goods_play_config"/>
        <result property="goodsExamConfig" column="goods_exam_config"/>

        <result property="supplyName" column="supply_name"/>
        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="schoolName" column="school_name"/>
        <result property="categoryName" column="category_name"/>
        <result property="handoutsId" column="handouts_id"/>
        <result property="templateType" column="template_type"/>
        <result property="period" column="period"/>
        <result property="makeStartTime" column="make_start_time"/>
        <result property="makeEndTime" column="make_end_time"/>
        <result property="studyCount" column="study_count"/>
        <result property="makeGoodsName" column="make_goods_name"/>
        <result property="makeGoodsCode" column="make_goods_code"/>
    </resultMap>

    <select id="selectList" parameterType="com.zhongzheng.modules.bank.bo.QuestionQueryBo" resultMap="QuestionResultVo">
       SELECT DISTINCT
        q.*
        FROM
        question q
        LEFT JOIN question_business qb ON q.question_id = qb.major_id
        AND qb.type = 1
        WHERE
        1 = 1 AND q.status !=-1
        <if test="status != null and status.size()!=0 ">
            AND q.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>

        <if test="educationTypeId != null and educationTypeId != ''">
            AND qb.education_type_id = #{educationTypeId}
        </if>
        <if test="subjectId != null and subjectId != ''">
            AND qb.subject_id = #{subjectId}
        </if>
        <if test="type != null and type != ''">
            AND q.type = #{type}
        </if>
        <if test="businessId != null and businessId != ''">
            AND qb.business_id = #{businessId}
        </if>
        <if test="prefixName != null and prefixName != ''">
            AND q.prefix_name like concat('%', #{prefixName}, '%')
        </if>
        <if test="key != null and key != ''">
            AND (q.content like concat('%', #{key}, '%') or q.prefix_name like concat('%', #{key}, '%') or q.code = #{key})
        </if>
        <if test="pageSizeSelf != null and pageSizeSelf != ''">
            LIMIT #{currIndex} , #{pageSizeSelf}
        </if>
        GROUP BY
        q.question_id
        ORDER BY q.question_id DESC
    </select>

    <select id="selectList_COUNT" resultType="Long">
        SELECT
        count(distinct q.question_id)
        FROM
        question q
        LEFT JOIN question_business qb ON q.question_id = qb.major_id and qb.type = 1
        WHERE
        1 = 1 AND q.status !=-1
        <if test="status != null and status.size()!=0 ">
            AND q.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>

        <if test="educationTypeId != null and educationTypeId != ''">
            AND qb.education_type_id = #{educationTypeId}
        </if>
        <if test="subjectId != null and subjectId != ''">
            AND qb.subject_id = #{subjectId}
        </if>
        <if test="businessId != null and businessId != ''">
            AND qb.business_id = #{businessId}
        </if>
        <if test="type != null and type != ''">
            AND q.type = #{type}
        </if>
        <if test="prefixName != null and prefixName != ''">
            AND q.prefix_name like concat('%', #{prefixName}, '%')
        </if>

    </select>


    <select id="listGoodsUserQuestionVo" parameterType="com.zhongzheng.modules.bank.bo.QuestionQueryBo" resultMap="GoodsUserQuestionVo">
        SELECT
            g.*,
            ps.supply_name,
            cet.education_name,
            cpt.project_name,
            cb.business_name,
            s.school_name,
            m.category_name,
            ot.type AS template_type,
            ( SELECT gs.goods_name FROM goods gs WHERE 1 = 1 AND g.make_goods_id = gs.goods_id ) AS make_goods_name,
            ( SELECT gs.CODE FROM goods gs WHERE 1 = 1 AND g.make_goods_id = gs.goods_id ) AS make_goods_code,
            o.user_id
        FROM
            goods g
                LEFT JOIN pay_supply ps ON g.supply_id = ps.supply_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
                LEFT JOIN school s ON s.id = g.school_id
                LEFT JOIN major m ON g.major_id = m.id
                LEFT JOIN order_input_template ot ON cb.template_status = ot.id
                LEFT JOIN goods_attached ga ON g.goods_id = ga.goods_id
                LEFT JOIN question_business qb ON ga.major_id = qb.major_id
                LEFT JOIN order_goods og on g.goods_id =og.goods_id
                LEFT JOIN `order` o on o.order_sn = og.order_sn
        WHERE
            1 = 1
          AND g.goods_type = 2
          AND o.user_id=#{userId}
          AND og.`status` =1
          and og.pay_status in (2,3)
          AND og.refund_status in (0,3,1)
        GROUP BY g.goods_id
        ORDER BY g.update_time DESC
    </select>
</mapper>
