<?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.testor.module.train.myManagement.dao.TTrainMyManagementDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.train.myManagement.model.domain.TTrainMyManagement">
        <id column="id" property="id"/>
        <id column="parent_id" property="parentId"/>
        <id column="person_id" property="personId"/>
        <id column="task_id" property="taskId"/>
        <id column="task_type" property="taskType"/>
        <id column="person_affiliation" property="personAffiliation"/>
        <id column="org_name" property="orgName"/>
        <id column="dept_name" property="deptName"/>
        <id column="person_name_id" property="personNameId"/>
        <id column="person_name" property="personName"/>
        <id column="sex" property="sex"/>
        <id column="percentage" property="percentage"/>
        <id column="tenant_id" property="tenantId"/>
        <id column="process_id" property="processId"/>
        <id column="process_status" property="processStatus"/>
    </resultMap>

    <select id="staffTrainCase" parameterType="com.testor.module.train.myManagement.model.dto.TTrainMyManagementParam"
            resultType="com.testor.module.train.myManagement.model.dto.TTrainMyManagementParam">
        select *
        from (select p.project_name
--         , p.visit_start_time
--         , p.visit_end_time
        , p.org_name
        , my.*
        from t_train_project p
        inner join t_train_my_management my
        on p.id = my.task_id
        where p.status = 0
        <if test="param.personName!=null and param.personName!=''">
            AND my.person_name LIKE CONCAT('%',#{param.personName},'%')
        </if>

        <if test="param.visitStartTime!=null and param.visitEndTime!=null">
            AND (p.visit_start_time >=#{param.visitStartTime}  and   #{param.visitEndTime} >=p.visit_end_time)
        </if>

        ) mm
        where mm.org_id in (select org.org_id
        from t_sys_org org
        where find_in_set(org.org_id, #{parentIds})
        or org.parent_ids like concat('%', #{orgId}, '%')
        or org.org_id = #{orgId})
    </select>

    <select id="getCourseDetail" parameterType="com.testor.module.train.course.model.dto.TTrainCourseParam"
            resultType="com.testor.module.train.course.model.dto.TTrainCourseParam">
        select ttl.course_id ,
        from t_train_course ttc
        join t_train_course_learn_statistics ttl
        on ttc.id = ttl.course_id
        where ttl.task_id = #{taskId} and ttl.person_id = #{userId} and ttl.course_id = #{id}

    </select>
    <select id="findStateCourseByUserId"
            resultType="com.testor.module.train.myManagement.model.dto.TrainMyManagementStatVo">
        SELECT
            m.id,
            m.person_id,
            u.login_name,
            m.tab_status,
            m.visit_start_time,
            m.visit_end_time,
            m.test_score,
            p.course_id
        FROM
            t_train_my_management m
                LEFT JOIN t_sys_user u on u.user_id = m.person_id
                LEFT JOIN t_train_project p ON m.task_id = p.id
        WHERE
            tab_status = 1
          AND p.status = 0
          AND u.login_name = #{loginName}


    </select>
    <select id="findStateExamByUserId"  resultType="com.testor.module.train.project.model.stat.TrainProjectUserStat" databaseId="mysql">
        SELECT
            COUNT( m.id ) AS examNum,
            ROUND(AVG(m.test_score), 1) AS examAvgScore
        FROM
            t_train_my_management m
                LEFT JOIN t_sys_user u ON u.user_id = m.person_id
        WHERE
            m.tab_status = 2
          AND m.is_add_exam = 1
          AND u.login_name =  #{loginName}
    </select>
    <select id="findStateExamByUserId"  resultType="com.testor.module.train.project.model.stat.TrainProjectUserStat" databaseId="kingbase">
        SELECT
            COUNT( m.id ) AS examNum,
            ROUND(AVG(CAST(m.test_score AS FLOAT)), 1) AS examAvgScore
        FROM
            t_train_my_management m
                LEFT JOIN t_sys_user u ON u.user_id = m.person_id
        WHERE
            m.tab_status = 2
          AND m.is_add_exam = 1
          AND u.login_name =  #{loginName}
    </select>



    <select id="findTrainListState"
            resultType="com.testor.module.train.project.model.stat.TrainProjectUserDetailStat">
        SELECT
            m.id,
            m.task_name,
            m.learn_way,
            m.person_id,
            se.user_type_code,
            o.org_name,
            m.org_id,
            m.tab_status,
            m.visit_start_time,
            m.visit_end_time,
            m.test_score,
            m.exam_duration,
            p.exam_site
        FROM
            t_train_my_management m
                LEFT JOIN t_train_project p ON p.id = m.task_id
                LEFT JOIN t_sys_org o ON m.org_id = o.org_id
                LEFT JOIN t_sys_user_extend se ON m.person_id = se.user_id
        WHERE
        (  m.STATUS != '1'  AND m.person_id = #{userId} AND m.tab_status = #{tabStatus} )
        ORDER BY
        m.update_date DESC,
        m.create_date DESC
    </select>
</mapper>