ProductController.php 4.96 KB
<?php
namespace backend\controllers;

use Yii;
use backend\components\Pagination;
use common\lib\YohoApi\Client as YohoApiClient;

/**
 * Live controller
 */
class ProductController extends BaseController
{

    public function init()
    {
        parent::init();
        
        $this->main_id = 'live';
        $this->sub_id = 'room';
        $this->sub_title = '商品管理';
    }
    
    /**
     * 房间商品列表
     * @return type
     */
    public function actionList()
    {
        if (!$room_id = Yii::$app->getRequest()->getQueryParam('id')){
            return $this->redirect($this->_refer);
        }
        
        $model = \app\models\RoomProduct::find()->where(['room_id'=>$room_id]);
        
        $count = clone $model;
        $pagination = new Pagination(['totalCount' =>$count->count()]);
        $list = $model
            ->offset($pagination->offset)->limit($pagination->limit)
            ->orderBy(['sort'=>SORT_ASC,'create_time'=>SORT_DESC])
            ->indexBy('product_id')
            ->all();
        
        /**
         * 获取有货商品信息
         */
        $productSkn = [];
        foreach ($list as $model){
            $productSkn[] = $model->product_id;
        }
        if ($productSkn){
            $ret = YohoApiClient::self()->h5ProductBatch($productSkn);
            if (empty($ret['product_list'])){
                array_shift($productSkn);
                $ret = YohoApiClient::self()->h5ProductBatch($productSkn);
            }
            
            //var_dump(YohoApiClient::self()->h5ProductBatch([51188407,51188408,51188414,51188421,51188468]));
            //var_dump(YohoApiClient::getRequestUrl());
            
            if (!empty($ret['product_list'])){
                foreach ($ret['product_list'] as $product){
                    $list[$product['product_skn']]->product_name = $product['product_name'];
                    $list[$product['product_skn']]->sales_price = $product['sales_price'];
                }
            }
        }
        
        return $this->render('list',[
            'room'=> \app\models\Room::findOne(['room_id'=>$room_id]),
            'pagination'=>$pagination,
            'list'=>$list,
        ]);
    }

    /**
     * 添加商品
     * @return type
     */
    public function actionAdd()
    {
        if (!$room_id = Yii::$app->getRequest()->getQueryParam('id')){
            return $this->redirect($this->_refer);
        }
        
        $model = new \app\models\RoomProduct;
        if (Yii::$app->getRequest()->isPost){
            $post = Yii::$app->getRequest()->post();
            $model->room_id = $room_id;
            if ($model->load($post,'') && $model->save()){
                Yii::$app->session->setFlash('success', '创建成功。');
                return $this->redirect($this->_refer);
            }else{
                Yii::$app->session->setFlash('warning', current($model->getFirstErrors()));
                $model->setAttributes($post);
            }
        }
        
        return $this->render('edit',[
            'model'=>$model,
        ]);
    }
    
    /**
     * 编辑商品
     * @return type
     */
    public function actionEdit()
    {
        if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
            return $this->redirect($this->_refer);
        }
        
        if (!$model = \app\models\RoomProduct::findOne($id)){
            Yii::$app->session->setFlash('error', '找不到该商品');
            return $this->redirect($this->_refer);
        }
        
        if (Yii::$app->getRequest()->isPost){
            $post = Yii::$app->getRequest()->post();
            if ($model->load($post,'') && $model->save()){
                Yii::$app->session->setFlash('success', '编辑成功。');
                return $this->redirect($this->_refer);
            }else{
                Yii::$app->session->setFlash('warning', current($model->getFirstErrors()));
            }
        }
        
        return $this->render('edit',[
            'model'=>$model,
        ]);
    }
    
    /**
     * 商品操作
     * @return type
     */
    public function actionOperate()
    {
        if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
            return $this->redirect($this->_refer);
        }
        if (!$type = Yii::$app->getRequest()->getQueryParam('type')){
            return $this->redirect($this->_refer);
        }
        
        if (!$model = \app\models\RoomProduct::findOne($id)){
            Yii::$app->session->setFlash('error', '找不到该商品');
            return $this->redirect($this->_refer);
        }
        
        switch ($type){
            case 'del'://删除
                $r = $model->delete();
                break;
            default:
                break;
        }
        if (!empty($r)){
            Yii::$app->session->setFlash('success', '操作成功');
        }else{
            Yii::$app->session->setFlash('error', '操作失败');
        }
        
        return $this->redirect($this->_refer);
    }
}