package com.muyu.server.service.message;


import com.muyu.domain.message.Message;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 业务逻辑层将负责实现消息的发送逻辑
 * @Author：weiran
 * @Package：com.muyu.cloud.faultmanage.service.message
 * @Project：cloud-server-8
 * @name：MessageService
 * @Date：2024/9/23 9:48
 */

public class MessageService {

    private MessageDao messageDao;

    public MessageService(MessageDao messageDao){
        this.messageDao=messageDao;
    }

    public void  sendMessage(String content,String sender,String receiver){

        // 定义一个DateTimeFormatter对象，用于格式化日期时间为yyyy-MM-dd HH:mm:ss
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 获取当前日期和时间
        LocalDateTime now = LocalDateTime.now();
        // 使用formatter格式化当前日期和时间
        String formattedDateTime = now.format(formatter);
        // 使用formatter将字符串解析回LocalDateTime
        LocalDateTime parsedDateTime = LocalDateTime.parse(formattedDateTime, formatter);
        // 然后按照上面的步骤将LocalDateTime转换为Date
        ZonedDateTime zdt = parsedDateTime.atZone(ZoneId.systemDefault());
        Date date = Date.from(zdt.toInstant());


        Message message = new Message();
        message.setContent(content);
        message.setSender(sender);
        message.setReceiver(receiver);
        message.setCreateTime(date);

        try {
            messageDao.sendMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
