<?php

namespace common\models;

use admin\util\AdminUtil;
use Yii;

/**
 * This is the model class for table "cms_category".
 *
 * @property integer $cms_category_id
 * @property string $category_name
 * @property integer $show_sort
 * @property integer $delete_flag
 * @property string $create_time
 * @property string $update_time
 * @property string $update_user
 */
class CmsCategory extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'cms_category';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['category_name'], 'required'],
            [['show_sort', 'delete_flag'], 'integer'],
            [['create_time', 'update_time'], 'safe'],
            [['category_name', 'update_user'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'cms_category_id' => 'Cms Category ID',
            'category_name' => 'Category Name',
            'show_sort' => 'Show Sort',
            'delete_flag' => 'Delete Flag',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'update_user' => 'Update User',
        ];
    }
    /**
     * Function Description:cms添加产品信息
     * Function Name: addCmsProd
     * @param $param
     *
     * @return bool
     *
     * @author 娄梦宁
     */
    public function addCmsProd($param){
        $transaction=Yii::$app->db->beginTransaction();
        if($param['cms_category_id']==0){
            //新增栏目
            $connection=Yii::$app->db;
            $istCount=$connection->createCommand()->insert('cms_category', [
                'update_user' => AdminUtil::$uid,
                'show_sort' => 6,
                'category_name'=>$param['cms_category_name'],
                'create_time'=>date('Y-m-d H:i:s')
            ])->execute();
            if($istCount!=1){
                $transaction->rollBack();
                return false;
            }
        };
        $values=[
            'cms_cate_id'=>$param['cms_category_id']==0 ? $connection->getLastInsertID() : $param['cms_category_id'],
            'prod_cate_id'=>$param['prod_cate_id'],
            'create_time'=>date('Y-m-d H:i:s'),
            'show_sort'=>1
        ];
        $cms_category_prod=new CmsCategoryProd();
        $cms_category_prod->attributes=$values;
        $res=$cms_category_prod->insert();
        if(!$res){
            $transaction->rollBack();
            return false;
        }
        $transaction->commit();
        return true;
    }
    /**
     * Function Description:大类型查询
     * Function Name: getCategoryArr
     *
     * @return array|\yii\db\ActiveRecord[]
     *
     * @author 娄梦宁
     */
    public function getCategoryArr(){
        $system = Yii::$app->params['system'];
        $where = [
            'and',
            ['=', 'system', $system],
            ['=', 'delete_flag', 0]
        ];
        $result=self::find()->select(['cms_category_id','category_name'])
            ->from(self::tableName())
            ->where($where)
            ->orderBy('show_sort desc')
            ->asArray()
            ->all();
        return $result;
    }
}
