package com.fs.qw.mapper;


import com.fs.company.param.CompanyParam;
import com.fs.qw.domain.QwMessage;
import com.fs.qw.domain.QwMessageListParam;
import com.fs.qw.domain.QwMessageListExport;

import com.fs.qw.domain.QwMessageRevoke;
import com.fs.qw.event.EventQwMessage;
import com.fs.qw.vo.QwMessageListExportVO;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 企微功能账号管理Mapper接口
 *
 * @author fs
 * @date 2023-08-23
 */
public interface QwMessageMapper
{
    /**
     * 查询企微消息
     *
     * @param id 企微消息ID
     * @return 企微消息
     */
    public QwMessage selectQwMessageById(Long id);

    /**
     * 查询企微消息列表
     *
     * @param qwMessage 企微消息
     * @return 企微消息集合
     */
    public List<QwMessage> selectQwMessageList(QwMessage qwMessage);

    /**
     * 新增企微消息
     *
     * @param qwMessage 企微消息
     * @return 结果
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertQwMessage(QwMessage qwMessage);

    /**
     * 修改企微消息
     *
     * @param qwMessage 企微消息
     * @return 结果
     */
    public int updateQwMessage(QwMessage qwMessage);

    /**
     * 删除企微消息
     *
     * @param id 企微消息ID
     * @return 结果
     */
    public int deleteQwMessageById(Long id);

    /**
     * 批量删除企微消息
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteQwMessageByIds(Long[] ids);

    QwMessage selectQwMessageByServerId(Long ServerId);


    @Select("select * from qw_message where conversation_id = #{conversationId} and is_revoked=0 order by send_time asc")
    List<QwMessage> selectQwMessageListVO(String conversationId);


    @Select({"<script> " +
            " select * from qw_message where 1=1  " +
            " and sender_id = #{senderId} and send_time=#{sendTime} and room_id = #{roomId} " +
            " order by send_time asc " +
            "</script>"})
    QwMessage selectQwMessageBySenderIdAndTime(@Param("senderId")Long senderId, @Param("sendTime")Long sendTime, @Param("roomId")Long roomId);

    @Update("update qw_message set conversation_id = #{conversationId} where id = #{id}")
    int updateConversationId(@Param("id")Long id, @Param("conversationId")String conversationId);

    @Select("select room_id,local_id,sender_id from qw_message where id = #{id}")
    QwMessageRevoke selectMessageRevokeById(Long id);

    @Select({"<script> " +
            "select * from qw_message where 1=1 " +
            " and conversation_id = #{maps.conversationId} " +
            "<if test = 'maps.msgType != null and maps.msgType==1 '> " +  //1 text  2 image  3 video  4 file 5 voice 7 image
            " and ( message_type = 2  or message_type = 3 )" +
            "</if>" +
            "<if test = 'maps.msgType != null and maps.msgType==2 '> " +
            " and message_type = 5  " +
            "</if>" +
            "<if test = 'maps.msgType != null and maps.msgType==3 '> " +
            " and message_type = 4  " +
            "</if>" +
            "<if test = 'maps.searchVal != null and  maps.searchVal !=\"\"     '> " +
            " and content like CONCAT('%',#{maps.searchVal},'%') " +
            "</if>" +
            " and is_revoked=0 " +
            " order by send_time desc" +
            "</script>"})
    List<QwMessage> selectQwMessageListByTypeVO(@Param("maps")QwMessageListParam param);


    @Select({"<script> " +
            "select * from qw_message where 1=1 " +
            " and conversation_id = #{maps.conversationId} " +
            "<if test = 'maps.pullAct != null and maps.pullAct==0 '>" +
            " and id  &lt;= #{maps.messageId} " +
            " and is_revoked=0 order by send_time desc " +
            "</if>" +
            "<if test = 'maps.pullAct != null and maps.pullAct==1 '>" +
            " and id &gt; #{maps.messageId} " +
            " and is_revoked=0 order by send_time desc " +
            "</if>" +
            "</script>"})
    List<QwMessage> selectQwMessageListContextVO(@Param("maps")QwMessageListParam param);

    @Select("select id,message_type,conversation_id,sender_id,send_time,content,url from qw_message where conversation_id = #{conversationId} order by id desc")
    List<QwMessageListExport> selectMessageListExportVO(String conversationId);


}
