{assign var="datetime" value=$file->GetFileCreateTime()}
{assign var="namespace" value=$file->GetNamespace()}
{assign var="classes" value=$file->GetClasses()}
{assign var="uses" value=$file->GetUses()}
<?php
/**
 * Created by FFB.
 * Company: Shenzhen Zhixun Information Tec. Nanchang Com.
 * Copyright: ShenzhenZhixun
 * Email: eastbus@126.com
 * User: FFB
 * 文件不允许修改，自动生成
 */

namespace {$namespace};

{section name=index loop=$uses}
use {{$uses[index]}};
{/section}
{section name=index loop=$classes}
{assign var="className" value=$classes[index]->GetClassName()}
{assign var="tableName" value=$classes[index]->GetTable()->GetName()}
{assign var="tableNameHump" value=$classes[index]->GetTable()->GetNameForHump()}
{assign var="attrs" value=$classes[index]->GetAttrs()}
{assign var="extends" value=$classes[index]->GetExtends()}
use common\table\{{$database}}\record\{{$tableNameHump}}Record;

class {$className}{foreach item=e from=$extends key=i}{if $i == 0} extends {/if}{$e}{/foreach}{
    /**
     * @return {$tableNameHump}Record|null|array
     */
    public function one($db = null)
    {
        return parent::one($db);
    }

    /**
     * @return array|{$tableNameHump}Record[]
     */
    public function all($db = null)
    {
        return parent::all($db);
    }


    {section name=index loop=$attrs}
    {assign var="fieldName" value=$attrs[index]->GetColumn()->GetName()}
    {assign var="fieldComment" value=$attrs[index]->GetColumn()->GetComment()}
    {assign var="attrName" value=$attrs[index]->GetName()}

    /**
     * ------------ 字段 {{$fieldName}} 
     * ------------ 表字段注释: {{$fieldComment}}
     * 如果$val不是function，通过传入的值，直接组成['tableName' => $val]给到where方法
     * 如果是function，则闭包函数的类型为  function(string $tableName, ActiveQuery $activeQuery)
     * @param mixed | callable 
     */
    public function filterBy{{$attrName}}($val, $isCanEmpty = false): self
    {
        if(empty($val) && !$isCanEmpty){
            return $this;
        }
        if(is_callable($val)){
            $val("{{$fieldName}}", $this);
            return $this;
        }

        $this->andWhere(["{{$fieldName}}" => $val]);
        return $this;
    }

    public function selectBy{{$attrName}}($val = null): self
    {
        if(!empty($val) && is_callable($val)){
            $val("{{$fieldName}}", $this);
            return $this;
        }

        $this->addSelect("{{$fieldName}}");
        return $this;
    }
    // ---------------------------------------------
    {/section}

}
{/section}
