<?php

namespace crmeb\subscribes;

use app\admin\model\system\SystemAttachment;
use app\api\controller\board\UserBoardController;
use app\models\store\StoreBargainUser;
use app\models\store\StoreOrder;
use app\models\store\StorePink;
use app\models\user\UserToken;
use app\models\redis\SystemCarousel;
use crmeb\services\async\ClearanceCalc;
use crmeb\services\async\LuckyCalc;
use think\facade\Db;
use think\facade\Log;
use crmeb\services\async\WechatNotify;
use app\admin\model\system\SystemLog;

/**
 * 定时任务类
 * Class TaskSubscribe
 * @package crmeb\subscribes
 */
class TimerSubscribe
{
    public function handle()
    {

    }

    /**
     * 2秒钟执行的方法
     */
    public function onTimer_2()
    {
    }

    /**
     * 6秒钟执行的方法
     */
    public function onTimer_6()
    {
    }

    /**
     * 10秒钟执行的方法
     */
    public function onTimer_10()
    {
        WechatNotify::notify();
    }

    /**
     * 30秒钟执行的方法
     */
    public function onTimer_30()
    {
        try {
            Db::startTrans();
            StoreBargainUser::startBargainUserStatus();//批量修改砍价状态为 砍价失败
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        try {
            Db::startTrans();
            StoreOrder::orderUnpaidCancel();//订单未支付默认取消
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        try {
            Db::startTrans();
            StoreOrder::startTakeOrder();//7天自动收货
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        try {
            Db::startTrans();
            StorePink::statusPink();//拼团到期修改状态
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }

    /**
     * 60秒钟执行的方法
     */
    public function onTimer_60()
    {
        $tm = localtime(time(), true);
        
        // 缓存排行榜
        if (intval($tm['tm_min']) % 5 == 0) {
            $ctl = new UserBoardController();
            $ctl->cache_board();
            // TODO: for testing, remove line below
            // SystemCarousel::add('who will win next race?', '/pages/leader_board/index');
        }

        if (intval($tm['tm_min']) == 0) {
            // 整点做活动
            $activities = [new ClearanceCalc(), new LuckyCalc()];
            foreach($activities as $activity) {
                $activity->calc();
            }
            
            SystemCarousel::removeTrash();

            Log::warning('onTimer_60() activities calculated.');
            // 每日 24
            if (intval($tm['tm_hour']) == 0) {
                SystemLog::deleteLog();
                Log::warning('onTimer_60() remove outdated admin logs');
            }
        }
    }

    /**
     * 180秒钟执行的方法
     */
    public function onTimer_180()
    {
    }

    /**
     * 300秒钟执行的方法
     */
    public function onTimer_300()
    {
        UserToken::delToken();//删除一天前的过期token
        SystemAttachment::emptyYesterdayAttachment();//清除昨日海报
        StoreOrder::sendTen();//10分钟未付款发送通知
    }
}