package com.sonic.frog.job;


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

/**
 * 文本聊天或语音聊天超过10分钟未聊天发起扣费，生成支付流水
 *
 * @author mzc
 */
@Slf4j
@Component
public class TextOrVoiceMinute10NoChatCheckoutJob {

    @Autowired
    private JobmanClient jobmanClient;
    @Autowired
    private UserDeductionStatService userDeductionStatService;

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

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