<?php
//model 的重写基类

namespace app\common\models;
use app\models\Linkmenu;
use app\models\ContentModel;
use app\modules\admin\models\Sysconfig;
use app\modules\admin\models\Role;
use app\modules\ucenter\models\UserGroup;
use app\modules\ad\models\AdInd;
use app\modules\ad\models\AdSpace;
use Yii;
use yii\db\ActiveRecord;

class EActiveRecord extends ActiveRecord
{

    public static function shortTableName()
    {
        $table_name = self::tableName();
        return str_replace(array('{{%','}}'),"",$table_name);
    }

    /**
     * 获取模型验证的第一个错误
     */
    public function returnFirstError()
    {
        $errors = $this->getErrors();
        if(!empty($errors)){
            $firstError = array_splice($errors, 0,1);
            $attribute = key($firstError);
            return $this->getFirstError($attribute);
        }
        return null;
    }

    //获取最后插入ID
    public static  function getLastInsertId()
    {
        return Yii::$app->db->getLastInsertID();
    }

    /**
     * @返回主键.
     */

    public static function modelPrimaryKey()
    {
        $pkInfo = parent::primaryKey();
        return $pkInfo[0];
    }

    //禁用状态
    public static function disableOptions($k=null)
    {
        $options = array('1'=>'禁用','0'=>'启用');
        if(is_numeric($k))return $options[$k];
        return $options;
    }

    //是否选择
    public static function boolOptions($k=null)
    {
        $options = array('1'=>'是','0'=>'否');
        if(is_numeric($k))return $options[$k];
        return $options;
    }

    //返回某个表(传入表名不含前缀)的主键
    public static function getPrikey($table=null)
    {
        if($table==null)$table = self::shortTableName();
        //查询表的主键
        $dsnInfo = explode(";",Yii::$app->db->dsn);
        $dbInfo = explode("=",$dsnInfo['1']);
        //$sql = "SELECT k.column_name,k.table_name FROM information_schema.table_constraints t JOIN information_schema.key_column_usage k USING ( constraint_name, table_schema, table_name ) WHERE t.constraint_type = 'PRIMARY KEY' AND k.table_name = '".Yii::$app->db->tablePrefix.$table."' AND t.table_schema = '$dbInfo[1]'";
        $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='".Yii::$app->db->tablePrefix.$table."' AND constraint_name LIKE '%_pkey'";
        $result = Yii::$app->db->createCommand($sql)->queryOne();
        return  $result['column_name'];
    }

    //返回属性名称
    public static function getAttributeName($field)
    {
        if($field=='operate')
        {
            return Yii::t('attr',$field);
        }
        else
        {
            $table_name = self::shortTableName();
            return Yii::t('attr',$table_name.'.'.$field);
        }
    }

    //后台角色
    public static function roleOptions($k=0)
    {
        $resultList = Role::find()->where("disabled=0")->all();
        foreach($resultList as $result)
        {
            $options[$result->role_id] = $result->role_name;
        }
        if($k>0)return $options[$k];
        return $options;
    }

    //联动菜单
    public static function linkmenuOptions($k=0)
    {
        $resultList = Linkmenu::find()->where("key_id=0")->all();
        foreach($resultList as $result)
        {
            $options[$result->menu_id] = $result->name;
        }
        if($k>0)return $options[$k];
        return $options;
    }

    //系统参数选项
    public static function sysconfigOptions($name,$k=null)
    {
        $sysconfig = Sysconfig::find()->where("name='$name'")->one();
        $resultList = json_decode($sysconfig->value,true);
        if($sysconfig->type==1)
        {
            if(is_array($resultList))foreach($resultList as $id=>$name)
            {
                $options[$id] = $name;
            }
            if(!is_null($k))return $options[$k];
        }
        else
        {
            if(is_array($resultList))foreach($resultList as $name)
            {
                $options[] = $name;
            }
            if($k!='') return $k;

        }
        return $options;
    }

    //是否选择(字段管理页面)
    public static function fieldBoolOptions($k=null)
    {
        $options = array('0'=>'否','1'=>'是');
        if(is_numeric($k))return $options[$k];
        return $options;
    }


    //返回广告行业选项
    public  function indOptions($k=NULL)
    {
        $resultList = AdInd::find()->where("disabled=0")->orderBy(['list_order'=>SORT_ASC])->all();
        if(is_array($resultList))foreach($resultList as $result)
        {
            $options[$result->ind_id] = $result->name;
        }
        if($k>0)return $options[$k];
        return $options;
    }

    //返回广告位选项
    public  function adspaceOptions($k=NULL)
    {
        $resultList = AdSpace::find()->where("disabled=0")->orderBy(['list_order'=>SORT_ASC])->all();
        if(is_array($resultList))foreach($resultList as $result)
        {
            $options[$result->space_id] = $result->name."(".$result->space_no.")";
        }
        if($k>0)return $options[$k];
        return $options;
    }

    //返回用户组选项
    public static function userGroupOptions($k=NULL)
    {
        $resultList = UserGroup::find()->where("disabled=0")->orderBy(['list_order'=>SORT_ASC])->all();
        if(is_array($resultList))foreach($resultList as $result)
        {
            $options[$result->group_id] = $result->group_name;
        }
        if($k>0)return $options[$k];
        return $options;
    }

    //返回VIP类型选择
    public static function viptypeOptions($k=null,$cat=null)
    {
        if($cat==null)
        {
            $vipTypeList = \app\modules\ucenter\models\UserVipType::find()->where("status=1")->orderBy(['vip_cat'=>SORT_ASC,'list_order'=>SORT_ASC])->all();
        }
        else
        {
            $vipTypeList = \app\modules\ucenter\models\UserVipType::find()->where("vip_cat='".$cat."' and status=1")->orderBy(['list_order'=>SORT_ASC])->all();
        }

        if(is_array($vipTypeList))foreach($vipTypeList as $vipType)
        {
            $options[$vipType->id] = $vipType->title;
        }
        if($k>0)return $options[$k];
        return $options;
    }

    //返回用户等级选项
    public static function userGroupLevelOptions($group_id,$k=NULL)
    {
        $levelList = \app\modules\ucenter\models\UserGroupLevel::find()->where("user_group_id=".$group_id."")->orderBy(['id'=>SORT_ASC])->all();
        $options = [];
        if(is_array($levelList))foreach($levelList as $levelInfo)
        {
            $options[$levelInfo['id']] = $levelInfo['name'];
        }
        if($k>0)return $options[$k];
        return $options;
    }

    //返回用户模型选项
    public static function userModelOptions($k=NULL)
    {
        $resultList = ContentModel::find()->where("type=2 and disabled=0")->all();
        if(is_array($resultList))foreach($resultList as $result)
        {
            $options[$result->model_id] = $result->name;
        }
        if($k>0)return $options[$k];
        return $options;
    }

    //返回当前模型ID
    public static function getContentModelId()
    {
        $table_name = self::shortTableName();
        $contentModel = ContentModel::find()->where("table_name='".$table_name."'")->one();
        return $contentModel->model_id;
    }


    //返回标签链接
    public static function getTagLinksByModel($tags,$model_id)
    {
        if(empty($tags))return[];
        $tags = explode(",",$tags);
        if(is_array($tags))foreach($tags as $tag)
        {
            $tagModel = \app\models\Tag::find()->where("tag='".$tag."'")->one();
            if(!empty($tagModel))
            {
                $links[] = ['title'=>$tagModel->tag,'link'=>\app\common\components\SiteUrl::tagLink($tagModel->pinyin,$model_id)];
            }
        }
        return $links;
    }

    //返回内容首个标签
    public static function getFirstTag($tags)
    {
        $tags = explode(",",$tags);
        return $tags[0]?$tags[0]:'无';
    }















    //文本型选项
    public static function boxOptions($str,$k=null)
    {
        $typeList = explode("\n",$str);
        if(!empty($typeList))foreach($typeList as $_k) {
            $v = explode("|",$_k);
            if(!empty($v[0])){
                $options[trim($v[1])] = $v[0];
            }
        }
        if($k>0)return $options[$k];
        return $options;
    }

    //售后步骤
    public static function refundStepOptions($type,$k=null)
    {
        $type==1?$options = array('1'=>'申请退货退款','2'=>'卖家处理','3'=>'买家寄回商品','4'=>'买家收货','5'=>'退款完毕'):$options = array('1'=>'申请退款','2'=>'卖家处理','3'=>'退款完毕');
        if(is_numeric($k))return $options[$k];
        return $options;
    }

}