package com.easyproject.scheduled.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.easyproject.scheduled.service.ICheckTokenSetTaskService;
import com.easyproject.service.domain.coze.CozeTokenSet;
import com.easyproject.service.mapper.coze.CozeTokenSetMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 方法说明: 检查token设置
 *
 * @param
 * @author java-101
 * @return
 * @date 2025-03-22 19:51:47
 */
@Slf4j
@Service
public class CheckTokenSetTaskServiceImpl implements ICheckTokenSetTaskService {

    @Resource
    private CozeTokenSetMapper cozeTokenSetMapper;

    /**
     * 方法说明: 定时检查【TOKEN设置-是否到期】
     *
     * @param
     * @return
     * @author java-101
     * @date 2025-03-22 19:55:23
     */
    @Override
    public void checkTokenSetIsExpirationTask() {

        Date now = DateUtil.date();
        LambdaUpdateWrapper<CozeTokenSet> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(CozeTokenSet::getTokenStatus, 2);
        updateWrapper.set(CozeTokenSet::getUpdateTime, now);
        updateWrapper.eq(CozeTokenSet::getTokenStatus, 1);
        updateWrapper.lt(CozeTokenSet::getExpirationDate, DateUtil.beginOfDay(now));
        int result = cozeTokenSetMapper.update(null, updateWrapper);
        if (result > 0) {
            log.info("【TOKEN设置-是否到期】：{}条数据已过期", result);
        } else {
            log.info("【TOKEN设置-是否到期】：无过期数据");
        }

    }
}
