RecruitmentPrizeRecord.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php namespace App\Models;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 拉新活动模型
  6. *
  7. */
  8. class RecruitmentPrizeRecord extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'recruitment_prize_record';
  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. 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. if( !$result ) return $result;
  46. // 更新缓存
  47. //$this->getList(true);
  48. // 返回结果
  49. return $result;
  50. }
  51. /**
  52. * 获取列表
  53. * @param Bool $force 是否强制更新
  54. *
  55. */
  56. public function getList($force = false)
  57. {
  58. // 结果数据
  59. $list = $force ? [] : cache('admin:coupon:active:list');
  60. // 不存在数据
  61. if ( !$list ) {
  62. // 从数据库获取数据
  63. $data = $this->query()->where(['status'=>0])->get(['id','name','banner_img','active_rule','status','start_time','end_time','tag_scope','city_ids']);
  64. // 是否有数据
  65. $data = $data ? $data->toArray() : [];
  66. // 循环处理数据
  67. $list = [];
  68. // 进行更新
  69. foreach ($data as $value) {
  70. // 重组数据
  71. $list[$value['id']] = $value;
  72. }
  73. // 存起来
  74. cache(['admin:coupon:active:list'=>$list]);
  75. }
  76. // 返回结果
  77. return $list;
  78. }
  79. /**
  80. * 获取配置平台对应的应用数据
  81. *
  82. * @param Array 用户ID
  83. * @param String 指定字段
  84. *
  85. */
  86. public function getOne($id,$field='')
  87. {
  88. // 获取列表数据
  89. $list = $this->getList();
  90. // 获取数据
  91. $one = isset($list[$id]) ? $list[$id] : [];
  92. // 返回值
  93. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  94. }
  95. }