package com.sonic.lion.job;


import com.sonic.common.client.JobmanClient;
import com.sonic.common.utils.LogUtils;
import com.sonic.lion.service.MemberService;
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;

/**
 * 用户会员每天赠送5个CrushCoin
 */
@Slf4j
@Component
public class UserMemberGiftBuffJob {

    @Autowired
    private JobmanClient jobmanClient;
    @Autowired
    private MemberService memberService;


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

    protected JobmanClient.JobResult userMemberGiftBuffJob(JobmanClient.JobContext jobContext) {
        try {
            LogUtils.setTraceId();
            log.info("===> userMemberGiftBuffJob start !!!");
            Integer count = memberService.userMemberGiftBuffJob();
            log.info("===> userMemberGiftBuffJob count : {} end !!!", 0);
            return JobmanClient.JobResult.success(count);
        } finally {
            LogUtils.removeTraceId();
        }
    }

}