<?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.goods.mapper.GoodsMapper">

    <resultMap type="com.zhongzheng.modules.goods.domain.Goods" id="GoodsResult">
        <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="supplyName" column="supply_name"/>
        <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="handoutsId" column="handouts_id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.goods.vo.GoodsVo" id="GoodsResultVo">
        <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="gradeId" column="grade_id"/>
        <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="goodsAuditionExamConfig" column="goods_audition_exam_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"/>
        <result property="courseNum" column="course_num"/>
        <result property="sectionNum" column="section_num"/>
    </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="selectList" parameterType="com.zhongzheng.modules.goods.bo.GoodsQueryBo" resultMap="GoodsResultVo">
        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
        <if test="sectionNum ==1">
        ,(SELECT COUNT(m.id)+(SELECT COUNT(n.id) FROM course_chapter_section n LEFT JOIN course_module_chapter p on n.chapter_id = p.chapter_id LEFT JOIN course_menu m on m.menu_id = p.module_id LEFT JOIN goods_course gc on gc.course_id = m.course_id  where gc.goods_id =  g.goods_id and m.type in(1))+(SELECT COUNT(n.id) FROM course_chapter_section n  LEFT JOIN course_menu m on m.menu_id = n.chapter_id LEFT JOIN goods_course gc on gc.course_id = m.course_id where gc.goods_id =  g.goods_id and m.type in(2)) FROM course_menu m LEFT JOIN goods_course gc on gc.course_id = m.course_id  where gc.goods_id =  g.goods_id  and m.type in(3)) as section_num
        </if>
        <if test="goodsType ==1">
        , (SELECT count(*) FROM goods_course where goods_id =g.goods_id ) as course_num
        </if>
        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
        WHERE
        1 = 1
        <if test="status != null and status.size()!=0 ">
            AND g.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="goodsIds != null and goodsIds.size()!=0 ">
            AND g.goods_id in
            <foreach collection="goodsIds" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="goodsType != null and goodsType != ''">
            AND g.goods_type = #{goodsType}
        </if>
        <if test="makeGoodsId != null and makeGoodsId != ''">
            AND g.make_goods_id = #{makeGoodsId}
        </if>
        <if test="goodsName != null and goodsName != ''">
            AND g.goods_name like concat('%', #{goodsName}, '%')
        </if>
        <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 test="schoolId != null and schoolId != ''">
            AND g.school_id = #{schoolId}
        </if>
        <if test="majorId != null and majorId != ''">
            AND g.major_id = #{majorId}
        </if>
          <if test="goodsStatus != null ">
            AND g.goods_status = #{goodsStatus}
        </if>
        <if test="studyStartTime != null and studyStartTime != '' and studyEndTime != null and studyEndTime != ''">
            and #{studyStartTime} BETWEEN g.validity_start_time and g.validity_end_time or #{studyEndTime} BETWEEN g.validity_start_time and g.validity_end_time
        </if>
        <if test="validityStartTime != null or validityEndTime != null ">
            AND (#{validityStartTime} BETWEEN g.validity_start_time and g.validity_end_time or #{validityEndTime} BETWEEN g.validity_start_time and g.validity_end_time)
        </if>
        <if test="goodsType ==1 and subjectId >0">
            AND (SELECT count(*) from goods_course gc LEFT JOIN course c ON gc.course_id = c.course_id where gc.goods_id = g.goods_id and c.`status` = 1
            AND c.subject_id = #{subjectId}) >0
        </if>
        <if test="goodsType ==2 and subjectId >0">
            AND g.goods_id in(
            SELECT
            ga.goods_id
            FROM
            goods_attached ga
            LEFT JOIN question_business qb ON ga.major_id = qb.major_id
            WHERE
            ((
            qb.type = 4
            AND ga.type = 1
            )
            OR ( qb.type = 3 AND ga.type = 2 )
            OR ( qb.type = 2 AND ga.type = 3 ))
            AND qb.subject_id = #{subjectId}
            )
        </if>
        ORDER BY g.create_time DESC
    </select>

    <select id="selectDetail" parameterType="Long" resultMap="GoodsResultVo">
        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,
            (SELECT
                 count( DISTINCT c.subject_id )
             FROM
                 course c
                     LEFT JOIN goods_course gc ON gc.course_id = c.course_id
             WHERE
                 gc.goods_id = g.goods_id) as subject_num
        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
        WHERE
            g.goods_id = #{goodsId}
    </select>

    <select id="listGoods" parameterType="com.zhongzheng.modules.exam.bo.ExamNumberGoodsQueryBo" resultMap="ExamNumberGoodsVoResult">
        SELECT
        goods_id,
        goods_name,
        CODE,
        stand_price,
        <if test="filtration != null and filtration == 1">
            (case WHEN (SELECT COUNT(gs.goods_id) FROM goods gs where 1=1 and gs.make_goods_id = g.goods_id and gs.status =1 and gs.goods_type =4) >0 then 0 ELSE 1 end) as goods_status
        </if>
        <if test="filtration != null and filtration == 2">
        (case WHEN (SELECT COUNT(gs.goods_id) FROM goods gs where 1=1 and gs.make_goods_id = g.goods_id and gs.status =1 and gs.goods_type =3) >0 then 0 ELSE 1 end) as goods_status
        </if>
        FROM
        goods g
        WHERE
        1 = 1
        and g.status in(1)
        and g.goods_type=1
        <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>
          <if test="goodsStatus != null and goodsStatus != ''">
            and g.goods_status = #{goodsStatus}
        </if>
        order by g.create_time desc
    </select>

    <select id="selectUserDetail" parameterType="map" resultMap="GoodsResultVo">
        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,
            (SELECT  DISTINCT cg.grade_id FROM class_grade_user cgu LEFT JOIN class_grade_goods cgg on cgg.grade_id = cgu.grade_id LEFT JOIN class_grade cg on cg.grade_id = cgu.grade_id
            where cgu.user_id = #{userId} and cgg.goods_id=g.goods_id and cg.`status` =1 and unix_timestamp(now()) BETWEEN cg.class_start_time and cg.class_end_time)  as grade_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
        WHERE
            g.goods_id = #{goodsId}
    </select>

    <select id="getQuestionNum" parameterType="Long" resultType="Long">
        SELECT
            count(  question_id ) question_num
        FROM
            exam_question
        WHERE
            find_in_set(
                    exam_id,(
                SELECT
                    concat(
                            IFNULL((
                                       SELECT
                                           GROUP_CONCAT(  exam_id )
                                       FROM
                                           question_chapter_exam
                                       WHERE
                                           FIND_IN_SET(chapter_exam_id,
                                                       (SELECT
                                                            concat(
                                                                    IFNULL(( SELECT GROUP_CONCAT(  major_id ) eids FROM goods_attached WHERE goods_id = #{goodsId} AND type = 2 ), '' ),
                                                                    ',',
                                                                    IFNULL(
                                                                            (
                                                                                SELECT
                                                                                    GROUP_CONCAT(  chapter_exam_id ) eids2
                                                                                FROM
                                                                                    question_module_chapter
                                                                                WHERE
                                                                                        module_exam_id IN ( SELECT major_id FROM goods_attached WHERE goods_id = #{goodsId} AND type = 1 )),
                                                                            ''
                                                                        ))))),
                                   ''
                                ),
                            ',',
                            IFNULL(( SELECT GROUP_CONCAT(  major_id ) FROM goods_attached WHERE goods_id = #{goodsId} AND type = 3 ), '' ))))
    </select>

    <update id="queryUpGoods">
        update goods g set g.goods_status=0 where unix_timestamp(now()) > g.validity_end_time and g.goods_status = 1
    </update>
</mapper>
