Lottery.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Created by CRMEB.
  4. * Copyright (c) 2017~2019 http://www.crmeb.com All rights reserved.
  5. * Author: liaofei <136327134@qq.com>
  6. * Date: 2019/3/27 21:44
  7. */
  8. namespace app\models\game;
  9. use app\models\store\StoreOrder;
  10. use app\models\user\User;
  11. use app\models\user\UserBill;
  12. use app\models\user\UserLevel;
  13. use crmeb\traits\ModelTrait;
  14. use crmeb\basic\BaseModel;
  15. use think\Exception;
  16. use think\facade\Cache;
  17. /**
  18. * TODO 用户消费新增金额明细 model
  19. * Class UserBill
  20. * @package app\models\user
  21. */
  22. class Lottery extends BaseModel
  23. {
  24. /**
  25. * 数据表主键
  26. * @var string
  27. */
  28. protected $pk = 'id';
  29. /**
  30. * 模型名称
  31. * @var string
  32. */
  33. protected $name = 'lottery';
  34. use ModelTrait;
  35. public static function joinLottery($uid, $num, $ticket)
  36. {
  37. $num_max = sys_config('game_num', 6, true);
  38. $num = (int)$num;
  39. if (!is_int($num) || $num > $num_max || $num < 1) return self::setErrorInfo('押注号码错误');
  40. $integral = sys_config('join_integral', 100, true);
  41. $all_integral = bcmul($ticket, $integral, 2);
  42. $user = User::get($uid);
  43. if ($user['integral'] < $all_integral) return self::setErrorInfo('积分不足');
  44. BaseModel::beginTrans();
  45. try {
  46. $res2 = self::create([
  47. 'uid' => $uid,
  48. 'ticket' => $ticket,
  49. 'num' => $num,
  50. 'add_time' => time(),
  51. ]);
  52. if ($all_integral > 0)
  53. $res1 = User::bcDec($uid, 'integral', $all_integral, 'uid')
  54. && UserBill::expend('参加游戏', $uid, 'integral', 'join_lottery', $all_integral, $res2->id, $user['integral'] - $all_integral, '参加游戏支付响亮积分' . $all_integral);
  55. else $res1 = true;
  56. $ratio = bcdiv(sys_config('game_award_lake', 5, true), 100, 4);
  57. $res1 = $res1 && StoreOrder::addAwardLake(bcmul($all_integral, $ratio, 2), $res2->id, 'game');
  58. $res1 = $res1 && self::groupIntegral($all_integral, $res2->id, $uid);
  59. if ($res1 && $res2) {
  60. BaseModel::commitTrans();
  61. return true;
  62. } else {
  63. BaseModel::rollbackTrans();
  64. return self::setErrorInfo('参加失败');
  65. }
  66. } catch (\Exception $e) {
  67. BaseModel::rollbackTrans();
  68. return self::setErrorInfo($e->getMessage());
  69. }
  70. }
  71. public static function groupIntegral($integral, $order_id, $uid): bool
  72. {
  73. $userInfo = User::getUserInfo($uid);
  74. $spread = User::getUserInfo($userInfo['spread_uid']);
  75. $send = 0;
  76. $res = true;
  77. while ($spread) {
  78. $level = UserLevel::getUserLevelInfo(UserLevel::getUserLevel($spread['uid']));
  79. if ($level) {
  80. $integral = bcmul($integral, bcdiv($level['group_game_award'], 100, 4), 2);
  81. if ($integral > $send) {
  82. $real_send = $integral - $send;
  83. $res = false != User::bcInc($spread['uid'], 'integral', $real_send, 'uid') && UserBill::income('团队用户参加游戏赠送积分', $spread['uid'], 'integral', 'group_gain', $real_send, $order_id, $spread['integral'], '团队用户参加游戏赠送' . floatval($real_send) . '积分');
  84. $send = $integral;
  85. }
  86. }
  87. $spread = User::getUserInfo($spread['spread_uid']);
  88. }
  89. return $res;
  90. }
  91. public static function openLottery()
  92. {
  93. $time_span = sys_config('open_time', 180, true);
  94. $last_time = LotteryLog::max('time');
  95. if ((int)$time_span + $last_time > time()) return true;
  96. $joins = Lottery::where('status', 0)->select();
  97. $tickets = [];
  98. $min = PHP_INT_MAX;
  99. $bingo = sys_config('open_num', 0, true);
  100. $num_max = sys_config('game_num', 6, true);
  101. if (!$bingo) {
  102. for ($i = 1; $i <= $num_max; $i++) {
  103. $tickets[$i] = 0;
  104. }
  105. foreach ($joins as $v) {
  106. if (isset($tickets[$v['num']])) {
  107. $tickets[$v['num']] += $v['ticket'];
  108. } else {
  109. $tickets[$v['num']] = $v['ticket'];
  110. }
  111. }
  112. $bingos = [];
  113. foreach ($tickets as $k => $v) {
  114. if ($v < $min) {
  115. $min = $v;
  116. $bingos = [$k];
  117. }
  118. if ($v == $min) {
  119. $bingos[] = $k;
  120. }
  121. }
  122. $bingo = $bingos[rand(0, count($bingos) - 1)];
  123. }
  124. $result = [$bingo];
  125. do {
  126. $res = rand(1, $num_max);
  127. if (!in_array($res, $result)) {
  128. $result = array_merge($result, [$res]);
  129. }
  130. } while (count($result) < $num_max);
  131. do {
  132. $name = date('Ymd') . substr(time(), 6, 4) . rand(1000, 9999);
  133. } while (LotteryLog::where('name', $name)->find());
  134. self::beginTrans();
  135. try {
  136. foreach ($joins as $v) {
  137. if ($v['num'] == $bingo) {
  138. self::where('id', $v['id'])->update(['status' => 1, 'open_time' => time(), 'name' => $name, 'result' => implode(',', $result)]);
  139. } else {
  140. self::where('id', $v['id'])->update(['status' => 2, 'open_time' => time(), 'name' => $name, 'result' => implode(',', $result)]);
  141. }
  142. }
  143. LotteryLog::create(['time' => time(), 'result' => implode(',', $result), 'name' => $name]);
  144. Cache::set('last_game', $name, 60);
  145. Cache::set('last_open', $result, 60);
  146. Cache::set('open_time', time(), 60);
  147. self::commitTrans();
  148. return true;
  149. } catch (\Exception $e) {
  150. self::rollbackTrans();
  151. return self::setErrorInfo($e->getMessage());
  152. }
  153. }
  154. public static function dayAward()
  155. {
  156. }
  157. }