package com.ruoyi.cai.ws.handler.message;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.cai.ws.bean.FdCtxData;
import com.ruoyi.cai.ws.bean.Room;
import com.ruoyi.cai.ws.cache.UserDataCache;
import com.ruoyi.cai.ws.dto.WsRMsgGen;
import com.ruoyi.cai.ws.handler.IMessageHandler;
import com.ruoyi.cai.ws.handler.AbstractMessageHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 心跳处理
 */
@Component("heartbeat")
public class HeartbeatMessageHandler extends AbstractMessageHandle implements IMessageHandler {
    @Autowired
    private UserDataCache userDataCache;
    @Override
    public void processOn(Room room, FdCtxData fdCtxData, JSONObject map) {
        if(!room.isCanCall()){
            return;
        }
        Map<String,Object> update = new HashMap<>();
        update.put("heartTime", DateUtil.currentSeconds());
        userDataCache.hMSet(room.getRoomId(),fdCtxData.getUserType(),update);
        sendToCurrent(fdCtxData, WsRMsgGen.heartbeat());
    }
}
