<?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.exam.mapper.ExamNumberMapper">

    <resultMap type="com.zhongzheng.modules.exam.domain.ExamNumber" id="ExamNumberResult">
        <result property="examNumberId" column="exam_number_id"/>
        <result property="code" column="code"/>
        <result property="examNumber" column="exam_number"/>
        <result property="doNumber" column="do_number"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="goodsType" column="goods_type"/>
        <result property="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="projectId" column="project_id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.exam.vo.ExamNumberVo" id="ExamNumberVoResult">
        <result property="examNumberId" column="exam_number_id"/>
        <result property="code" column="code"/>
        <result property="examNumber" column="exam_number"/>
        <result property="doNumber" column="do_number"/>
        <result property="status" column="status"/>
        <result property="goodsType" column="goods_type"/>
        <result property="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="projectId" column="project_id"/>
        <result property="userId" column="user_id"/>
        <result property="goodsIds" column="goods_id"/>
    </resultMap>

    <select id="ListExamNumber" parameterType="com.zhongzheng.modules.exam.bo.ExamNumberQueryBo" resultMap="ExamNumberVoResult">
        SELECT
            *,
            (SELECT cet.education_name FROM course_education_type cet where cet.id = en.education_type_id) as education_name,
            (SELECT cpt.project_name FROM course_project_type cpt where cpt.id = en.project_id) as project_name,
            (SELECT cb.business_name FROM course_business cb where cb.id = en.business_id) as business_name
        FROM
            exam_number en where 1=1
        <if test="status != null and status.size()!=0 ">
            AND en.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="examNumberId != null and examNumberId != ''">
            AND en.exam_number_id = #{examNumberId}
        </if>
        <if test="goodsId != null and goodsId != ''">
            AND  (SELECT COUNT(eng.id) FROM exam_number_goods eng where eng.goods_id =#{goodsId}) > 0
        </if>
        order by en.create_time desc
    </select>

    <select id="selectExam" parameterType="map" resultType="integer">
        SELECT
            COUNT( en.exam_number_id )
        FROM
            exam_number en
                LEFT JOIN exam_number_goods eng ON en.exam_number_id = eng.exam_number_id
        WHERE
            1 = 1
          AND eng.goods_id = #{goodsId}
          and en.exam_number_id != #{examNumberId}
          AND en.STATUS =1
    </select>

    <select id="selectApply" parameterType="map" resultType="integer">
        SELECT
            COUNT(ea.apply_id)
        FROM
            exam_apply ea
                LEFT JOIN exam_apply_goods eag ON ea.apply_id = eag.apply_id
        where 1=1
          and eag.goods_id =#{goodsId}
          and ea.`status`in (1,0,2)
    </select>

    <select id="selectApplyBefore" parameterType="map" resultType="integer">
        SELECT
            COUNT(1)
        FROM
            exam_before_goods ebg
                LEFT JOIN exam_before eb on ebg.before_id = eb.before_id
        where ebg.goods_id = #{goodsId}
          and eb.`status` in (1,0,2)
    </select>

    <select id="selectUpExamNum" resultMap="ExamNumberVoResult">
        SELECT
        en.*,
        od.user_id,
        eng.goods_id
        FROM
        `order` od
        LEFT JOIN order_goods og on od.order_sn = og.order_sn
        LEFT JOIN exam_number_goods eng  on eng.goods_id = og.goods_id
        LEFT JOIN exam_number en ON eng.exam_number_id = en.exam_number_id
        where en.`status` = 1
        and og.`status` = 1
        and og.refund_status in (0,1,3)
        and og.pay_status in (2,3)
        and (SELECT COUNT(1) FROM user_exam_goods_supplement uegs LEFT JOIN user_exam_goods ueg on ueg.user_exam_goods_id = uegs.user_exam_goods_id where uegs.goods_id = eng.goods_id and ueg.user_id = od.user_id) &lt; 1
    </select>

    <select id="selectUpExam" resultMap="ExamNumberVoResult">
        SELECT
            en.*,
            od.user_id,
            eng.goods_id
        FROM
            `order` od
                LEFT JOIN order_goods og on od.order_sn = og.order_sn
                LEFT JOIN exam_number_goods eng  on eng.goods_id = og.goods_id
                LEFT JOIN exam_number en ON eng.exam_number_id = en.exam_number_id
        where en.`status` = 1
          and og.`status` = 1
          and og.refund_status in (0,1,3)
          and og.pay_status in (2,3)
          and (SELECT COUNT(1) FROM user_exam_goods_supplement uegs LEFT JOIN user_exam_goods ueg on ueg.user_exam_goods_id = uegs.user_exam_goods_id where uegs.goods_id = eng.goods_id and ueg.user_id = od.user_id and (uegs.exam_number != en.exam_number or uegs.do_number != en.do_number)) > 0
    </select>
</mapper>
