RiddleReward.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php namespace App\Models\Lottery;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\Traits\Lottery\RewardType;
  5. /**
  6. * 抽奖奖品模型
  7. *
  8. */
  9. class RiddleReward extends Model
  10. {
  11. use HasFactory,RewardType;
  12. // 与模型关联的表名
  13. protected $table = 'lottery_riddle_reward';
  14. // 是否主动维护时间戳
  15. public $timestamps = false;
  16. // 定义时间戳字段名
  17. // const CREATED_AT = 'insert_time';
  18. // const UPDATED_AT = 'update_time';
  19. /**
  20. * 添加数据
  21. *
  22. */
  23. public function add($data)
  24. {
  25. // 时间
  26. $data['insert_time'] = time();
  27. $data['update_time'] = time();
  28. // 写入数据表
  29. $id = $this->query()->insertGetId($data);
  30. // 如果操作失败
  31. if( !$id ) return $id;
  32. // 更新缓存
  33. if( isset($data['lottery_id'])) $this->getList($data['lottery_id'],true);
  34. // 返回结果
  35. return $id;
  36. }
  37. /**
  38. * 添加数据
  39. *
  40. */
  41. public function edit($id,$data)
  42. {
  43. // 更新时间
  44. $data['update_time'] = time();
  45. // 写入数据表
  46. $result = $this->query()->where(['id'=>$id])->update($data);
  47. // 如果操作失败
  48. if( !$result ) return $result;
  49. // 更新缓存
  50. if( isset($data['lottery_id'])) $this->getList($data['lottery_id'],true);
  51. // 返回结果
  52. return $result;
  53. }
  54. /**
  55. * 获取列表
  56. * @param bool $force 是否强制更新
  57. *
  58. */
  59. public function getList($lotteryId, $force = false)
  60. {
  61. // 结果数据
  62. $list = $force ? [] : cache('admin:lottery:riddle:reward:list:'.$lotteryId);
  63. // 不存在数据
  64. if ( !$list ) {
  65. // 从数据库获取数据
  66. $data = $this->query()->where([['lottery_id','=',$lotteryId],['status','=',0]])->get(['id','reward_name','reward_thumb','reward_type','reward_all','reward_total','reward_info','probability','lottery_id'])->toArray();
  67. // 循环处理数据
  68. $list = [];
  69. // 进行更新
  70. foreach ($data as $value) {
  71. // 处理图片
  72. $value['reward_thumb'] = $value['reward_thumb'] ? path_compat($value['reward_thumb']) : '';
  73. // 重组数据
  74. $list[$value['id']] = $value;
  75. }
  76. // 存起来
  77. cache(['admin:lottery:riddle:reward:list:'.$lotteryId=>$list]);
  78. }
  79. // 返回结果
  80. return $list;
  81. }
  82. /**
  83. * 获取配置平台对应的应用数据
  84. *
  85. * @param array 用户ID
  86. * @param string 指定字段
  87. *
  88. */
  89. public function getOne($lotteryId,$id,$field='')
  90. {
  91. // 获取列表数据
  92. $list = $this->getList($lotteryId);
  93. // 获取数据
  94. $one = isset($list[$id]) ? $list[$id] : [];
  95. // 返回值
  96. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  97. }
  98. /**
  99. * 获取列表
  100. * @param int $lotteryId 抽奖活动ID
  101. *
  102. */
  103. public function getListByLottery($lotteryId)
  104. {
  105. // 结果数据
  106. $list = $this->getList($lotteryId);
  107. // 查询奖品总份数
  108. $total = $list ? array_sum(array_column($list,'reward_all')) : 0;
  109. // 循环处理
  110. foreach ($list as $key=>$value) {
  111. // 中奖概率,如果不存在的话
  112. $value['probability'] = $total ? round($value['reward_all'] / $total * 100,2) : 0;
  113. // 重组
  114. $list[$key] = $value;
  115. }
  116. // 获取列表结构
  117. $list = array_values($list);
  118. // 增补一个谢谢参与
  119. if( $list ) array_unshift($list,['id'=>0,'reward_name'=>'谢谢参与','reward_thumb'=>'','reward_type'=>0,'reward_total'=>0,'reward_all'=>0,'reward_info'=>'','probability'=>0,'lottery_id'=>$lotteryId]);
  120. // 返回结果
  121. return $list;
  122. }
  123. /**
  124. * 获取抽奖结果
  125. * @param int $lotteryId 活动
  126. *
  127. */
  128. public function getRewardResult($lotteryId){
  129. // 获取奖项
  130. $reward = $this->getListByLottery($lotteryId);
  131. // 如果已经没有奖项
  132. if( !$reward ) return [];
  133. // 没有任何奖品的话,直接返回
  134. $total = array_sum(array_column($reward,'reward_all'));
  135. // 如果已经没有奖项
  136. if( !$total ) return ['reward_list'=>$reward,'index'=>0];
  137. // 计算剩余总数
  138. $total = array_sum(array_column($reward,'reward_total'));
  139. // 如果已经没有奖项
  140. if( !$total ) return ['reward_list'=>$reward,'index'=>0];
  141. // 从0开始, 包含0
  142. $offset = 0;
  143. // 随机数,包含起始值,不含结束值
  144. $randInt = random_int($offset,10000);
  145. // 中奖下标
  146. $index = -1;
  147. // 循环奖品
  148. foreach ($reward as $key => $value) {
  149. // 概率为0 或者产品份数为0,不参与
  150. if( $value['probability'] <= 0 || $value['reward_total']<= 0 ) continue;
  151. // 开始数值
  152. $start = $offset;
  153. // 结束数值
  154. $end = $value['probability'] ? $start + intval($value['probability'] * 100) : 0;
  155. // 重新计算开始数值
  156. $offset = $end ? $end : $offset;
  157. // 区间内即抽中
  158. if( $start <= $randInt && $end >= $randInt ) $index = $key;
  159. }
  160. // 如果未抽中,继续抽奖
  161. if( $index < 0 ) return $this->getRewardResult($lotteryId);
  162. // 是否中奖,以及奖项下标
  163. return ['reward_list'=>$reward,'index'=>$index];
  164. }
  165. }