<?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.xunmei.core.edu.mapper.CoreEduTrainingTaskMapper">
    <resultMap type="com.xunmei.common.core.domain.edu.domain.CoreEduTrainingTask" id="CoreEduTrainingTaskResult">
        <result property="id" column="id"/>
        <result property="planCycle" column="plan_cycle"/>
        <result property="planId" column="plan_id"/>
        <result property="orgId" column="org_id"/>
        <result property="orgName" column="org_name"/>
        <result property="orgPath" column="org_path"/>
        <result property="title" column="title"/>
        <result property="type" column="type"/>
        <result property="status" column="status"/>
        <result property="content" column="content"/>
        <result property="note" column="note"/>
        <result property="hostId" column="host_id"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="trainingStartDateTime" column="training_start_date_time"/>
        <result property="trainingEndDateTime" column="training_end_date_time"/>
        <result property="fileList" column="file_list"/>
        <result property="imageList" column="image_list"/>
        <result property="pdfUrl" column="pdf_url"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectCoreEduTrainingTaskVo">
        select id,
               plan_cycle,
               plan_id,
               org_id,
               org_name,
               org_path,
               title,
               type,
               status,
               content,
               note,
               host_id,
               start_date,
               end_date,
               training_start_date_time,
               training_end_date_time,
               file_list,
               image_list,
               pdf_url,
               create_by,
               create_time,
               update_by,
               update_time
        from core_edu_training_task
    </sql>

    <select id="checkHasTaskIsDone" resultType="java.lang.Integer">
        select 1 from core_edu_training_task where status !=0 and plan_id in
        <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
        limit 1
    </select>

    <select id="selectPageData" resultType="com.xunmei.common.core.domain.edu.vo.CoreEduTrainingTaskPageVo"
            parameterType="com.xunmei.common.core.domain.edu.dto.CoreEduTrainingTaskPageDto">
        <!--        <include refid="selectCoreEduTrainingTaskVo"/>-->
        <!--        <where>-->
        <!--            <if test="request.title != null  and request.title != ''">-->
        <!--                and title like concat('%', #{request.title}, '%')-->
        <!--            </if>-->
        <!--            <if test="request.type != null ">-->
        <!--                and type = #{request.type}-->
        <!--            </if>-->
        <!--            <if test="request.status != null ">-->
        <!--                and status = #{request.status}-->
        <!--            </if>-->
        <!--            <if test="request.range != null ">-->
        <!--                and start_date >= #{request.range[0]}-->
        <!--                and end_date <![CDATA[ <= ]]> #{request.range[1]}-->
        <!--            </if>-->
        <!--        </where>-->
        select t.id, plan_id, t.org_id, t.org_name, title, type, status, host_id,u.name as hostName,
        start_date, end_date, training_start_date_time,training_end_date_time
        from core_edu_training_task t left join sys_user u on t.host_id=u.id
        <where>
            <choose>
                <when test="request.checkSub==true">
                    t.org_path like concat(#{request.orgPath}, '%')
                </when>
                <otherwise>
                    t.org_id = #{request.orgId}
                </otherwise>
            </choose>
            <if test="request.title!= null and request.title !=''">
                and t.title like concat('%',#{request.title}, '%')
            </if>
            <if test="request.type!= null">
                and t.type=#{request.type}
            </if>
            <if test="request.status != null">
                and t.status=#{request.status}
            </if>
            <if test="request.startTime != null">
                and t.date_time >=#{request.startTime}
            </if>
            <if test="request.endTime != null">
                and t.date_time <![CDATA[<=]]> #{request.endTime}
            </if>
        </where>
    </select>

    <select id="selectEduTrainingTaskDetail"
            resultType="com.xunmei.common.core.domain.edu.vo.CoreEduTrainingTaskDetailVo">
        select t.id,
               plan_id,
               t.org_id,
               t.org_name,
               title,
               type,
               start_date,
               end_date,
               training_start_date_time,
               training_end_date_time,
               t.host_id,
               t.recorder_id,
               u.name  as hostName,
               u2.name as recorderName
        from core_edu_training_task t
                 left join sys_user u on u.id = t.host_id
                 left join sys_user u2 on u2.id = t.recorder_id

        where t.id = #{id}
    </select>

    <select id="selectReport" resultType="com.xunmei.common.core.domain.edu.vo.CoreEduTrainingTaskReportVo">
        SELECT t.org_id AS orgId,
        t.org_name AS orgName,
        ifnull(count(0),0) AS shouldFinish,
        ifnull(SUM(t.status != 3),0) AS finish
        from core_edu_training_task t inner join sys_org o on t.org_id = o.id
        <where>
            <if test="request.orgId != null">
                and o.parent_id=#{request.orgId}
            </if>
            <if test="request.planId != null">
                and t.plan_id=#{request.planId}
            </if>
            <if test="request.startDate != null">
                and t.start_date>=#{request.startDate}
            </if>
            <if test="request.endDate != null">
                and t.start_date <![CDATA[<=]]> #{request.endDate}
            </if>
        </where>
    </select>
</mapper>