package com.pnkx.service.impl;

import com.pnkx.common.constant.RedisConstants;
import com.pnkx.domain.po.PxChatMessage;
import com.pnkx.service.IPxChatMessageService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * PxChatServiceImpl
 *
 * @author 裴浩宇
 * @version 1.0
 * @date 2023/11/14 11:57
 * @description 描述
 */
@Service
public class PxChatMessageServiceImpl implements IPxChatMessageService {

    @Resource
    private RedisTemplate<String, List<PxChatMessage>> redisTemplate;
    /**
     * 发送消息
     *
     * @param pxChatMessage 参数
     * @return 发送结果
     */
    @Override
    public List<PxChatMessage> sendMessage(PxChatMessage pxChatMessage) {
        ValueOperations<String, List<PxChatMessage>> operations = redisTemplate.opsForValue();
        boolean hasKey = Boolean.TRUE.equals(redisTemplate.hasKey(RedisConstants.PX_CHAT_MESSAGE));
        DateTimeFormatter fmTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        pxChatMessage.setSendTime(now.format(fmTime));
        List<PxChatMessage> result = new ArrayList<>();
        if (hasKey) {
            result = operations.get(RedisConstants.PX_CHAT_MESSAGE);
        }
        if (result != null) {
            result.add(pxChatMessage);
            operations.set(RedisConstants.PX_CHAT_MESSAGE, result);
        }
        return result;
    }

    @Override
    public List<PxChatMessage> getMessageRecord() {
        ValueOperations<String, List<PxChatMessage>> operations = redisTemplate.opsForValue();
        return operations.get(RedisConstants.PX_CHAT_MESSAGE);
    }
}
