ActiveReward.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 ActiveReward extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'redpacket_active_reward';
  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. $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 $result;
  48. // 更新缓存
  49. $this->getList(true);
  50. // 返回结果
  51. return $result;
  52. }
  53. /**
  54. * 获取列表
  55. * @param bool $force 是否强制更新
  56. *
  57. */
  58. public function getList($force = false)
  59. {
  60. // 结果数据
  61. $list = $force ? [] : cache('admin:redpacket:active:reward:list');
  62. // 不存在数据
  63. if ( !$list ) {
  64. // 从数据库获取数据
  65. $data = $this->query()->where([['status','=',0]])->get(['id','reward_total','money','probability','active_id']);
  66. // 是否有数据
  67. $data = $data ? $data->toArray() : [];
  68. // 循环处理数据
  69. $list = [];
  70. // 进行更新
  71. foreach ($data as $value) {
  72. // 重组数据
  73. $list[$value['id']] = $value;
  74. }
  75. // 存起来
  76. cache(['admin:redpacket:active:reward:list'=>$list]);
  77. }
  78. // 返回结果
  79. return $list;
  80. }
  81. /**
  82. * 获取配置平台对应的应用数据
  83. *
  84. * @param array 用户ID
  85. * @param string 指定字段
  86. *
  87. */
  88. public function getOne($id,$field='')
  89. {
  90. // 获取列表数据
  91. $list = $this->getList();
  92. // 获取数据
  93. $one = isset($list[$id]) ? $list[$id] : [];
  94. // 返回值
  95. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  96. }
  97. /**
  98. * 获取列表
  99. * @param int $lotteryId 抽奖活动ID
  100. *
  101. */
  102. public function getListByLottery($lotteryId)
  103. {
  104. // 结果数据
  105. $data = $this->getList();
  106. // 返回结果
  107. $list = [];
  108. // 循环处理
  109. foreach ($data as $value) {
  110. // 获取数据
  111. if( $lotteryId == $value['active_id'] ) $list[] = $value;
  112. }
  113. // 追加一个谢谢参与
  114. if( $list ) array_unshift($list,['id'=>0,'reward_total'=>0,'money'=>0,'probability'=>0,'active_id'=>$lotteryId]);
  115. // 返回结果
  116. return $list;
  117. }
  118. /**
  119. * 获取抽奖结果
  120. * @param array $reward 奖品列表
  121. *
  122. */
  123. public function getRewardResult($reward){
  124. // 从0开始
  125. $offset = 0;
  126. // 随机数,包含起始值,不含结束值
  127. $randInt = random_int($offset,10000);
  128. // 中奖下标
  129. $index = 0;
  130. // 循环奖品
  131. foreach ($reward as $key => $value) {
  132. // 概率为0 或者产品份数为0,不参与
  133. if( $value['probability'] <= 0 || $value['reward_total']<= 0 ) continue;
  134. // 开始数值
  135. $start = $offset;
  136. // 结束数值
  137. $end = $value['probability'] ? $start + intval($value['probability'] * 100) : 0;
  138. // 重新计算开始数值
  139. $offset = $end ? $end : $offset;
  140. // 区间内即抽中
  141. if( $start <= $randInt && $end >= $randInt ) $index = $key;
  142. }
  143. // 是否中奖,以及奖项下标
  144. return $index;
  145. }
  146. }