Pārlūkot izejas kodu

开箱抽奖优化

lizhen_gitee 1 gadu atpakaļ
vecāks
revīzija
8321ba6138
1 mainītis faili ar 17 papildinājumiem un 19 dzēšanām
  1. 17 19
      application/api/controller/Eggnew.php

+ 17 - 19
application/api/controller/Eggnew.php

@@ -32,9 +32,9 @@ class Eggnew extends Api
      * 查找下一个奖池ID
      */
     private function getNextJackpot($jackpot_id) {
-        $type = \app\common\model\EggJackpot::where(['id'=>$jackpot_id])->value('type');
+        $type = Db::name('egg_jackpot')->where(['id'=>$jackpot_id])->value('type');
 
-        $jackpotIds = \app\common\model\EggJackpot::order('weigh asc,id asc')->where('type',$type)->column("id");
+        $jackpotIds = Db::name('egg_jackpot')->order('weigh asc,id asc')->where('type',$type)->column("id");
         $next = 0;
         foreach($jackpotIds as $k => $v) {
             if($v == $jackpot_id) {
@@ -71,20 +71,28 @@ class Eggnew extends Api
 
 
         Db::startTrans();
-        try{
+
+        $user_jewel = model('wallet')->getwalletname($user_id,'jewel');
+        if($user_jewel < $total_jewel){
+            $this->error('钻石不足');
+            Db::rollback();
+        }
+
             // 查找正在开放的奖池
-            $jackpot = \app\common\model\EggJackpot::where(["status"=>1,'type'=>$type])->order('weigh asc,id asc')->lock(true)->find();
+            $jackpot = Db::name('egg_jackpot')->where(["status"=>1,'type'=>$type])->order('weigh asc,id asc')->lock(true)->find();
             if($jackpot) { // 有开放的奖池
                 $jackpot_id = $jackpot["id"];
             } else { // 没有开放的奖池
-                $jackpot = \app\common\model\EggJackpot::where('type',$type)->order('weigh asc,id asc')->lock(true)->find();
-                if(empty($jackpot)){
+                $jackpotlist = Db::name('egg_jackpot')->where('type',$type)->order('weigh asc,id asc')->lock(true)->select();//该类型全锁
+                if(empty($jackpotlist)){
                     Db::rollback();
                     $this->error('暂时还没有奖池');
                 }
+
+                $jackpot    = $jackpotlist[0];
                 $jackpot_id = $jackpot["id"];
-                \app\common\model\EggJackpot::update(["status"=>1,'starttime'=>$nowtime],["id"=>$jackpot_id]);//打开,启用时间
-                \app\common\model\EggGift::update(["is_use"=>0,'starttime'=>$nowtime],["Jackpot_id"=>$jackpot_id]);//重置礼物,启用时间
+                Db::name('egg_jackpot')->where(["id"=>$jackpot_id])->update(["status"=>1,'starttime'=>$nowtime]);//打开,启用时间
+                Db::name('egg_gift')->where(["Jackpot_id"=>$jackpot_id])->update(["is_use"=>0,'starttime'=>$nowtime]);//重置礼物,启用时间
 
                 //省的再查一次
                 $jackpot['status'] = 1;
@@ -92,7 +100,7 @@ class Eggnew extends Api
             }
 
             // 查找奖池对应的奖池礼物
-            $jackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$jackpot_id,"is_use"=>0])->select();
+            $jackpotGift = Db::name('egg_gift')->where(["Jackpot_id"=>$jackpot_id,"is_use"=>0])->select();
 
             $giftCount = count($jackpotGift);
             $next_jackpot_id = $this->getNextJackpot($jackpot_id);
@@ -253,16 +261,6 @@ class Eggnew extends Api
                 $this->error('开奖失败');
             }
 
-        }catch (ValidateException $e) {
-            Db::rollback();
-            $this->error($e->getMessage());
-        } catch (PDOException $e) {
-            Db::rollback();
-            $this->error($e->getMessage());
-        } catch (Exception $e) {
-            Db::rollback();
-            $this->error($e->getMessage());
-        }
 
         // 返回抽到的礼物列表
         $result = [