<?php
/**
 * Created by PhpStorm.
 * User: chenkuan
 * Date: 2017/12/14
 * Time: 下午4:18
 */

namespace frontend\controllers;


use backend\helpers\PaginationHelper;
use backend\helpers\ValidatorHelper;
use frontend\models\AdvantageRule;

class AdvantageRuleController extends BaseController
{
    /**
     * 获取优势广告横条
     * @return \yii\web\Response
     */
    public function actionGetRuleList()
    {
        $request = \Yii::$app->getRequest()->get();
        $data['pageNumber'] = isset($request['pageNumber']) ? (int)($request['pageNumber']) : 1;
        $data['pageSize'] = isset($request['pageSize']) ? (int)($request['pageSize']) : 20;
        $data['orderBy'] = isset($request['orderBy']) ? trim($request['orderBy']) : 'create_date';
        $data['order'] = isset($request['order']) ? trim($request['order']) : 'desc';

        $data = ValidatorHelper::validateData($data, [
            ['pageNumber', 'integer', 'min' => 1],
            ['pageSize', 'integer', 'min' => 1],
            ['orderBy', 'string'],
            ['order', 'string'],
        ], $errors);
        if ($data == false) {
            return $this->outJson(0, '', $errors);
        }

        $query = AdvantageRule::find()->orderBy($data['orderBy'].' '.$data['order']);
        $result = PaginationHelper::queryPage($query, $data['pageNumber'], $data['pageSize']);
        return $this->outJson(1, $result);
    }

    /**
     * 获取优势广告横条详情
     */
    public function actionGetRuleDetail()
    {
        $request = \Yii::$app->getRequest()->get();
        $id = isset($request['id']) ? (int)$request['id'] : '';
        $result = AdvantageRule::find()->where(['id' => $id])->asArray()->one();
        return $this->outJson(1, $result);
    }

    /**
     * 更新优势广告横条详情
     */
    public function actionUpdateRule()
    {
        $request = \Yii::$app->getRequest()->post();
        $model = AdvantageRule::findOne(['id' => $request['id']]);
        $model->modify_date = date('Y-m-d H:i:s', time());
        $model->setAttributes($request);
        if ($rs=$model->save()) {
            return $this->outJson(1, $rs);
        } else {
            return $this->outJson(0, [], '编辑失败');
        }
    }

    /**
     * 删除优势广告横条
     */
    public function actionDeleteRule()
    {
        $ids = \Yii::$app->request->post('ids');

        $rs = AdvantageRule::deleteAll(['in', 'id', $ids]);
        return $this->outJson(1, $rs);
    }
}
