1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Models;
- use App\Models\ProjectCate;
- use App\Models\ShopService;
- use App\Enums\ProjectStatus;
- use App\Models\CoachProject;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Slowlyo\OwlAdmin\Models\BaseModel as Model;
- /**
- * 项目服务
- */
- class Project extends Model
- {
- use SoftDeletes;
- protected $table = 'project';
- protected $guarded = [];
- /**
- * @Author FelixYin
- * @description 项目服务所属分类
- */
- public function cate()
- {
- return $this->belongsTo(ProjectCate::class, 'cate_id', 'id');
- }
- /**
- * @Author FelixYin
- * @description 项目服务关联店铺服务
- */
- public function shopServices()
- {
- return $this->hasMany(ShopService::class, 'service_id', 'id');
- }
- /**
- * 验证项目是否存在且状态正常
- *
- * @param int $projectId 项目ID
- * @param string|null $message 自定义错误消息
- * @return self 返回项目模型实例
- * @throws \Illuminate\Http\Exceptions\HttpResponseException 当项目不存在或状态异常时抛出异常
- */
- public static function validateOpenProject(int $projectId, ?string $message = null): self
- {
- $project = self::where('id', $projectId)
- ->where('state', ProjectStatus::OPEN->value)
- ->first();
- abort_if(!$project, 404, $message ?? '项目不存在或已下架');
- return $project;
- }
- /**
- * 格式化项目操作结果
- *
- * @param int $state 项目状态
- * @param string|null $stateText 状态描述文本
- * @return array 格式化后的操作结果,包含:
- * - project_id: int 项目ID
- * - project_name: string 项目名称
- * - state: int 项目状态
- * - state_text: string 状态描述
- */
- public function formatActionResult(int $state, ?string $stateText = null): array
- {
- return [
- 'project_id' => $this->id,
- 'project_name' => $this->title,
- 'state' => $state,
- 'state_text' => $stateText ?? ($state === ProjectStatus::OPEN->value ? '已开通' : '已关闭'),
- ];
- }
- /**
- * @Author FelixYin
- * @description 项目所属分类
- */
- public function category()
- {
- return $this->belongsTo(ProjectCate::class, 'category_id', 'id');
- }
- /**
- * @Author FelixYin
- * @description 项目关联技师
- */
- public function coachProjects()
- {
- return $this->hasMany(CoachProject::class, 'project_id', 'id');
- }
- }
|