<?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="jnpf.cultivate.mapper.TeachingStudentMapper">


    <select id="queryTeachingDataList" resultType="jnpf.model.cultivate.vo.teaching.TeachingDataListVo">
        SELECT
        fcts.F_RecordId recordId,
        fctr.F_UserId teachingUserId,
        fctr.F_UserName teachingUsername,
        fcts.F_UserName username,
        fcts.F_PostName postName,
        fcts.F_UserId userId,
        fctss.F_Id skillId,
        fctr.F_StoreId storeId,
        SUM( 1 ) teachingCount
        FROM
        ftb_cultivate_teaching_stu fcts
        LEFT JOIN ftb_cultivate_teaching_record fctr on fctr.F_Id = fcts.F_RecordId
        LEFT JOIN ftb_cultivate_teaching_skill fctss on fctss.F_Id = fctr.F_SkillId

        <where>
            <if test="queryTeachingRecordDto.recordIds != null and queryTeachingRecordDto.recordIds.size > 0">
                and fcts.F_RecordId in
                <foreach collection="queryTeachingRecordDto.recordIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="queryTeachingRecordDto.keyword != null and queryTeachingRecordDto.keyword != ''">
                and fcts.F_Username like concat('%', #{queryTeachingRecordDto.keyword}, '%')
            </if>
        </where>
        GROUP BY CONCAT(fcts.F_UserId, fctr.F_StoreId)
        ORDER BY teachingCount desc, username asc
    </select>

    <select id="querySkillKeyList" resultType="jnpf.model.cultivate.vo.teaching.SkillKeyVo">
        SELECT
        fctss.F_Id skillId,
        fctss.F_Name skillName,
        CONCAT(fcts.F_UserId, fctr.F_StoreId) skillKey
        FROM
        ftb_cultivate_teaching_stu fcts
        LEFT JOIN ftb_cultivate_teaching_record fctr on fctr.F_Id = fcts.F_RecordId
        LEFT JOIN ftb_cultivate_teaching_skill fctss on fctss.F_Id = fctr.F_SkillId

        <where>
            <if test="queryTeachingRecordDto.recordIds != null and queryTeachingRecordDto.recordIds.size > 0">
                and fcts.F_RecordId in
                <foreach collection="queryTeachingRecordDto.recordIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="queryTeachingRecordDto.keyword != null and queryTeachingRecordDto.keyword != ''">
                and fcts.F_Username like concat('%', #{queryTeachingRecordDto.keyword}, '%')
            </if>
        </where>
        ORDER BY fctss.F_DeleteMark asc, fctss.F_Sort asc
    </select>
    <!-- 根据用户名查询带教记录ids -->
    <select id="getRecordIdByKeyword" resultType="java.lang.String">
        select distinct F_RecordId
        from ftb_cultivate_teaching_stu
        where instr(F_Username, #{keyword}) > 0 and F_DeleteMark = 0
    </select>
</mapper>