Browse Source

伴声最新砸金蛋和副本

lizhen_gitee 1 year ago
parent
commit
70e453795d
2 changed files with 418 additions and 1 deletions
  1. 11 1
      application/api/controller/Egg.php
  2. 407 0
      application/api/controller/Eggnew.php

+ 11 - 1
application/api/controller/Egg.php

@@ -170,8 +170,18 @@ class Egg extends Api
                 \app\common\model\EggJackpot::update(["status"=>1],["id"=>$jackpot_id]);
             }
 
+            $currentNotUsedWhere = [];
+            if (in_array($num, [1, 10])) {
+                // 砸一锤 最大出11111,砸十锤 最大出88888, 奖池数不够时不受限制
+                $priceSection = $num == 1 ? '11111' : '88888';
+                $currentNotUsedCount = \app\common\model\EggGift::where(["Jackpot_id" => $jackpot_id, "is_use" => 0])->where('price', '<=', $priceSection)->count();
+                if ($currentNotUsedCount >= $num) {
+                    $currentNotUsedWhere['price'] = ['<=', $priceSection];
+                }
+            }
             // 查找奖池对应的奖池礼物
-            $jackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$jackpot_id,"is_use"=>0])->select();
+            $jackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$jackpot_id,"is_use"=>0])->where($currentNotUsedWhere)->select();
+
             $giftCount = count($jackpotGift);
             $next_jackpot_id = $this->getNextJackpot($jackpot_id);
 

+ 407 - 0
application/api/controller/Eggnew.php

@@ -0,0 +1,407 @@
+<?php
+
+namespace app\api\controller;
+
+
+use addons\faqueue\library\QueueApi;
+use app\common\controller\Api;
+use think\Db;
+use Redis;
+/**
+ * 砸蛋接口
+ */
+class Eggnew extends Api
+{
+    protected $noNeedLogin = ['getThisJackpot'];
+    protected $noNeedRight = ['*'];
+
+    /**
+     * 获取本期奖池列表
+     */
+    public function getThisJackpot() {
+        $jackId = \app\common\model\EggJackpot::where(["status"=>1])->value("id");
+        $egggiftModel = new \app\common\model\EggGift();
+        $where = [];
+        $where["Jackpot_id"] = $jackId;
+        $egggiftList = $egggiftModel->where($where)->group("gift_id")->order("price","desc")->select();
+        return $this->success("获取成功!",$egggiftList);
+    }
+
+    /**
+     * 购买锤子
+     */
+    public function buyHammer() {
+        // 接口防并发
+        if (!$this->apiLimit(1, 2000)) {
+            $this->error(__('Operation frequently'));
+        }
+
+        $num = $this->request->request("num");
+        if($num <=0) {
+            $this->error("购买数量不能小于0!");
+        }
+        $user_id = $this->auth->id;
+        $hammerPrice = config("site.hammerPrice");
+        if($hammerPrice <=0) {
+            $this->error("系统配置出错!");
+        }
+        $userModel = new \app\common\model\User;
+        $userInfo = $userModel->where('id',$user_id)->lock(true)->find();
+
+        $money = $hammerPrice*$num;
+        // 判断用户钻石余额是否充足
+        if($userInfo->jewel < $money) {
+            $this->error("您的钻石余额不足!",null,100);
+        }
+
+        $userjewellogModel = new \app\common\model\UserJewelLog();
+        Db::startTrans();
+        try{
+            $userjewel = $userInfo->jewel;
+            // 添加购买订单
+            $data = [];
+            $data["user_id"] = $user_id;
+            $data["price"] = $hammerPrice;
+            $data["number"] = $num;
+            $data["money"] = $money;
+            $data["createtime"] = time();
+            $res1 = \app\common\model\EggOrder::insert($data);
+            // 为用户增加锤子
+            $res2 = $userInfo->setInc("hammer",$num);
+            // 扣除用户钻石并生成钻石变动记录
+            $where = [];
+            $where["id"] = $user_id;
+            $res3 = $userInfo->setDec("jewel",$money);
+            $res4 = $userjewellogModel->addUserJewelLog($user_id, $money, "-", $userjewel, "购买{$num}锤", 2);
+
+            $redis = new Redis();
+            $redisconfig = config("redis");
+            $redis->connect($redisconfig["host"], $redisconfig["port"]);
+            if ($redisconfig['redis_pwd']) {
+                $redis->auth($redisconfig['redis_pwd']);
+            }
+            if($redisconfig['redis_selectdb'] > 0){
+                $redis->select($redisconfig['redis_selectdb']);
+            }
+            for($i=1;$i<=$num;$i++) {
+                $val = date("YmdHis").$i;
+                $redis->lpush("hammer_num_".$user_id,$val);
+            }
+
+            if($res1 && $res2 && $res3 && $res4) {
+                Db::commit();
+                $this->success("购买成功!");
+            } else {
+                $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());
+        }
+
+    }
+
+    /**
+     * 查找下一个奖池
+     */
+    public function getNextJackpot($jackpot_id) {
+        $jackpotIds = \app\common\model\EggJackpot::order("id","asc")->column("id");
+        $next = 0;
+        foreach($jackpotIds as $k => $v) {
+            if($v == $jackpot_id) {
+                $next = isset($jackpotIds[$k+1])?$jackpotIds[$k+1]:$jackpotIds[0];
+            }
+        }
+        return $next;
+    }
+
+    /**
+     * 砸金蛋
+     */
+    public function strike() {
+//        $this->error("系统正在维护中。。。");
+
+        $num = $this->request->request("num");
+        $party_id = $this->request->request("party_id", 0);// 派对ID
+        if($num <=0) {
+            $this->error("参数错误");
+        }
+        $giftdata = [];
+        $user_id = $this->auth->id;
+
+        // 防止超卖
+        $redis = new Redis();
+        $redisconfig = config("redis");
+        $redis->connect($redisconfig["host"], $redisconfig["port"]);
+        if ($redisconfig['redis_pwd']) {
+            $redis->auth($redisconfig['redis_pwd']);
+        }
+        if($redisconfig['redis_selectdb'] > 0){
+            $redis->select($redisconfig['redis_selectdb']);
+        }
+//        if($redis->lLen("hammer_num_".$user_id) < $num) $this->error("小金锤数量不足!");
+
+        $do_no = date("YmdHis").rand(10000,99999);
+        $userinfo = \app\common\model\User::lock(true)->find($this->auth->id);
+        if($userinfo->hammer < $num) {
+            $this->error("小金锤数量不足!");
+        }
+
+        for($i=1;$i<=$num;$i++) { // 扣除
+            $redis->lpop("hammer_num_".$user_id);
+        }
+
+        Db::startTrans();
+        try{
+            // 查找正在开放的奖池
+            $jackpot = \app\common\model\EggJackpot::where(["status"=>1])->find();
+            if($jackpot) { // 有开放的奖池
+                \app\common\model\EggJackpot::order("id","asc")->find();
+                $jackpot_id = $jackpot["id"];
+            } else { // 没有开放的奖池
+                $jackpotInfo = \app\common\model\EggJackpot::order("id","asc")->find();
+                $jackpot_id = $jackpotInfo["id"];
+                \app\common\model\EggJackpot::update(["status"=>1],["id"=>$jackpot_id]);
+            }
+
+            $currentNotUsedWhere = [];
+            if (in_array($num, [1, 10])) {
+                // 砸一锤 最大出11111,砸十锤 最大出88888, 奖池数不够时不受限制
+                $priceSection = $num == 1 ? '11111' : '88888';
+                $currentNotUsedCount = \app\common\model\EggGift::where(["Jackpot_id" => $jackpot_id, "is_use" => 0])->where('price', '<=', $priceSection)->count();
+                if ($currentNotUsedCount >= $num) {
+                    $currentNotUsedWhere['price'] = ['<=', $priceSection];
+                }
+            }
+            // 查找奖池对应的奖池礼物
+            $jackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$jackpot_id,"is_use"=>0])->where($currentNotUsedWhere)->select();
+
+            $giftCount = count($jackpotGift);
+            $next_jackpot_id = $this->getNextJackpot($jackpot_id);
+
+            if($giftCount <= $num) {
+                // 先获取$giftCount个礼物
+                $giftArr1 = $jackpotGift;
+
+                // 更新礼物抽取状态
+                $giftids = array_column($giftArr1,"id");
+                $res1 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
+                $res1Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
+
+
+                // 更新奖池
+                $res2 = \app\common\model\EggJackpot::update(["status"=>0],["tes"=>1]);
+                $res3 = \app\common\model\EggJackpot::update(["status"=>1],["id"=>$next_jackpot_id]);
+
+                // 下个奖池所有礼物改为未使用
+                $res4 = \app\common\model\EggGift::update(["is_use"=>0],["Jackpot_id"=>$next_jackpot_id]);
+                // 获取下个奖池礼物
+                $nextjackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$next_jackpot_id])->select();
+
+                $newnum = $num-$giftCount;
+
+                if($newnum <= 0) {
+                    $giftArr = [];
+                    foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
+
+                    if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
+
+                    $giftids = array_rand($giftArr,50);
+
+                    // 更新礼物抽取状态
+                    \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
+
+                    $res5 = \app\common\model\EggGift::update(["updatetime"=>time()],["id"=>["in",$giftids]]);
+
+                    $giftdata = $giftArr1;
+                } else {
+                    $giftArr = [];
+                    foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
+
+                    if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
+
+                    $giftids = array_rand($giftArr,$newnum);
+
+                    // 更新礼物抽取状态
+                    $res5Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
+
+                    $res5 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
+
+                    $giftdata = array_merge($giftArr1,$res5Info);
+                }
+
+            } else {
+                // 随机抽取$num个礼物
+                $giftArr = [];
+                foreach($jackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
+                $giftids = array_rand($giftArr,$num);
+
+                // 更新礼物抽取状态
+                $res1 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
+                $res1Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
+
+
+                // ------------------ 附加逻辑开始 ---------------------//
+                // 更新奖池
+                $res2 = \app\common\model\EggJackpot::update(["updatetime"=>time()],["tes"=>1]);
+                $res3 = \app\common\model\EggJackpot::update(["updatetime"=>time()],["id"=>$next_jackpot_id]);
+
+                // 下个奖池所有礼物改为未使用
+                $res4 = \app\common\model\EggGift::update(["updatetime"=>time()],["Jackpot_id"=>$next_jackpot_id]);
+                // 获取下个奖池礼物
+                $nextjackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$next_jackpot_id])->select();
+
+                $giftArr = [];
+                foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
+
+                if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
+
+                $giftids = array_rand($giftArr,50);
+
+                // 更新礼物抽取状态
+                \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
+
+                $res5 = \app\common\model\EggGift::update(["updatetime"=>time()],["id"=>["in",$giftids]]);
+
+                // ------------------ 附加逻辑结束 ---------------------//
+
+                $giftdata = $res1Info;
+            }
+
+            if(!$giftdata) $this->error("系统错误!");
+
+            $data = [];
+            foreach($giftdata as $k => $v) {
+                // 保存砸蛋记录
+                $data[] = [
+                    "do_no" => $do_no,
+                    "user_id" => $user_id,
+                    "egg_gift_id" => $v["gift_id"],
+                    "image" => $v["image"],
+                    "special" => $v["special"],
+                    "gift_name" => $v["gift_name"],
+                    "Jackpot_id" => $v["Jackpot_id"],
+                    "prize_no" => $v["prize_no"],
+                    "price" => $v["price"],
+                    "createtime" => time(),
+                ];
+                // 添加用户背包
+                $backdata[] = [
+                    "user_id" => $user_id,
+                    'gift_id' => $v["gift_id"],
+                    "name" => $v["gift_name"],
+                    "image" => $v["image"],
+                    "gif_image" => $v["special"],
+                    "value" => $v["price"],
+                    "number" => 1,
+                    "is_use" => 0,
+                    "get_way" => 1,
+                    "createtime" => time(),
+                ];
+            }
+            $data && $res5 = \app\common\model\EggDo::insertAll($data);
+            // 扣除用户小锤子
+            $res6 = \app\common\model\User::where(["id"=>$user_id])->setDec("hammer",$num);
+
+            // 添加到用户背包
+            $backdata && $res7 = \app\common\model\GiftBack::insertAll($backdata);
+
+
+            if($res1 !== false && $res2 && $res3 && $res4 && $res5 && $res6 && $res7) {
+                Db::commit();
+                $eggStrikeNotice = config("site.eggStrikeNotice");
+                if($party_id){
+                    $partyInfo = \app\common\model\Party::field("id,room_type,party_name")->where(["id"=>$party_id])->find();
+                    foreach ($backdata as $backdatum) {
+                        $realMoney = $backdatum['value'] / 100;
+                        if ($realMoney >= $eggStrikeNotice){
+                            $giftUserParty = ['number'=>$backdatum['number'],'gift_name'=>$backdatum['name'],'gift_gif_image'=>$backdatum['image']];
+                            QueueApi::sendGroupMessage(73, '', $this->auth->nickname, $partyInfo, $giftUserParty);
+                        }
+                    }
+                }
+            } else {
+                // 返还锤子
+                for($i=1;$i<=$num;$i++) {
+                    $val = date("YmdHis").$i;
+                    $redis->lpush("hammer_num_".$user_id,$val);
+                }
+            }
+
+        }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());
+        }
+
+        // 返回抽到的礼物列表
+        $list = \app\common\model\EggDo::where(["do_no"=>$do_no])->select();
+
+        $this->success("获取成功!",$list);
+    }
+
+    /**
+     * 获取排行榜
+     */
+    public function getRankList() {
+        $time = $this->request->request("time"); // 1=今天,2=昨天
+        if(!in_array($time,[1,2])) {
+            $this->error("参数缺失!");
+        }
+        // 先根据用户抽奖总值排序 筛选
+        $today = strtotime(date("Y-m-d 00:00:00"));
+        $yestoday = $today - 86400;
+        $where = [];
+        $time == 1 && $where["a.createtime"] = ["gt",$today];
+        $time == 2 && $where["a.createtime"] = ["between","$yestoday,$today"];
+        $ranklist = \app\common\model\EggDo::alias("a")->field("a.user_id,sum(a.price) as money,u.avatar,u.nickname")
+            ->join("hx_user u","u.id = a.user_id","inner")
+            ->where($where)
+            ->group("a.user_id")
+            ->order("money","desc")
+            ->limit(20)
+            ->select();
+        if(!$ranklist) $this->success("获取成功!",[]);
+
+        foreach($ranklist as $k => $v) {
+            // 查询
+            $where["user_id"] = $v["user_id"];
+            $ranklist[$k]["gifts"] = \app\common\model\EggDo::alias("a")
+                ->field("image,count(id) as number,sum(a.price) as money")
+                ->limit(3)
+                ->where($where)
+                ->group("egg_gift_id")
+                ->order("money","desc")
+                ->select();
+        }
+
+        $this->success("获取成功!",$ranklist);
+    }
+
+
+    /**
+     * 获取砸蛋基本信息
+     */
+    public function getBaseInfo() {
+        $user_id = $this->auth->id;
+        // 本人拥有锤子个数
+        $hammerNum = \app\common\model\User::where(["id"=>$user_id])->value("hammer");
+        // 构建数据
+        $data = [];
+        $data["hammerNum"] = $hammerNum;
+        $data["hammerPrice"] = config("site.hammerPrice");
+        $data["playdetail"] = config("site.playdetail");
+        $this->success("获取成功!",$data);
+    }
+}