<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace {{namespace_start_model}}model{{namespace_end}};

{% if stub == 'eloquent' %}
use plugin\saiadmin\basic\eloquent\BaseModel;
{% else %}
use plugin\saiadmin\basic\think\BaseModel;
{% endif %}

/**
 * {{menu_name}}模型
 *
 * {{table_name}} {{table_comment}}
 *
{% for column in columns %}
 * @property {{column.php_type}} ${{column.column_name}} {{column.column_comment}}
{% endfor %}
 */
class {{class_name}} extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
{% if stub == 'eloquent' %}
    protected $primaryKey = '{{pk}}';
{% else %}
    protected $pk = '{{pk}}';
{% endif %}

    /**
     * 数据库表名称
     * @var string
     */
    protected $table = '{{table_name}}';

{% if source != db_source and source != '' %}
    /**
     * 数据库连接
     * @var string
     */
    protected $connection = '{{source}}';

{% endif %}
{% if stub == 'eloquent' %}
    /**
     * 属性转换
     */
    protected function casts(): array
    {
        return array_merge(parent::casts(), [
{% for column in columns %}
{% if column.view_type == 'inputTag' or column.view_type == 'checkbox' %}
            '{{column.column_name}}' => 'array',
{% endif %}
{% if column.view_type == 'uploadImage' and column.options.limit > 1 %}
            '{{column.column_name}}' => 'array',
{% endif %}
{% if column.view_type == 'imagePicker' and column.options.limit > 1 %}
            '{{column.column_name}}' => 'array',
{% endif %}
{% if column.view_type == 'uploadFile' and column.options.limit > 1 %}
            '{{column.column_name}}' => 'array',
{% endif %}
{% if column.view_type == 'chunkUpload' and column.options.limit > 1 %}
            '{{column.column_name}}' => 'array',
{% endif %}
{% endfor %}
        ]);
    }
{% else %}
{% for column in columns %}
{% if column.view_type == 'inputTag' or column.view_type == 'checkbox' %}
    /**
     * {{column.column_comment}} 保存数组转换
     */
    public function set{{column.column_name | camel}}Attr($value)
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }

    /**
     * {{column.column_comment}} 读取数组转换
     */
    public function get{{column.column_name | camel}}Attr($value)
    {
        return json_decode($value ?? '', true);
    }

{% endif %}
{% if column.view_type == 'uploadImage' and column.options.limit > 1 %}
    /**
     * {{column.column_comment}} 保存数组转换
     */
    public function set{{column.column_name | camel}}Attr($value)
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }

    /**
     * {{column.column_comment}} 读取数组转换
     */
    public function get{{column.column_name | camel}}Attr($value)
    {
        return json_decode($value ?? '', true);
    }

{% endif %}
{% if column.view_type == 'imagePicker' and column.options.limit > 1 %}
    /**
     * {{column.column_comment}} 保存数组转换
     */
    public function set{{column.column_name | camel}}Attr($value)
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }

    /**
     * {{column.column_comment}} 读取数组转换
     */
    public function get{{column.column_name | camel}}Attr($value)
    {
        return json_decode($value ?? '', true);
    }

{% endif %}
{% if column.view_type == 'uploadFile' and column.options.limit > 1 %}
    /**
     * {{column.column_comment}} 保存数组转换
     */
    public function set{{column.column_name | camel}}Attr($value)
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }

    /**
     * {{column.column_comment}} 读取数组转换
     */
    public function get{{column.column_name | camel}}Attr($value)
    {
        return json_decode($value ?? '', true);
    }

{% endif %}
{% if column.view_type == 'chunkUpload' and column.options.limit > 1 %}
    /**
     * {{column.column_comment}} 保存数组转换
     */
    public function set{{column.column_name | camel}}Attr($value)
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }

    /**
     * {{column.column_comment}} 读取数组转换
     */
    public function get{{column.column_name | camel}}Attr($value)
    {
        return json_decode($value ?? '', true);
    }

{% endif %}
{% endfor %}
{% endif %}
{% for column in columns %}
{% if column.is_query == 2 and column.query_type == 'neq' %}
    /**
     * {{column.column_comment}} 搜索
     */
    public function search{{column.column_name | camel}}Attr($query, $value)
    {
        $query->where('{{column.column_name}}', '<>', $value);
    }

{% endif %}
{% if column.is_query == 2 and column.query_type == 'gt' %}
    /**
     * {{column.column_comment}} 搜索
     */
    public function search{{column.column_name | camel}}Attr($query, $value)
    {
        $query->where('{{column.column_name}}', '>', $value);
    }

{% endif %}
{% if column.is_query == 2 and column.query_type == 'gte' %}
    /**
     * {{column.column_comment}} 搜索
     */
    public function search{{column.column_name | camel}}Attr($query, $value)
    {
        $query->where('{{column.column_name}}', '>=', $value);
    }

{% endif %}
{% if column.is_query == 2 and column.query_type == 'lt' %}
    /**
     * {{column.column_comment}} 搜索
     */
    public function search{{column.column_name | camel}}Attr($query, $value)
    {
        $query->where('{{column.column_name}}', '<', $value);
    }

{% endif %}
{% if column.is_query == 2 and column.query_type == 'lte' %}
    /**
     * {{column.column_comment}} 搜索
     */
    public function search{{column.column_name | camel}}Attr($query, $value)
    {
        $query->where('{{column.column_name}}', '<=', $value);
    }

{% endif %}
{% if column.is_query == 2 and column.query_type == 'like' %}
    /**
     * {{column.column_comment}} 搜索
     */
    public function search{{column.column_name | camel}}Attr($query, $value)
    {
        $query->where('{{column.column_name}}', 'like', '%'.$value.'%');
    }

{% endif %}
{% if column.is_query == 2 and column.query_type == 'in' %}
    /**
     * {{column.column_comment}} 搜索
     */
    public function search{{column.column_name | camel}}Attr($query, $value)
    {
        $query->whereIn('{{column.column_name}}', $value);
    }

{% endif %}
{% if column.is_query == 2 and column.query_type == 'notin' %}
    /**
     * {{column.column_comment}} 搜索
     */
    public function search{{column.column_name | camel}}Attr($query, $value)
    {
        $query->whereNotIn('{{column.column_name}}', $value);
    }

{% endif %}
{% if column.is_query == 2 and column.query_type == 'between' %}
    /**
     * {{column.column_comment}} 搜索
     */
    public function search{{column.column_name | camel}}Attr($query, $value)
    {
        $query->whereBetween('{{column.column_name}}', $value);
    }

{% endif %}
{% endfor %}
{% for item in options.relations %}
{% if item.type == 'belongsTo' %}
    /**
     * 关联模型 {{item.name}}
     */
    public function {{item.name}}()
    {
        return $this->{{item.type}}({{item.model}}::class, '{{item.localKey}}', '{{item.foreignKey}}');
    }

{% endif %}
{% if item.type == 'hasOne' or item.type == 'hasMany' %}
    /**
     * 关联模型 {{item.name}}
     */
    public function {{item.name}}()
    {
        return $this->{{item.type}}({{item.model}}::class, '{{item.localKey}}', '{{item.foreignKey}}');
    }

{% endif %}
{% if item.type == 'belongsToMany' and stub == 'think' %}
    /**
     * 关联模型 {{item.name}}
     */
    public function {{item.name}}()
    {
        return $this->{{item.type}}({{item.model}}::class, {{item.table}}::class, '{{item.localKey}}', '{{item.foreignKey}}');
    }

{% endif %}
{% if item.type == 'belongsToMany' and stub == 'eloquent' %}
    /**
     * 关联模型 {{item.name}}
     */
    public function {{item.name}}()
    {
        return $this->{{item.type}}({{item.model}}::class, {{item.table}}::class, '{{item.foreignKey}}', '{{item.localKey}}');
    }

{% endif %}
{% endfor %}
}
