<?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.TeachingRecordMapper">

    <resultMap type="jnpf.model.cultivate.vo.teaching.SummaryPageListVo" id="SummaryPageListMap">
        <result property="storeName" column="storeName"/>
        <result property="userName" column="userName"/>
        <result property="postName" column="postName"/>
        <result property="totalCount" column="totalCount"/>
        <result property="skillList" column="skillList" typeHandler="jnpf.cultivate.event.JsonToListTypeHandler"/>
    </resultMap>

    <resultMap type="jnpf.model.cultivate.vo.teaching.EmployeeViewDataVo" id="EmployeeViewDataVoMap">
        <result property="userName" column="userName"/>
        <result property="postName" column="postName"/>
        <result property="totalCount" column="totalCount"/>
        <result property="skillList" column="skillList" typeHandler="jnpf.cultivate.event.JsonToListTypeHandler"/>
    </resultMap>

    <select id="skillCount" resultType="jnpf.model.cultivate.vo.teaching.SkillCountVo">
        SELECT
        fcts.F_Name name,
        count(1) count
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_skill fcts ON fcts.F_Id = fctr.F_SkillId
        <where>
            <if test="recordIds != null and recordIds.size > 0">
                and fctr.F_Id in
                <foreach collection="recordIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY fctr.F_SkillId
        ORDER BY fcts.F_DeleteMark asc, fcts.F_Sort asc
    </select>

    <select id="summaryPageList" resultMap="SummaryPageListMap">
        SELECT
        storeName,
        userId,
        SUBSTRING_INDEX( MAX( postName ), '~', -1 ) AS postName,
        SUM( skillCount ) AS totalCount,
        JSON_ARRAYAGG( JSON_OBJECT( 'name', skillName, 'count', skillCount, 'sort', skillSort, 'deleteMark', skillDeleteMark ) ) AS skillList
        FROM
        (SELECT
        fctr.F_StoreName AS storeName,
        fctr.F_UserId userId,
        MAX(CONCAT( fctr.F_CreatorTime, '~', fctr.F_PostName )) AS postName,
        fcs.F_Name AS skillName,
        COUNT( fctr.F_Id ) AS skillCount,
        fcs.F_Sort AS skillSort,
        fcs.F_DeleteMark AS skillDeleteMark
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_skill fcs ON fcs.F_Id = fctr.F_SkillId AND fcs.F_Name IS NOT NULL
        <where>
            fcs.F_Name IS NOT NULL
            AND fctr.F_DeleteMark = 0 AND fctr.F_Type = 2
            <if test="pageDto.storeIdList != null and pageDto.storeIdList.size()>0">
                AND fctr.F_StoreId in
                <foreach collection="pageDto.storeIdList" item="value" separator="," open="(" close=")">
                    #{value}
                </foreach>
            </if>
            <if test="pageDto.keyword != null and pageDto.keyword != ''">
                AND fctr.F_UserName LIKE CONCAT('%', #{pageDto.keyword}, '%')
            </if>
            <if test="(pageDto.startDate != null and pageDto.startDate != '') and (pageDto.endDate != null and pageDto.endDate != '')">
                AND fctr.F_Date BETWEEN #{pageDto.startDate} AND #{pageDto.endDate}
            </if>
            <if test="null != pageDto.categoryId and '' != pageDto.categoryId">
                <choose>
                    <when test="-1 == pageDto.categoryId">
                        and fcs.F_CategoryId is null
                    </when>
                    <otherwise>
                        and fcs.F_CategoryId = #{pageDto.categoryId}
                    </otherwise>
                </choose>
            </if>
        </where>
        GROUP BY fctr.F_StoreId, fctr.F_UserId, fcs.F_Id
        ) AS detail
        GROUP BY storeName, userId
        ORDER BY totalCount DESC
    </select>

    <select id="summarySkillList" resultType="jnpf.model.cultivate.vo.teaching.SkillCountVo">
        SELECT
        fcs.F_Name AS name,
        fcs.F_Sort AS sort,
        fcs.F_DeleteMark AS deleteMark
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_skill fcs ON fcs.F_Id = fctr.F_SkillId AND fcs.F_Name IS NOT NULL
        <where>
            fcs.F_Name IS NOT NULL
            AND fctr.F_DeleteMark = 0 AND fctr.F_Type = 2
            <if test="pageDto.storeIdList != null and pageDto.storeIdList.size()>0">
                AND fctr.F_StoreId in
                <foreach collection="pageDto.storeIdList" item="value" separator="," open="(" close=")">
                    #{value}
                </foreach>
            </if>
            <if test="pageDto.keyword != null and pageDto.keyword != ''">
                AND fctr.F_UserName LIKE CONCAT('%', #{pageDto.keyword}, '%')
            </if>
            <if test="(pageDto.startDate != null and pageDto.startDate != '') and (pageDto.endDate != null and pageDto.endDate != '')">
                AND fctr.F_Date BETWEEN #{pageDto.startDate} AND #{pageDto.endDate}
            </if>
            <if test="null != pageDto.categoryId and '' != pageDto.categoryId">
                <choose>
                    <when test="-1 == pageDto.categoryId">
                        and fcs.F_CategoryId is null
                    </when>
                    <otherwise>
                        and fcs.F_CategoryId = #{pageDto.categoryId}
                    </otherwise>
                </choose>
            </if>
        </where>
        GROUP BY fcs.F_Id
        ORDER BY fcs.F_Sort
    </select>

    <select id="recordPageList" resultType="jnpf.model.cultivate.vo.teaching.RecordPageListVo">
        SELECT
        fctr.F_Id AS id,
        fctr.F_StoreName AS storeName,
        fctr.F_UserName AS userName,
        fctr.F_PostName AS postName,
        fcs.F_Name AS skillName,
        fctr.F_VideoPath AS videoPath,
        concat(fctr.F_VideoPath, '.0.jpg') AS videoCover,
        fctr.F_VideoActionType AS videoActionType,
        fctr.F_VideoDuration AS videoDuration,
        fctr.F_Date AS date
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_skill fcs ON fcs.F_Id = fctr.F_SkillId AND fcs.F_Name IS NOT NULL
        <where>
            fcs.F_Name IS NOT NULL
            AND fctr.F_DeleteMark = 0 AND fctr.F_Type = 2
            <if test="pageDto.storeIdList != null and pageDto.storeIdList.size()>0">
                AND fctr.F_StoreId in
                <foreach collection="pageDto.storeIdList" item="value" separator="," open="(" close=")">
                    #{value}
                </foreach>
            </if>
            <if test="pageDto.keyword != null and pageDto.keyword != ''">
                AND (fctr.F_UserName LIKE CONCAT('%', #{pageDto.keyword}, '%')
                OR fctr.F_PostName LIKE CONCAT('%', #{pageDto.keyword}, '%'))
            </if>
            <if test="(pageDto.startDate != null and pageDto.startDate != '') and (pageDto.endDate != null and pageDto.endDate != '')">
                AND fctr.F_Date BETWEEN #{pageDto.startDate} AND #{pageDto.endDate}
            </if>
            <if test="null != skillIds and skillIds.size > 0">
                AND fcs.F_Id IN
                <foreach collection="skillIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY fctr.F_Date DESC, fctr.F_CreatorTime DESC
    </select>

    <select id="myRecordPageList" resultType="jnpf.model.cultivate.vo.teaching.MyRecordPageListVo">
        SELECT
        fctr.F_Id AS id,
        fcs.F_Name AS skillName,
        fctr.F_VideoPath AS videoPath,
        concat(fctr.F_VideoPath, '.0.jpg') AS videoCover,
        fctr.F_VideoActionType AS videoActionType,
        fctr.F_Date AS date
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_skill fcs ON fcs.F_Id = fctr.F_SkillId AND fcs.F_Name IS NOT NULL
        <where>
            fcs.F_Name IS NOT NULL
            AND fctr.F_DeleteMark = 0 AND fctr.F_Type = 2
            AND fctr.F_UserId = #{userId}
            AND fctr.F_StoreId = #{pageDto.storeId}
            <if test="pageDto.keyword != null and pageDto.keyword != ''">
                AND fcs.F_Name LIKE CONCAT('%', #{pageDto.keyword}, '%')
            </if>
            <if test="(pageDto.startDate != null and pageDto.startDate != '') and (pageDto.endDate != null and pageDto.endDate != '')">
                AND fctr.F_Date BETWEEN #{pageDto.startDate} AND #{pageDto.endDate}
            </if>
        </where>
        ORDER BY fctr.F_Date DESC, fctr.F_CreatorTime DESC
    </select>

    <select id="getMySkillTable" resultType="jnpf.model.cultivate.vo.teaching.RecordViewDataModel">
        SELECT
        fcs.F_Name AS skillName,
        COUNT( fctr.F_Id ) AS count
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_skill fcs ON fcs.F_Id = fctr.F_SkillId AND fcs.F_Name IS NOT NULL
        <where>
            fcs.F_Name IS NOT NULL
            AND fctr.F_DeleteMark = 0 AND fctr.F_Type = 2
            AND fctr.F_UserId = #{userId}
            <if test="pageDto.storeIdList != null and pageDto.storeIdList.size()>0">
                AND fctr.F_StoreId in
                <foreach collection="pageDto.storeIdList" item="value" separator="," open="(" close=")">
                    #{value}
                </foreach>
            </if>
            <if test="(pageDto.startDate != null and pageDto.startDate != '') and (pageDto.endDate != null and pageDto.endDate != '')">
                AND fctr.F_Date BETWEEN #{pageDto.startDate} AND #{pageDto.endDate}
            </if>
        </where>
        GROUP BY fctr.F_SkillId
        ORDER BY fcs.F_Sort
    </select>

    <select id="employeePageList" resultType="jnpf.model.cultivate.vo.teaching.EmployeePageListVo">
        SELECT
            fcs.F_Name AS skillName,
            fctr.F_UserName AS userName,
            fctr.F_VideoPath AS videoPath,
            concat(fctr.F_VideoPath, '.0.jpg') AS videoCover,
            fctr.F_VideoActionType AS videoActionType,
            fctr.F_Date AS date
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_skill fcs ON fcs.F_Id = fctr.F_SkillId AND fcs.F_Name IS NOT NULL
        <where>
            fcs.F_Name IS NOT NULL
            AND fctr.F_DeleteMark = 0 AND fctr.F_Type = 2
            <if test="pageDto.storeIdList != null and pageDto.storeIdList.size()>0">
                AND fctr.F_StoreId in
                <foreach collection="pageDto.storeIdList" item="value" separator="," open="(" close=")">
                    #{value}
                </foreach>
            </if>
            <if test="pageDto.keyword != null and pageDto.keyword != ''">
                AND (fctr.F_UserName LIKE CONCAT('%', #{pageDto.keyword}, '%')
                OR fcs.F_Name LIKE CONCAT('%', #{pageDto.keyword}, '%'))
            </if>
            <if test="(pageDto.startDate != null and pageDto.startDate != '') and (pageDto.endDate != null and pageDto.endDate != '')">
                AND fctr.F_Date BETWEEN #{pageDto.startDate} AND #{pageDto.endDate}
            </if>
        </where>
        ORDER BY fctr.F_Date DESC, fctr.F_CreatorTime DESC
    </select>
    <select id="employeeViewData" resultMap="EmployeeViewDataVoMap">
        SELECT
        userName AS userName,
        SUBSTRING_INDEX( MAX( postName ), '~', -1 ) AS postName,
        SUM( skillCount ) AS totalCount,
        JSON_ARRAYAGG( JSON_OBJECT( 'name', skillName, 'count', skillCount, 'sort', skillSort, 'deleteMark', skillDeleteMark) ) AS skillList
        FROM
        (SELECT
        fctr.F_UserName AS userName,
        CONCAT( fctr.F_CreatorTime, '~', fctr.F_PostName ) AS postName,
        fcs.F_Name AS skillName,
        COUNT( fctr.F_Id ) AS skillCount,
        fcs.F_Sort AS skillSort,
        fcs.F_DeleteMark AS skillDeleteMark
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_skill fcs ON fcs.F_Id = fctr.F_SkillId AND fcs.F_Name IS NOT NULL
        <where>
            fcs.F_Name IS NOT NULL
            AND fctr.F_DeleteMark = 0 AND fctr.F_Type = 2
            <if test="pageDto.storeIdList != null and pageDto.storeIdList.size()>0">
                AND fctr.F_StoreId in
                <foreach collection="pageDto.storeIdList" item="value" separator="," open="(" close=")">
                    #{value}
                </foreach>
            </if>
            <if test="(pageDto.startDate != null and pageDto.startDate != '') and (pageDto.endDate != null and pageDto.endDate != '')">
                AND fctr.F_Date BETWEEN #{pageDto.startDate} AND #{pageDto.endDate}
            </if>
        </where>
        GROUP BY fctr.F_UserId, fcs.F_Id ) AS detail
        GROUP BY userName
        ORDER BY totalCount
    </select>
    <select id="employeeSkillList" resultType="jnpf.model.cultivate.vo.teaching.SkillCountVo">
        SELECT
        fcs.F_Name AS name,
        fcs.F_Sort AS sort,
        fcs.F_DeleteMark AS deleteMark
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_skill fcs ON fcs.F_Id = fctr.F_SkillId AND fcs.F_Name IS NOT NULL
        <where>
            fcs.F_Name IS NOT NULL
            AND fctr.F_DeleteMark = 0 AND fctr.F_Type = 2
            <if test="pageDto.storeIdList != null and pageDto.storeIdList.size()>0">
                AND fctr.F_StoreId in
                <foreach collection="pageDto.storeIdList" item="value" separator="," open="(" close=")">
                    #{value}
                </foreach>
            </if>
            <if test="(pageDto.startDate != null and pageDto.startDate != '') and (pageDto.endDate != null and pageDto.endDate != '')">
                AND fctr.F_Date BETWEEN #{pageDto.startDate} AND #{pageDto.endDate}
            </if>
        </where>
        GROUP BY fcs.F_Id
        ORDER BY fcs.F_Sort
    </select>
    <select id="teachingSummaryQuery" resultType="jnpf.model.cultivate.vo.teaching.TeachingDataListVo">
        SELECT
        fctr.F_Id recordId,
        fctr.F_UserId teachingUserId,
        fctr.F_Username username,
        fctr.F_PostName postName,
        fctr.F_UserId userId,
        fctr.F_StoreId storeId,
        fctr.F_StoreName storeName,
        fctss.F_Id skillId,
        SUM( 1 ) teachingCount
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_skill fctss on fctss.F_Id = fctr.F_SkillId
        <where>
            fctr.F_Type = 1
            and fctr.F_DeleteMark = 0
            <if test="queryTeachingRecordDto.storeIdList != null and queryTeachingRecordDto.storeIdList.size > 0">
                and fctr.F_StoreId in
                <foreach collection="queryTeachingRecordDto.storeIdList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="queryTeachingRecordDto.teachingUserIds != null and queryTeachingRecordDto.teachingUserIds.size > 0">
                and fctr.F_UserId in
                <foreach collection="queryTeachingRecordDto.teachingUserIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="queryTeachingRecordDto.keyword != null and queryTeachingRecordDto.keyword != ''">
                and fctr.F_UserName like CONCAT('%',#{queryTeachingRecordDto.keyword},'%')
            </if>
            <if test="queryTeachingRecordDto.startDate != null and queryTeachingRecordDto.startDate != ''">
                <![CDATA[ and DATE_FORMAT(fctr.F_Date, '%Y-%m-%d') >= #{queryTeachingRecordDto.startDate} ]]>
            </if>
            <if test="queryTeachingRecordDto.endDate != null and queryTeachingRecordDto.endDate != ''">
                <![CDATA[ and DATE_FORMAT(fctr.F_Date, '%Y-%m-%d') <= #{queryTeachingRecordDto.endDate} ]]>
            </if>
            <if test="null != queryTeachingRecordDto.categoryId and '' != queryTeachingRecordDto.categoryId">
                <choose>
                    <when test="-1 == queryTeachingRecordDto.categoryId">
                        and fctss.F_CategoryId is null
                    </when>
                    <otherwise>
                        and fctss.F_CategoryId = #{queryTeachingRecordDto.categoryId}
                    </otherwise>
                </choose>
            </if>
        </where>
        GROUP BY CONCAT(fctr.F_UserId,fctr.F_StoreId)
        ORDER BY teachingCount desc, fctr.F_UserName
    </select>

    <select id="queryCountSkillList" resultType="jnpf.model.cultivate.vo.teaching.SkillKeyVo">
        SELECT
        fctss.F_Id skillId,
        fctss.F_Name skillName,
        CONCAT(fctr.F_UserId, fctr.F_StoreId) skillKey
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_skill fctss on fctss.F_Id = fctr.F_SkillId
        <where>
            fctr.F_Type = 1
            and fctr.F_DeleteMark = 0
            <if test="queryTeachingRecordDto.storeIdList != null and queryTeachingRecordDto.storeIdList.size > 0">
                and fctr.F_StoreId in
                <foreach collection="queryTeachingRecordDto.storeIdList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="queryTeachingRecordDto.keyword != null and queryTeachingRecordDto.keyword != ''">
                and fctr.F_UserName like CONCAT('%',#{queryTeachingRecordDto.keyword},'%')
            </if>
            <if test="queryTeachingRecordDto.startDate != null and queryTeachingRecordDto.startDate != ''">
                <![CDATA[ and DATE_FORMAT(fctr.F_Date, '%Y-%m-%d') >= #{queryTeachingRecordDto.startDate} ]]>
            </if>
            <if test="queryTeachingRecordDto.endDate != null and queryTeachingRecordDto.endDate != ''">
                <![CDATA[ and DATE_FORMAT(fctr.F_Date, '%Y-%m-%d') <= #{queryTeachingRecordDto.endDate} ]]>
            </if>
            <if test="null != queryTeachingRecordDto.categoryId and '' != queryTeachingRecordDto.categoryId">
                <choose>
                    <when test="-1 == queryTeachingRecordDto.categoryId">
                        and fctss.F_CategoryId is null
                    </when>
                    <otherwise>
                        and fctss.F_CategoryId = #{queryTeachingRecordDto.categoryId}
                    </otherwise>
                </choose>
            </if>
        </where>
        ORDER BY fctss.F_DeleteMark asc, fctss.F_Sort asc;
    </select>
    <select id="getSuperiorTeachingSummary"
            resultType="jnpf.model.cultivate.vo.teaching.SuperiorTeachingSummaryVo">
        SELECT
        SUM( 1 ) count,
        FORMAT(SUM( fctr.F_VideoDuration )/60, 2) duration
        FROM
        ftb_cultivate_teaching_record fctr
        LEFT JOIN ftb_cultivate_teaching_stu fcts ON fcts.F_RecordId = fctr.F_Id
        WHERE
        fctr.F_Type = 1
        AND fctr.F_DeleteMark = 0
        AND fcts.F_DeleteMark = 0
        <if test="storeId != null and storeId != ''">
            AND fctr.F_StoreId = #{storeId}
        </if>
        <if test="loginUserId != null and loginUserId != ''">
            AND fcts.F_UserId = #{loginUserId}
        </if>
    </select>
    <!-- 根据storeIds查询每个门店下的带教数量 -->
    <select id="getStoreRecordCount" resultType="jnpf.model.cultivate.v2.teaching.vo.StoreTeachingVo">
        select s.storeId, count(tr.F_Id) recordCount
        from (
            values
            <foreach collection="queryDto.storeIdList" item="item" separator=",">
                row(#{item})
            </foreach>
        ) as s(storeId)
        left join ftb_cultivate_teaching_record tr
        on s.storeId = tr.F_StoreId and tr.F_DeleteMark = 0 and tr.F_Type = #{queryDto.type} and tr.F_Date between #{queryDto.startDate} and #{queryDto.endDate}
        left join ftb_cultivate_teaching_skill ts
        on tr.F_SkillId = ts.F_Id
        <if test="null != queryDto.keyword and '' != queryDto.keyword">
            and (instr(ts.F_Name, #{queryDto.keyword}) > 0
                <if test="null != recordIds and recordIds.size > 0">
                    or tr.F_Id in
                    <foreach collection="recordIds" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            )
        </if>
        group by s.storeId
        order by recordCount desc, s.storeId desc
    </select>
    <!-- 根据storeId查询门店下的带教记录 -->
    <select id="getStoreRecordList" resultType="jnpf.model.cultivate.v2.teaching.vo.V2TeachingRecordVo">
        select tr.F_Id id, tr.F_Type `type`, tr.F_StoreId storeId, tr.F_StoreName storeName, tr.F_SkillId skillId, ts.F_Name skillName, tr.F_UserId userId,
               tr.F_UserName userName, tr.F_PostId postId, tr.F_PostName postName, tr.F_Date `date`, ta.F_Id approveId, ta.F_ApproveStatus approveStatus,
               case when uv.F_UserId is not null then 1 else 0 end viewed
        from ftb_cultivate_teaching_record tr
        left join ftb_cultivate_teaching_skill ts on tr.F_SkillId = ts.F_Id
        left join ftb_cultivate_teaching_approve ta on tr.F_Id = ta.F_TeachId
        left join ftb_cultivate_user_view uv on uv.F_BizId = tr.F_Id and uv.F_UserId = #{userId}
        where tr.F_Type = #{queryDto.type} and tr.F_DeleteMark = 0 and tr.F_StoreId = #{queryDto.storeId}
        <if test="null != queryDto.keyword and '' != queryDto.keyword">
            and instr(ts.F_Name, #{queryDto.keyword}) > 0
            <if test="null != recordIds and recordIds.size > 0">
                or tr.F_Id in
                <foreach collection="recordIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </if>
        and tr.F_Date between #{queryDto.startDate} and #{queryDto.endDate}
        order by tr.F_CreatorTime desc
    </select>
    <!-- 查询带教详情 -->
    <select id="getTeachingDetailById" resultType="jnpf.model.cultivate.v2.teaching.vo.TeachingDetailVo">
        select tr.F_Id id, tr.F_Type `type`, tr.F_StoreId storeId, tr.F_StoreName storeName, tr.F_SkillId skillId, ts.F_Name skillName, ts.F_Description skillDescription,
               tr.F_UserId userId, tr.F_UserName userName, tr.F_PostId postId, tr.F_PostName postName, tr.F_Date `date`, tr.F_Remark remark
        from ftb_cultivate_teaching_record tr
        left join ftb_cultivate_teaching_skill ts on tr.F_SkillId = ts.F_Id
        where tr.F_Id = #{bizId}
    </select>
</mapper>