generated from liutk/owl-admin-base
99 lines
3.3 KiB
PHP
99 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
class Person extends Model
|
|
{
|
|
use HasFactory;
|
|
use Filterable;
|
|
|
|
protected $table = 'persons';
|
|
|
|
protected $appends = ['age', 'now_address'];
|
|
|
|
protected function serializeDate(\DateTimeInterface $date)
|
|
{
|
|
return $date->format('Y-m-d H:i:s');
|
|
}
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
// 监听 人口 的创建事件,用于初始化 位置信息
|
|
static::created(function ($person) {
|
|
//处理户主信息
|
|
if(empty($person->master_id) && $person->master_connect){
|
|
$person->master_id = $person->id;
|
|
$person->is_master = 1;
|
|
$person->save();
|
|
}
|
|
//处理新增时变动记录
|
|
if(in_array($person->type, [11, 12])){
|
|
PersonChange::create([
|
|
'person_id' => $person->id,
|
|
'type' => $person->type == 11 ? PersonChange::TYPE_BIRTH : PersonChange::TYPE_IN,
|
|
'changed_at' => $person->type == 11 ? $person->birthday : $person->created_at,
|
|
'new_master' => $person->master_id,
|
|
]);
|
|
}
|
|
});
|
|
static::saving(function ($person) {
|
|
if($person->origin_city_code){
|
|
list($originProvince, $originCity, $originArea) = Zone::codeToZone($person->origin_city_code);
|
|
$person->origin_province_id = $originProvince?->id ?? 0;
|
|
$person->origin_city_id = $originCity?->id ?? 0;
|
|
$person->origin_area_id = $originArea?->id ?? 0;
|
|
$person->origin_complete_address = ($originProvince?->name ?? '未知').'-'.($originCity?->name ?? '未知').'-'.($originArea?->name ?? '未知');
|
|
}
|
|
if($person->card_city_code){
|
|
list($cardProvince, $cardCity, $cardArea) = Zone::codeToZone($person->card_city_code);
|
|
$person->card_province_id = $cardProvince?->id ?? 0;
|
|
$person->card_city_id = $cardCity?->id ?? 0;
|
|
$person->card_area_id = $cardArea?->id ?? 0;
|
|
$person->card_complete_address = ($cardProvince?->name ?? '未知').'-'.($cardCity?->name ?? '未知').'-'.($cardArea?->name ?? '未知').$person->card_address;
|
|
}
|
|
});
|
|
}
|
|
|
|
public function scopeValid($q){
|
|
$q->where('valid', 1);
|
|
}
|
|
|
|
public function scopeSort($q)
|
|
{
|
|
$q->orderBy('master_id', 'asc')->orderBy('id', 'asc');
|
|
}
|
|
|
|
//户主
|
|
public function master()
|
|
{
|
|
return $this->belongsTo(static::class, 'master_id');
|
|
}
|
|
|
|
public function organizedBody()
|
|
{
|
|
return $this->belongsTo(Keyword::class, 'organized_body_id');
|
|
}
|
|
|
|
//年龄
|
|
protected function age():Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn($value) => $this->birthday ? Carbon::parse($this->birthday)->diffInYears(now()).'岁' : "未知",
|
|
);
|
|
}
|
|
|
|
protected function nowAddress(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn($value) => $this->house_complete_address ? : $this->real_address,
|
|
);
|
|
}
|
|
}
|