<?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="cn.iocoder.yudao.module.knowledge.mapper.TTrainRecordMapper">

    <resultMap type="cn.iocoder.yudao.module.knowledge.entity.TTrainRecord" id="tTrainRecordMap">
            <result property="id" column="id"/>
            <result property="name" column="name"/>
            <result property="trainId" column="train_id"/>
            <result property="startTime" column="start_time"/>
            <result property="userId" column="user_id"/>
            <result property="paperInfoId" column="paperInfoId"/>
             <result property="userName" column="user_name"/>
    </resultMap>

    <resultMap type="cn.iocoder.yudao.module.knowledge.entity.TTrainRecord" id="tTrainRecordMapss">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="userName" column="user_name"/>
        <result property="holdUnit" column="hold_unit"/>
        <result property="handAddress" column="hand_address"/>
        <result property="startTime" column="start_time"/>
        <result property="content" column="content"/>
    </resultMap>

    <resultMap type="cn.iocoder.yudao.module.knowledge.entity.TTrainRecord" id="tTrainRecordMaps">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="trainId" column="train_id"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="userId" column="user_id"/>
        <result property="paperInfoId" column="paperInfoId"/>
        <result property="allScore" column="all_score"/>
        <result property="passScore" column="pass_score"/>
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="columns">
        id,name,train_id,start_time,user_id
    </sql>
    <select id="findList" resultMap="tTrainRecordMap">
        SELECT
        distinct ttr.id,
        ttr.train_id,
        ttr.start_time,
        ttr.NAME,
        ttr.user_id userId,
        ttpi.id paperInfoId,
        ttr.create_time,
        ttr.user_name
        FROM
        t_train_record ttr
        LEFT JOIN t_test_paper_info ttpi ON ttr.train_id = ttpi.train_id
        <where>
            ttr.deleted = 0
            <if test="params.name!=null and params.name!=''">
               and (ttr.name like concat('%',#{params.name},'%') or  ttr.user_name like concat('%',#{params.name},'%'))
            </if>
            <if test="params.userId!=null and params.userId!=''">
                and ttr.user_id = #{params.userId}
            </if>
            <if test="params.startTime!=null">
                and ttr.start_time like concat(#{params.startTime},'%')
            </if>
        </where>

        order by ttr.create_time desc
    </select>

    <select id="findListAll" resultMap="tTrainRecordMapss">
        SELECT
        tt.id,
        tt.name,
        ttr.user_name,
        tt.hold_unit,
        tt.hand_address,
        tt.start_time,
        ttr.content
        FROM
        t_train_record ttr
        left join  t_train tt on tt.id = ttr.train_id
        <where>
            ttr.deleted = 0 	AND ttr.paper_info_id IS NULL
            <if test="params.name!=null and params.name!=''">
                and ttr.name like concat('%',#{params.name},'%')
            </if>

            <if test="params.userName!=null and params.userName!=''">
                and ttr.username like concat('%',#{params.userName},'%')
            </if>
        </where>
        group by tt.id,ttr.user_id
        order by ttr.create_time desc
    </select>

<!--    <select id="findListOwn" resultMap="tTrainRecordMaps">-->
<!--        SELECT-->
<!--        distinct ttr.id,-->
<!--        ttpi.start_date,-->
<!--        ttpi.end_date,-->
<!--        ttpi.NAME,-->
<!--        ttr.user_id userId,-->
<!--        ttpi.id paperInfoId,-->
<!--        ttpi.all_score,-->
<!--        ttr.create_time-->
<!--        FROM-->
<!--        t_train_record ttr-->
<!--        LEFT JOIN t_test_paper_info ttpi ON ttr.paper_info_id = ttpi.id-->
<!--        <where>-->
<!--            ttr.deleted = 0-->
<!--            <if test="params.name!=null and params.name!=''">-->
<!--                and ttr.name like concat('%',#{params.name},'%')-->
<!--            </if>-->
<!--            <if test="params.userId!=null and params.userId!=''">-->
<!--                and ttr.user_id = #{params.userId}-->
<!--            </if>-->
<!--            <if test="params.startTime!=null">-->
<!--                and ttpi.start_date like concat(#{params.startTime},'%')-->
<!--            </if>-->
<!--        </where>-->

<!--        order by ttr.create_time desc-->
<!--    </select>-->


    <select id="findListOwn" resultMap="tTrainRecordMaps">
        select
        ttpi.id paperInfoId,
        ttpi.all_score,
        ttpi.start_date,
        ttpi.end_date,
        ttpi.NAME,
        ttpi.pass_score,
        ttu.usercode userId
        from t_train_user ttu
        LEFT JOIN t_test_paper_info ttpi ON ttu.paper_info_id = ttpi.id
        <where>
            ttpi.deleted = 0
            <if test="params.name!=null and params.name!=''">
                and ttpi.name like concat('%',#{params.name},'%')
            </if>
            <if test="params.userId!=null and params.userId!=''">
                and ttu.usercode = #{params.userId}
            </if>
            <if test="params.startTime!=null">
                and #{params.startTime} between ttpi.start_date and ttpi.end_date
            </if>
        </where>

        order by ttpi.create_time desc
    </select>

    <select id="selectByTrainId" resultMap="tTrainRecordMaps">
        select
        *
        from t_train_record
        where
            deleted = 0
            and train_id = #{trainId}
    </select>

</mapper>
