<?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.ruoyi.mxjb.mapper.MxjbStudyMapper">

    <resultMap type="MxjbStudyStat" id="MxjbStudyStatResult">
        <result property="time"    column="time"    />
        <result property="id"    column="id"    />
        <result property="count"    column="cnt"    />
        <result property="cardNo"    column="card_no"    />
        <result property="account"    column="account"    />
        <result property="title"    column="title"    />
        <result property="school"    column="school"    />
        <result property="gradeName"    column="grade_name"    />
        <result property="className"    column="class_name"    />
        <result property="videoCount"    column="video_count"    />
        <result property="videoAvgTime"    column="video_avg_time"    />
        <result property="microClassCount"    column="micro_class_count"    />
        <result property="microClassTime"    column="micro_class_time"    />
        <result property="questionCount"    column="question_count"    />
        <result property="questionTime"    column="question_time"    />
        <result property="scoreRate"    column="scoreRate"    />
        <result property="workCount"    column="work_count"    />
        <result property="workRate"    column="work_rate"    />
    </resultMap>


    <select id="selectStudyLoginStatByStudent" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT max(l.login_time) `time`, sum(IF(l.msg = '登录成功', 1, 0)) cnt
        FROM sys_user u
        JOIN sys_logininfor l ON u.user_name = SUBSTRING_INDEX(l.user_name, ':', -1)
        AND l.msg IN ('退出成功', '登录成功') AND l.status = 0
        <where>
            <if test="userId != null "> and u.`user_id` = #{userId}</if>
            <if test="beginTime != null "> and l.login_time &gt;= #{beginTime}</if>
            <if test="endTime != null "> and l.login_time &lt; #{endTime}</if>
        </where>
        ORDER BY l.login_time DESC
    </select>

    <select id="selectStudyRegisters" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT cc.`schoolId` id, s.`name` school, COUNT(*) cnt
          FROM `b_customer_class` cc
          JOIN `b_busi_schools` s ON cc.`schoolId` = s.`id`
        <where>  cc.`type` = 3
            <if test="schoolId != null "> and cc.schoolId = #{schoolId}</if>
            <if test="gradeId != null "> and cc.gradeId = #{gradeId}</if>
            <if test="classId != null ">and cc.classId in <foreach item="s" collection="classId" open="(" separator="," close=")">#{s}</foreach></if>
        </where>
         GROUP BY s.`id`, s.`name`
    </select>

    <select id="selectStudyVideoByStudent" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT s.`name` school, bc.`name` class_name, cb.`name` account, cvw.`time`, cvw.`title`, cvw.`course` card_no, cvw.`time`, ROUND(IF(cvw.duration * cvw.percent &lt; 6000, 6000, cvw.duration * cvw.percent) / 100) video_avg_time
          FROM `b_customer_class` cc
          JOIN b_customer_video_watches cvw ON cvw.customerCode = cc.`customerCode`
          JOIN `b_busi_schools` s ON cc.`schoolId` = s.`id`
          JOIN `b_customer_basic` cb ON cb.`code` = cc.`customerCode`
          JOIN `b_busi_classes` bc ON bc.`id` = cc.`classId`
        <where> cc.`type` = 3
            <if test="schoolId != null "> and cc.`schoolId` = #{schoolId}</if>
            <if test="customerCode != null "> and cc.`customerCode` = #{customerCode}</if>
            <if test="gradeId != null "> and cc.gradeId = #{gradeId}</if>
            <if test="classId != null "> and cc.classId in <foreach item="s" collection="classId" open="(" separator="," close=")">#{s}</foreach></if>
            <if test="types != null "> and cvw.type IN <foreach item="v" collection="types" open="(" separator="," close=")">#{v}</foreach></if>
            <if test="beginTime != null "> and cvw.time &gt;= #{beginTime}</if>
            <if test="endTime != null "> and cvw.time &lt; #{endTime}</if>
        </where>
    </select>
    <select id="selectSchoolQuestionByStudent" parameterType="map"  resultMap="MxjbStudyStatResult">
      SELECT s.`name` school, bc.`name` class_name, cb.`name` account, ab.`courseName` card_no, SUM(ab.rights) video_count, SUM(ab.wrongs) question_count
        FROM `b_customer_class` cc
        JOIN mxjb_customer_answer_batches ab ON ab.customerCode = cc.`customerCode`
        JOIN `b_busi_schools` s ON cc.`schoolId` = s.`id`
        JOIN `b_customer_basic` cb ON cb.`code` = cc.`customerCode`
        JOIN `b_busi_classes` bc ON bc.`id` = cc.`classId`
        <where> cc.`type` = 3
            <if test="schoolId != null "> and cc.`schoolId` = #{schoolId}</if>
            <if test="customerCode != null "> and cc.`customerCode` = #{customerCode}</if>
            <if test="gradeId != null "> and cc.gradeId = #{gradeId}</if>
            <if test="classId != null "> and cc.classId in <foreach item="s" collection="classId" open="(" separator="," close=")">#{s}</foreach></if>
            <if test="beginTime != null "> and ab.day &gt;= #{beginTime}</if>
            <if test="endTime != null "> and ab.day &lt; #{endTime}</if>
        </where>
        GROUP BY s.`name`, cb.`name`, bc.`name`, ab.`courseName`
    </select>

    <select id="selectStudyLogins" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT l.login_time `time`, u.`user_name` account, IF(l.msg = '登录成功', 1, 0) cnt
          FROM sys_user u
        JOIN sys_logininfor l ON u.user_name = SUBSTRING_INDEX(l.user_name, ':', -1)
        AND l.msg IN ('退出成功', '登录成功') AND l.status = 0
        <where>
            <if test="userId != null "> and u.`user_id` = #{userId}</if>
            <if test="beginTime != null "> and l.login_time &gt;= #{beginTime}</if>
            <if test="endTime != null "> and l.login_time &lt; #{endTime}</if>
        </where>
        ORDER BY l.login_time DESC
    </select>

    <select id="selectStudyVideos" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT cc.`schoolId` id, COUNT(*) video_count, ROUND(SUM(IF(cvw.duration * cvw.percent &lt; 6000, 6000, cvw.duration * cvw.percent)) / 100 ) video_avg_time, COUNT(DISTINCT cc.customerCode) cnt
          FROM b_customer_class cc
          JOIN b_customer_video_watches cvw ON cvw.customerCode = cc.customerCode
        <where>  cc.type = 3
            <if test="schoolId != null "> and cc.schoolId = #{schoolId}</if>
            <if test="gradeId != null "> and cc.gradeId = #{gradeId}</if>
            <if test="classId != null ">and cc.classId in <foreach item="s" collection="classId" open="(" separator="," close=")">#{s}</foreach></if>
            <if test="types != null "> and cvw.type IN <foreach item="v" collection="types" open="(" separator="," close=")">#{v}</foreach></if>
            <if test="course != null "> and cvw.course = #{course}</if>
            <if test="beginTime != null "> and cvw.time &gt;= #{beginTime}</if>
            <if test="endTime != null "> and cvw.time &lt;= #{endTime}</if>
        </where>
        GROUP BY cc.`schoolId`
    </select>

    <select id="selectStudyQuestions" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT cc.`schoolId` id, SUM(ab.rights) video_count, SUM(ab.wrongs) question_count, SUM(ab.seconds) question_time, COUNT(DISTINCT cc.customerCode) cnt
        FROM b_customer_class cc
        JOIN mxjb_customer_answer_batches ab ON ab.customerCode = cc.customerCode
        <where>  cc.type = 3
            <if test="schoolId != null "> and cc.schoolId = #{schoolId}</if>
            <if test="gradeId != null "> and cc.gradeId = #{gradeId}</if>
            <if test="classId != null ">and cc.classId in <foreach item="s" collection="classId" open="(" separator="," close=")">#{s}</foreach></if>
            <if test="course != null "> and ab.courseName = #{course}</if>
            <if test="beginTime != null "> and ab.day &gt;= #{beginTime}</if>
            <if test="endTime != null "> and ab.day &lt; #{endTime}</if>
        </where>
        GROUP BY cc.`schoolId`
    </select>

    <select id="selectStudyVideoByCourse" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT cvw.course account, COUNT(DISTINCT cc.customerCode) cnt, COUNT(*) video_count, ROUND(SUM(IF(cvw.duration * cvw.percent &lt; 6000, 6000, cvw.duration * cvw.percent)) / 100) video_avg_time
        FROM b_customer_class cc
        JOIN b_customer_video_watches cvw ON cvw.customerCode = cc.customerCode
        <where>  cc.type = 3
            <if test="schoolId != null "> and cc.schoolId = #{schoolId}</if>
            <if test="gradeId != null "> and cc.gradeId = #{gradeId}</if>
            <if test="classId != null ">and cc.classId in <foreach item="s" collection="classId" open="(" separator="," close=")">#{s}</foreach></if>
            <if test="types != null "> and cvw.type IN <foreach item="v" collection="types" open="(" separator="," close=")">#{v}</foreach></if>
            <if test="course != null "> and cvw.course = #{course}</if>
            <if test="beginTime != null "> and cvw.time &gt;= #{beginTime}</if>
            <if test="endTime != null "> and cvw.time &lt; #{endTime}</if>
        </where>
        GROUP BY cvw.course
    </select>

    <select id="selectStudyQuestionByCourse" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT ab.courseName account, COUNT(DISTINCT cc.customerCode) cnt, SUM(ab.rights) video_count, SUM(ab.wrongs) question_count, SUM(ab.seconds) question_time
          FROM b_customer_class cc
          JOIN mxjb_customer_answer_batches ab ON ab.customerCode = cc.customerCode
        <where>  cc.type = 3
            <if test="schoolId != null "> and cc.schoolId = #{schoolId}</if>
            <if test="gradeId != null "> and cc.gradeId = #{gradeId}</if>
            <if test="classId != null ">and cc.classId in <foreach item="s" collection="classId" open="(" separator="," close=")">#{s}</foreach></if>
            <if test="course != null "> and ab.courseName = #{course}</if>
            <if test="beginTime != null "> and ab.day &gt;= #{beginTime}</if>
            <if test="endTime != null "> and ab.day &lt; #{endTime}</if>
        </where>
         GROUP BY ab.courseName
    </select>

    <select id="selectStudyVideoDetails" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT cvw.time, cvw.course class_name, cvw.title card_no, ROUND(IF(cvw.duration * cvw.percent &lt; 6000, 6000, cvw.duration * cvw.percent) / 100) video_avg_time
        FROM b_customer_video_watches cvw
        <where> cvw.customerCode = #{customerCode}
            <if test="types != null "> and cvw.type IN <foreach item="v" collection="types" open="(" separator="," close=")">#{v}</foreach></if>
            <if test="beginTime != null "> and cvw.time &gt;= #{beginTime}</if>
            <if test="endTime != null "> and cvw.time &lt; #{endTime}</if>
        </where>
    </select>
    <select id="selectStudyQuestionDetails" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT ab.day `time`, ab.courseName class_name, a.questionId account, q.title card_no, ab.source school, a.seconds question_time, IF(a.answer = a.correct, 1, 0) scoreRate
        FROM mxjb_customer_answers a
        JOIN mxjb_customer_answer_batches ab ON a.customerCode = ab.customerCode AND a.batchNo = ab.batchNo
        JOIN questions q ON q.id = a.questionId
        <where> a.customerCode = #{customerCode}
            <if test="beginTime != null "> and ab.day &gt;= #{beginTime}</if>
            <if test="endTime != null "> and ab.day &lt; #{endTime}</if>
        </where>
    </select>

    <select id="selectStudyVideoTotal" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT count(*) cnt, ROUND(sum(IF(cvw.duration * cvw.percent &lt; 6000, 6000, cvw.duration * cvw.percent)) / 100) video_avg_time
        FROM b_customer_video_watches cvw
        <where> cvw.customerCode = #{customerCode}
            <if test="types != null "> and cvw.type IN <foreach item="v" collection="types" open="(" separator="," close=")">#{v}</foreach></if>
            <if test="beginTime != null "> and cvw.time &gt;= #{beginTime}</if>
            <if test="endTime != null "> and cvw.time &lt; #{endTime}</if>
        </where>
    </select>
    <select id="selectStudyQuestionTotal" parameterType="map"  resultMap="MxjbStudyStatResult">
        SELECT COUNT(DISTINCT e.`examinee_id`) cnt, SUM(a.`duration`)
          FROM `mxjb_paper_examinee` e
          JOIN `mxjb_paper_answers` a ON a.`examinee_id` = e.`examinee_id`
        <where> e.`state` > 3 and e.`customer_code` = #{customerCode}
            <if test="beginTime != null "> and e.`end_time` &gt;= #{beginTime}</if>
            <if test="endTime != null "> and e.`begin_time` &lt;= #{endTime}</if>
        </where>
    </select>

    <select id="selectMxjbStudyStatList" parameterType="MxjbStudyStatCond" resultMap="MxjbStudyStatResult">
        SELECT  cc.customerCode id, c.cardNo card_no, CONCAT(cb.name, '_' , cb.username) account, bs.name school, g.gradeName grade_name, bc.name class_name
               , t1.video_count, t1.video_avg_time, t5.micro_class_count, t5.micro_class_time, t2.question_count, t2.scoreRate, t3.work_count, t4.work_rate
          FROM b_busi_schools bs
          JOIN b_busi_classes bc ON bc.schoolId = bs.id
          JOIN b_customer_class cc ON cc.classId = bc.id
          JOIN b_busi_cards c ON c.customerCode = cc.customerCode
          JOIN b_customer_basic cb ON cb.code = cc.customerCode
          JOIN grade g ON g.gradeId = cc.gradeId
        LEFT JOIN ( SELECT cvw.customerCode, COUNT(section_id) video_count, ROUND(sum(IF(cvw.duration is null, 0, IF(cvw.duration * cvw.percent &lt; 6000, 6000, cvw.duration * cvw.percent))) / 100 / 60) video_avg_time FROM b_customer_video_watches cvw
            WHERE cvw.type IN (1, 12)
            <if test="begin != null "> and cvw.time &gt;= #{begin}</if>
            <if test="end != null "> and cvw.time &lt;= #{end}</if>
            GROUP BY cvw.customerCode ) t1 ON t1.customerCode = cc.customerCode
        LEFT JOIN ( SELECT cvw.customerCode, COUNT(section_id) micro_class_count, ROUND(sum(IF(cvw.duration is null, 0, IF(cvw.duration * cvw.percent &lt; 6000, 6000, cvw.duration * cvw.percent))) / 100 / 60) micro_class_time FROM b_customer_video_watches cvw
            WHERE cvw.type IN (2, 3, 4, 5, 6, 7, 8, 9, 10)
            <if test="begin != null "> and cvw.time &gt;= #{begin}</if>
            <if test="end != null "> and cvw.time &lt;= #{end}</if>
            GROUP BY cvw.customerCode ) t5 ON t5.customerCode = cc.customerCode
        LEFT JOIN ( SELECT ab.customerCode, IFNULL(SUM(rights+wrongs),0) question_count, ROUND(SUM(rate)/COUNT(1)) scoreRate FROM mxjb_customer_answer_batches ab where 1 = 1
            <if test="begin != null "> and ab.day &gt;= #{begin}</if>
            <if test="end != null "> and ab.day &lt;= #{end}</if>
            GROUP BY ab.customerCode ) t2 ON t2.customerCode = cc.customerCode
        LEFT JOIN ( SELECT ch.customerCode, COUNT(*) work_count FROM b_customer_homework ch WHERE ch.status > 0
            <if test="begin != null "> and ch.createTime &gt;= #{begin}</if>
            <if test="end != null "> and ch.createTime &lt;= #{end}</if>
            GROUP BY ch.customerCode ) t3 ON t3.customerCode = cc.customerCode
        LEFT JOIN ( SELECT chs.customerCode, ROUND(SUM(IF(chs.answer = chs.correct, 1, 0)) * 100 / COUNT(*)) work_rate FROM b_customer_homework_answers chs
            GROUP BY chs.customerCode ) t4 ON t4.customerCode = cc.customerCode
        <where>  cc.status > 0 and cc.type = 3 AND c.status > 0 AND (t1.customerCode IS NOT NULL OR t2.customerCode IS NOT NULL OR t3.customerCode IS NOT NULL OR t4.customerCode IS NOT NULL)
            <if test="schoolId != null "> and bs.id = #{schoolId}</if>
            <if test="cardNo != null "> and c.cardNo like concat('%', #{cardNo},'%')</if>
            <if test="gradeId != null "> and cb.greadId = #{gradeId}</if>
        </where>
        order by c.cardNo
    </select>
</mapper>
