package com.zanxiang.game.back.serve.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zanxiang.game.back.base.ServerInfo;
import com.zanxiang.game.back.serve.config.NacosDynamicParamConfig;
import com.zanxiang.game.back.serve.pojo.entity.GameOceanengineAppCallback;
import com.zanxiang.game.back.serve.pojo.entity.GameOceanengineCallback;
import com.zanxiang.game.back.serve.pojo.entity.GameTencentAppCallback;
import com.zanxiang.game.back.serve.pojo.entity.GameTencentMiniGameCallback;
import com.zanxiang.game.back.serve.service.IGameOceanengineAppCallbackService;
import com.zanxiang.game.back.serve.service.IGameOceanengineCallbackService;
import com.zanxiang.game.back.serve.service.IGameTencentAppCallbackService;
import com.zanxiang.game.back.serve.service.IGameTencentMiniGameCallbackService;
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.time.LocalDate;

@Slf4j
@Component
public class CallbackClearTask {

    private static final String CLEAR_KEY = ServerInfo.SERVER_NAME + ":CALLBACK_DATA_CLEAR";

    @Autowired
    private NacosDynamicParamConfig nacosDynamicParamConfig;

    @Autowired
    private IGameOceanengineAppCallbackService gameOceanengineAppCallbackService;

    @Autowired
    private IGameTencentAppCallbackService gameTencentAppCallbackService;

    @Autowired
    private IGameTencentMiniGameCallbackService gameTencentMiniGameCallbackService;

    @Autowired
    private IGameOceanengineCallbackService gameOceanengineCallbackService;

    /**
     * 每天凌晨 2点清理一次 15天前的监测链接数据
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void execute() {
        log.error("开始清理监测链接的日志");
        try {
            //头条-APP
            gameOceanengineAppCallbackService.remove(new LambdaQueryWrapper<GameOceanengineAppCallback>()
                    .lt(GameOceanengineAppCallback::getDay, LocalDate.now().minusDays(nacosDynamicParamConfig.getCallbackClearDay()))
            );
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        try {
            //腾讯-APP
            gameTencentAppCallbackService.remove(new LambdaQueryWrapper<GameTencentAppCallback>()
                    .lt(GameTencentAppCallback::getDay, LocalDate.now().minusDays(nacosDynamicParamConfig.getCallbackClearDay()))
            );
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        try {
            //头条-微信小游戏
            gameOceanengineCallbackService.remove(new LambdaQueryWrapper<GameOceanengineCallback>()
                    .lt(GameOceanengineCallback::getDay, LocalDate.now().minusDays(nacosDynamicParamConfig.getCallbackClearDay()))
            );
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        try {
            //腾讯-微信小游戏
            gameTencentMiniGameCallbackService.remove(new LambdaQueryWrapper<GameTencentMiniGameCallback>()
                    .lt(GameTencentMiniGameCallback::getDay, LocalDate.now().minusDays(nacosDynamicParamConfig.getCallbackClearDay()))
            );
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
