<?php

namespace common\models;

use admin\util\AdminUtil;
use Yii;
use yii\base\Exception;

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

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

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'prod_cate_id' => 'Prod Cate ID',
            'cms_cate_id' => 'Cms Cate ID',
            'show_sort' => 'Show Sort',
            'delete_flag' => 'Delete Flag',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'update_user' => 'Update User',
        ];
    }

    /**
     * Function Description:修改产品与左侧菜单绑定关系
     * Function Name: uptCmsAdmin
     * @param $param
     *
     * @return bool|int
     *
     * @author 娄梦宁
     */
    public function uptCmsAdmin($param){
        $transaction = Yii::$app->db->beginTransaction();
        if($param['cms_category_id']==-1){
            //产品下架
            $res=$this->deleteAll(['=','prod_cate_id',$param['pro_cate_id']]);
            if($res===false){
                $transaction->rollBack();
                return false;
            }
        }elseif($param['cms_category_id']==0){
            //新增左侧菜单
            $connection=Yii::$app->db;
            $istCount=$connection->createCommand()->insert('cms_category', [
                'category_name'=>$param['cms_category_name'],
                'create_time'=>date('Y-m-s H:i:s'),
                'show_sort'=>6,
                'update_user'=>1
            ])->execute();
            if($istCount!=1){
                $transaction->rollBack();
                return false;
            };
            $id=Yii::$app->db->getLastInsertID();
            $istCount=$connection->createCommand()->insert('cms_category_prod', [
                'update_user' => AdminUtil::$uid,
                'show_sort' => 1000,
                'cms_cate_id'=>$id,
                'create_time'=>date('Y-m-d H:i:s'),
                'prod_cate_id'=>(int)$param['pro_cate_id'],
            ])->execute();
            if($istCount!=1){
                $transaction->rollBack();
                return false;
            }
//            $cms_category=new CmsCategory();
//            $value=[
//                'category_name'=>$param['cms_category_name'],
//                'create_time'=>date('Y-m-s H:i:s'),
//                'show_sort'=>6,
//                'update_user'=>1
//            ];
//            $cms_category->attributes=$value;
//            $res=$cms_category->insert();
//            if(!$res){
//                $transaction->rollBack();
//                return false;
//            }
        }else{
            //修改产品所属菜单
            $prod_cate_id=self::find()->select(['count(1) as cnt'])->from(self::tableName())->where(['prod_cate_id'=>$param['pro_cate_id']])->asArray()->one();
            if($prod_cate_id['cnt']!=0){//有则修改
                $count = self::updateAll(['cms_cate_id'=>$param['cms_category_id'],'update_user'=>AdminUtil::$uid], ['and',['=','prod_cate_id',$param['pro_cate_id']],['=','delete_flag',0]]);
                if($count===false){
                    return false;
                };
            }else{//没有则新增
                $connection=Yii::$app->db;
                $istCount=$connection->createCommand()->insert('cms_category_prod', [
                    'update_user' => AdminUtil::$uid,
                    'show_sort' => 1000,
                    'cms_cate_id'=>(int)$param['cms_category_id'],
                    'create_time'=>date('Y-m-d H:i:s'),
                    'prod_cate_id'=>(int)$param['pro_cate_id'],
                ])->execute();
                if($istCount!=1){
                    $transaction->rollBack();
                    return false;
                }
//                $values=[
//                    'prod_cate_id'=>(int)$param['pro_cate_id'],
//                    'cms_cate_id'=>(int)$param['cms_category_id'],
//                    'show_sort'=>1000,
//                    'create_time'=>date('Y-m-d H:i:s'),
//                    'update_user'=>AdminUtil::$uid
//                ];
//                $this->attributes=$values;
//                $res=$this->insert();
//                if(!$res){
//                    throw new Exception('添加出错');
//                    return false;
//                }
            }
        }
        $transaction->commit();
        return true;
    }

}
