package cn.sikey.websocket.service;

import cn.sikey.websocket.controller.app.message.vo.SaveMessageSendReqVO;
import cn.sikey.websocket.convert.MessageConvert;
import cn.sikey.websocket.dal.dataobject.MessageSendDO;
import cn.sikey.websocket.dal.mysql.MessageSendMapper;
import cn.sikey.websocket.message.Message;
import cn.sikey.websocket.message.MessageId;
import cn.sikey.websocket.util.ObjectMapperUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author: nelson
 * @Date: 2025/7/2
 * @Description: 消息发送
 */
@Service
@Slf4j
public class MessageSendServiceImpl implements MessageSendService {

    @Resource
    private MessageSendMapper messageSendMapper;

    /**
     * 保存消息发送
     *
     * @param message 消息
     * @return
     */
    @Override
    public void saveMessageSend(Message message) {
        SaveMessageSendReqVO saveMessageSendReqVO = new SaveMessageSendReqVO();
        saveMessageSendReqVO.setId(message.getId().getId());
        saveMessageSendReqVO.setMsgType(message.getType().getValue());
        saveMessageSendReqVO.setSenderId(message.getSenderId());
        saveMessageSendReqVO.setSendTime(message.getSendTime());
        saveMessageSendReqVO.setContent(ObjectMapperUtil.convertValue(message.getContent(), ObjectMapperUtil.getValueTypeRef()));

        MessageSendDO messageSendDO = MessageConvert.INSTANCE.convertMessageSendDO(saveMessageSendReqVO);
        messageSendMapper.saveMessageSend(messageSendDO);
    }

    @Override
    public SaveMessageSendReqVO findMessageSendById(MessageId messageId) {
        MessageSendDO messageSendDO = messageSendMapper.findById(messageId);
        return MessageConvert.INSTANCE.convertSaveMessageSendReqVO(messageSendDO);
    }
}
