<?php

namespace app\model;


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

/**
 * @property int $organization_id
 * @property int $parent_id
 * @property string $organization_name
 * @property string $organization_full_name
 * @property string $organization_code
 * @property string $organization_type
 * @property int $leader_id
 * @property int $sort_number
 * @property string $comments
 * @property int $deleted
 * @property string $create_time
 * @property string $update_time
 * @property string $delete_time
 */
class SysOrganization extends BaseModel
{
    use SoftDelete;

    protected $name = "sys_organization";
    protected $pk = "organization_id";

    // 事件定义

    public static function onAfterInsert(Model|SysOrganization $model): void
    {
        security_log_record([SysOrganization::class, 'onAfterInsert'], "添加了新机构{$model->organization_full_name}", $model);
    }

    public static function onAfterDelete(Model|SysOrganization $model): void
    {
        security_log_record([SysOrganization::class, 'onAfterDelete'], "机构{$model->organization_full_name}被删除", $model);
    }

    // 搜索定义
    public function searchOrganizationNameAttr(Query $query, string $value): void
    {
        $value != '' && $query->where('organization_name', 'like', '%' . $value . '%');
    }

    public function searchOrganizationFullNameAttr(Query $query, string $value): void
    {
        $value != '' && $query->where('organization_full_name', 'like', '%' . $value . '%');
    }

    public function searchOrganizationCodeAttr(Query $query, string $value): void
    {
        $value != '' && $query->where('organization_code', 'like', '%' . $value . '%');
    }

    public function searchOrganizationTypeAttr(Query $query, int $value): void
    {
        $value > 0 && $query->where('organization_type', $value);
    }

    public function getOrganizationTypeNameAttr($value, $data): string
    {
        return dict_get('organization_type.' . $data['organization_type']);
    }
}