package com.fs.qw.mapper;

import com.fs.qw.domain.QwGroupChatUser;
import com.fs.qw.param.QwGroupChatUserDataType;
import com.fs.qw.vo.QwGroupChatUserVO;
import com.fs.sop.domain.SopUserLogsInfo;
import com.fs.sop.params.SopUserLogsInfoByIdsHasUserIdParam;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

/**
 * 客户群成员列Mapper接口
 *
 * @author fs
 * @date 2024-06-20
 */
@Repository
public interface QwGroupChatUserMapper
{
    /**
     * 查询客户群成员列
     *
     * @param id 客户群成员列主键
     * @return 客户群成员列
     */
    public QwGroupChatUser selectQwGroupChatUserById(Long id);

    /**
     * 查询客户群成员列列表
     *
     * @param qwGroupChatUser 客户群成员列
     * @return 客户群成员列集合
     */
    public List<QwGroupChatUser> selectQwGroupChatUserList(QwGroupChatUser qwGroupChatUser);
    @Select("<script> " +
            "SELECT " +
            "    a.user_id,\n" +
            "    ANY_VALUE(COALESCE(b.name, a.name)) AS name,\n" +
            "    ANY_VALUE(COALESCE(a.invitor, b.user_id)) AS invitor,\n" +
            "    ANY_VALUE(qu.qw_user_name) AS invitorName,\n" +
            "    a.chat_id,\n" +
            "    ANY_VALUE(a.type) AS type,\n" +
            "    ANY_VALUE(a.join_time) AS join_time,\n" +
            "    ANY_VALUE(a.join_scene) AS join_scene,\n" +
            "    ANY_VALUE(a.company_id) AS company_id,\n" +
            "    ANY_VALUE(a.corp_id) AS corp_id,\n" +
            "    ANY_VALUE(a.is_out) AS is_out,\n" +
            "    ANY_VALUE(a.out_time) AS out_time,\n" +
            "    ANY_VALUE(a.quit_scene) AS quit_scene " +
            "FROM " +
            "  qw_group_chat_user a " +
            "  LEFT JOIN qw_external_contact b ON a.user_id = b.external_user_id " +
            "  LEFT JOIN qw_user qu on qu.qw_user_id=a.invitor " +
            "<where> " +
            "            <if test=\"chatId != null and chatId != ''\"> and a.chat_id = #{chatId}</if>\n" +
            "            <if test=\"userId != null and userId != ''\"> and a.user_id = #{userId}</if>\n" +
            "            <if test=\"type != null and type != ''\"> and a.type = #{type}</if>\n" +
            "            <if test=\"unionid != null\"> and a.unionid = #{unionid}</if>\n" +
            "            <if test=\"joinTime != null\"> and a.join_time = #{joinTime}</if>\n" +
            "            <if test=\"joinScene != null and joinScene != ''\"> and a.join_scene = #{joinScene}</if>\n" +
            "            <if test=\"invitor != null and invitor != ''\"> and a.invitor = #{invitor}</if>\n" +
            "            <if test=\"groupNickname != null and groupNickname != ''\"> and a.group_nickname like concat( #{groupNickname}, '%')</if>\n" +
            "            <if test=\"name != null and name != ''\"> and a.name like concat('%', #{name}, '%')</if>\n" +
            "            <if test=\"companyId != null\"> and a.company_id = #{companyId}</if>\n" +
            "            <if test=\"isOut != null\"> and a.is_out = #{isOut}</if>\n" +
            "            <if test=\"corpId != null and corpId != ''\"> and a.corp_id = #{corpId}</if>\n" +
            "            <if test=\"outTime != null\"> and a.out_time = #{outTime}</if>\n" +
            "            <if test=\"quitScene != null\"> and a.quit_scene = #{quitScene}</if>" +
            "</where>" +
            "GROUP BY a.user_id " +
            "</script> ")
    public List<QwGroupChatUserVO> selectQwGroupChatUserListVO(QwGroupChatUser qwGroupChatUser);

    @Select("<script> " +
            "SELECT " +
            "  DATE(join_time) AS toDay, " +
            "  COUNT(join_time) AS dailyJoinCount ," +
            "  COUNT(out_time) AS dailyOutCount " +
            "FROM " +
            "  qw_group_chat_user " +
            "WHERE " +
            "  chat_id = #{map.chatId} " +
            "  <if test='map.countType==1'>" +
            "  AND join_time BETWEEN #{map.beginTime} AND #{map.endTime}" +
            "  </if> " +
            "  <if test='map.countType==2'>" +
            "  AND join_time >= CURDATE() - INTERVAL 1 WEEK" +
            "  </if> " +
            "GROUP BY " +
            "  DATE(join_time) " +
            "ORDER BY " +
            "  DATE(join_time) ASC " +
            "</script> ")
    public List<HashMap<String,String>> getDataWeekMonthCount(@Param("map") QwGroupChatUserDataType qwGroupChatUserDataType);

    @Select("select " +
            "DATE_FORMAT(join_time,'%Y-%m') as toDay, " +
            "COUNT(join_time) AS dailyJoinCount , " +
            "COUNT(out_time) AS dailyOutCount " +
            "from qw_group_chat_user " +
            "where DATE_FORMAT(join_time,'%Y-%m')> DATE_FORMAT(date_sub(curdate(), interval 12 month),'%Y-%m') " +
            "group  by  DATE_FORMAT(join_time,'%Y-%m') " +
            "ORDER BY  " +
            "  DATE_FORMAT(join_time,'%Y-%m') asc ")
    public List<HashMap<String,String>> getDataWeekMonthCountMonth(@Param("map") QwGroupChatUserDataType qwGroupChatUserDataType);


    public int upDateDelGroupChatUser(QwGroupChatUser qwGroupChatUser);
    public int groupChatUserJoinAgain(QwGroupChatUser qwGroupChatUser);

    /**
     * 新增客户群成员列
     *
     * @param qwGroupChatUser 客户群成员列
     * @return 结果
     */
    public int insertQwGroupChatUser(QwGroupChatUser qwGroupChatUser);
    public int insertOrUpdateQwGroupChatUser(QwGroupChatUser qwGroupChatUser);

    public QwGroupChatUser selectQwGroupChatUserByExternalUserId(QwGroupChatUser qwGroupChatUser);

    /**
     * 修改客户群成员列
     *
     * @param qwGroupChatUser 客户群成员列
     * @return 结果
     */
    public int updateQwGroupChatUser(QwGroupChatUser qwGroupChatUser);

    /**
     * 删除客户群成员列
     *
     * @param chatId 客户群成员列主键
     * @return 结果
     */
    @Delete("delete from qw_group_chat_user where chat_id=#{chatId} and corp_id=#{corpId}")
    public int deleteQwGroupChatUserByIdAndCompanyId(@Param("chatId") String chatId,@Param("corpId") String corpId);

    List<QwGroupChatUser> selectQwGroupChatUserByChatIds(@Param("ids") String[] ids);

    List<QwGroupChatUser> selectUserIsChat(@Param("externalUserId") String externalUserId);

    List<QwGroupChatUser> selectByChatId(SopUserLogsInfo sopUserLogsInfo);

    List<SopUserLogsInfo> selectByIdAndRegister(SopUserLogsInfoByIdsHasUserIdParam userIdParam);
}
