package com.fs.qw.mapper;

import com.fs.qw.domain.QwGroupChat;
import com.fs.qw.param.QwGroupChatParam;
import com.fs.qw.vo.QwGroupChatOptionsVO;
import com.fs.qw.vo.QwGroupChatVO;
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.List;

/**
 * 客户群详情Mapper接口
 *
 * @author fs
 * @date 2024-06-25
 */
@Repository
public interface QwGroupChatMapper
{
    /**
     * 查询客户群详情
     *
     * @param chatId 客户群详情主键
     * @return 客户群详情
     */
    public QwGroupChat selectQwGroupChatByChatId(String chatId);

    /**
     * 查询客户群详情列表
     *
     * @param qwGroupChat 客户群
     * @return 客户群详情集合
     */
    public List<QwGroupChat> selectQwGroupChatList(QwGroupChat qwGroupChat);

    @Select("<script> " +
            "SELECT " +
            "    ANY_VALUE(cu.nick_name) as ownerName , " +
            "    ANY_VALUE(qu.qw_user_name) as qwName, " +
            "    COUNT(CASE WHEN gcu.is_out = 1 AND DATE(gcu.join_time) = CURDATE() THEN 1 END) AS todayJoinByChartUser, " +
            "    COUNT(CASE WHEN gcu.is_out = 2 AND DATE(gcu.out_time) = CURDATE() THEN 1 END) AS todayOutByChartUser, " +
            "    COUNT(CASE WHEN gcu.is_out = 1 THEN gcu.chat_id END) AS groupSizeByChartUser, " +
            "    gc.* " +
            "FROM " +
            "    qw_group_chat gc " +
            "LEFT JOIN qw_group_chat_user gcu ON gc.chat_id = gcu.chat_id  " +
            " left join qw_user qu on gc.owner=qu.qw_user_id  and qu.is_del=0 and qu.corp_id=gc.corp_id " +
            " left join company_user cu on cu.qw_user_id=qu.id " +
            "    AND gc.corp_id = gcu.corp_id  " +
            "<where> " +
            "            <if test=\"map.chatId != null  and map.chatId != ''\"> and gc.chat_id like concat( #{map.chatId}, '%')</if>\n" +
            "            <if test=\"map.name != null  and map.name != ''\"> and gc.name like concat( #{map.name}, '%')</if>\n" +
            "            <if test=\"map.ownerName != null  and map.ownerName != ''\"> and cu.nick_name like concat( #{map.ownerName}, '%') </if>\n" +
            "            <if test=\"map.qwName != null  and map.qwName != ''\"> and qu.qw_user_name like concat('%', #{map.qwName}, '%') </if>\n" +
            "            <if test=\"map.notice != null  and map.notice != ''\"> and gc.notice like concat('%', #{map.notice}, '%')</if>\n" +
            "            <if test=\"map.status != null  and map.status != ''\"> and gc.status = #{map.status}</if>\n " +
            "            <if test=\"map.companyId != null \"> and gc.company_id = #{map.companyId}</if>" +
            "            <if test=\"map.companyUserId != null \"> and qu.company_user_id = #{map.companyUserId}</if>" +
            "             <if test=\"map.corpId != null  and map.corpId != ''\"> and gc.corp_id = #{map.corpId}</if>" +
            "             <if test=\"map.corpId != null  and map.corpId != ''\"> and qu.corp_id = #{map.corpId}</if>" +
            "             <if test=\"map.qwUserList != null  and map.qwUserList != ''\"> " +
            "           and gc.owner in " +
            "                   <foreach  item='item' index='index' collection='qwUserIds' open='(' separator=',' close=')'> #{item}   </foreach> " +
            "           </if>" +
            "        </where>" +
            "GROUP BY " +
            "    gc.chat_id, cu.nick_name,qu.qw_user_name   " +
            "ORDER BY gc.create_time desc " +
            "</script>")
    public List<QwGroupChatVO> selectQwGroupChatListVO(@Param("map") QwGroupChatParam qwGroupChat,@Param("qwUserIds") List<String> qwUserIds);

    /**
     * 新增客户群
     *
     * @param qwGroupChat 客户群
     * @return 结果
     */
    public int insertQwGroupChat(QwGroupChat qwGroupChat);

    public int insertOrUpdateQwGroupChat(QwGroupChat qwGroupChat);

    /**
     * 修改客户群
     *
     * @param qwGroupChat 客户群
     * @return 结果
     */
    public int updateQwGroupChat(QwGroupChat qwGroupChat);

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

    /**
     * 批量删除客户群
     *
     * @param chatIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteQwGroupChatByChatIds(String[] chatIds);

    @Select("select chat_id,name from qw_group_chat where  corp_id=#{corpId}")
    List<QwGroupChatOptionsVO> selectGroupChatOptionsVOList(String corpId);

}
