package com.muyu.enterpise.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.security.service.TokenService;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.domain.message.Message;
import com.muyu.domain.message.MessageReq;
import com.muyu.domain.message.MessageSendReq;
import com.muyu.enterpise.mapper.MessageMapper;
import com.muyu.enterpise.service.MessageService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 消息服务实现类
 *  * @className: FaultLog  ️✈️
 *  * @author: Yang 鹏  🦅
 *  * @date: 2024/9/23 21:06 ⏰
 *  * @Version: 1.0
 *  * @description: 故障检测
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private MessageMapper messageMapper;
    /**
     * 消息发送
     * @param messageSendReq
     * @return
     */
    @Override
    public void sendmessage(MessageSendReq messageSendReq) {
        String token = SecurityUtils.getToken();// 获取当前Token
        LoginUser loginUser = tokenService.getLoginUser(token); // 获取当前登录用户
        if (loginUser == null) {
            throw new RuntimeException("用户未登录或Token无效");
        }
        Long userid = loginUser.getUserid();
        messageSendReq.setUserId(userid);

        // 定义一个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());
        // 现在date是一个包含了字符串中日期和时间的Date对象
        messageSendReq.setCreateTime(date);



    }


    /**
     * 消息查看
     * @param messageReq
     * @param
     * @return
     */
    @Override
    public List<Message> selectmessage(MessageReq messageReq) {
        String token = SecurityUtils.getToken();// 获取当前Token
        LoginUser loginUser = tokenService.getLoginUser(token); // 获取当前登录用户
        if (loginUser == null) {
            throw new RuntimeException("用户未登录或Token无效");
        }
        Long userid = loginUser.getUserid();
        messageReq.setUserId(userid);
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(StringUtils.isNotEmpty(String.valueOf(messageReq.getStatus())),
//                Message::getStatus,messageReq.getStatus());
//        queryWrapper.eq(StringUtils.isNotEmpty(String.valueOf(messageReq.getUserId())),
//                Message::getUserId,messageReq.getUserId());
        if (messageReq.getStatus() != null) {
            queryWrapper.eq(Message::getStatus, messageReq.getStatus());
        }
        queryWrapper.eq(Message::getUserId, messageReq.getUserId());
        List<Message> list = this.list(queryWrapper);
        return list;
    }

    /**
     * 查看消息改变状态
     * @param message
     * @return
     */
    @Override
    public void changestatus(Message message) {
        messageMapper.changestatus(message);
    }

    /**
     * 查看未读的消息
     * @return
     */
    @Override
    public List<Message> unread() {
//        return messageMapper.unread();
    return null;
    }





}
