package thyyxxk.webserver.dao.his.socketmessage;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.socketmessage.MessageIndex;
import thyyxxk.webserver.entity.socketmessage.SocketMessage;
import thyyxxk.webserver.entity.socketmessage.SystemMessage;

import java.util.List;

@Mapper
public interface SocketMessageDao {

    @Select("select top 1 convert(varchar(10), send_datetime, 21) from " +
            "t_socket_message where sender='00000' order by send_datetime desc")
    String selectLatestDate();

    @Select("select top 1 convert(varchar(10), send_datetime, 21) from t_socket_message where sender='00000' " +
            "and send_datetime<#{date} order by send_datetime desc")
    String selectLastDate(@Param("date") String date);

    @Select("select id,type,sender,title,content,send_datetime as simpledate from " +
            "t_socket_message where sender='00000' and datediff(day, send_datetime, #{date})=0")
    List<SystemMessage> selectMessagesByDate(@Param("date") String date);


    @Select("<script>" +
            "select * from t_socket_message with(nolock) where level=1 and sender='00000' and " +
            "datediff(day, send_datetime, getdate())=0 and id not in " +
            "<foreach collection='excludes' item='itm' separator=',' open='(' close=')'>#{itm}</foreach> " +
            "</script>")
    List<SystemMessage> selectImportantSystemMessages(@Param("excludes") List<String> excludes);

    @Select("select count(1) from t_socket_message_consumer with(nolock) where receiver=#{receiver} and status=0")
    int selectUnreadCount(@Param("receiver") String receiver);

    @Select("select count(1) from t_socket_message where sender='00000'")
    int selectSystemMessageCount();

    @Select("WITH UnreadCounts AS (\n" +
            "    SELECT\n" +
            "        sender,\n" +
            "        COUNT(1) AS unreadCount\n" +
            "    FROM\n" +
            "        t_socket_message_consumer\n" +
            "    WHERE\n" +
            "        receiver = '${receiver}'\n" +
            "      AND status = 0\n" +
            "    GROUP BY\n" +
            "        sender\n" +
            ")\n" +
            "SELECT\n" +
            "    a.sender,\n" +
            "    ISNULL(uc.unreadCount, 0) AS unreadCount,\n" +
            "    (SELECT (SELECT rtrim(name) FROM zd_unit_code WHERE dept_code = zd_unit_code.code) + '-' + rtrim(name)\n" +
            "     FROM a_employee_mi WHERE code = a.sender) AS sender_name\n" +
            "FROM\n" +
            "    (SELECT DISTINCT sender FROM t_socket_message_consumer WHERE receiver = '${receiver}') a\n" +
            "        LEFT JOIN\n" +
            "    UnreadCounts uc ON uc.sender = a.sender;")
    List<MessageIndex> selectMessageIndex(@Param("receiver") String receiver);

    @Select("select * from t_socket_message where sender='00000'")
    List<SocketMessage> selectAllSystemMessages();

    @Select("select a.* , t.status " +
            "from t_socket_message_consumer t " +
            "         left join t_socket_message a on (a.id = t.msg_id) " +
            "where t.sender = #{sender} " +
            "  and t.receiver = #{receiver} " +
            "order by a.id desc;")
    List<SocketMessage> selectNeededMessages(@Param("sender") String sender, @Param("receiver") String receiver);

    @Select("select count(1) " +
            "from t_socket_message_consumer t " +
            "         left join t_socket_message a on (a.id = t.msg_id) " +
            "where t.sender = #{sender} " +
            "  and t.receiver = #{receiver};")
    int selectNeededMessagesTotal(@Param("sender") String sender, @Param("receiver") String receiver);

    @Select("select top ${pageSize} *\n" +
            "from (select row_number() over (order by send_datetime desc ) row_index, a.*, t.status\n" +
            "      from t_socket_message_consumer t\n" +
            "               left join t_socket_message a on (a.id = t.msg_id)\n" +
            "      where t.sender = #{sender}\n" +
            "        and t.receiver = #{receiver}) as table1\n" +
            "where row_index > ${pageSize} * (#{currentPage} - 1)")
    List<SocketMessage> selectNeededMessagesPage(@Param("sender") String sender,
                                                 @Param("receiver") String receiver,
                                                 @Param("currentPage") long currentPage,
                                                 @Param("pageSize") long pageSize);

    @Update("update t_socket_message_consumer set status=1 where sender=#{sender} and receiver=#{receiver}")
    void updateReadStatus(@Param("sender") String sender, @Param("receiver") String receiver);
}
