Timing.php 3.4 KB
<?php

namespace app\api\controller;

use app\api\model\OrderDetail;
use app\api\model\TeamGroup;
use app\api\model\Ticket;
use app\api\model\UserTicket;
use app\api\model\User;
use app\common\controller\Api;
use think\Db;
use think\Validate;
use think\Config;


/**
 * 定时任务接口
 * @ApiInternal
 */
class Timing extends Api
{
    /**
     * @ApiTitle    (自动收货)
     * @ApiSummary  (自动收货 每天执行)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Timing/automaticReceiving)
     * @ApiReturn({
    })
     */
    public function automaticReceiving(){
        $model = new OrderDetail();
        $model->where(['status'=>3,'receivingtime'=>['<',time()]])->update(['status'=>4]);
    }

    /**
     * @ApiTitle    (修改用户会员状态)
     * @ApiSummary  (修改用户会员状态 每天执行)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Timing/user)
     * @ApiReturn({
    })
     */
    public function user(){
        $userModel = new User();
        $userModel->where(['type'=>2,'deadline'=>['<',time()]])->update(['type'=>1,'deadline'=>'']);
    }

    /**
     * @ApiTitle    (修改优惠券过期状态)
     * @ApiSummary  (修改优惠券过期状态 每天执行)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Timing/ticket)
     * @ApiReturn({
    })
     */
    public function ticket(){
        $userTicketModel = new UserTicket();
        $userTicketModel->where(['status'=>1,'pasttime'=>['<',time()]])->update(['status'=>'3']);
    }

    /**
     * @ApiTitle    (发放优惠券(会员用户))
     * @ApiSummary  (发放优惠券(会员用户)每月第一天执行)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Timing/grantVipTicket)
     * @ApiReturn({
    })
     */
    public function grantVipTicket(){
        $ticketModel = new Ticket();
        $userTicketModel = new UserTicket();
        $userModel = new User();
        $ticket = $ticketModel->where(['setting_month'=>1])->field('createtime,updatetime',true)->select();
        $userIds = $userModel->where('type',2)->column('id');
        $data = [];

        foreach ($ticket as $k => $ticketInfo){
            foreach ($userIds as $kk => $userId){
                $date = explode('-',date('Y-m-d',strtotime('+ '.$ticketInfo['days'].'day')));
                $time = mktime(23,59,00,$date[1],$date[2],$date[0]);
                $data[] = [
                    'user_id' => $userId,
                    'ticket_id' => $ticketInfo['id'],
                    'ch_name' => $ticketInfo['ch_name'],
                    'en_name' => $ticketInfo['en_name'],
                    'price' => $ticketInfo['price'],
                    'type' => 2, //获取途径:1=后台发放,2=其他
                    'status' => 1, //状态:1=未使用,2=已使用,3=已过期
                    'pasttime' => $time //过期时间
                ];
            }
        }
        $userTicketModel->saveAll($data);
    }

    /**
     * @ApiTitle    (修改拼团过期状态)
     * @ApiSummary  (修改拼团过期状态 每分钟执行)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Timing/teamGroup)
     * @ApiReturn({
    })
     */
    public function teamGroup(){

        $teamGroupModel = new TeamGroup();
        $ids = $teamGroupModel->where(['status'=>1,'grouptime'=>['<',time()]])->column('id');
        if (!$ids) return false;
        $teamGroupModel->pastDue($ids);
    }

}