OrderReward.php 4.8 KB

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