package com.sonic.frog.job;


import com.sonic.common.client.JobmanClient;
import com.sonic.common.utils.LogUtils;
import com.sonic.frog.service.UserCreateCountStatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 会员每隔一月赠送用户相册创作次数
 *
 * @author mzc
 */
@Slf4j
@Component
public class MemberGiftUserCreateCountJob {

    @Autowired
    private JobmanClient jobmanClient;
    @Autowired
    private UserCreateCountStatService userCreateCountStatService;

    /**
     * 每5分钟执行一次
     */
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void memberGiftUserCreateCountJobSchedule() {
        // 锁 1 分钟过期
        jobmanClient.run("memberGiftUserCreateCountJob", TimeUnit.MINUTES.toSeconds(2), this::memberGiftUserCreateCountJob);
    }

    protected JobmanClient.JobResult memberGiftUserCreateCountJob(JobmanClient.JobContext jobContext) {
        try {
            LogUtils.setTraceId();
            log.info("===> updateAiUserChatNumJob start !!!");
            userCreateCountStatService.giftMemberNumJob();
            return JobmanClient.JobResult.success(0);
        } finally {
            LogUtils.removeTraceId();
        }
    }
}