You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.9 KiB

5 years ago
<?php
namespace App\Models;
use App\Scopes\AdminProjectScope;
class Paramedic extends SoftDeletesModel
{
protected $table = "paramedic";
5 years ago
public $appends = ["avatar_url", "age", "status_name"];
const STATUS_ACTIVE = "active";
const STATUS_INACTIVE = "inactive";
5 years ago
const TEXT_ACTIVE = "正常";
5 years ago
const TEXT_INACTIVE = "请假";
5 years ago
protected static function booted()
{
static::addGlobalScope(new AdminProjectScope());
}
5 years ago
public function getStatusNameAttribute()
{
5 years ago
$status_name = $this->status;
switch ($this->status) {
case self::STATUS_ACTIVE:
$status_name = self::TEXT_ACTIVE;
break;
case self::STATUS_INACTIVE:
$status_name = self::TEXT_INACTIVE;
break;
}
return $status_name;
}
5 years ago
public function toggle()
{
switch ($this->status) {
case self::STATUS_ACTIVE:
$this->update([
"status" => self::STATUS_INACTIVE
]);
break;
case self::STATUS_INACTIVE:
$this->update([
"status" => self::STATUS_ACTIVE
]);
break;
}
return $this;
}
5 years ago
public function getAvatarUrlAttribute()
{
$protocol = request()->secure() ? "https" : "http";
5 years ago
if (!$this->avatar) {
switch ($this->sex) {
case "男":
$this->avatar = "/images/male.png";
break;
case "女":
$this->avatar = "/images/female.png";
break;
}
}
5 years ago
return $this->avatar ? $protocol . "://" . request()->getHost() . $this->avatar : $this->avatar;
}
5 years ago
public function getAgeAttribute()
{
if (date("Y-m-d", strtotime($this->birthday)) == $this->birthday) {
return date("Y") - date("Y", strtotime($this->birthday));
5 years ago
}
return "";
}
5 years ago
public function scopeOfProject($query, $project_id)
{
return $query->whereIn("project_id", (array)$project_id);
}
public function project()
{
return $this->belongsTo(Project::class);
}
public function level()
{
return $this->belongsTo(ParamedicLevel::class, "paramedic_level_id", "id");
}
5 years ago
public function levelInProject()
{
return $this->hasOneThrough(ProductParamedicLevel::class, ParamedicLevel::class, "id", "paramedic_level_id", "paramedic_level_id", "id");
5 years ago
}
5 years ago
public function orders()
{
return $this->hasMany(Orders::class);
}
public function ongoingOrders()
{
return $this->hasMany(Orders::class)->where("status", Orders::STATUS_ONGOING);
}
public function orderItems()
{
return $this->hasMany(OrderItems::class);
}
}