Course.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Course extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'video_course';
  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. $this->getList(true);
  31. // 返回结果
  32. return $id;
  33. }
  34. /**
  35. * 添加数据
  36. *
  37. */
  38. public function edit($id,$data)
  39. {
  40. // 更新时间
  41. $data['update_time'] = time();
  42. // 写入数据表
  43. $result = $this->query()->where(['id'=>$id])->update($data);
  44. // 更新缓存
  45. $this->getList(true);
  46. // 返回结果
  47. return $result;
  48. }
  49. /**
  50. * 获取列表
  51. * @param Bool $force 是否强制更新
  52. *
  53. */
  54. public function getList($force = false)
  55. {
  56. // 结果数据
  57. $list = $force ? [] : cache('admin:video:course:list');
  58. // 不存在数据
  59. if ( !$list ) {
  60. // 从数据库获取数据
  61. $data = $this->query()->where([['status','=',0]])->get(['id','name','thumb','video_src','start_time','end_time'])->toArray();
  62. // 循环处理数据
  63. $list = [];
  64. // 进行更新
  65. foreach ($data as $value) {
  66. // 重组数据
  67. $list[$value['id']] = $value;
  68. }
  69. // 存起来
  70. cache(['admin:video:course:list'=>$list]);
  71. }
  72. // 返回结果
  73. return $list;
  74. }
  75. /**
  76. * 获取配置平台对应的应用数据
  77. *
  78. * @param Array 用户ID
  79. * @param String 指定字段
  80. *
  81. */
  82. public function getOne($id,$field='')
  83. {
  84. // 获取列表数据
  85. $list = $this->getList();
  86. dd($list);
  87. // 获取数据
  88. $one = isset($list[$id]) ? $list[$id] : [];
  89. // 返回值
  90. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  91. }
  92. }