123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <?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() {
- $type = input('type',1);
- $jackId = \app\common\model\EggJackpot::where(["status"=>1,'type'=>$type])->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);
- }
- /**
- * 查找下一个奖池
- */
- private function getNextJackpot($jackpot_id) {
- $type = \app\common\model\EggJackpot::where(['id'=>$jackpot_id])->value('type');
- $jackpotIds = \app\common\model\EggJackpot::order('weigh asc,id asc')->where('type',$type)->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");
- $type = $this->request->request("type",1);
- $party_id = $this->request->request("party_id", 0);// 派对ID
- if($num <=0) {
- $this->error("参数错误");
- }
- $total_jewel = Db::name('egg_timesprice')->where('type',$type)->where('times',$num)->find();
- if(empty($total_jewel)){
- $this->error('错误的次数');
- }
- $total_jewel = $total_jewel['price'];
- $giftdata = [];
- $user_id = $this->auth->id;
- $do_no = createUniqueNo('E',$user_id);
- Db::startTrans();
- try{
- // 查找正在开放的奖池
- $jackpot = \app\common\model\EggJackpot::where(["status"=>1,'type'=>$type])->find();
- if($jackpot) { // 有开放的奖池
- // \app\common\model\EggJackpot::order("id","asc")->find();
- $jackpot_id = $jackpot["id"];
- } else { // 没有开放的奖池
- $jackpotInfo = \app\common\model\EggJackpot::where('type',$type)->order('weigh asc,id asc')->find();
- if(!empty($jackpotInfo)){
- Db::rollback();
- $this->error('暂时还没有奖池');
- }
- $jackpot_id = $jackpotInfo["id"];
- \app\common\model\EggJackpot::update(["status"=>1],["id"=>$jackpot_id]);//打开
- }
- // 查找奖池对应的奖池礼物
- $jackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$jackpot_id,"is_use"=>0])->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"=>-1],["id"=>$jackpot_id]);
- $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]]);
- */
- $res5 = true;
- $giftdata = $giftArr1;
- } else {
- if($newnum > count($nextjackpotGift)){
- Db::rollback();
- $this->error("奖池礼物待更新,请耐心等待!");
- }
- $giftArr = [];
- foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
- if(!$giftArr){
- Db::rollback();
- $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]]);
- // ------------------ 附加逻辑结束 ---------------------//
- */
- $res2 = true;
- $res3 = true;
- $res4 = true;
- $res5 = true;
- $giftdata = $res1Info;
- }
- if(!$giftdata) {
- Db::rollback();
- $this->error('礼物为空');
- }
- $data = [];
- foreach($giftdata as $k => $v) {
- // 保存砸蛋记录
- $data[] = [
- "do_no" => $do_no,
- "party_id" => $party_id,
- "user_id" => $user_id,
- "gift_id" => $v["gift_id"],
- "egg_gift_id" => $v["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 = true;
- // 添加到用户背包
- $backdata && $res7 = \app\common\model\GiftBack::insertAll($backdata);
- //扣钻石
- $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$total_jewel,'-',0,'开箱子和大转盘',13,'jewel');
- if($rs_wallet['status'] === false){
- $this->error($rs_wallet['msg']);
- Db::rollback();
- }
- if($res1 !== false && $res2 !== false && $res3 !== false && $res4 !== false && $res5 !== false && $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 {
- Db::rollback();
- $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 = [
- 'list' => [],
- 'bigshow' => [],
- ];
- $list = Db::name('egg_do')->alias('do')->join('gift','do.gift_id = gift.id','LEFT')->field('do.*,gift.is_big')->where('do.do_no',$do_no)->order('gift.is_big desc,gift.value desc')->select();
- $result['list'] = $list;
- //只弹一个大礼物,专门拿出结构
- if(!empty($list)){
- if($list[0]['is_big'] == 1){
- $result['bigshow'] = $list[0];
- }
- }
- $this->success("获取成功!",$result);
- }
- //手气榜单
- public function getRankList(){
- $today = strtotime(date("Y-m-d"));
- $where = [];
- $where["a.createtime"] = ["gt",$today];
- $ranklist = \app\common\model\EggDo::alias("a")->field("a.user_id,a.image,a.price as money,a.createtime,u.nickname")
- ->join("hx_user u","u.id = a.user_id","inner")
- ->where($where)
- // ->group("a.user_id")
- ->order("money","desc")
- ->limit(20)
- ->select();
- $this->success("获取成功!",$ranklist);
- }
- /**
- * 获取排行榜
- */
- public function getRankList_old() {
- $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("gift_id")
- ->order("money","desc")
- ->select();
- }
- $this->success("获取成功!",$ranklist);
- }
- /**
- * 获取砸蛋基本信息
- */
- public function getBaseInfo() {
- $type = input('type',1);
- // 构建数据
- $data = [];
- $data["jewel"] = $this->auth->jewel;
- $data["playdetail"] = config("site.playdetail");
- $data['pay_config'] = Db::name('egg_timesprice')->field('times,price')->where('type',$type)->order('times asc')->select();
- $this->success("获取成功!",$data);
- }
- }
|