<?php

namespace app\common\model\school\classes;

use app\common\model\BaseModel;
use app\common\model\dyqc\ManystoreShop;
use think\Model;


class Verification extends BaseModel
{

    

    

    // 表名
    protected $name = 'school_verification';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function manystore()
    {
        return $this->belongsTo('app\admin\model\Manystore', 'manystore_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function shop()
    {
        return $this->belongsTo(ManystoreShop::class, 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }




    public function detail($id){
        $self = $this->get($id,["shop","user","manystore"]);
//        $self["shop"]->visible(['name']);
        return $self;
    }





    /**得到基础条件
     * @param $status
     * @param null $model
     * @param string $alisa
     */
    public static function getBaseWhere($whereData = [], $model = null, $alisa = '',$with = false)
    {

        if (!$model) {
            $model = new static;
            if ($alisa&&!$with) $model = $model->alias($alisa);
        }
        if ($alisa) $alisa = $alisa . '.';
        $tableFields = (new static)->getTableFields();
        foreach ($tableFields as $fields)
        {
            if(in_array($fields, ['status']))continue;
//            if (isset($whereData[$fields]) && $whereData[$fields]) $model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);

            if (isset($whereData[$fields]) && $whereData[$fields]){
                if(is_array($whereData[$fields])){
                    $model = $model->where("{$alisa}{$fields}", $whereData[$fields][0], $whereData[$fields][1]);
                }else{
                    $model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);
                }

            }


        }
        if (isset($whereData['status']) && $whereData['status']) $model = $model->where("{$alisa}status", 'in', $whereData['status']);
        if (isset($whereData['not_status']) && $whereData['not_status']) $model = $model->where("{$alisa}status", 'not in', $whereData['not_status']);


//        if (isset($whereData['keywords'])&&$whereData['keywords']) $model = $model->where("{$alisa}name|{$alisa}id", '=', $whereData['keywords']);
        if (isset($whereData['time'])&&$whereData['time']){
            $model = $model->time(["{$alisa}createtime",$whereData['time']]);
        }
        if (isset($whereData['user_id']) && $whereData['user_id']) $model = $model->where("{$alisa}user_id", '=', $whereData['user_id']);

        if (isset($whereData['shop_id']) && $whereData['shop_id']) $model = $model->where("{$alisa}shop_id", 'in', $whereData['shop_id']);


        if (isset($whereData['keywords'])&&$whereData['keywords']){

            if($with){
                $model = $model->where( function($query) use($whereData,$alisa,$with){
                    $query = $query->where("{$alisa}classes_lib_ids|{$alisa}user_id", 'like', "%". $whereData['keywords']."%");
                    if(in_array('user',$with)){
                        $query = $query->whereOr("user.nickname|user.realname|user.mobile", 'like', "%". $whereData['keywords']."%");
                    }
                    if(in_array('shop',$with)){
                        $query = $query->whereOr("shop.name|shop.address|shop.address_detail|shop.tel", 'like', "%". $whereData['keywords']."%");
                    }
                    if(in_array('manystore',$with)){
                        $query = $query->whereOr("manystore.username|manystore.nickname|manystore.email", 'like', "%". $whereData['keywords']."%");
                    }
                });
            }else{
                $model = $model->where("{$alisa}classes_lib_ids|{$alisa}user_id", 'like', "%". $whereData['keywords']."%");
            }

        }



        return $model;
    }






    public static function allList($page, $limit,$params=[]){
        $with_field = [
            'base'=>['*'],
            'user'=>['nickname',"realname","mobile","avatar"],
            'shop'=>['name','address',"address_detail","tel","logo","image"],
            'manystore'=>['username','nickname',"email","user_id","avatar"],
        ];
        $alisa = (new self)->getWithAlisaName();
        $sort = "{$alisa}.id desc";
//        if(!empty($params['status'])){
//            $params['status'] = '1';
//        }
        return (new self)->getBaseList($params, $page, $limit,$sort,$with_field);
    }



}
