Project.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Models\Service;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. class Project extends Model
  7. {
  8. use HasFactory;
  9. protected $table = 'service_project';
  10. protected $guarded = [];
  11. protected $appends = [];
  12. protected $casts = [
  13. 'isAdd' => 'bool',
  14. 'isStore' => 'bool',
  15. 'isDoor' => 'bool'
  16. ];
  17. public function getPriceAttribute($value)
  18. {
  19. return number_format($value / 100, 2,'.','');
  20. }
  21. public function getInitPriceAttribute($value)
  22. {
  23. return number_format($value / 100, 2,'.','');
  24. }
  25. public function getMaterialPriceAttribute($value)
  26. {
  27. return number_format($value / 100, 2,'.','');
  28. }
  29. public function getCoachProjectStatus($coach_id)
  30. {
  31. $status = DB::table('service_project_has_coach')->where('project_id', $this->attributes['id'])
  32. ->where('coach_id', $coach_id)->value('status');
  33. return $status ?: 0;
  34. }
  35. public function updateCoachProjectStatus(int $coach_id, int $status): void
  36. {
  37. $model = DB::table('service_project_has_coach');
  38. $project_id = $model->where('project_id', $this->attributes['id'])
  39. ->where('coach_id', $coach_id)->value('id');
  40. if($project_id)
  41. $model->where('id',$project_id)->update(['status' => $status]);
  42. else
  43. $model->insert(['project_id' => $this->attributes['id'], 'coach_id' => $coach_id, 'status' => $status]);
  44. }
  45. // 关联服务项目所属分类
  46. public function saveCategory(array $category_ids)
  47. {
  48. $data = collect($category_ids)->map(fn($category_id) => ['project_id' => $this->attributes['id'], 'category_id' => $category_id]);
  49. DB::table('service_project_has_category')->insert($data->all());
  50. }
  51. // 获取服务项目所属分类
  52. public function getCategory()
  53. {
  54. return DB::table('service_project_has_category')->where('project_id', $this->attributes['id'])->pluck('category_id');
  55. }
  56. public function deleteCategory()
  57. {
  58. return DB::table('service_project_has_category')->where('project_id', $this->attributes['id'])->delete();
  59. }
  60. // public function hasCoach()
  61. // {
  62. // return $this->hasOne(\App\Models\Coach\User::class,'user_id');
  63. //
  64. // }
  65. }