<?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.fs.statis.mapper.FsStatisSalerWatchMapper"> <!-- 请替换为你的Mapper接口的完整路径 -->

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="com.fs.statis.domain.FsStatisSalerWatch"> <!-- 请替换为你的Entity的完整路径 -->
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="dept_id" property="deptId" jdbcType="INTEGER"/>
        <result column="company_user_id" property="companyUserId" jdbcType="INTEGER"/>
        <result column="train_camp_num" property="trainCampNum" jdbcType="INTEGER"/>
        <result column="not_registered_num" property="notRegisteredNum" jdbcType="INTEGER"/>
        <result column="registered_num" property="registeredNum" jdbcType="INTEGER"/>
        <result column="reg_rate" property="regRate" jdbcType="FLOAT"/>
        <result column="finished_rate" property="finishedRate" jdbcType="FLOAT"/>
        <result column="offline_total" property="offlineTotal" jdbcType="INTEGER"/>
        <result column="offline_not_part" property="offlineNotPart" jdbcType="INTEGER"/> <!-- 对应修正后的 offlineNotPart -->
        <result column="offline_not_watched" property="offlineNotWatched" jdbcType="INTEGER"/>
        <result column="online_total" property="onlineTotal" jdbcType="INTEGER"/>
        <result column="online_online_rate" property="onlineOnlineRate" jdbcType="FLOAT"/>
        <result column="online_playback_comple_rate" property="onlinePlaybackCompleRate" jdbcType="FLOAT"/>
        <result column="online_incomplete_playback" property="onlineIncompletePlayback" jdbcType="INTEGER"/>
        <result column="online_complete_playback" property="onlineCompletePlayback" jdbcType="INTEGER"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, dept_id, company_user_id, train_camp_num, not_registered_num, registered_num,
        reg_rate, finished_rate, offline_total, offline_not_part, offline_not_watched,
        online_total, online_online_rate, online_playback_comple_rate,
        online_incomplete_playback, online_complete_playback
    </sql>

    <select id="queryList" resultType="com.fs.statis.domain.FsStatisSalerWatch">
        SELECT
        company_user_id,
        ANY_VALUE(dept_id) as dept_id,
        SUM(train_camp_num) as train_camp_num,
        SUM(not_registered_num) as not_registered_num,
        SUM(registered_num) as registered_num,
        SUM(offline_total) as offline_total,
        SUM(offline_not_part) as offline_not_part,
        SUM(offline_not_watched) as offline_not_watched,
        SUM(online_total) as online_total,
        SUM(online_incomplete_playback) as online_incomplete_playback,
        SUM(online_complete_playback) as online_complete_playback,
        CASE WHEN SUM(train_camp_num) > 0
        THEN ROUND(SUM(registered_num) * 1.0 / SUM(train_camp_num), 4)
        ELSE 0 END as reg_rate,
        CASE WHEN SUM(registered_num) > 0
        THEN ROUND((SUM(registered_num) - SUM(not_registered_num)) * 1.0 / SUM(registered_num), 4)
        ELSE 0 END as finished_rate,
        CASE WHEN SUM(online_total) > 0
        THEN ROUND(SUM(online_complete_playback) * 1.0 / SUM(online_total), 4)
        ELSE 0 END as online_online_rate,
        CASE WHEN SUM(online_total) > 0
        THEN ROUND(SUM(online_complete_playback) * 1.0 / SUM(online_total), 4)
        ELSE 0 END as online_playback_comple_rate
        FROM fs_statis_saler_watch
        <where>
            <if test="userIds != null and userIds.size() > 0">
                AND company_user_id IN
                <foreach collection="userIds" open="(" close=")" separator="," item="item">
                    ${item}
                </foreach>
            </if>
            <if test="periodList != null and periodList.size() > 0">
                AND period_id IN
                <foreach collection="periodList" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="startDate != null and endDate != null">
                AND data_date BETWEEN #{startDate} AND #{endDate}
            </if>
        </where>
        GROUP BY company_user_id
    </select>
    <select id="queryPeriodList" resultType="com.fs.statis.domain.FsStatisSalerWatch">
        SELECT
        period_id,
        ANY_VALUE(dept_id) as dept_id,
        SUM(train_camp_num) as train_camp_num,
        SUM(not_registered_num) as not_registered_num,
        SUM(registered_num) as registered_num,
        SUM(offline_total) as offline_total,
        SUM(offline_not_part) as offline_not_part,
        SUM(offline_not_watched) as offline_not_watched,
        SUM(online_total) as online_total,
        SUM(online_incomplete_playback) as online_incomplete_playback,
        SUM(online_complete_playback) as online_complete_playback,
        CASE WHEN SUM(train_camp_num) > 0
        THEN ROUND(SUM(registered_num) * 1.0 / SUM(train_camp_num), 4)
        ELSE 0 END as reg_rate,
        CASE WHEN SUM(registered_num) > 0
        THEN ROUND((SUM(registered_num) - SUM(not_registered_num)) * 1.0 / SUM(registered_num), 4)
        ELSE 0 END as finished_rate,
        CASE WHEN SUM(online_total) > 0
        THEN ROUND(SUM(online_complete_playback) * 1.0 / SUM(online_total), 4)
        ELSE 0 END as online_online_rate,
        CASE WHEN SUM(online_total) > 0
        THEN ROUND(SUM(online_complete_playback) * 1.0 / SUM(online_total), 4)
        ELSE 0 END as online_playback_comple_rate
        FROM fs_statis_saler_watch
        <where>
            <if test="userIds != null and userIds.size() > 0">
                AND company_user_id IN
                <foreach collection="userIds" open="(" close=")" separator="," item="item">
                    ${item}
                </foreach>
            </if>
            <if test="periodList != null and periodList.size() > 0">
                AND period_id IN
                <foreach collection="periodList" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="startDate != null and endDate != null">
                AND data_date BETWEEN #{startDate} AND #{endDate}
            </if>
        </where>
        GROUP BY period_id
    </select>
    <select id="queryTodayList" resultType="com.fs.statis.domain.FsStatisSalerWatch">
        SELECT
        data_date,
        ANY_VALUE(dept_id) as dept_id,
        SUM(train_camp_num) as train_camp_num,
        SUM(not_registered_num) as not_registered_num,
        SUM(registered_num) as registered_num,
        SUM(offline_total) as offline_total,
        SUM(offline_not_part) as offline_not_part,
        SUM(offline_not_watched) as offline_not_watched,
        SUM(online_total) as online_total,
        SUM(online_incomplete_playback) as online_incomplete_playback,
        SUM(online_complete_playback) as online_complete_playback,
        CASE WHEN SUM(train_camp_num) > 0
        THEN ROUND(SUM(registered_num) * 1.0 / SUM(train_camp_num), 4)
        ELSE 0 END as reg_rate,
        CASE WHEN SUM(registered_num) > 0
        THEN ROUND((SUM(registered_num) - SUM(not_registered_num)) * 1.0 / SUM(registered_num), 4)
        ELSE 0 END as finished_rate,
        CASE WHEN SUM(online_total) > 0
        THEN ROUND(SUM(online_complete_playback) * 1.0 / SUM(online_total), 4)
        ELSE 0 END as online_online_rate,
        CASE WHEN SUM(online_total) > 0
        THEN ROUND(SUM(online_complete_playback) * 1.0 / SUM(online_total), 4)
        ELSE 0 END as online_playback_comple_rate
        FROM fs_statis_saler_watch
        <where>
            <if test="userIds != null and userIds.size() > 0">
                AND company_user_id IN
                <foreach collection="userIds" open="(" close=")" separator="," item="item">
                    ${item}
                </foreach>
            </if>
            <if test="periodList != null and periodList.size() > 0">
                AND period_id IN
                <foreach collection="periodList" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="startDate != null and endDate != null">
                AND data_date BETWEEN #{startDate} AND #{endDate}
            </if>
        </where>
        GROUP BY data_date
    </select>


    <insert id="batchSave">
        INSERT INTO fs_statis_saler_watch (
            dept_id, company_user_id, train_camp_num,
            not_registered_num, registered_num, reg_rate,
            finished_rate, offline_total, offline_not_part,
            offline_not_watched, online_total, online_online_rate,
            online_playback_comple_rate, online_incomplete_playback,
            online_complete_playback,period_id,data_date
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.deptId,jdbcType=INTEGER},
                #{item.companyUserId,jdbcType=INTEGER},
                #{item.trainCampNum,jdbcType=INTEGER},
                #{item.notRegisteredNum,jdbcType=INTEGER},
                #{item.registeredNum,jdbcType=INTEGER},
                #{item.regRate,jdbcType=FLOAT},
                #{item.finishedRate,jdbcType=FLOAT},
                #{item.offlineTotal,jdbcType=INTEGER},
                #{item.offlineNotPart,jdbcType=INTEGER},
                #{item.offlineNotWatched,jdbcType=INTEGER},
                #{item.onlineTotal,jdbcType=INTEGER},
                #{item.onlineOnlineRate,jdbcType=FLOAT},
                #{item.onlinePlaybackCompleRate,jdbcType=FLOAT},
                #{item.onlineIncompletePlayback,jdbcType=INTEGER},
                #{item.onlineCompletePlayback,jdbcType=INTEGER},
                #{item.periodId,jdbcType=VARCHAR},
                #{item.dataDate,jdbcType=VARCHAR}
            )
        </foreach>
        ON DUPLICATE KEY UPDATE
        train_camp_num = VALUES(train_camp_num),
        not_registered_num = VALUES(not_registered_num),
        registered_num = VALUES(registered_num),
        reg_rate = VALUES(reg_rate),
        finished_rate = VALUES(finished_rate),
        offline_total = VALUES(offline_total),
        offline_not_part = VALUES(offline_not_part),
        offline_not_watched = VALUES(offline_not_watched),
        online_total = VALUES(online_total),
        online_online_rate = VALUES(online_online_rate),
        online_playback_comple_rate = VALUES(online_playback_comple_rate),
        online_incomplete_playback = VALUES(online_incomplete_playback),
        online_complete_playback = VALUES(online_complete_playback)
    </insert>

</mapper>
