@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\Database\Model\Collection;
|
||||
use Hyperf\Database\Model\Relations\HasOne;
|
||||
use Hyperf\Snowflake\Concern\Snowflake;
|
||||
|
||||
/**
|
||||
* @property int|string $doctor_pharmacy_id 主键ID
|
||||
* @property int|string $doctor_id 医生ID
|
||||
* @property int|string $pharmacy_id 药房ID
|
||||
* @property int $is_default 是否默认药房(0:否 1:是)
|
||||
* @property int $status 绑定状态(0:禁用 1:正常)
|
||||
* @property \Carbon\Carbon $created_at 创建时间
|
||||
* @property \Carbon\Carbon $updated_at 修改时间
|
||||
* @property-read Pharmacy|null $Pharmacy
|
||||
* @property-read UserDoctor|null $UserDoctor
|
||||
*/
|
||||
class DoctorPharmacy extends Model
|
||||
{
|
||||
use Snowflake;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'doctor_pharmacy';
|
||||
|
||||
/**
|
||||
* The primary key for the model.
|
||||
*/
|
||||
protected string $primaryKey = 'doctor_pharmacy_id';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [
|
||||
'doctor_pharmacy_id',
|
||||
'doctor_id',
|
||||
'pharmacy_id',
|
||||
'is_default',
|
||||
'status',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = [
|
||||
'doctor_pharmacy_id' => 'string',
|
||||
'doctor_id' => 'string',
|
||||
'pharmacy_id' => 'string',
|
||||
'is_default' => 'integer',
|
||||
'status' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* 关联药房信息表
|
||||
*/
|
||||
public function Pharmacy(): HasOne
|
||||
{
|
||||
return $this->hasOne(Pharmacy::class, 'pharmacy_id', 'pharmacy_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联医生表
|
||||
*/
|
||||
public function UserDoctor(): HasOne
|
||||
{
|
||||
return $this->hasOne(UserDoctor::class, 'doctor_id', 'doctor_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取医生当前绑定的有效默认药房(包含药房有效性及 pharmacy_code 非空校验)
|
||||
*/
|
||||
public static function getDoctorDefaultPharmacy(int|string $doctorId): ?Pharmacy
|
||||
{
|
||||
$bind = self::where([
|
||||
'doctor_id' => (string) $doctorId,
|
||||
'is_default' => 1,
|
||||
'status' => 1,
|
||||
])->first();
|
||||
|
||||
if (empty($bind)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$pharmacy = Pharmacy::getValidPharmacy($bind->pharmacy_id);
|
||||
if (empty($pharmacy) || empty($pharmacy->pharmacy_code)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $pharmacy;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单条数据
|
||||
*/
|
||||
public static function getOne(array $params, array $fields = ['*']): ?self
|
||||
{
|
||||
return self::where($params)->first($fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表数据
|
||||
*/
|
||||
public static function getList(array $params = [], array $fields = ['*']): Collection
|
||||
{
|
||||
return self::where($params)->get($fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增绑定
|
||||
*/
|
||||
public static function addDoctorPharmacy(array $data): self
|
||||
{
|
||||
return self::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
*/
|
||||
public static function edit(array $params, array $data): int
|
||||
{
|
||||
return self::where($params)->update($data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user