Lottery.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\api\model\plus\lottery;
  3. use app\common\model\plus\lottery\Lottery as LotteryModel;
  4. use app\api\model\plus\coupon\UserCoupon;
  5. use think\facade\Cache;
  6. /**
  7. *转盘模型
  8. */
  9. class Lottery extends LotteryModel
  10. {
  11. /**
  12. * 转盘详情
  13. * @param $data
  14. */
  15. public function getDetail()
  16. {
  17. $detail = self::detail();
  18. $detail['lottery_data'] = json_decode($detail['lottery_data'], 1);
  19. return $detail;
  20. }
  21. /**
  22. * 已抽奖次数
  23. * @param $data
  24. */
  25. public function getNum($user)
  26. {
  27. //抽奖次数
  28. $time = strtotime(date('Y-m-d', time()));
  29. $num = Cache::get('draw_' . $time . $user['user_id']);
  30. return $num ? $num : 0;
  31. }
  32. /**
  33. * 抽奖
  34. * @param $data
  35. */
  36. public function getdraw($user)
  37. {
  38. $this->startTrans();
  39. try {
  40. //奖品详情
  41. $detail = self::detail();
  42. if ($detail['status'] == 0) {
  43. $this->error = "抽奖未开启";
  44. return false;
  45. }
  46. if ($user['points'] < $detail['points']) {
  47. $this->error = "积分不足";
  48. return false;
  49. }
  50. $time = strtotime(date('Y-m-d', time()));
  51. //判断今日抽奖次数
  52. $num = Cache::get('draw_' . $time . $user['user_id']);
  53. if ($num && $num >= $detail['times']) {
  54. $this->error = "今日抽奖次数已用完";
  55. return false;
  56. }
  57. $LotteryPrize = new LotteryPrize;
  58. $drawArr = $LotteryPrize::detail($detail['lottery_id']);//json_decode($detail['lottery_data'], 1);
  59. //shuffle($drawArr); //打乱数组顺序
  60. $arr = [];
  61. $default = [];
  62. foreach ($drawArr as $key => $val) {
  63. if ($val['stock'] - $val['draw_num'] > 0) {
  64. $arr[$key] = $val['stock'];//概率数组
  65. }
  66. if ($val['is_default'] == 1) {
  67. $default = $val;//默认中奖项
  68. }
  69. }
  70. if ($arr) {
  71. $rid = $this->get_rand($arr); //根据概率获取奖项
  72. $result = $drawArr[$rid]; //中奖项
  73. } else {
  74. $result = $default; //默认中奖项
  75. }
  76. if (!$result) {
  77. $this->error = "礼品已抽完,请稍后再试";
  78. return false;
  79. }
  80. $arr && $LotteryPrize->where('prize_id', '=', $drawArr[$rid]['prize_id'])->inc('draw_num', 1)->update();
  81. //添加中奖记录
  82. $record = [
  83. 'record_name' => $result['name'],
  84. 'user_id' => $user['user_id'],
  85. 'prize_id' => $result['prize_id'],
  86. 'prize_type' => $result['type'],
  87. 'award_id' => $result['award_id'],
  88. 'status' => $result['type'] == 3 ? 0 : 1,
  89. 'app_id' => self::$app_id,
  90. 'points' => $result['points'],
  91. ];
  92. //更新用户积分
  93. $user->setIncPoints(-$detail['points'], '抽奖消费积分');
  94. //更新积分优惠券
  95. if (in_array($result['type'], [1, 2])) {
  96. $this->addDraw($result, $user);
  97. }
  98. (new Record)->save($record);
  99. //今日时间 记录缓存, 24小时
  100. if ($num) {
  101. Cache::inc('draw_' . $time . $user['user_id']);
  102. } else {
  103. $num = 0;
  104. Cache::set('draw_' . $time . $user['user_id'], $num + 1, 3600 * 24);
  105. }
  106. $this->commit();
  107. return $result;
  108. } catch (\Exception $e) {
  109. $this->error = $e->getMessage();
  110. $this->rollback();
  111. return false;
  112. }
  113. }
  114. //更新积分优惠券
  115. private function addDraw($data, $user)
  116. {
  117. if ($data['type'] == 1) {//优惠券
  118. $UserCouponModel = new UserCoupon;
  119. $UserCouponModel->addUserCoupon([$data['award_id']], $user);
  120. } elseif ($data['type'] == 2) {//积分
  121. $user->setIncPoints($data['points'], '抽奖获取积分');
  122. }
  123. }
  124. //计算中奖
  125. private function get_rand($proArr)
  126. {
  127. $result = '';
  128. //概率数组的总概率精度
  129. $proSum = array_sum($proArr);
  130. //概率数组循环
  131. foreach ($proArr as $key => $proCur) {
  132. $randNum = mt_rand(1, $proSum); //返回随机整数
  133. if ($randNum <= $proCur) {
  134. $result = $key;
  135. break;
  136. } else {
  137. $proSum -= $proCur;
  138. }
  139. }
  140. unset ($proArr);
  141. return $result;
  142. }
  143. }