CoachStatistic.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class CoachStatistic extends Model
  6. {
  7. use HasFactory;
  8. protected $table = 'coach_statistics';
  9. protected $fillable = [
  10. 'coach_id',
  11. 'avg_score',
  12. 'comment_count',
  13. 'good_comment_count',
  14. 'medium_comment_count',
  15. 'bad_comment_count',
  16. 'tag_statistics',
  17. ];
  18. protected $casts = [
  19. 'avg_score' => 'decimal:2',
  20. 'comment_count' => 'integer',
  21. 'good_comment_count' => 'integer',
  22. 'medium_comment_count' => 'integer',
  23. 'bad_comment_count' => 'integer',
  24. 'tag_statistics' => 'array',
  25. ];
  26. /**
  27. * 获取统计所属的技师
  28. */
  29. public function coach()
  30. {
  31. return $this->belongsTo(CoachUser::class, 'coach_id');
  32. }
  33. /**
  34. * 更新标签统计
  35. */
  36. public function updateTagStatistics(array $tagCounts): bool
  37. {
  38. return $this->update([
  39. 'tag_statistics' => $tagCounts,
  40. ]);
  41. }
  42. /**
  43. * 更新评分统计
  44. */
  45. public function updateScoreStatistics(
  46. float $avgScore,
  47. int $commentCount,
  48. int $goodCount,
  49. int $mediumCount,
  50. int $badCount
  51. ): bool {
  52. return $this->update([
  53. 'avg_score' => $avgScore,
  54. 'comment_count' => $commentCount,
  55. 'good_comment_count' => $goodCount,
  56. 'medium_comment_count' => $mediumCount,
  57. 'bad_comment_count' => $badCount,
  58. ]);
  59. }
  60. }