package com.fs.qw.mapper;

import com.alibaba.fastjson.JSONObject;
import com.fs.qw.domain.QwGroupchatStatistic;
import com.fs.qw.param.QwGroupchatStatisticParams;
import com.fs.qw.vo.QwGroupchatStatisticVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 企微群聊数据统计数据Mapper接口
 *
 * @author qw
 * @date 2024-08-05
 */
public interface QwGroupchatStatisticMapper
{
    /**
     * 查询企微群聊数据统计数据
     *
     * @param id 企微群聊数据统计数据主键
     * @return 企微群聊数据统计数据
     */
    public QwGroupchatStatistic selectQwGroupchatStatisticById(Long id);

    /**
     * 查询企微群聊数据统计数据列表
     *
     * @param qwGroupchatStatistic 企微群聊数据统计数据
     * @return 企微群聊数据统计数据集合
     */
    public List<QwGroupchatStatistic> selectQwGroupchatStatisticList(QwGroupchatStatistic qwGroupchatStatistic);

    @Select("<script>" +
            "select qgcs.*,cu.nick_name as ownerName,DATE(FROM_UNIXTIME(qgcs.day_time)) AS converted_date from qw_groupchat_statistic qgcs " +
            "RIGHT join qw_user qu on qgcs.owner=qu.qw_user_id and qu.is_del=0 " +
            "left join company_user cu on qu.id=cu.qw_user_id " +
            "where  qgcs.corp_id=#{maps.corpId} " +
            "and qgcs.owner in " +
            "<foreach  item='item' index='index' collection='maps.users' open='(' separator=',' close=')'> #{item}    </foreach> " +
            "<if test='maps.type != null and maps.type == 1'> " +
            "AND DATE(FROM_UNIXTIME(qgcs.day_time)) = CURDATE() " +
            "</if> " +
            "<if test='maps.type != null and maps.type == 2'> " +
            "AND DATE(FROM_UNIXTIME(qgcs.day_time)) = CURDATE() - INTERVAL 1 DAY " +
            "</if> " +
            "<if test='maps.type != null and maps.type == 3'> " +
            "AND YEARWEEK(FROM_UNIXTIME(qgcs.day_time), 1) = YEARWEEK(CURDATE(), 1) " +
            "</if> " +
            "<if test='maps.type != null and maps.type == 4'> " +
            "AND YEARWEEK(FROM_UNIXTIME(qgcs.day_time), 1) = YEARWEEK(CURDATE(), 1) - 1 " +
            "</if> " +
            "<if test='maps.type != null and maps.type == 5'> " +
            "AND YEAR(FROM_UNIXTIME(qgcs.day_time)) = YEAR(CURDATE()) " +
            "AND MONTH(FROM_UNIXTIME(qgcs.day_time)) = MONTH(CURDATE()) " +
            "</if> " +
            "<if test='maps.type != null and maps.type == 6'> " +
            "AND YEAR(FROM_UNIXTIME(qgcs.day_time)) = YEAR(CURDATE()) " +
            "AND MONTH(FROM_UNIXTIME(qgcs.day_time)) = MONTH(CURDATE()) - 1 " +
            "</if> " +
            "<if test='maps.type != null and maps.type == 7'> " +
            "AND QUARTER(FROM_UNIXTIME(qgcs.day_time)) = QUARTER(CURDATE()) " +
            "AND YEAR(FROM_UNIXTIME(qgcs.day_time)) = YEAR(CURDATE()) " +
            "</if> " +
            "<if test='maps.type != null and maps.type == 8'> " +
            "AND QUARTER(FROM_UNIXTIME(qgcs.day_time)) = QUARTER(CURDATE()) - 1 " +
            "AND YEAR(FROM_UNIXTIME(qgcs.day_time)) = YEAR(CURDATE()) " +
            "</if> " +
            "<if test='maps.type != null and maps.type == 9'> " +
            "AND YEAR(FROM_UNIXTIME(qgcs.day_time)) = YEAR(CURDATE()) " +
            "</if> " +
            "<if test='maps.type != null and maps.type == 10'> " +
            "AND YEAR(FROM_UNIXTIME(qgcs.day_time)) = YEAR(CURDATE()) - 1 " +
            "</if> " +
            "<if test='maps.startTime != null and maps.startTime != \"\" '> " +
            "AND DATE(FROM_UNIXTIME(qgcs.day_time)) >= DATE(#{maps.startTime}) " +
            "</if> " +
            "<if test='maps.endTime != null and maps.endTime != \"\" '> " +
            "AND DATE(#{maps.endTime}) >= DATE(FROM_UNIXTIME(qgcs.day_time)) " +
            "</if> " +
            "</script>" )
    public List<QwGroupchatStatisticVO> selectQwGroupchatStatisticListVO(@Param("maps") QwGroupchatStatisticParams params);


    @Select("<script> " +
            "      SELECT COUNT(*) AS qgcsCount,  t.time, " +
            "sum(IFNULL(t.new_chat_cnt,0)) as newChatCntAll, " +
            "sum(IFNULL(t.chat_total,0)) as chatTotalAll," +
            "sum(IFNULL(t.chat_has_msg,0)) as chatHasMsgAll, " +
            "sum(IFNULL(t.new_member_cnt,0)) as newMemberCntAll, " +
            "sum(IFNULL(t.member_total,0)) as memberTotalAll," +
            "sum(IFNULL(t.member_has_msg,0)) as memberHasMsgAll, " +
            "sum(IFNULL(t.msg_total,0)) as msgTotalAll " +
            "        FROM " +
            "        (" +
            "        SELECT DATE_FORMAT( FROM_UNIXTIME(q.day_time), #{maps.sqlDateFormat} ) AS time,  q.new_chat_cnt, q.chat_total,q.chat_has_msg, q.new_member_cnt, " +
            "q.member_total, q.member_has_msg, q.msg_total " +
            "        FROM " +
            "        qw_groupchat_statistic  AS q " +
            "        WHERE " +
            "        (DATE_FORMAT(FROM_UNIXTIME(q.day_time),#{maps.sqlDateFormat}) between #{maps.beginTime} and #{maps.finalTime}) " +
            "        <if test=\"qwUserIds != null \"> " +
            "            AND q.owner IN " +
                        "<foreach  item='item' index='index' collection='qwUserIds' open='(' separator=',' close=')'> #{item}   </foreach> " +
            "        </if> " +
            "        <if test=\"maps.corpId != null  \"> " +
            "            AND q.corp_id = #{maps.corpId} " +
            "        </if> " +
            "        ) t " +
            "        GROUP BY t.time" +
            "</script>")
    public List<JSONObject> selectQwGroupchatStatisticListVOCount(@Param("maps") Map<String, Object> map, @Param("qwUserIds") List<String> userIds);

    /**
     * 新增企微群聊数据统计数据
     *
     * @param qwGroupchatStatistic 企微群聊数据统计数据
     * @return 结果
     */
    public int insertQwGroupchatStatistic(QwGroupchatStatistic qwGroupchatStatistic);

    /**
     * 修改企微群聊数据统计数据
     *
     * @param qwGroupchatStatistic 企微群聊数据统计数据
     * @return 结果
     */
    public int updateQwGroupchatStatistic(QwGroupchatStatistic qwGroupchatStatistic);

    /**
     * 删除企微群聊数据统计数据
     *
     * @param id 企微群聊数据统计数据主键
     * @return 结果
     */
    public int deleteQwGroupchatStatisticById(Long id);

    /**
     * 批量删除企微群聊数据统计数据
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteQwGroupchatStatisticByIds(Long[] ids);

    /**
    * 更新或者插入
    */
    public int updateOrInsertQwGroupchatStatistic(QwGroupchatStatistic qwGroupchatStatistic);
}
