Bladeren bron

开箱抽奖优化

lizhen_gitee 1 jaar geleden
bovenliggende
commit
af94549e22
1 gewijzigde bestanden met toevoegingen van 76 en 29 verwijderingen
  1. 76 29
      application/api/controller/Eggnew.php

+ 76 - 29
application/api/controller/Eggnew.php

@@ -31,8 +31,8 @@ class Eggnew extends Api
     /**
      * 查找下一个奖池ID
      */
-    private function getNextJackpot($jackpot_id) {
-        $type = Db::name('egg_jackpot')->where(['id'=>$jackpot_id])->value('type');
+    private function getNextJackpot($jackpot_id,$type) {
+        //$type = Db::name('egg_jackpot')->where(['id'=>$jackpot_id])->value('type');
 
         $jackpotIds = Db::name('egg_jackpot')->order('weigh asc,id asc')->where('type',$type)->column("id");
         $next = 0;
@@ -56,6 +56,8 @@ class Eggnew extends Api
         if($num <=0) {
             $this->error("参数错误");
         }
+
+        //走次数配置
         $total_jewel = Db::name('egg_timesprice')->where('type',$type)->where('times',$num)->find();
         if(empty($total_jewel)){
             $this->error('错误的次数');
@@ -72,18 +74,21 @@ class Eggnew extends Api
 
         Db::startTrans();
 
+        //判断钻石余额
         $user_jewel = model('wallet')->getwalletname($user_id,'jewel');
         if($user_jewel < $total_jewel){
             $this->error('钻石不足');
             Db::rollback();
         }
 
-            // 查找正在开放的奖池
+            // 查找正在开放的奖池,奖池锁
             $jackpot = Db::name('egg_jackpot')->where(["status"=>1,'type'=>$type])->order('weigh asc,id asc')->lock(true)->find();
-            if($jackpot) { // 有开放的奖池
+            if($jackpot) {
+                // 有开放的奖池
                 $jackpot_id = $jackpot["id"];
-            } else { // 没有开放的奖池
-                $jackpotlist = Db::name('egg_jackpot')->where('type',$type)->order('weigh asc,id asc')->lock(true)->select();//该类型全锁
+            } else {
+                // 没有开放的奖池
+                $jackpotlist = Db::name('egg_jackpot')->where('type',$type)->order('weigh asc,id asc')->lock(true)->select();//该类型奖池全锁
                 if(empty($jackpotlist)){
                     Db::rollback();
                     $this->error('暂时还没有奖池');
@@ -91,8 +96,19 @@ class Eggnew extends Api
 
                 $jackpot    = $jackpotlist[0];
                 $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]);//重置礼物,启用时间
+
+                //打开,启用时间
+                $open1 = Db::name('egg_jackpot')->where(["id"=>$jackpot_id])->update(["status"=>1,'starttime'=>$nowtime]);
+                if($open1 === false){
+                    Db::rollback();
+                    $this->error('开奖失败');
+                }
+                //重置礼物,启用时间
+                $open2 = Db::name('egg_gift')->where(["Jackpot_id"=>$jackpot_id])->update(["is_use"=>0,'starttime'=>$nowtime]);
+                if($open2 === false){
+                    Db::rollback();
+                    $this->error('开奖失败');
+                }
 
                 //省的再查一次
                 $jackpot['status'] = 1;
@@ -100,35 +116,59 @@ class Eggnew extends Api
             }
 
             // 查找奖池对应的奖池礼物
+                           Db::name('egg_gift')->where(["Jackpot_id"=>$jackpot_id])->lock(true)->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);
 
-            if($giftCount <= $num) {   //当前奖池已不足,或需要启用下一池
+            //当前奖池已不足,或需要启用下一池
+            if($giftCount <= $num) {
+
+                //下一个奖池锁
+                $next_jackpot_id = $this->getNextJackpot($jackpot_id,$jackpot['type']);
+                Db::name('egg_jackpot')->where('id',$next_jackpot_id)->lock(true)->find();
+
+                // 下个奖池礼物,全锁
+                Db::name('egg_gift')->where(["Jackpot_id"=>$next_jackpot_id])->lock(true)->select();
+
                 // 先获取$giftCount个礼物
                 $giftArr1 = $jackpotGift;
 
                 // 更新礼物抽取状态
                 $giftids = array_column($giftArr1,"id");
                 $res1 = Db::name('egg_gift')->where(["id"=>["in",$giftids]])->update(["is_use"=>1]);
-
+                if($res1 === false){
+                    Db::rollback();
+                    $this->error('开奖失败');
+                }
 
                 // 更新奖池
                 $res2 = Db::name('egg_jackpot')->where(["id"=>$jackpot_id])->update(["status"=>-1]);
+                if($res2 === false){
+                    Db::rollback();
+                    $this->error('开奖失败');
+                }
+                // 更新新奖池
                 $res3 = Db::name('egg_jackpot')->where(["id"=>$next_jackpot_id])->update(["status"=>1,'starttime'=>$nowtime]);//打开,启用时间
+                if($res3 === false){
+                    Db::rollback();
+                    $this->error('开奖失败');
+                }
 
                 // 下个奖池所有礼物改为未使用
                 $res4 = Db::name('egg_gift')->where(["Jackpot_id"=>$next_jackpot_id])->update(["is_use"=>0,'starttime'=>$nowtime]);//重置礼物,启用时间
+                if($res4 === false){
+                    Db::rollback();
+                    $this->error('开奖失败');
+                }
 
-                // 获取下个奖池礼物
-                $nextjackpotGift = Db::name('egg_gift')->where(["Jackpot_id"=>$next_jackpot_id,"is_use"=>0])->select();
+                $nextjackpotGift = Db::name('egg_gift')->where(["Jackpot_id"=>$next_jackpot_id])->select();
 
                 $newnum = $num-$giftCount;
 
                 if($newnum == 0) {
 
-                    $res5 = true;
+
                     $giftdata = $giftArr1;
                 } else {
                     if($newnum > count($nextjackpotGift)){ //需要开启下下个奖池
@@ -150,6 +190,10 @@ class Eggnew extends Api
                     $res5Info = Db::name('egg_gift')->where(["id"=>["in",$giftids]])->select();
 
                     $res5 = Db::name('egg_gift')->where(["id"=>["in",$giftids]])->update(["is_use"=>1]);
+                    if($res5 === false){
+                        Db::rollback();
+                        $this->error('开奖失败');
+                    }
 
                     $giftdata = array_merge($giftArr1,$res5Info);
                 }
@@ -165,18 +209,18 @@ class Eggnew extends Api
 
                 // 更新礼物抽取状态
                 $res1 = Db::name('egg_gift')->where(["id"=>["in",$giftids]])->update(["is_use"=>1]);
+                if($res1 === false){
+                    Db::rollback();
+                    $this->error('开奖失败');
+                }
                 $res1Info = Db::name('egg_gift')->where(["id"=>["in",$giftids]])->select();
 
 
-                $res2 = true;
-                $res3 = true;
-                $res4 = true;
-                $res5 = true;
 
                 $giftdata = $res1Info;
             }
 
-            if(!$giftdata) {
+            if(empty($giftdata)) {
                 Db::rollback();
                 $this->error('礼物为空');
             }
@@ -214,16 +258,25 @@ class Eggnew extends Api
                     "createtime" => time(),
                 ];
             }
-            $data && $res5 = Db::name('egg_do')->insertAll($data);
+            $res6 = Db::name('egg_do')->insertAll($data);
+            if($res6 === false){
+                Db::rollback();
+                $this->error('开奖失败');
+            }
 
             // 添加到用户背包
-            $backdata && $res7 = Db::name('gift_back')->insertAll($backdata);
+            $res7 = Db::name('gift_back')->insertAll($backdata);
+            if($res7 === false){
+                Db::rollback();
+                $this->error('开奖失败');
+            }
+
 
             //扣钻石
             $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$total_jewel,'-',0,'开箱子和大转盘',13,'jewel');
             if($rs_wallet['status'] === false){
-                $this->error($rs_wallet['msg']);
                 Db::rollback();
+                $this->error($rs_wallet['msg']);
             }
 
             //每一期奖池的产出统计
@@ -253,13 +306,7 @@ class Eggnew extends Api
                 }
             }
 
-            if($res1 !== false && $res2 !== false && $res3 !== false && $res4 !== false && $res5 !== false && $res7 !== false) {
-                Db::commit();
-
-            } else {
-                Db::rollback();
-                $this->error('开奖失败');
-            }
+            Db::commit();
 
 
         // 返回抽到的礼物列表