<?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.ExamBeforeMapper">

    <resultMap type="com.zhongzheng.modules.exam.domain.ExamBefore" id="ExamBeforeResult">
        <result property="beforeId" column="before_id"/>
        <result property="code" column="code"/>
        <result property="beforeName" column="before_name"/>
        <result property="beforeStartTime" column="before_start_time"/>
        <result property="beforeEndTime" column="before_end_time"/>
        <result property="beforeStatus" column="before_status"/>
        <result property="beforeUrl" column="before_url"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.exam.vo.ExamBeforeVo" id="ExamBeforeVo">
        <result property="beforeId" column="before_id"/>
        <result property="code" column="code"/>
        <result property="beforeName" column="before_name"/>
        <result property="beforeStartTime" column="before_start_time"/>
        <result property="beforeEndTime" column="before_end_time"/>
        <result property="beforeStatus" column="before_status"/>
        <result property="beforeUrl" column="before_url"/>
        <result property="status" column="status"/>
        <result property="urlName" column="url_name"/>
        <result property="url" column="url"/>
        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="people" column="people"/>
        <result property="businessName" column="business_name"/>
    </resultMap>


    <resultMap type="com.zhongzheng.modules.exam.vo.ExamNumberGoodsVo" id="ExamNumberGoodsVoResult">
        <result property="id" column="id"/>
        <result property="examNumberId" column="exam_number_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="goodsName" column="goods_name"/>
        <result property="code" column="code"/>
        <result property="standPrice" column="stand_price"/>
        <result property="goodsStatus" column="goods_status"/>
    </resultMap>

    <select id="listGoods" parameterType="com.zhongzheng.modules.exam.bo.ExamNumberGoodsQueryBo" resultMap="ExamNumberGoodsVoResult">
        SELECT
        goods_id,
        goods_name,
        CODE,
        stand_price,
        (case WHEN (SELECT COUNT(eb.before_id) FROM exam_before eb LEFT JOIN exam_before_goods ebg on eb.before_id =ebg.before_id where 1=1 and ebg.goods_id = g.goods_id and eb.status in (0,1)) >0 then 0 ELSE 1 end) as goods_status
        FROM
        goods g
        WHERE
        1 = 1
        and g.status in(1)
        and (SELECT COUNT(eng.id) FROM exam_number_goods eng LEFT JOIN exam_number en on eng.exam_number_id = en.exam_number_id where eng.goods_id =g.goods_id and en.`status` = 1) > 0
        <if test="businessId != null and businessId != ''">
            and g.business_id = #{businessId}
        </if>
        <if test="goodsId != null and goodsId != ''">
            and g.goods_id = #{goodsId}
        </if>
        <if test="projectId != null and projectId != ''">
            and g.project_id = #{projectId}
        </if>
        <if test="goodsType != null and goodsType != ''">
            and g.goods_type = #{goodsType}
        </if>
        order by g.create_time desc
    </select>

    <select id="examNumberGoodsVos" parameterType="long" resultMap="ExamNumberGoodsVoResult">
        SELECT
            g.goods_id,
            g.goods_name,
            g.CODE,
            g.stand_price
        FROM
            goods g
                LEFT JOIN exam_before_goods ebg ON g.goods_id = ebg.goods_id
        WHERE
            1 = 1
          AND g.STATUS IN ( 1 )
          AND ebg.before_id =#{beforeId}

    </select>

    <select id="selectByBefore" parameterType="long" resultMap="ExamBeforeVo">
        SELECT
            *,
            (select cet.education_name FROM course_education_type cet where eb.education_type_id= cet.id) as education_name,
            (select cpt.project_name FROM course_project_type cpt where eb.project_id= cpt.id) as project_name,
            (select cb.business_name FROM course_business cb where eb.business_id= cb.id) as business_name
        FROM
            exam_before eb
                LEFT JOIN exam_before_url ebu on eb.before_url=ebu.before_url_id
        where 1=1
          and eb.before_id = #{beforeId}
    </select>

    <select id="countBeforeGoods" parameterType="long" resultType="integer">
        SELECT
            COUNT( 1 )
        FROM
            exam_before_goods ebg
        WHERE
            ebg.before_id = #{beforeId}
    </select>

    <select id="beforeGoods" parameterType="long" resultType="integer">
        SELECT
            COUNT( 1 )
        FROM
            exam_before_goods ebg
                LEFT JOIN exam_before eb ON ebg.before_id = eb.before_id
        WHERE
            ebg.before_id != #{beforeId}
	AND ( SELECT COUNT( 1 ) FROM exam_before_goods ebgs WHERE ebgs.before_id = #{beforeId} AND ebgs.goods_id = ebg.goods_id ) > 0
	AND eb.`status` =1
    </select>

    <select id="verifyInfo" parameterType="long" resultType="Long">
        SELECT
            COUNT(1)
        FROM
            user_subscribe us
        WHERE
            1 =1
          AND us.goods_id = #{goodsId}
          AND us.exam_status = 0
          AND us.subscribe_status = 1
          AND us.before_status =1
    </select>


    <select id="verifyExamBeforeVo" parameterType="long" resultMap="ExamBeforeVo">
        SELECT
            eb.*,
            ebu.url_name,
            ebu.url
        FROM
            exam_before eb
                LEFT JOIN exam_before_goods ebg ON eb.before_id = ebg.before_id
                LEFT JOIN exam_before_url ebu on eb.before_url=ebu.before_url_id
        where ebg.goods_id =#{goodsId}
          and eb.`status` =1
            LIMIT 1
    </select>

    <select id="queryBefore" parameterType="com.zhongzheng.modules.exam.bo.ExamBeforeQueryBo" resultMap="ExamBeforeVo">
        SELECT
            *,
            (select cet.education_name FROM course_education_type cet where eb.education_type_id= cet.id) as education_name,
            (select cpt.project_name FROM course_project_type cpt where eb.project_id= cpt.id) as project_name,
            (select cb.business_name FROM course_business cb where eb.business_id= cb.id) as business_name,
            (SELECT COUNT(us.apply_id) FROM user_subscribe us where us.before_id = eb.before_id and us.before_status = 1) as people
        FROM
            exam_before eb
                LEFT JOIN exam_before_url ebu on eb.before_url=ebu.before_url_id
        where 1=1
        <if test="status != null and status.size()!=0 ">
            AND eb.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by eb.create_time desc
    </select>
</mapper>
