package com.fs.qw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.qw.domain.QwWatchLog;
import com.fs.qw.param.QwWatchLogStatisticsListParam;
import com.fs.qw.vo.QwWatchLogAllStatisticsListVO;
import com.fs.qw.vo.QwWatchLogStatisticsListVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * 企微看课Mapper接口
 *
 * @author fs
 * @date 2025-03-08
 */
public interface QwWatchLogMapper extends BaseMapper<QwWatchLog>{
    /**
     * 查询企微看课
     *
     * @param id 企微看课主键
     * @return 企微看课
     */
    QwWatchLog selectQwWatchLogById(Long id);

    /**
     * 查询企微看课列表
     *
     * @param qwWatchLog 企微看课
     * @return 企微看课集合
     */
    List<QwWatchLog> selectQwWatchLogList(QwWatchLog qwWatchLog);

    /**
     * 新增企微看课
     *
     * @param qwWatchLog 企微看课
     * @return 结果
     */
    int insertQwWatchLog(QwWatchLog qwWatchLog);
    void insertQwWatchLogBatch(@Param("watchLogs")List<QwWatchLog> qwWatchLog);
    /**
     * 修改企微看课
     *
     * @param qwWatchLog 企微看课
     * @return 结果
     */
    int updateQwWatchLog(QwWatchLog qwWatchLog);

    /**
     * 删除企微看课
     *
     * @param id 企微看课主键
     * @return 结果
     */
    int deleteQwWatchLogById(Long id);

    /**
     * 批量删除企微看课
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteQwWatchLogByIds(Long[] ids);
    @Select({"<script> " +
            "SELECT\n" +
            "    qec.qw_user_id id,\n" +
            "    qu.qw_user_name AS qw_user_name, \n" +
            "    DATE(qec.create_time) AS create_time, \n" +
            "    COUNT(1) AS line,\n" +
            "    COUNT(CASE WHEN qec.is_interact = 1 THEN 1 END) AS interact,\n" +
            "    COUNT(CASE WHEN qec.`level` = 1 THEN 1 END) AS A,\n" +
            "    COUNT(CASE WHEN qec.`level` = 2 THEN 1 END) AS B,\n" +
            "    COUNT(CASE WHEN qec.`level` = 3 THEN 1 END) AS C,\n" +
            "    COUNT(CASE WHEN qec.`level` = 4 THEN 1 END) AS D,\n" +
            "    COUNT(CASE WHEN qec.fs_user_id IS NOT NULL THEN 1 END) AS sign,\n" +
            "    COUNT(CASE WHEN qec.`status` =3 THEN 1 END) AS los,\n" +
            "    COUNT(CASE WHEN qec.`status` IN (4, 5,6) THEN 1 END) AS del\n" +
            "FROM\n" +
            "    qw_external_contact qec\n" +
            "JOIN\n" +
            "    qw_user qu ON qec.qw_user_id = qu.id \n" +
            "WHERE\n" +
            "    DATE(qec.create_time) &gt;= DATE(#{sTime}) and  DATE(qec.create_time) &lt;= DATE(#{eTime}) and qec.company_id =#{companyId} " +
            "<if test ='nickName !=null and nickName!=\"\"'>\n" +
            "   and qu.qw_user_name like concat( #{nickName}, '%')\n" +
            "</if>" +
            "<if test ='ids !=null and ids!=\"\"'>\n" +
            "   and qec.qw_user_id in (${ids})\n" +
            "</if>" +
            "GROUP BY\n" +
            "    qec.qw_user_id, DATE(qec.create_time) \n" +
            "ORDER BY\n" +
            "    DATE(qec.create_time) "+
            "</script>"})
    List<QwWatchLogStatisticsListVO> selectQwExtCountByDayAnd(QwWatchLogStatisticsListParam param);
    @Select("select \n" +
            "COUNT(CASE WHEN day = 0 and status in (1,2) THEN 1 END) AS firstOnline,\n" +
            "COUNT(CASE WHEN day = 0 and status=2 THEN 1 END) AS firstOver,\n" +
            "COUNT(CASE WHEN day = 1 and status in (1,2) THEN 1 END) AS d1Online,\n" +
            "COUNT(CASE WHEN day = 1 and status=2 THEN 1 END) AS d1Over\n" +
            " from qw_watch_log where qw_user_id=#{id} and DATE(line_time) = DATE(#{createTime})")
    QwWatchLogStatisticsListVO selectQwWatchLogByQwUserId(@Param("id")Long id,@Param("createTime") Date createTime);
    @Select("SELECT count(1) from qw_watch_log where ext_id=#{id} and `day`=0 ")
    int selectQwWatchLogIsFirst(Long id);
    @Select("select \n" +
            "COUNT(CASE WHEN day = 0 and status in (1,2) THEN 1 END) AS firstOnline,\n" +
            "COUNT(CASE WHEN day = 0 and status=2 THEN 1 END) AS firstOver,\n" +
            "COUNT(CASE WHEN day = 1 and status in (1,2) THEN 1 END) AS d1Online,\n" +
            "COUNT(CASE WHEN day = 1 and status=2 THEN 1 END) AS d1Over,\n" +
            "COUNT(CASE WHEN day = 2 and status in (1,2) THEN 1 END) AS d2Online,\n" +
            "COUNT(CASE WHEN day = 2 and status=2 THEN 1 END) AS d2Over,\n" +
            "COUNT(CASE WHEN day = 3 and status in (1,2) THEN 1 END) AS d3Online,\n" +
            "COUNT(CASE WHEN day = 3 and status=2 THEN 1 END) AS d3Over,\n" +
            "COUNT(CASE WHEN day = 4 and status in (1,2) THEN 1 END) AS d4Online,\n" +
            "COUNT(CASE WHEN day = 4 and status=2 THEN 1 END) AS d4Over,\n" +
            "COUNT(CASE WHEN day = 5 and status in (1,2) THEN 1 END) AS d5Online,\n" +
            "COUNT(CASE WHEN day = 5 and status=2 THEN 1 END) AS d5Over,\n" +
            "COUNT(CASE WHEN day = 6 and status in (1,2) THEN 1 END) AS d6Online,\n" +
            "COUNT(CASE WHEN day = 6 and status=2 THEN 1 END) AS d6Over,\n" +
            "COUNT(CASE WHEN day = 7 and status in (1,2) THEN 1 END) AS d7Online,\n" +
            "COUNT(CASE WHEN day = 7 and status=2 THEN 1 END) AS d7Over,\n" +
            "COUNT(CASE WHEN day = 8 and status in (1,2) THEN 1 END) AS d8Online,\n" +
            "COUNT(CASE WHEN day = 8 and status=2 THEN 1 END) AS d8Over,\n" +
            "COUNT(CASE WHEN day = 9 and status in (1,2) THEN 1 END) AS d9Online,\n" +
            "COUNT(CASE WHEN day = 9 and status=2 THEN 1 END) AS d9Over,\n" +
            "COUNT(CASE WHEN day = 10 and status in (1,2) THEN 1 END) AS d10Online,\n" +
            "COUNT(CASE WHEN day = 10 and status=2 THEN 1 END) AS d10Over,\n" +
            "COUNT(CASE WHEN day = 11 and status in (1,2) THEN 1 END) AS d11Online,\n" +
            "COUNT(CASE WHEN day = 11 and status=2 THEN 1 END) AS d11Over,\n" +
            "COUNT(CASE WHEN day = 12 and status in (1,2) THEN 1 END) AS d12Online,\n" +
            "COUNT(CASE WHEN day = 12 and status=2 THEN 1 END) AS d12Over,\n" +
            "COUNT(CASE WHEN day = 13 and status in (1,2) THEN 1 END) AS d13Online,\n" +
            "COUNT(CASE WHEN day = 13 and status=2 THEN 1 END) AS d13Over,\n" +
            "COUNT(CASE WHEN day = 14 and status in (1,2) THEN 1 END) AS d14Online,\n" +
            "COUNT(CASE WHEN day = 14 and status=2 THEN 1 END) AS d14Over,\n" +
            "COUNT(CASE WHEN day = 15 and status in (1,2) THEN 1 END) AS d15Online,\n" +
            "COUNT(CASE WHEN day = 15 and status=2 THEN 1 END) AS d15Over,\n" +
            "COUNT(CASE WHEN day = 16 and status in (1,2) THEN 1 END) AS d16Online,\n" +
            "COUNT(CASE WHEN day = 16 and status=2 THEN 1 END) AS d16Over,\n" +
            "COUNT(CASE WHEN day = 17 and status in (1,2) THEN 1 END) AS d17Online,\n" +
            "COUNT(CASE WHEN day = 17 and status=2 THEN 1 END) AS d17Over,\n" +
            "COUNT(CASE WHEN day = 18 and status in (1,2) THEN 1 END) AS d18Online,\n" +
            "COUNT(CASE WHEN day = 18 and status=2 THEN 1 END) AS d18Over,\n" +
            "COUNT(CASE WHEN day = 19 and status in (1,2) THEN 1 END) AS d19Online,\n" +
            "COUNT(CASE WHEN day = 19 and status=2 THEN 1 END) AS d19Over,\n" +
            "COUNT(CASE WHEN day = 20 and status in (1,2) THEN 1 END) AS d20Online,\n" +
            "COUNT(CASE WHEN day = 20 and status=2 THEN 1 END) AS d20Over,\n" +
            "COUNT(CASE WHEN day = 21 and status in (1,2) THEN 1 END) AS d21Online,\n" +
            "COUNT(CASE WHEN day = 21 and status=2 THEN 1 END) AS d21Over,\n" +
            "COUNT(CASE WHEN day = 22 and status in (1,2) THEN 1 END) AS d22Online,\n" +
            "COUNT(CASE WHEN day = 22 and status=2 THEN 1 END) AS d22Over,\n" +
            "COUNT(CASE WHEN day = 23 and status in (1,2) THEN 1 END) AS d23Online,\n" +
            "COUNT(CASE WHEN day = 23 and status=2 THEN 1 END) AS d23Over,\n" +
            "COUNT(CASE WHEN day = 24 and status in (1,2) THEN 1 END) AS d24Online,\n" +
            "COUNT(CASE WHEN day = 24 and status=2 THEN 1 END) AS d24Over,\n" +
            "COUNT(CASE WHEN day = 25 and status in (1,2) THEN 1 END) AS d25Online,\n" +
            "COUNT(CASE WHEN day = 25 and status=2 THEN 1 END) AS d25Over,\n" +
            "COUNT(CASE WHEN day = 26 and status in (1,2) THEN 1 END) AS d26Online,\n" +
            "COUNT(CASE WHEN day = 26 and status=2 THEN 1 END) AS d26Over,\n" +
            "COUNT(CASE WHEN day = 27 and status in (1,2) THEN 1 END) AS d27Online,\n" +
            "COUNT(CASE WHEN day = 27 and status=2 THEN 1 END) AS d27Over,\n" +
            "COUNT(CASE WHEN day = 28 and status in (1,2) THEN 1 END) AS d28Online,\n" +
            "COUNT(CASE WHEN day = 28 and status=2 THEN 1 END) AS d28Over,\n" +
            "COUNT(CASE WHEN day = 29 and status in (1,2) THEN 1 END) AS d29Online,\n" +
            "COUNT(CASE WHEN day = 29 and status=2 THEN 1 END) AS d29Over,\n" +
            "COUNT(CASE WHEN day = 30 and status in (1,2) THEN 1 END) AS d30Online,\n" +
            "COUNT(CASE WHEN day = 30 and status=2 THEN 1 END) AS d30Over" +
            " from qw_watch_log where qw_user_id=#{id} and DATE(line_time) = DATE(#{createTime})")
    QwWatchLogAllStatisticsListVO selectQwWatchLogAllStatisticsListVO(@Param("id")Long id,@Param("createTime") Date createTime);
    @Select({"<script> " +
            "SELECT\n" +
            "    qec.qw_user_id id,\n" +
            "    qu.qw_user_name AS qw_user_name, \n" +
            "    DATE(qec.create_time) AS create_time, \n" +
            "    COUNT(1) AS line\n" +
            "FROM\n" +
            "    qw_external_contact qec\n" +
            "JOIN\n" +
            "    qw_user qu ON qec.qw_user_id = qu.id \n" +
            "WHERE\n" +
            "    DATE(qec.create_time) &gt;= DATE(#{sTime}) and  DATE(qec.create_time) &lt;= DATE(#{eTime}) and qec.company_id =#{companyId} " +
            "<if test ='ids !=null and ids!=\"\"'>\n" +
            "   and qec.qw_user_id in (${ids})\n" +
            "</if>" +
            "<if test ='nickName !=null and nickName!=\"\"'>\n" +
            "   and qu.qw_user_name like concat( #{nickName}, '%')\n" +
            "</if>" +
            "GROUP BY\n" +
            "    qec.qw_user_id, DATE(qec.create_time) \n" +
            "ORDER BY\n" +
            "    DATE(qec.create_time) "+
            "</script>"})
    List<QwWatchLogAllStatisticsListVO> selectQwExtCountByDayAndLine(QwWatchLogStatisticsListParam param);
}
