RoomController.php 1.88 KB
<?php
namespace console\controllers;

use Yii;
use yii\console\Controller;
use common\models\Room;

/**
 * Site controller
 */
class RoomController extends Controller
{

    /**
     * 重置房间状态
     */
    public function actionStat(){
        while(true){

            //获取未开始的房间
            $result = Room::find()
                ->where(['living'=>[0,2]])
                ->orderBy(['starting_time'=>SORT_ASC])
                ->all();

            if($result){
                foreach($result as $row){
                    //状态为未开始 && 当前时间大于预告时间->标记成已结束
                    if(($row->living == 0) && (time() > $row->starting_time)){
                        $room_id = $row->room_id;
                        $row->setAttributes([
                            'living'=>2,
                            'update_time'=>time()
                        ]);
                        if($row->save()){
                            echo "room ".$room_id."has been reset to 2. \n";
                        }else{
                            echo "room ".$room_id."reset failed. \n";
                        }
                    }
                    //状态为已结束 && 当前时间小于预告时间->标记成未开始
                    if(($row->living == 2) && (time() < $row->starting_time)){
                        $room_id = $row->room_id;
                        $row->setAttributes([
                            'living'=>0,
                            'update_time'=>time()
                        ]);
                        if($row->save()){
                            echo "room ".$room_id."has been reset to 0. \n";
                        }else{
                            echo "room ".$room_id."reset failed. \n";
                        }
                    }
                }
            }
            sleep(10);
        }
        
    }
}