<?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.grade.mapper.ClassGradeMapper">

    <resultMap type="com.zhongzheng.modules.grade.domain.ClassGrade" id="ClassGradeResult">
        <result property="gradeId" column="grade_id"/>
        <result property="classStatus" column="class_status"/>
        <result property="officialName" column="official_name"/>
        <result property="className" column="class_name"/>
        <result property="studentUpper" column="student_upper"/>
        <result property="learningTimeStart" column="learning_time_start"/>
        <result property="learningStatus" column="learning_status"/>
        <result property="status" column="status"/>
        <result property="classStartTime" column="class_start_time"/>
        <result property="classEndTime" column="class_end_time"/>
        <result property="examineId" column="examine_id"/>
        <result property="areasId" column="areas_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="sysUserId" column="sys_user_id"/>
        <result property="interfacePushId" column="interface_push_id"/>
        <result property="remark" column="remark"/>
        <result property="interfaceAccountId" column="interface_account_id"/>
        <result property="interfacePeriodId" column="interface_period_id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.grade.vo.ClassGradeVo" id="ClassGradeVoResult">
        <result property="gradeId" column="grade_id"/>
        <result property="classStatus" column="class_status"/>
        <result property="officialName" column="official_name"/>
        <result property="className" column="class_name"/>
        <result property="studentUpper" column="student_upper"/>
        <result property="learningTimeStart" column="learning_time_start"/>
        <result property="learningStatus" column="learning_status"/>
        <result property="status" column="status"/>
        <result property="classStartTime" column="class_start_time"/>
        <result property="classEndTime" column="class_end_time"/>
        <result property="examineId" column="examine_id"/>
        <result property="areasId" column="areas_id"/>
        <result property="sysUserId" column="sys_user_id"/>
        <result property="interfacePushId" column="interface_push_id"/>
        <result property="remark" column="remark"/>
        <result property="interfaceAccountId" column="interface_account_id"/>
        <result property="interfacePeriodId" column="interface_period_id"/>
        <result property="interfaceAccountName" column="interface_account_name"/>
        <result property="interfacePeriodName" column="interface_period_name"/>
        <result property="interfacePushName" column="interface_push_name"/>
        <result property="studentNum" column="student_num"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.grade.vo.ClassGradeUserListVo" id="ClassGradeUserListVo">
        <result property="gradeId" column="grade_id"/>
        <result property="classStatus" column="class_status"/>
        <result property="officialName" column="official_name"/>
        <result property="className" column="class_name"/>
        <result property="studentUpper" column="student_upper"/>
        <result property="learningTimeStart" column="learning_time_start"/>
        <result property="learningStatus" column="learning_status"/>
        <result property="status" column="status"/>
        <result property="classStartTime" column="class_start_time"/>
        <result property="classEndTime" column="class_end_time"/>
        <result property="examineId" column="examine_id"/>
        <result property="areasId" column="areas_id"/>
        <result property="sysUserId" column="sys_user_id"/>
        <result property="interfacePushId" column="interface_push_id"/>
        <result property="remark" column="remark"/>
        <result property="interfaceAccountId" column="interface_account_id"/>
        <result property="interfacePeriodId" column="interface_period_id"/>
        <result property="interfaceAccountName" column="interface_account_name"/>
        <result property="interfacePeriodName" column="interface_period_name"/>
        <result property="interfacePushName" column="interface_push_name"/>
        <result property="studentNum" column="student_num"/>
    </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="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"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.grade.vo.ClassGradeStudentVo" id="ClassGradeStudentVo">
        <result property="userId" column="user_id"/>
        <result property="id" column="id"/>
        <result property="gradeId" column="grade_id"/>
        <result property="studentCode" column="student_code"/>
        <result property="realName" column="realname"/>
        <result property="idCard" column="id_card"/>
        <result property="telPhone" column="telphone"/>
        <result property="userStatus" column="user_status"/>
        <result property="status" column="status"/>
        <result property="classHours" column="class_hours"/>
        <result property="periodStatus" column="period_status"/>
        <result property="finishStatus" column="finish_status"/>
        <result property="officialStatus" column="official_status"/>
        <result property="learnStatus" column="learn_status"/>
        <result property="secAllNum" column="sec_all_num"/>
        <result property="examNum" column="exam_num"/>
        <result property="recordNum" column="record_num"/>
        <result property="classStartTime" column="class_start_time"/>
        <result property="classEndTime" column="class_end_time"/>
        <result property="studyStartTime" column="study_start_time"/>
        <result property="studyEndTime" column="study_end_time"/>
    </resultMap>



    <select id="queryList" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeQueryBo" resultMap="ClassGradeVoResult">
        SELECT
            g.*,
            (SELECT name FROM class_grade_interface i where g.interface_push_id = i.id) as interface_push_name,
            (SELECT name FROM class_grade_interface i where g.interface_account_id = i.id) as interface_account_name,
            (SELECT name FROM class_grade_interface i where g.interface_period_id = i.id) as interface_period_name,
            (SELECT nick_name FROM sys_user u where g.sys_user_id = u.user_id) as nick_name,
            (SELECT area_name FROM apply_areas a where a.area_id = g.areas_id) as area_name,
            (SELECT area_name FROM apply_areas a where a.area_id = g.city_id) as city_name,
            (SELECT COUNT(a.id) FROM class_grade_user a where a.grade_id = g.grade_id and a.status =1) as student_num
        FROM
            class_grade g
        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="gradeId != null and gradeId !=0 ">
            AND g.grade_id =  #{gradeId}
        </if>
        <if test="className != null and className !='' ">
            AND g.class_name like concat('%', #{className}, '%')
        </if>
        <if test="classStartTime != null or classEndTime != null ">
            AND (#{classStartTime} BETWEEN g.class_start_time and g.class_end_time or #{classEndTime} BETWEEN g.class_start_time and g.class_end_time)
        </if>
        <if test="educationTypeId != null and educationTypeId !='' ">
            AND (
            SELECT
            COUNT(*)
            FROM
            class_grade_goods s
            LEFT JOIN goods d ON s.goods_id = d.goods_id
            WHERE
            1 = 1
            AND g.grade_id = s.grade_id
            AND d.education_type_id =#{educationTypeId}
            ) >0
        </if>
        <if test="businessId != null and businessId !='' ">
            AND (
            SELECT
            COUNT(*)
            FROM
            class_grade_goods s
            LEFT JOIN goods d ON s.goods_id = d.goods_id
            WHERE
            1 = 1
            AND g.grade_id = s.grade_id
            AND d.business_id =#{businessId}
            ) >0
        </if>
        <if test="schoolId != null and schoolId !='' ">
            AND (
            SELECT
            COUNT(*)
            FROM
            class_grade_goods s
            LEFT JOIN goods d ON s.goods_id = d.goods_id
            WHERE
            1 = 1
            AND g.grade_id = s.grade_id
            AND d.school_id =#{schoolId}
            ) >0
        </if>
        <if test="majorId != null and majorId !='' ">
            AND (
            SELECT
            COUNT(*)
            FROM
            class_grade_goods s
            LEFT JOIN goods d ON s.goods_id = d.goods_id
            WHERE
            1 = 1
            AND g.grade_id = s.grade_id
            AND d.major_id =#{majorId}
            ) >0
        </if>
        <if test="goodsId != null and goodsId !='' ">
            AND (
            SELECT
            COUNT(*)
            FROM
            class_grade_goods s
            WHERE
            1 = 1
            AND g.grade_id = s.grade_id
            AND s.goods_id =#{goodsId}
            ) >0
        </if>
        order by g.update_time desc
    </select>

    <select id="queryGoodsList" 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
        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 class_grade_goods o ON o.goods_id = g.goods_id
        WHERE
            1 = 1
          AND o.grade_id =#{gradeId}
    </select>

    <select id="listGrade"  parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo" resultMap="ClassGradeStudentVo">
        SELECT
        cgu.id,
        u.student_code,
        u.user_id,
        cgu.`status`,
        u.realname,
        (SELECT cgg.goods_id FROM class_grade_goods cgg where cg.grade_id=cgg.grade_id) as goods_id,
        u.id_card,
        cgu.grade_id,
        u.telphone,
        (case WHEN (select COUNT(uu.id) FROM user_update uu where cgu.user_id = uu.user_id) >0 then 1 ELSE 0 end) as user_status,
        (SELECT g.class_hours FROM goods g LEFT JOIN class_grade_goods cgg on cgg.goods_id = g.goods_id where cg.grade_id = cgg.grade_id) as class_hours,
        cgu.period_status,
        cgu.finish_status,
        (SELECT g.study_start_time FROM goods g LEFT JOIN class_grade_goods cgg on cgg.goods_id = g.goods_id where cg.grade_id = cgg.grade_id) as study_start_time,
        (SELECT g.study_end_time FROM goods g LEFT JOIN class_grade_goods cgg on cgg.goods_id = g.goods_id where cg.grade_id = cgg.grade_id) as study_end_time,
        cg.class_start_time,
        cg.class_end_time,
        cgu.official_status,
        cgu.learn_status,
        (SELECT COUNT(m.id) FROM course_menu_exam m LEFT JOIN goods_course c on m.course_id=c.course_id LEFT JOIN class_grade_goods cgg on cgg.goods_id = c.goods_id where cg.grade_id=cgg.grade_id and m.type = 1 ) as exam_num,
        (SELECT COUNT(DISTINCT ubr.record_id) FROM user_bank_record ubr LEFT JOIN class_grade_goods cgg on cgg.goods_id = ubr.goods_id where ubr.`status`=1) as record_num
        FROM
        class_grade_user cgu
        LEFT JOIN class_grade cg ON cgu.grade_id = cg.grade_id
        LEFT JOIN `user` u ON u.user_id = cgu.user_id
        where 1=1
        and cgu.`status` =1
        <if test="gradeId != null and gradeId !='' ">
          and cgu.grade_id = #{gradeId}
        </if>
        order by cgu.create_time desc
    </select>

    <select id="listGradeInform" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeQueryBo" resultMap="ClassGradeUserListVo">
        SELECT
        g.*,
        (SELECT name FROM class_grade_interface i where g.interface_push_id = i.id) as interface_push_name,
        (SELECT name FROM class_grade_interface i where g.interface_account_id = i.id) as interface_account_name,
        (SELECT name FROM class_grade_interface i where g.interface_period_id = i.id) as interface_period_name,
        (SELECT nick_name FROM sys_user u where g.sys_user_id = u.user_id) as nick_name,
        (SELECT area_name FROM apply_areas a where a.area_id = g.areas_id) as area_name,
        (SELECT area_name FROM apply_areas a where a.area_id = g.city_id) as city_name,
        (SELECT COUNT(a.id) FROM class_grade_user a where a.grade_id = g.grade_id and a.status =1) as student_num
        FROM
        class_grade g
        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="gradeId != null and gradeId !=0 ">
            AND g.grade_id =  #{gradeId}
        </if>
        <if test="className != null and className !='' ">
            AND g.class_name like concat('%', #{className}, '%')
        </if>
        <if test="classStartTime != null and classStartTime !=0 ">
            AND #{classStartTime} BETWEEN g.class_start_time and g.class_end_time or #{classEndTime} BETWEEN g.class_start_time and g.class_end_time
        </if>
        <if test="educationTypeId != null and educationTypeId !='' ">
            AND (
            SELECT
            COUNT(*)
            FROM
            class_grade_goods s
            LEFT JOIN goods d ON s.goods_id = d.goods_id
            WHERE
            1 = 1
            AND g.grade_id = s.grade_id
            AND d.education_type_id =#{educationTypeId}
            ) >0
        </if>
        <if test="businessId != null and businessId !='' ">
            AND (
            SELECT
            COUNT(*)
            FROM
            class_grade_goods s
            LEFT JOIN goods d ON s.goods_id = d.goods_id
            WHERE
            1 = 1
            AND g.grade_id = s.grade_id
            AND d.business_id =#{businessId}
            ) >0
        </if>
        <if test="schoolId != null and schoolId !='' ">
            AND (
            SELECT
            COUNT(*)
            FROM
            class_grade_goods s
            LEFT JOIN goods d ON s.goods_id = d.goods_id
            WHERE
            1 = 1
            AND g.grade_id = s.grade_id
            AND d.school_id =#{schoolId}
            ) >0
        </if>
        <if test="majorId != null and majorId !='' ">
            AND (
            SELECT
            COUNT(*)
            FROM
            class_grade_goods s
            LEFT JOIN goods d ON s.goods_id = d.goods_id
            WHERE
            1 = 1
            AND g.grade_id = s.grade_id
            AND d.major_id =#{majorId}
            ) >0
        </if>
        <if test="goodsId != null and goodsId !='' ">
            AND (
            SELECT
            COUNT(*)
            FROM
            class_grade_goods s
            WHERE
            1 = 1
            AND g.grade_id = s.grade_id
            AND s.goods_id =#{goodsId}
            ) >0
        </if>
        order by g.update_time desc
    </select>

    <select id="listGradeStudy"  parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo" resultMap="ClassGradeStudentVo">
        SELECT
            u.student_code,
            u.user_id,
            u.realname,
            u.id_card
        FROM
            `user` u
        WHERE
            1 = 1
                and u.realname is NOT NULL
          and u.user_id not in (SELECT DISTINCT cgu.user_id from class_grade_user cgu )
        <if test="status != null and status.size()!=0 ">
            AND u.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>
    <select id="listGradeService"  parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo" resultMap="ClassGradeStudentVo">
        SELECT
            DISTINCT u.*
        FROM
        `user` u
        LEFT JOIN `order` o ON u.user_id = o.user_id
        LEFT JOIN order_goods og ON o.order_sn = og.order_sn
        LEFT JOIN goods g on g.goods_id=og.goods_id
        WHERE
        1 = 1
        AND og.status=1
        and u.status=1
        and og.refund_status=0
        and pay_status in (2,3)
        and g.business_id =#{businessId}
    </select>
</mapper>
