<?php

namespace app\model;

use app\BaseModel;
use app\observer\DictionaryObserver;
use think\db\Query;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * 系统字典模型
 * @property int $dict_data_id
 * @property int $dict_id
 * @property string $dict_data_code
 * @property string $dict_data_name
 * @property int $sort_number
 * @property string $comments
 * @property int $deleted
 * @property string $create_time
 * @property string $update_time
 * @property string $delete_time
 */
class SysDictionaryData extends BaseModel
{
    use SoftDelete;

    protected $name = "sys_dictionary_data";
    protected $pk = "dict_data_id";

    protected string $eventObserver = DictionaryObserver::class;

//
//    public static function onBeforeRestore(Model|SysDictionaryData $model)
//    {
//        $model->set('deleted', 0);
//        return true;
//    }
//
//    public static function onBeforeDelete(Model|SysDictionaryData $model)
//    {
//        $model->set('deleted', 1);
//        return true;
//    }
//
//    public static function onAfterInsert(Model|SysDictionaryData $model): void
//    {
//        security_log_record([SysDictionaryData::class, 'onAfterInsert'], "添加了新字典", $model);
//    }
//
//    public static function onAfterDelete(Model|SysDictionaryData $model): void
//    {
//        security_log_record([SysDictionaryData::class, 'onAfterDelete'], "字典{$model->dict_data_name}被删除", $model);
//    }
//
//    public static function onBeforeUpdate(Model|SysDictionaryData $model): void
//    {
//        security_log_record([SysDictionaryData::class, 'onBeforeUpdate'], "修改了{$model->dict_data_name}字典", $model->getChangedDataDA());
//    }

//    public function searchDictCodeAttr($query, $value): void
//    {
//        $query->where('dict_id', function (Query $query) use ($value) {
//            return $query->table(SysDictionary::getTable())->where('dict_code', $value)->field('dict_id');
//        });
//    }


    public function searchDictDataNameAttr($query, string $value, array $data = []): void
    {
        $value != "" && $query->where('dict_data_name', 'like', '%' . $value . '%');
    }

    public function searchDictDataCodeAttr($query, string $value, array $data = []): void
    {
        $value != "" && $query->where('dict_data_code', 'like', '%' . $value . '%');
    }

    public function searchDictIdAttr($query, string|int $value, array $data = []): void
    {
        (is_numeric($value) && $value > 0) && $query->where('dict_id', $value);
    }
}