ProductController.php 1.23 KB
<?php
namespace soa\controllers\v1;

use Yii;
use soa\controllers\BaseController;
use common\models\RoomProduct;
/**
 * Site controller
 */
class ProductController extends BaseController
{
    
    /**
     * 房间商品列表
     * @return type
     */
    public function actionList()
    {
        if (!$room_id = $this->requests('room_id')){
            $this->renderJson(Yii::$app->params['failed_code'],'缺少id');
        }
        
        $key = __CLASS__.__FUNCTION__.'room_id'.$room_id;
        if (!$ret = $this->cache->get($key)){
            
            $ret = RoomProduct::find()
            ->where(['room_id'=>$room_id])
            ->orderBy(['sort'=>SORT_ASC,'create_time'=>SORT_DESC])
            ->select('product_id')->asArray()->column();     
            
            $dependency = new \yii\caching\DbDependency(['sql' => Yii::$app->db
                ->createCommand('SELECT MAX(update_time),count(*) FROM {{%room_product}} WHERE room_id=:room_id')
                ->bindValue(':room_id', $room_id)
                ->getRawSql()]);
            $this->cache->set($key, $ret, Yii::$app->params['defaultCacheExpire'], $dependency);
        }
        
        $this->renderJson(Yii::$app->params['success_code'],'',$ret);
    }
    
}