123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?php
- namespace App\Models;
- use App\Models\Order;
- use App\Models\Wallet;
- use App\Models\ShopInfo;
- use App\Models\CoachScore;
- use App\Models\MemberUser;
- use App\Enums\ProjectStatus;
- use App\Models\CoachProject;
- use App\Models\OrderComment;
- use App\Traits\HasStateText;
- use App\Models\CoachLocation;
- use App\Models\CoachStatistic;
- use App\Enums\TechnicianStatus;
- use App\Models\CoachInfoRecord;
- use App\Models\CoachQualRecord;
- use App\Models\CoachRealRecord;
- use App\Models\OrderGrabRecord;
- use App\Models\ShopCoachService;
- use App\Enums\TechnicianLocationType;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Slowlyo\OwlAdmin\Models\BaseModel as Model;
- use App\Models\SettingValue;
- use App\Enums\TechnicianWorkStatus;
- use Illuminate\Database\Eloquent\Casts\Attribute;
- /**
- * 技师用户模型
- */
- class CoachUser extends Model
- {
- use HasStateText, SoftDeletes;
- protected $table = 'coach_users';
- protected $guarded = [];
- protected $casts = [
- 'vip_time' => 'datetime',
- ];
- /**
- * 状态枚举类
- */
- protected string $stateEnumClass = TechnicianStatus::class;
- /**
- * 创建技师时创建钱包
- *
- * @return void
- */
- protected static function booted()
- {
- static::created(function ($coach) {
- $coach->wallet()->create([
- 'owner_type' => CoachUser::class,
- 'owner_id' => $coach->id,
- ]);
- });
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联信息
- */
- public function info()
- {
- return $this->hasOne(CoachInfoRecord::class, 'id', 'info_record_id');
- }
- /**
- * 获取技师关联的用户
- */
- public function user()
- {
- return $this->belongsTo(MemberUser::class, 'user_id');
- }
- /**
- * 获取技师的统计数据
- */
- public function statistic()
- {
- return $this->hasOne(CoachStatistic::class, 'coach_id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师所属会员
- */
- public function member()
- {
- return $this->belongsTo(MemberUser::class, 'user_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 基本信息认证记录
- */
- public function infoRecords()
- {
- return $this->hasMany(CoachInfoRecord::class, 'coach_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联资质信息
- */
- public function qual()
- {
- return $this->hasOne(CoachQualRecord::class, 'id', 'qualification_record_id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联评分
- */
- public function score()
- {
- return $this->hasOne(CoachScore::class, 'coach_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联定位
- */
- public function locations()
- {
- return $this->hasMany(CoachLocation::class, 'coach_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联订单
- */
- public function orders()
- {
- return $this->hasMany(Order::class, 'coach_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联抢单记录
- */
- public function grabRecords()
- {
- return $this->hasMany(OrderGrabRecord::class, 'coach_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联评论
- */
- public function comments()
- {
- return $this->hasMany(OrderComment::class, 'coach_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联钱包
- */
- public function wallet()
- {
- return $this->morphOne(Wallet::class, 'owner', 'owner_type', 'owner_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师所属店铺
- */
- public function shop()
- {
- return $this->belongsTo(ShopInfo::class, 'shop_id', 'shop_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联店铺开通服务
- */
- public function shopOpenService()
- {
- return $this->hasMany(ShopCoachService::class, 'coach_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联实名信息
- */
- public function real()
- {
- return $this->hasOne(CoachRealRecord::class, 'id', 'real_auth_record_id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师开通项目
- */
- public function projects()
- {
- return $this->hasMany(CoachProject::class, 'coach_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联资质记录
- */
- public function qualRecords()
- {
- return $this->hasMany(CoachQualRecord::class, 'coach_id', 'id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联实名认证记录
- */
- public function realAuthRecords()
- {
- return $this->hasMany(CoachRealRecord::class, 'coach_id', 'id');
- }
- /**
- * 验证技师状态是否正常
- *
- * @param string|null $message 自定义错误消息
- * @throws \Illuminate\Http\Exceptions\HttpResponseException 当技师状态异常时抛出异常
- */
- public function validateActiveStatus(?string $message = null): void
- {
- abort_if(
- $this->state != TechnicianStatus::ACTIVE->value,
- 422,
- $message ?? '技师状态异常'
- );
- }
- /**
- * 创建或更新技师项目关联
- *
- * @param int $projectId 项目ID
- * @param int $state 项目状态
- * @return CoachProject 返回创建或更新的技师项目关联实例
- */
- public function updateOrCreateProjectRelation(int $projectId, int $state): CoachProject
- {
- return $this->projects()->updateOrCreate(
- ['project_id' => $projectId],
- [
- 'state' => $state,
- 'discount_amount' => 0.00, // 默认折扣金额
- 'service_gender' => 0, // 默认服务性别(不限)
- 'service_distance' => 0, // 默认服务距离
- 'traffic_fee_type' => 2, // 默认交通费类型(双程)
- ]
- );
- }
- /**
- * 获取最新的基本信息审核记录
- * 通过 latest() 获取最新的一条记录
- *
- * 业务逻辑:
- * 1. 建立与基本信息记录表的一对一关联
- * 2. 按创建时间倒序排序
- * 3. 获取最新的一条记录
- *
- * @return \Illuminate\Database\Eloquent\Relations\HasOne
- */
- public function latestInfoRecord()
- {
- // 一对一关联,并按创建时间倒序获取最新记录
- return $this->hasOne(CoachInfoRecord::class, 'coach_id')
- ->latest();
- }
- /**
- * 获取最新的实名认证审核记录
- * 通过 latest() 获取最新的一条记录
- *
- * 业务逻辑:
- * 1. 建立与实名认证记录表的一对一关联
- * 2. 按创建时间倒序排序
- * 3. 获取最新的一条记录
- *
- * @return \Illuminate\Database\Eloquent\Relations\HasOne
- */
- public function latestRealRecord()
- {
- // 一对一关联,并按创建时间倒序获取最新记录
- return $this->hasOne(CoachRealRecord::class, 'coach_id')
- ->latest();
- }
- /**
- * 获取最新的资质认证审核记录
- * 通过 latest() 获取最新的一条记录
- *
- * @return \Illuminate\Database\Eloquent\Relations\HasOne
- */
- public function latestQualRecord()
- {
- // 一对一关联,并按创建时间倒序获取最新记录
- return $this->hasOne(CoachQualRecord::class, 'coach_id')
- ->latest();
- }
- /**
- * 获取常用位置
- * 只获取类型为常用位置的记录
- *
- * @return \Illuminate\Database\Eloquent\Relations\HasOne
- */
- public function commonLocation()
- {
- // 一对一关联,并限定位置类型为常用位置
- return $this->hasOne(CoachLocation::class, 'coach_id')
- ->where('type', TechnicianLocationType::COMMON->value);
- }
- /**
- * 获取技师统计数据
- * 包含订单数、评分等统计信息
- *
- * @return \Illuminate\Database\Eloquent\Relations\HasOne
- */
- public function statistics()
- {
- // 一对一关联技师统计数据
- return $this->hasOne(CoachStatistic::class, 'coach_id');
- }
- /**
- * @Author FelixYin
- *
- * @description 技师关联设置
- */
- public function settings()
- {
- return $this->morphMany(SettingValue::class, 'object');
- }
- /**
- * 获取工作状态的文字说明
- */
- protected function workStatusText(): Attribute
- {
- return Attribute::make(
- get: function ($value, array $attributes) {
- if (!isset($attributes['work_status'])) {
- return '';
- }
- $status = TechnicianWorkStatus::tryFrom($attributes['work_status']);
- return $status ? $status->label() : '';
- }
- );
- }
- }
|