PlayQuestion.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php namespace App\Models\Video;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 模型
  6. *
  7. */
  8. class PlayQuestion extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'video_play_question';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. // 定义时间戳字段名
  16. // const CREATED_AT = 'insert_time';
  17. // const UPDATED_AT = 'update_time';
  18. /**
  19. * 添加数据
  20. *
  21. */
  22. public function add($data)
  23. {
  24. // 时间
  25. $data['insert_time'] = time();
  26. $data['update_time'] = time();
  27. // 写入数据表
  28. $id = $this->query()->insertGetId($data);
  29. // 如果操作失败
  30. if( !$id ) return $id;
  31. // 更新缓存
  32. if( isset($data['course_id']) ) $this->getList($data['course_id'],true);
  33. // 返回结果
  34. return $id;
  35. }
  36. /**
  37. * 添加数据
  38. *
  39. */
  40. public function edit($id,$data)
  41. {
  42. // 更新时间
  43. $data['update_time'] = time();
  44. // 写入数据表
  45. $result = $this->query()->where(['id'=>$id])->update($data);
  46. // 如果操作失败
  47. if( !$result ) return $result;
  48. // 返回结果
  49. return $result;
  50. }
  51. /**
  52. * 获取列表
  53. * @param Bool $force 是否强制更新
  54. *
  55. */
  56. public function getList($courseId,$force = false)
  57. {
  58. // 结果数据
  59. $list = $force ? [] : cache('admin:video:play:question:list:'.$courseId);
  60. // 不存在数据
  61. if ( !$list ) {
  62. // 从数据库获取数据
  63. $data = $this->query()->where([['status','=',0]])->get(['id','course_id','question_id','play_time'])->toArray();
  64. // 循环处理数据
  65. $list = [];
  66. // 进行更新
  67. foreach ($data as $value) {
  68. // 重组数据
  69. $list[$value['course_id']][$value['question_id']] = $value;
  70. }
  71. // 存起来
  72. cache(['admin:video:play:question:list:'.$courseId=>$list]);
  73. }
  74. // 返回结果
  75. return $list;
  76. }
  77. }