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

    <resultMap type="com.zhongzheng.modules.exam.domain.ExamApply" id="ExamApplyResult">
        <result property="applyId" column="exam_apply_id"/>
        <result property="applyUrl" column="apply_url"/>
        <result property="applyName" column="apply_name"/>
        <result property="applyIntroduce" column="apply_introduce"/>
        <result property="applyStatus" column="apply_status"/>
        <result property="applyStartTime" column="apply_start_time"/>
        <result property="applyEndTime" column="apply_end_time"/>
        <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.ExamApplyVo" id="ExamApplyVo">
        <result property="applyId" column="apply_id"/>
        <result property="applyUrl" column="apply_url"/>
        <result property="applyName" column="apply_name"/>
        <result property="applyIntroduce" column="apply_introduce"/>
        <result property="applyStatus" column="apply_status"/>
        <result property="applyStartTime" column="apply_start_time"/>
        <result property="applyEndTime" column="apply_end_time"/>
        <result property="status" column="status"/>
        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="cancelPeople" column="cancel_people"/>
        <result property="people" column="people"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.exam.vo.ExamUserApplyVo" id="ExamUserApplyVo">
        <result property="applyId" column="apply_id"/>
        <result property="applyUrl" column="apply_url"/>
        <result property="applyName" column="apply_name"/>
        <result property="applyIntroduce" column="apply_introduce"/>
        <result property="applyStartTime" column="apply_start_time"/>
        <result property="applyEndTime" column="apply_end_time"/>
        <result property="major" column="major"/>
        <result property="realname" column="realname"/>
        <result property="idCard" column="id_card"/>
    </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>

    <resultMap type="com.zhongzheng.modules.exam.vo.ExamApplySiteVo" id="ExamApplySiteVo">
        <result property="id" column="id"/>
        <result property="siteId" column="site_id"/>
        <result property="applyId" column="apply_id"/>
        <result property="status" column="status"/>
        <result property="status" column="status"/>
        <result property="siteAddress" column="site_address"/>
        <result property="people" column="people"/>
    </resultMap>
    <resultMap type="com.zhongzheng.modules.exam.vo.ExamUserApplySiteVo" id="ExamUserApplySiteVo">
        <result property="id" column="id"/>
        <result property="siteId" column="site_id"/>
        <result property="applyId" column="apply_id"/>
        <result property="status" column="status"/>
        <result property="siteAddress" column="site_address"/>
        <result property="people" column="people"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.exam.vo.ExamApplySiteTimeVo" id="ExamApplySiteTimeVo">
        <result property="id" column="id"/>
        <result property="applySiteId" column="apply_site_id"/>
        <result property="siteTime" column="site_time"/>
        <result property="examTime" column="exam_time"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.exam.vo.ExamUserApplySiteTimeVo" id="ExamUserApplySiteTimeVo">
        <result property="id" column="id"/>
        <result property="applySiteId" column="apply_site_id"/>
        <result property="siteTime" column="site_time"/>
        <result property="examTime" column="exam_time"/>
    </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(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 = g.goods_id and ea.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="countGoods" parameterType="long" resultType="long">
        SELECT
        count(eag.goods_id)
        FROM
            exam_apply ea
            LEFT JOIN exam_apply_goods eag ON ea.apply_id = eag.apply_id where ea.apply_id =#{applyId}
	</select>

    <select id="countGoodsHold" parameterType="long" resultType="long">
        SELECT
            eag.goods_id
        FROM
            exam_apply ea
                LEFT JOIN exam_apply_goods eag ON ea.apply_id = eag.apply_id where ea.apply_id =#{applyId}
                                                                               and (
                SELECT
                    COUNT( eay.apply_id )
                FROM
                    exam_apply eay
                        LEFT JOIN exam_apply_goods eags ON eay.apply_id = eags.apply_id
                WHERE
                    eags.goods_id = eag.goods_id
                  AND eags.apply_id != eag.apply_id and eay.status = 1) > 0
    </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_apply_goods eag ON g.goods_id = eag.goods_id
        WHERE
            1 = 1
          AND g.STATUS IN ( 1 )
          AND eag.apply_id =#{applyId}

    </select>

    <select id="addressExam" parameterType="map" resultMap="ExamApplySiteVo">
        SELECT
            *,
            es.site_address,
            es.people
        FROM
            exam_apply_site eas
                LEFT JOIN exam_site es on eas.site_id = es.site_id
        WHERE
            eas.apply_id =#{applyId}
          and eas.`status` =#{status}
    </select>

    <select id="addressTimeExam" parameterType="map" resultMap="ExamApplySiteTimeVo">
        SELECT
            *
        FROM
            exam_apply_site_time east
        where 1=1
          and east.apply_site_id =#{id}
    </select>

    <select id="selectByApply" parameterType="long" resultMap="ExamApplyVo">
        SELECT
            * ,
            (select cet.education_name FROM course_education_type cet where ea.education_type_id= cet.id) as education_name,
            (select cpt.project_name FROM course_project_type cpt where ea.project_id= cpt.id) as project_name,
            (select cb.business_name FROM course_business cb where ea.business_id= cb.id) as business_name
        FROM
            exam_apply ea
        where 1=1
          and ea.apply_id = #{applyId}
    </select>

    <select id="queryExam" parameterType="com.zhongzheng.modules.exam.bo.ExamApplyQueryBo" resultMap="ExamApplyVo">
        SELECT
        * ,
        (select cet.education_name FROM course_education_type cet where ea.education_type_id= cet.id) as education_name,
        (select cpt.project_name FROM course_project_type cpt where ea.project_id= cpt.id) as project_name,
        (select cb.business_name FROM course_business cb where ea.business_id= cb.id) as business_name,
        (SELECT COUNT(us.apply_id) FROM user_subscribe us where us.apply_id = ea.apply_id and us.subscribe_status = 1) as people,
        (SELECT COUNT(us.apply_id) FROM user_subscribe us where us.apply_id = ea.apply_id and us.subscribe_status = 2) as cancel_people
        FROM
        exam_apply ea
        where 1=1
        <if test="status != null and status.size()!=0 ">
            AND ea.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by ea.create_time desc
    </select>

    <select id="countGradePeriod" parameterType="long" resultType="integer">
        SELECT
            COUNT(cg.grade_id)
        FROM
            class_grade cg
                LEFT JOIN class_grade_user cgu  ON cg.grade_id = cgu.grade_id
        WHERE
            1 = 1
          AND cgu.user_id = #{userId}
          and cgu.grade_id = #{gradeId}
          AND cgu.period_status = 1
           and (unix_timestamp(now()) &lt; cg.class_end_time or cg.class_start_time is null)
          and cg.`status` =1
          and cgu.`status` =1
    </select>
    <select id="countSubscribe" parameterType="long" resultType="integer">
        SELECT
            COUNT(1)
        FROM
            user_subscribe us
        WHERE
            1 =1
          and us.user_id = #{userId}
          and us.result = 1
          and us.goods_id = #{goodsId}
    </select>

    <select id="countHaveSubscribe" parameterType="long" resultType="integer">
        SELECT
            COUNT(1)
        FROM
            user_subscribe us
        WHERE
            1 =1
          and us.user_id = #{userId}
          and us.exam_status = 0
          and subscribe_status = 1
          and us.goods_id = #{goodsId}
    </select>
    <select id="selectExamUserApplyVo" parameterType="com.zhongzheng.modules.exam.bo.ExamApplyQueryBo" resultMap="ExamUserApplyVo">
        SELECT
            ea.apply_id,
            ea.apply_url,
            ea.apply_name,
            ea.apply_introduce,
            ea.apply_start_time,
            ea.apply_end_time,
            ea.apply_status,
            (SELECT m.category_name FROM major m where m.id = g.major_id) as major,
            (SELECT u.realname FROM `user` u where u.user_id = #{userId}) as realname,
(SELECT u.id_card FROM `user` u where u.user_id = #{userId}) as id_card
        FROM
            exam_apply ea
            LEFT JOIN exam_apply_goods eag ON ea.apply_id = eag.apply_id
            LEFT JOIN goods g on eag.goods_id=g.goods_id
        WHERE
            1 = 1
          AND eag.goods_id = #{goodsId}
          AND ea.`status` =1
    </select>


    <select id="countHaveSubscribeNext" parameterType="long" resultType="integer">
        SELECT
            COUNT(1)
        FROM
            user_subscribe us
        where 1=1
          and us.user_id =#{userId}
          and us.exam_expend = 1
          and us.goods_id = #{goodsId}
    </select>
    <select id="residueSubscribeNext" parameterType="long" resultType="integer">
        SELECT
            ueg.exam_number - ueg.expend_number
        FROM
            user_exam_goods ueg
        WHERE
            ueg.user_id =#{userId}
          AND ueg.goods_id = #{goodsId}
    </select>

    <select id="beforeSubscribeNext" parameterType="long" resultType="integer">
        SELECT
            COUNT(eb.before_id)
        FROM
            exam_before eb
                LEFT JOIN exam_before_goods ebg on eb.before_id=ebg.before_id
        WHERE
            ebg.goods_id =#{goodsId}
          and eb.`status`=1
    </select>

    <select id="subscribeApplySite" parameterType="com.zhongzheng.modules.exam.bo.ExamApplyQueryBo" resultMap="ExamUserApplySiteVo">
        SELECT
            eas.apply_id,
            eas.id,
            es.*
        FROM
            exam_apply_site eas
                LEFT JOIN exam_site es ON es.site_id = eas.site_id
        WHERE
            eas.apply_id = #{applyId}
            and eas.status = 1
    </select>

    <select id="subscribeApplySiteTime" parameterType="Long" resultMap="ExamUserApplySiteTimeVo">
        SELECT * FROM exam_apply_site_time east where east.apply_site_id = #{id}
    </select>

    <select id="registration" parameterType="map" resultType="long">
        SELECT
            COUNT( 1 )
        FROM
            user_subscribe us
        WHERE
            subscribe_status = 1
          AND us.apply_id = #{applyId}
          AND us.apply_site_exam_time = #{examTime}
          AND us.apply_site_address = #{siteAddress}
          AND us.apply_site_start_time =#{startTime}
          AND us.apply_site_end_time =#{endTime}
    </select>


    <select id="userStatus" parameterType="map" resultType="long">
        SELECT
            COUNT( 1 )
        FROM
            user_subscribe us
        WHERE
            subscribe_status = 1
          AND us.apply_site_exam_time = #{examTime}
          AND us.user_id = #{userId}
          AND us.apply_site_start_time =#{startTime}
          AND us.apply_site_end_time =#{endTime}
    </select>

    <select id="subscribeApplySiteTrain" parameterType="com.zhongzheng.modules.exam.bo.ExamApplyQueryBo" resultMap="ExamUserApplySiteVo">
        SELECT
            eas.apply_id,
            eas.id,
            es.*
        FROM
            exam_apply_site eas
                LEFT JOIN exam_site es ON es.site_id = eas.site_id
        WHERE
            eas.apply_id = #{applyId}
          and eas.status = 2
    </select>

    <select id="registrationTrain" parameterType="map" resultType="long">
        SELECT
            COUNT( 1 )
        FROM
            user_subscribe us
        WHERE
            subscribe_status = 1
          AND us.apply_id = #{applyId}
          AND us.apply_site_exam_train_time = #{examTime}
          AND us.apply_site_address_train = #{siteAddress}
          AND us.apply_site_start_train_time =#{startTime}
          AND us.apply_site_end_train_time =#{endTime}
    </select>


    <select id="userStatusTrain" parameterType="map" resultType="long">
        SELECT
            COUNT( 1 )
        FROM
            user_subscribe us
        WHERE
            subscribe_status = 1
          AND us.apply_site_exam_train_time = #{examTime}
          AND us.user_id = #{userId}
          AND us.apply_site_start_train_time =#{startTime}
          AND us.apply_site_end_train_time =#{endTime}
    </select>
</mapper>
