Eggnew.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. namespace app\api\controller;
  3. use addons\faqueue\library\QueueApi;
  4. use app\common\controller\Api;
  5. use think\Db;
  6. use Redis;
  7. /**
  8. * 砸蛋接口
  9. */
  10. class Eggnew extends Api
  11. {
  12. protected $noNeedLogin = ['getThisJackpot'];
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 获取本期奖池列表
  16. */
  17. public function getThisJackpot() {
  18. $type = input('type',1);
  19. $jackId = \app\common\model\EggJackpot::where(["status"=>1,'type'=>$type])->value("id");
  20. $egggiftModel = new \app\common\model\EggGift();
  21. $where = [];
  22. $where["Jackpot_id"] = $jackId;
  23. $egggiftList = $egggiftModel->where($where)->group("gift_id")->order("price","desc")->select();
  24. return $this->success("获取成功!",$egggiftList);
  25. }
  26. /**
  27. * 查找下一个奖池ID
  28. */
  29. private function getNextJackpot($jackpot_id,$type) {
  30. //$type = Db::name('egg_jackpot')->where(['id'=>$jackpot_id])->value('type');
  31. $jackpotIds = Db::name('egg_jackpot')->order('weigh asc,id asc')->where('type',$type)->column("id");
  32. $next = 0;
  33. foreach($jackpotIds as $k => $v) {
  34. if($v == $jackpot_id) {
  35. $next = isset($jackpotIds[$k+1])?$jackpotIds[$k+1]:$jackpotIds[0];
  36. }
  37. }
  38. return $next;
  39. }
  40. /**
  41. * 砸金蛋
  42. */
  43. public function strike() {
  44. // $this->error("系统正在维护中。。。");
  45. $nowtime = time();
  46. $num = $this->request->request("num");
  47. $type = $this->request->request("type",1);
  48. $party_id = $this->request->request("party_id", 0);// 派对ID
  49. if($num <=0) {
  50. $this->error("参数错误");
  51. }
  52. //走次数配置
  53. $total_jewel = Db::name('egg_timesprice')->where('type',$type)->where('times',$num)->find();
  54. if(empty($total_jewel)){
  55. $this->error('错误的次数');
  56. }
  57. $total_jewel = $total_jewel['price'];
  58. $giftdata = [];
  59. $user_id = $this->auth->id;
  60. $do_no = createUniqueNo('E',$user_id);
  61. $jockpot_finish = 0; //产出统计
  62. Db::startTrans();
  63. //判断钻石余额
  64. $user_jewel = model('wallet')->getwallet($user_id,'jewel');
  65. if($user_jewel < $total_jewel){
  66. $this->error('钻石不足');
  67. Db::rollback();
  68. }
  69. // 查找正在开放的奖池,奖池锁
  70. $jackpot = Db::name('egg_jackpot')->where(["status"=>1,'type'=>$type])->order('weigh asc,id asc')->lock(true)->find();
  71. if($jackpot) {
  72. // 有开放的奖池
  73. $jackpot_id = $jackpot["id"];
  74. } else {
  75. // 没有开放的奖池
  76. $jackpotlist = Db::name('egg_jackpot')->where('type',$type)->order('weigh asc,id asc')->lock(true)->select();//该类型奖池全锁
  77. if(empty($jackpotlist)){
  78. Db::rollback();
  79. $this->error('暂时还没有奖池');
  80. }
  81. $jackpot = $jackpotlist[0];
  82. $jackpot_id = $jackpot["id"];
  83. //打开,启用时间
  84. $open1 = Db::name('egg_jackpot')->where(["id"=>$jackpot_id])->update(["status"=>1,'starttime'=>$nowtime]);
  85. if($open1 === false){
  86. Db::rollback();
  87. $this->error('开奖失败');
  88. }
  89. //重置礼物,启用时间
  90. $open2 = Db::name('egg_gift')->where(["Jackpot_id"=>$jackpot_id])->update(["is_use"=>0,'starttime'=>$nowtime]);
  91. if($open2 === false){
  92. Db::rollback();
  93. $this->error('开奖失败');
  94. }
  95. //省的再查一次
  96. $jackpot['status'] = 1;
  97. $jackpot['starttime'] = $nowtime;
  98. }
  99. // 查找奖池对应的奖池礼物
  100. Db::name('egg_gift')->where(["Jackpot_id"=>$jackpot_id])->lock(true)->select(); //这个奖池礼物全锁
  101. $jackpotGift = Db::name('egg_gift')->where(["Jackpot_id"=>$jackpot_id,"is_use"=>0])->select();
  102. $giftCount = count($jackpotGift);
  103. //当前奖池已不足,或需要启用下一池
  104. if($giftCount <= $num) {
  105. //下一个奖池锁
  106. $next_jackpot_id = $this->getNextJackpot($jackpot_id,$jackpot['type']);
  107. Db::name('egg_jackpot')->where('id',$next_jackpot_id)->lock(true)->find();
  108. // 下个奖池礼物,全锁
  109. Db::name('egg_gift')->where(["Jackpot_id"=>$next_jackpot_id])->lock(true)->select();
  110. // 先获取$giftCount个礼物
  111. $giftArr1 = $jackpotGift;
  112. // 更新礼物抽取状态
  113. $giftids = array_column($giftArr1,"id");
  114. $res1 = Db::name('egg_gift')->where(["id"=>["in",$giftids]])->update(["is_use"=>1]);
  115. if($res1 === false){
  116. Db::rollback();
  117. $this->error('开奖失败');
  118. }
  119. // 更新奖池
  120. $res2 = Db::name('egg_jackpot')->where(["id"=>$jackpot_id])->update(["status"=>-1]);
  121. if($res2 === false){
  122. Db::rollback();
  123. $this->error('开奖失败');
  124. }
  125. // 更新新奖池
  126. $res3 = Db::name('egg_jackpot')->where(["id"=>$next_jackpot_id])->update(["status"=>1,'starttime'=>$nowtime]);//打开,启用时间
  127. if($res3 === false){
  128. Db::rollback();
  129. $this->error('开奖失败');
  130. }
  131. // 下个奖池所有礼物改为未使用
  132. $res4 = Db::name('egg_gift')->where(["Jackpot_id"=>$next_jackpot_id])->update(["is_use"=>0,'starttime'=>$nowtime]);//重置礼物,启用时间
  133. if($res4 === false){
  134. Db::rollback();
  135. $this->error('开奖失败');
  136. }
  137. $nextjackpotGift = Db::name('egg_gift')->where(["Jackpot_id"=>$next_jackpot_id])->select();
  138. $newnum = $num-$giftCount;
  139. if($newnum == 0) {
  140. $giftdata = $giftArr1;
  141. } else {
  142. if($newnum > count($nextjackpotGift)){ //需要开启下下个奖池
  143. Db::rollback();
  144. $this->error("奖池礼物待更新,请耐心等待!");
  145. }
  146. $giftArr = [];
  147. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  148. if(!$giftArr){
  149. Db::rollback();
  150. $this->error("奖池礼物待更新,请耐心等待!");
  151. }
  152. $giftids = array_rand($giftArr,$newnum);
  153. // 更新礼物抽取状态
  154. $res5Info = Db::name('egg_gift')->where(["id"=>["in",$giftids]])->select();
  155. $res5 = Db::name('egg_gift')->where(["id"=>["in",$giftids]])->update(["is_use"=>1]);
  156. if($res5 === false){
  157. Db::rollback();
  158. $this->error('开奖失败');
  159. }
  160. $giftdata = array_merge($giftArr1,$res5Info);
  161. }
  162. //产出统计,在这里记录奖池产出统计,老一轮的奖池已经消耗完毕
  163. $jockpot_finish = 1;
  164. } else {
  165. // 随机抽取$num个礼物
  166. $giftArr = [];
  167. foreach($jackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  168. $giftids = array_rand($giftArr,$num);
  169. // 更新礼物抽取状态
  170. $res1 = Db::name('egg_gift')->where(["id"=>["in",$giftids]])->update(["is_use"=>1]);
  171. if($res1 === false){
  172. Db::rollback();
  173. $this->error('开奖失败');
  174. }
  175. $res1Info = Db::name('egg_gift')->where(["id"=>["in",$giftids]])->select();
  176. $giftdata = $res1Info;
  177. }
  178. if(empty($giftdata)) {
  179. Db::rollback();
  180. $this->error('礼物为空');
  181. }
  182. $data = [];
  183. foreach($giftdata as $k => $v) {
  184. // 保存砸蛋记录
  185. $data[] = [
  186. "do_no" => $do_no,
  187. "pay_fee" => $k == 0 ? $total_jewel : 0,
  188. "party_id" => $party_id,
  189. "user_id" => $user_id,
  190. "gift_id" => $v["gift_id"],
  191. "egg_gift_id" => $v["id"],
  192. "image" => $v["image"],
  193. "special" => $v["special"],
  194. "gift_name" => $v["gift_name"],
  195. "Jackpot_id" => $v["Jackpot_id"],
  196. // "prize_no" => $v["prize_no"],
  197. "starttime" => $v["starttime"],
  198. "price" => $v["price"],
  199. "createtime" => time(),
  200. ];
  201. // 添加用户背包
  202. $backdata[] = [
  203. "user_id" => $user_id,
  204. 'gift_id' => $v["gift_id"],
  205. "name" => $v["gift_name"],
  206. "image" => $v["image"],
  207. "gif_image" => $v["special"],
  208. "value" => $v["price"],
  209. "number" => 1,
  210. "is_use" => 0,
  211. "get_way" => 1,
  212. "createtime" => time(),
  213. ];
  214. }
  215. $res6 = Db::name('egg_do')->insertAll($data);
  216. if($res6 === false){
  217. Db::rollback();
  218. $this->error('开奖失败');
  219. }
  220. // 添加到用户背包
  221. $res7 = Db::name('gift_back')->insertAll($backdata);
  222. if($res7 === false){
  223. Db::rollback();
  224. $this->error('开奖失败');
  225. }
  226. //扣钻石
  227. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$total_jewel,'-',0,'开箱子和大转盘',13,'jewel');
  228. if($rs_wallet['status'] === false){
  229. Db::rollback();
  230. $this->error($rs_wallet['msg']);
  231. }
  232. //每一期奖池的产出统计
  233. if($jockpot_finish == 1){
  234. $jackpot_chanchu = [
  235. //奖池的
  236. 'jp_giftnum' => Db::name('egg_gift')->where('Jackpot_id',$jackpot_id)->count(), //总礼物数量
  237. 'jp_giftprice' => Db::name('egg_gift')->where('Jackpot_id',$jackpot_id)->sum('price'), //总礼物价值
  238. //抽奖的
  239. 'do_payfee' => Db::name('egg_do')->where('Jackpot_id',$jackpot_id)->where('starttime',$jackpot['starttime'])->sum('pay_fee'), //物抽总礼奖费用
  240. 'do_usercount' => Db::name('egg_do')->where('Jackpot_id',$jackpot_id)->where('starttime',$jackpot['starttime'])->group('user_id')->count('id'),//抽奖总人数
  241. 'do_usertimes' => Db::name('egg_do')->where('Jackpot_id',$jackpot_id)->where('starttime',$jackpot['starttime'])->group('do_no')->count('id'),//抽奖总次数
  242. 'do_giftnum' => Db::name('egg_do')->where('Jackpot_id',$jackpot_id)->where('starttime',$jackpot['starttime'])->count('id'),//抽奖总礼物数量
  243. 'do_giftprice' => Db::name('egg_do')->where('Jackpot_id',$jackpot_id)->where('starttime',$jackpot['starttime'])->sum('price'),//抽奖总礼物价值
  244. //基础信息
  245. 'jackpot_id' => $jackpot_id,
  246. 'starttime' => $jackpot['starttime'],
  247. 'createtime' => time(),
  248. ];
  249. //产出比
  250. if($jackpot_chanchu['jp_giftprice'] != 0){
  251. $jackpot_chanchu['chanchubi'] = bcdiv($jackpot_chanchu['do_giftprice'],$jackpot_chanchu['jp_giftprice'],2);
  252. }else{
  253. $jackpot_chanchu['chanchubi'] = 0;
  254. }
  255. $rs_chanchu = Db::name('egg_jackpot_chanchu')->insertGetId($jackpot_chanchu);
  256. if(!$rs_chanchu){
  257. Db::rollback();
  258. $this->error('开奖失败');
  259. }
  260. }
  261. Db::commit();
  262. // 返回抽到的礼物列表
  263. $result = [
  264. 'list' => [],
  265. 'bigshow' => [],
  266. 'sumvalue' => Db::name('egg_do')->where('do_no',$do_no)->sum('price'),
  267. ];
  268. $list = Db::name('egg_do')->alias('do')->join('gift','do.gift_id = gift.id','LEFT')->field('do.gift_id,do.gift_name,do.image,do.special,do.price,count(do.id) as number,gift.is_big')->where('do.do_no',$do_no)->order('gift.is_big desc,gift.value desc')->group('do.gift_id')->select();
  269. $list = list_domain_image($list,['image','special']);
  270. $result['list'] = $list;
  271. //只弹一个大礼物,专门拿出结构
  272. if(!empty($list)){
  273. if($list[0]['is_big'] == 1){
  274. $result['bigshow'] = $list[0];
  275. }
  276. }
  277. $this->success("获取成功!",$result);
  278. }
  279. //手气榜单
  280. public function getRankList(){
  281. $type = $this->request->request("type",1);
  282. $today = strtotime(date("Y-m-d"));
  283. $where = [];
  284. $where["a.createtime"] = ["gt",$today];
  285. $where["jp.type"] = $type;
  286. $ranklist = \app\common\model\EggDo::alias("a")->field("a.user_id,a.image,a.price as money,a.createtime,u.nickname")
  287. ->join("hx_user u","u.id = a.user_id","left")
  288. ->join("egg_jackpot jp","a.Jackpot_id = jp.id","left")
  289. ->where($where)
  290. // ->group("a.user_id")
  291. ->order("money","desc")
  292. ->limit(20)
  293. ->select();
  294. $this->success("获取成功!",$ranklist);
  295. }
  296. /**
  297. * 获取排行榜
  298. */
  299. public function getRankList_old() {
  300. $time = $this->request->request("time"); // 1=今天,2=昨天
  301. if(!in_array($time,[1,2])) {
  302. $this->error("参数缺失!");
  303. }
  304. // 先根据用户抽奖总值排序 筛选
  305. $today = strtotime(date("Y-m-d 00:00:00"));
  306. $yestoday = $today - 86400;
  307. $where = [];
  308. $time == 1 && $where["a.createtime"] = ["gt",$today];
  309. $time == 2 && $where["a.createtime"] = ["between","$yestoday,$today"];
  310. $ranklist = \app\common\model\EggDo::alias("a")->field("a.user_id,sum(a.price) as money,u.avatar,u.nickname")
  311. ->join("hx_user u","u.id = a.user_id","inner")
  312. ->where($where)
  313. ->group("a.user_id")
  314. ->order("money","desc")
  315. ->limit(20)
  316. ->select();
  317. if(!$ranklist) $this->success("获取成功!",[]);
  318. foreach($ranklist as $k => $v) {
  319. // 查询
  320. $where["user_id"] = $v["user_id"];
  321. $ranklist[$k]["gifts"] = \app\common\model\EggDo::alias("a")
  322. ->field("image,count(id) as number,sum(a.price) as money")
  323. ->limit(3)
  324. ->where($where)
  325. ->group("gift_id")
  326. ->order("money","desc")
  327. ->select();
  328. }
  329. $this->success("获取成功!",$ranklist);
  330. }
  331. /**
  332. * 获取砸蛋基本信息
  333. */
  334. public function getBaseInfo() {
  335. $type = input('type',1);
  336. // 构建数据
  337. $data = [];
  338. $data["jewel"] = $this->auth->jewel;
  339. $data["playdetail"] = config("site.playdetail");
  340. $data['pay_config'] = Db::name('egg_timesprice')->field('times,price')->where('type',$type)->order('times asc')->select();
  341. $this->success("获取成功!",$data);
  342. }
  343. }