|
|
@@ -0,0 +1,139 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * Created by CRMEB.
|
|
|
+ * Copyright (c) 2017~2019 http://www.crmeb.com All rights reserved.
|
|
|
+ * Author: liaofei <136327134@qq.com>
|
|
|
+ * Date: 2019/3/27 21:44
|
|
|
+ */
|
|
|
+
|
|
|
+namespace app\models\game;
|
|
|
+
|
|
|
+use app\models\store\StoreOrder;
|
|
|
+use app\models\user\User;
|
|
|
+use app\models\user\UserBill;
|
|
|
+use crmeb\traits\ModelTrait;
|
|
|
+use crmeb\basic\BaseModel;
|
|
|
+use think\Exception;
|
|
|
+use think\facade\Cache;
|
|
|
+
|
|
|
+/**
|
|
|
+ * TODO 用户消费新增金额明细 model
|
|
|
+ * Class UserBill
|
|
|
+ * @package app\models\user
|
|
|
+ */
|
|
|
+class Lottery extends BaseModel
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 数据表主键
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $pk = 'id';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 模型名称
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $name = 'lottery';
|
|
|
+
|
|
|
+ use ModelTrait;
|
|
|
+
|
|
|
+ public static function joinLottery($uid, $num, $ticket)
|
|
|
+ {
|
|
|
+ $num_max = sys_config('game_num', 6, true);
|
|
|
+ $num = (int)$num;
|
|
|
+ if (!is_int($num) || $num > $num_max || $num < 1) return self::setErrorInfo('押注号码错误');
|
|
|
+ $integral = sys_config('join_integral', 100, true);
|
|
|
+ $all_integral = bcmul($ticket, $integral, 2);
|
|
|
+ $user = User::get($uid);
|
|
|
+ if ($user['integral'] < $all_integral) return self::setErrorInfo('积分不足');
|
|
|
+ BaseModel::beginTrans();
|
|
|
+ try {
|
|
|
+ $res2 = self::create([
|
|
|
+ 'uid' => $uid,
|
|
|
+ 'ticket' => $ticket,
|
|
|
+ 'num' => $num,
|
|
|
+ 'add_time' => time(),
|
|
|
+ ]);
|
|
|
+ if ($all_integral > 0)
|
|
|
+ $res1 = User::bcDec($uid, 'integral', $all_integral, 'uid')
|
|
|
+ && UserBill::expend('参加游戏', $uid, 'integral', 'join_lottery', $all_integral, $res2->id, $user['integral'] - $all_integral, '参加游戏支付响亮积分' . $all_integral);
|
|
|
+ else $res1 = true;
|
|
|
+ $ratio = bcdiv(sys_config('game_award_lake', 5, true), 100, 4);
|
|
|
+ $res1 = $res1 && StoreOrder::addAwardLake(bcmul($all_integral, $ratio, 2), $res2->id, 'game');
|
|
|
+ if ($res1 && $res2) {
|
|
|
+ BaseModel::commitTrans();
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ BaseModel::rollbackTrans();
|
|
|
+ return self::setErrorInfo('参加失败');
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ BaseModel::rollbackTrans();
|
|
|
+ return self::setErrorInfo($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function openLottery()
|
|
|
+ {
|
|
|
+
|
|
|
+ $time_span = sys_config('open_time', 180, true);
|
|
|
+ $last_time = LotteryLog::max('time');
|
|
|
+ if ((int)$time_span + $last_time > time()) return true;
|
|
|
+
|
|
|
+ $joins = Lottery::where('status', 0)->select();
|
|
|
+ $tickets = [];
|
|
|
+ $min = PHP_INT_MAX;
|
|
|
+ $bingo = sys_config('open_num', 0, true);
|
|
|
+ $num_max = sys_config('game_num', 6, true);
|
|
|
+ if (!$bingo) {
|
|
|
+ for ($i = 1; $i <= $num_max; $i++) {
|
|
|
+ $tickets[$i] = 0;
|
|
|
+ }
|
|
|
+ foreach ($joins as $v) {
|
|
|
+ if (isset($tickets[$v['num']])) {
|
|
|
+ $tickets[$v['num']] += $v['ticket'];
|
|
|
+ } else {
|
|
|
+ $tickets[$v['num']] = $v['ticket'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $bingos = [];
|
|
|
+ foreach ($tickets as $k => $v) {
|
|
|
+ if ($v < $min) {
|
|
|
+ $min = $v;
|
|
|
+ $bingos = [$k];
|
|
|
+ }
|
|
|
+ if ($v == $min) {
|
|
|
+ $bingos[] = $k;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $bingo = $bingos[rand(0, count($bingos) - 1)];
|
|
|
+ }
|
|
|
+ $result = [$bingo];
|
|
|
+ do {
|
|
|
+ $res = rand(1, $num_max);
|
|
|
+ if (!in_array($res, $result)) {
|
|
|
+ $result = array_merge($result, [$res]);
|
|
|
+ }
|
|
|
+ } while (count($result) < $num_max);
|
|
|
+ do {
|
|
|
+ $name = date('Ymd') . substr(time(), 6, 4) . rand(1000, 9999);
|
|
|
+ } while (LotteryLog::where('name', $name)->find());
|
|
|
+ self::beginTrans();
|
|
|
+ try {
|
|
|
+ foreach ($joins as $v) {
|
|
|
+ if ($v['num'] == $bingo) {
|
|
|
+ self::where('id', $v['id'])->update(['status' => 1, 'open_time' => time(), 'name' => $name, 'result' => implode(',', $result)]);
|
|
|
+ } else {
|
|
|
+ self::where('id', $v['id'])->update(['status' => 2, 'open_time' => time(), 'name' => $name, 'result' => implode(',', $result)]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ LotteryLog::create(['time' => time(), 'result' => implode(',', $result), 'name' => $name]);
|
|
|
+ Cache::set('last_game', $name, 60);
|
|
|
+ Cache::set('last_open', $result, 60);
|
|
|
+ Cache::set('open_time', time(), 60);
|
|
|
+ return true;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return self::setErrorInfo($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|