package shop.alien.store.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shop.alien.entity.result.R;
import shop.alien.store.service.CommonPushTaskService;

/**
 * 推送任务定时任务回调（供 XXL-JOB 通过 Feign 调用）
 */
@Slf4j
@Api(tags = {"推送定时任务"})
@RestController
@RequestMapping("/commonPushTask/job")
@RequiredArgsConstructor
public class CommonPushTaskJobController {

    private final CommonPushTaskService commonPushTaskService;

    @ApiOperation("执行到期的定时推送任务")
    @PostMapping("/executeScheduled")
    public R<Integer> executeScheduled() {
        log.info("commonPushTask job: executeScheduled 开始");
        int count = commonPushTaskService.executeScheduledPushTasks();
        log.info("commonPushTask job: executeScheduled 结束，成功发送任务数={}", count);
        return R.data(count);
    }

    @ApiOperation("同步已发送任务的厂商推送统计数据")
    @PostMapping("/syncStatistics")
    public R<Integer> syncStatistics() {
        log.info("commonPushTask job: syncStatistics 开始");
        int count = commonPushTaskService.syncPushTaskStatistics();
        log.info("commonPushTask job: syncStatistics 结束，成功更新任务数={}", count);
        return R.data(count);
    }
}
