Redpacket.php 2.9 KB

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