old-hotel-new/app/Models/Oldmen.php

65 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use Carbon\Carbon;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Slowlyo\OwlAdmin\Models\BaseModel as Model;
class Oldmen extends Model
{
use Filterable;
public const STATUS_NORMAL = 0; //正常
public const STATUS_LIVE = 1; //入住
public const PAY_NORMAL = 0; //正常
public const PAY_NOTICE = 1; //需缴费
public const PAY_ARREAR = 2; //欠费
protected $table = 'oldmen';
protected $casts = [
'birthday' => 'datetime:Y-m-d',
];
protected $appends = ['age'];
protected $fillable = [
'name', 'sex', 'birthday', 'card_no', 'card_city_code', 'card_province_id', 'card_city_id', 'card_area_id', 'card_address', 'card_complete_address',
'client_name', 'client_phone', 'client_city_code', 'client_province_id', 'client_city_id', 'client_area_id', 'client_address', 'client_complete_address',
'floor_name', 'agreement_no', 'nurse_lv',
'live_in', 'live_in_at', 'avliable_at', 'need_pay', 'bonds'
];
protected static function boot()
{
parent::boot();
// 监听 oldman 的创建事件,用于初始化 位置信息
static::saving(function ($oldmen) {
if($oldmen->card_city_code){
list($cardProvince, $cardCity, $cardArea) = Zone::codeToZone($oldmen->card_city_code);
$oldmen->card_province_id = $cardProvince?->id ?? 0;
$oldmen->card_city_id = $cardCity?->id ?? 0;
$oldmen->card_area_id = $cardArea?->id ?? 0;
$oldmen->card_complete_address = ($cardProvince?->name ?? '未知').'-'.($cardCity?->name ?? '未知').'-'.($cardArea?->name ?? '未知').$oldmen->card_address;
}
if($oldmen->client_city_code){
list($clientProvince, $clientCity, $clientArea) = Zone::codeToZone($oldmen->client_city_code);
$oldmen->client_province_id = $clientProvince?->id ?? 0;
$oldmen->client_city_id = $clientCity?->id ?? 0;
$oldmen->client_area_id = $clientArea?->id ?? 0;
$oldmen->client_complete_address = ($clientProvince?->name ?? '未知').'-'.($clientCity?->name ?? '未知').'-'.($clientArea?->name ?? '未知').$oldmen->client_address;
}
});
}
protected function age():Attribute
{
return Attribute::make(
get: fn($value) => $this->birthday ? Carbon::parse($this->birthday)->diffInYears(now()).'岁' : "未知",
);
}
}