ActiveReward.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if( isset($data['active_id'])) $this->getList($data['active_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. if( isset($data['active_id'])) $this->getList($data['active_id'],true);
  50. // 返回结果
  51. return $result;
  52. }
  53. /**
  54. * 获取列表
  55. * @param bool $force 是否强制更新
  56. *
  57. */
  58. public function getList($activeId,$force = false)
  59. {
  60. // 结果数据
  61. $list = $force ? [] : cache('admin:redpacket:active:reward:list:'.$activeId);
  62. // 不存在数据
  63. if ( !$list ) {
  64. // 从数据库获取数据
  65. $data = $this->query()->where([['active_id','=',$activeId],['status','=',0]])->get(['id','reward_all','reward_total','money','probability','active_id'])->toArray();
  66. // 循环处理数据
  67. $list = [];
  68. // 进行更新
  69. foreach ($data as $value) {
  70. // 重组数据
  71. $list[$value['id']] = $value;
  72. }
  73. // 存起来
  74. cache(['admin:redpacket:active:reward:list:'.$activeId=>$list]);
  75. }
  76. // 返回结果
  77. return $list;
  78. }
  79. /**
  80. * 获取配置平台对应的应用数据
  81. *
  82. * @param array 用户ID
  83. * @param string 指定字段
  84. *
  85. */
  86. public function getOne($activeId,$id,$field='')
  87. {
  88. // 获取列表数据
  89. $list = $this->getList($activeId);
  90. // 获取数据
  91. $one = isset($list[$id]) ? $list[$id] : [];
  92. // 返回值
  93. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  94. }
  95. /**
  96. * 获取列表
  97. * @param int $lotteryId 抽奖活动ID
  98. *
  99. */
  100. public function getListByLottery($lotteryId)
  101. {
  102. // 结果数据
  103. $list = $this->getList($lotteryId);
  104. // 查询奖品总份数
  105. $total = $list ? array_sum(array_column($list,'reward_all')) : 0;
  106. // 循环处理
  107. foreach ($list as $key=>$value) {
  108. // 中奖概率,如果不存在的话
  109. $value['probability'] = $total ? round($value['reward_all'] / $total * 100,2) : 0;
  110. // 重组
  111. $list[$key] = $value;
  112. }
  113. // 获取列表结构
  114. $list = array_values($list);
  115. // 追加一个谢谢参与
  116. if( $list ) array_unshift($list,['id'=>0,'reward_all'=>0,'reward_total'=>0,'money'=>0,'probability'=>0,'active_id'=>$lotteryId]);
  117. // 返回结果
  118. return $list;
  119. }
  120. /**
  121. * 获取抽奖结果
  122. * @param int $activeId 活动
  123. *
  124. */
  125. public function getRewardResult($activeId){
  126. // 获取奖项
  127. $reward = $this->getListByLottery($activeId);
  128. // 如果已经没有奖项
  129. if( !$reward ) return [];
  130. // 没有任何奖品的话,直接返回
  131. $total = array_sum(array_column($reward,'reward_all'));
  132. // 如果已经没有奖项
  133. if( !$total ) return ['reward_list'=>$reward,'index'=>0];
  134. // 计算剩余总数
  135. $total = array_sum(array_column($reward,'reward_total'));
  136. // 如果已经没有奖项
  137. if( !$total ) return ['reward_list'=>$reward,'index'=>0];
  138. // 从0开始, 包含0
  139. $offset = 0;
  140. // 随机数,包含起始值,不含结束值
  141. $randInt = random_int($offset,10000);
  142. // 中奖下标
  143. $index = -1;
  144. // 循环奖品
  145. foreach ($reward as $key => $value) {
  146. // 概率为0 或者产品份数为0,不参与
  147. if( $value['probability'] <= 0 || $value['reward_total']<= 0 ) continue;
  148. // 开始数值
  149. $start = $offset;
  150. // 结束数值
  151. $end = $value['probability'] ? $start + intval($value['probability'] * 100) : 0;
  152. // 重新计算开始数值
  153. $offset = $end ? $end : $offset;
  154. // 区间内即抽中
  155. if( $start <= $randInt && $end >= $randInt ) $index = $key;
  156. }
  157. // 如果未抽中,继续抽奖
  158. if( $index < 0 ) return $this->getRewardResult($activeId);
  159. // 是否中奖,以及奖项下标
  160. return ['reward_list'=>$reward,'index'=>$index];
  161. }
  162. }