package com.yfd.platform.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.component.ServerSendEventServer;
import com.yfd.platform.config.MessageConfig;
import com.yfd.platform.config.WebConfig;
import com.yfd.platform.system.domain.Message;
import com.yfd.platform.system.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author LiMengNan
 * @Date: 2023/3/22 15:39
 * @Description:
 */
@Component
public class TaskMessage {

    @Resource
    private IMessageService messageService;

    /**********************************
     * 用途说明: 定时监控消息是否过期
     * 参数说明
     * 返回值说明: void
     ***********************************/
    public void examineMessage() {
        List<Message> list =
                messageService.list(new LambdaQueryWrapper<Message>().eq(Message::getStatus, "1"));
        for (Message message : list) {
            Timestamp createtime = message.getCreatetime();
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            long create = createtime.getTime();
            long now = timestamp.getTime();
            Integer validperiod = message.getValidperiod();
            long v = validperiod * 60 * 60 * 1000;
            if ((now - create) > v) {
                message.setStatus("9");
                message.setReadtime(timestamp);
                messageService.updateById(message);
            }

        }
    }

    /*public void sendMessage() {
        String loginToken = webConfig.loginuserCache().get("loginToken");
        long count =
                messageService.count(new LambdaQueryWrapper<Message>().eq(Message::getStatus, "1"));
        ServerSendEventServer.sendMessage(loginToken, count + "");
    }*/
}
