Lottery.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 crmeb\traits\ModelTrait;
  13. use crmeb\basic\BaseModel;
  14. use think\Exception;
  15. use think\facade\Cache;
  16. /**
  17. * TODO 用户消费新增金额明细 model
  18. * Class UserBill
  19. * @package app\models\user
  20. */
  21. class Lottery extends BaseModel
  22. {
  23. /**
  24. * 数据表主键
  25. * @var string
  26. */
  27. protected $pk = 'id';
  28. /**
  29. * 模型名称
  30. * @var string
  31. */
  32. protected $name = 'lottery';
  33. use ModelTrait;
  34. public static function joinLottery($uid, $num, $ticket)
  35. {
  36. $num_max = sys_config('game_num', 6, true);
  37. $num = (int)$num;
  38. if (!is_int($num) || $num > $num_max || $num < 1) return self::setErrorInfo('押注号码错误');
  39. $integral = sys_config('join_integral', 100, true);
  40. $all_integral = bcmul($ticket, $integral, 2);
  41. $user = User::get($uid);
  42. if ($user['integral'] < $all_integral) return self::setErrorInfo('积分不足');
  43. BaseModel::beginTrans();
  44. try {
  45. $res2 = self::create([
  46. 'uid' => $uid,
  47. 'ticket' => $ticket,
  48. 'num' => $num,
  49. 'add_time' => time(),
  50. ]);
  51. if ($all_integral > 0)
  52. $res1 = User::bcDec($uid, 'integral', $all_integral, 'uid')
  53. && UserBill::expend('参加游戏', $uid, 'integral', 'join_lottery', $all_integral, $res2->id, $user['integral'] - $all_integral, '参加游戏支付响亮积分' . $all_integral);
  54. else $res1 = true;
  55. $ratio = bcdiv(sys_config('game_award_lake', 5, true), 100, 4);
  56. $res1 = $res1 && StoreOrder::addAwardLake(bcmul($all_integral, $ratio, 2), $res2->id, 'game');
  57. if ($res1 && $res2) {
  58. BaseModel::commitTrans();
  59. return true;
  60. } else {
  61. BaseModel::rollbackTrans();
  62. return self::setErrorInfo('参加失败');
  63. }
  64. } catch (\Exception $e) {
  65. BaseModel::rollbackTrans();
  66. return self::setErrorInfo($e->getMessage());
  67. }
  68. }
  69. public static function openLottery()
  70. {
  71. $time_span = sys_config('open_time', 180, true);
  72. $last_time = LotteryLog::max('time');
  73. if ((int)$time_span + $last_time > time()) return true;
  74. $joins = Lottery::where('status', 0)->select();
  75. $tickets = [];
  76. $min = PHP_INT_MAX;
  77. $bingo = sys_config('open_num', 0, true);
  78. $num_max = sys_config('game_num', 6, true);
  79. if (!$bingo) {
  80. for ($i = 1; $i <= $num_max; $i++) {
  81. $tickets[$i] = 0;
  82. }
  83. foreach ($joins as $v) {
  84. if (isset($tickets[$v['num']])) {
  85. $tickets[$v['num']] += $v['ticket'];
  86. } else {
  87. $tickets[$v['num']] = $v['ticket'];
  88. }
  89. }
  90. $bingos = [];
  91. foreach ($tickets as $k => $v) {
  92. if ($v < $min) {
  93. $min = $v;
  94. $bingos = [$k];
  95. }
  96. if ($v == $min) {
  97. $bingos[] = $k;
  98. }
  99. }
  100. $bingo = $bingos[rand(0, count($bingos) - 1)];
  101. }
  102. $result = [$bingo];
  103. do {
  104. $res = rand(1, $num_max);
  105. if (!in_array($res, $result)) {
  106. $result = array_merge($result, [$res]);
  107. }
  108. } while (count($result) < $num_max);
  109. do {
  110. $name = date('Ymd') . substr(time(), 6, 4) . rand(1000, 9999);
  111. } while (LotteryLog::where('name', $name)->find());
  112. self::beginTrans();
  113. try {
  114. foreach ($joins as $v) {
  115. if ($v['num'] == $bingo) {
  116. self::where('id', $v['id'])->update(['status' => 1, 'open_time' => time(), 'name' => $name, 'result' => implode(',', $result)]);
  117. } else {
  118. self::where('id', $v['id'])->update(['status' => 2, 'open_time' => time(), 'name' => $name, 'result' => implode(',', $result)]);
  119. }
  120. }
  121. LotteryLog::create(['time' => time(), 'result' => implode(',', $result), 'name' => $name]);
  122. Cache::set('last_game', $name, 60);
  123. Cache::set('last_open', $result, 60);
  124. Cache::set('open_time', time(), 60);
  125. return true;
  126. } catch (\Exception $e) {
  127. return self::setErrorInfo($e->getMessage());
  128. }
  129. }
  130. public static function dayAward()
  131. {
  132. }
  133. }