package com.microservices.zone.task;

import com.microservices.zone.member.mapper.ZonePointsDocMapper;
import com.microservices.zone.project.mapper.ZoneProjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时取消已过期的积分置顶
 *
 * @author amypeng
 */
@Component
public class ExpiredPointsTopJob {
    private static final Logger logger = LoggerFactory.getLogger(ExpiredPointsTopJob.class);

    @Autowired
    private ZonePointsDocMapper zonePointsDocMapper;

    @Autowired
    private ZoneProjectMapper zoneProjectMapper;

    /**
     * 每日凌晨00:00执行，将 zone_points_doc、zone_project 表中
     * is_points_top 为 1 且 expiry_time 早于当前时间的数据
     * 更新为 is_points_top = 0, expiry_time = null
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void cancelExpiredPointsTop() {
        logger.info("开始执行取消过期积分置顶任务");

        try {
            int docCount = zonePointsDocMapper.updateExpiredPointsTop();
            logger.info("zone_points_doc 表取消过期置顶完成，影响行数: {}", docCount);
        } catch (Exception e) {
            logger.error("zone_points_doc 表取消过期置顶失败", e);
        }

        try {
            int projectCount = zoneProjectMapper.updateExpiredPointsTop();
            logger.info("zone_project 表取消过期置顶完成，影响行数: {}", projectCount);
        } catch (Exception e) {
            logger.error("zone_project 表取消过期置顶失败", e);
        }

        logger.info("取消过期积分置顶任务执行结束");
    }
}
