Eggnew.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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) {
  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. $total_jewel = Db::name('egg_timesprice')->where('type',$type)->where('times',$num)->find();
  53. if(empty($total_jewel)){
  54. $this->error('错误的次数');
  55. }
  56. $total_jewel = $total_jewel['price'];
  57. $giftdata = [];
  58. $user_id = $this->auth->id;
  59. $do_no = createUniqueNo('E',$user_id);
  60. $jockpot_finish = 0; //产出统计
  61. Db::startTrans();
  62. $user_jewel = model('wallet')->getwalletname($user_id,'jewel');
  63. if($user_jewel < $total_jewel){
  64. $this->error('钻石不足');
  65. Db::rollback();
  66. }
  67. // 查找正在开放的奖池
  68. $jackpot = Db::name('egg_jackpot')->where(["status"=>1,'type'=>$type])->order('weigh asc,id asc')->lock(true)->find();
  69. if($jackpot) { // 有开放的奖池
  70. $jackpot_id = $jackpot["id"];
  71. } else { // 没有开放的奖池
  72. $jackpotlist = Db::name('egg_jackpot')->where('type',$type)->order('weigh asc,id asc')->lock(true)->select();//该类型全锁
  73. if(empty($jackpotlist)){
  74. Db::rollback();
  75. $this->error('暂时还没有奖池');
  76. }
  77. $jackpot = $jackpotlist[0];
  78. $jackpot_id = $jackpot["id"];
  79. Db::name('egg_jackpot')->where(["id"=>$jackpot_id])->update(["status"=>1,'starttime'=>$nowtime]);//打开,启用时间
  80. Db::name('egg_gift')->where(["Jackpot_id"=>$jackpot_id])->update(["is_use"=>0,'starttime'=>$nowtime]);//重置礼物,启用时间
  81. //省的再查一次
  82. $jackpot['status'] = 1;
  83. $jackpot['starttime'] = $nowtime;
  84. }
  85. // 查找奖池对应的奖池礼物
  86. $jackpotGift = Db::name('egg_gift')->where(["Jackpot_id"=>$jackpot_id,"is_use"=>0])->select();
  87. $giftCount = count($jackpotGift);
  88. $next_jackpot_id = $this->getNextJackpot($jackpot_id);
  89. if($giftCount <= $num) { //当前奖池已不足,或需要启用下一池
  90. // 先获取$giftCount个礼物
  91. $giftArr1 = $jackpotGift;
  92. // 更新礼物抽取状态
  93. $giftids = array_column($giftArr1,"id");
  94. $res1 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  95. // 更新奖池
  96. $res2 = \app\common\model\EggJackpot::update(["status"=>-1],["id"=>$jackpot_id]);
  97. $res3 = \app\common\model\EggJackpot::update(["status"=>1,'starttime'=>$nowtime],["id"=>$next_jackpot_id]);//打开,启用时间
  98. // 下个奖池所有礼物改为未使用
  99. $res4 = \app\common\model\EggGift::update(["is_use"=>0,'starttime'=>$nowtime],["Jackpot_id"=>$next_jackpot_id]);//重置礼物,启用时间
  100. // 获取下个奖池礼物
  101. $nextjackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$next_jackpot_id,"is_use"=>0])->select();
  102. $newnum = $num-$giftCount;
  103. if($newnum == 0) {
  104. $res5 = true;
  105. $giftdata = $giftArr1;
  106. } else {
  107. if($newnum > count($nextjackpotGift)){ //需要开启下下个奖池
  108. Db::rollback();
  109. $this->error("奖池礼物待更新,请耐心等待!");
  110. }
  111. $giftArr = [];
  112. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  113. if(!$giftArr){
  114. Db::rollback();
  115. $this->error("奖池礼物待更新,请耐心等待!");
  116. }
  117. $giftids = array_rand($giftArr,$newnum);
  118. // 更新礼物抽取状态
  119. $res5Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  120. $res5 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  121. $giftdata = array_merge($giftArr1,$res5Info);
  122. }
  123. //产出统计,在这里记录奖池产出统计,老一轮的奖池已经消耗完毕
  124. $jockpot_finish = 1;
  125. } else {
  126. // 随机抽取$num个礼物
  127. $giftArr = [];
  128. foreach($jackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  129. $giftids = array_rand($giftArr,$num);
  130. // 更新礼物抽取状态
  131. $res1 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  132. $res1Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  133. $res2 = true;
  134. $res3 = true;
  135. $res4 = true;
  136. $res5 = true;
  137. $giftdata = $res1Info;
  138. }
  139. if(!$giftdata) {
  140. Db::rollback();
  141. $this->error('礼物为空');
  142. }
  143. $data = [];
  144. foreach($giftdata as $k => $v) {
  145. // 保存砸蛋记录
  146. $data[] = [
  147. "do_no" => $do_no,
  148. "pay_fee" => $k == 0 ? $total_jewel : 0,
  149. "party_id" => $party_id,
  150. "user_id" => $user_id,
  151. "gift_id" => $v["gift_id"],
  152. "egg_gift_id" => $v["id"],
  153. "image" => $v["image"],
  154. "special" => $v["special"],
  155. "gift_name" => $v["gift_name"],
  156. "Jackpot_id" => $v["Jackpot_id"],
  157. // "prize_no" => $v["prize_no"],
  158. "starttime" => $v["starttime"],
  159. "price" => $v["price"],
  160. "createtime" => time(),
  161. ];
  162. // 添加用户背包
  163. $backdata[] = [
  164. "user_id" => $user_id,
  165. 'gift_id' => $v["gift_id"],
  166. "name" => $v["gift_name"],
  167. "image" => $v["image"],
  168. "gif_image" => $v["special"],
  169. "value" => $v["price"],
  170. "number" => 1,
  171. "is_use" => 0,
  172. "get_way" => 1,
  173. "createtime" => time(),
  174. ];
  175. }
  176. $data && $res5 = \app\common\model\EggDo::insertAll($data);
  177. // 添加到用户背包
  178. $backdata && $res7 = \app\common\model\GiftBack::insertAll($backdata);
  179. //扣钻石
  180. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$total_jewel,'-',0,'开箱子和大转盘',13,'jewel');
  181. if($rs_wallet['status'] === false){
  182. $this->error($rs_wallet['msg']);
  183. Db::rollback();
  184. }
  185. //每一期奖池的产出统计
  186. if($jockpot_finish == 1){
  187. $jackpot_chanchu = [
  188. //奖池的
  189. 'jp_giftnum' => Db::name('egg_gift')->where('Jackpot_id',$jackpot_id)->count(), //总礼物数量
  190. 'jp_giftprice' => Db::name('egg_gift')->where('Jackpot_id',$jackpot_id)->sum('price'), //总礼物价值
  191. //抽奖的
  192. 'do_payfee' => Db::name('egg_do')->where('Jackpot_id',$jackpot_id)->where('starttime',$jackpot['starttime'])->sum('pay_fee'), //物抽总礼奖费用
  193. 'do_usercount' => Db::name('egg_do')->where('Jackpot_id',$jackpot_id)->where('starttime',$jackpot['starttime'])->group('user_id')->count('id'),//抽奖总人数
  194. 'do_usertimes' => Db::name('egg_do')->where('Jackpot_id',$jackpot_id)->where('starttime',$jackpot['starttime'])->group('do_no')->count('id'),//抽奖总次数
  195. 'do_giftnum' => Db::name('egg_do')->where('Jackpot_id',$jackpot_id)->where('starttime',$jackpot['starttime'])->count('id'),//抽奖总礼物数量
  196. 'do_giftprice' => Db::name('egg_do')->where('Jackpot_id',$jackpot_id)->where('starttime',$jackpot['starttime'])->sum('price'),//抽奖总礼物价值
  197. //基础信息
  198. 'jackpot_id' => $jackpot_id,
  199. 'starttime' => $jackpot['starttime'],
  200. 'createtime' => time(),
  201. ];
  202. //产出比
  203. $jackpot_chanchu['chanchubi'] = bcdiv($jackpot_chanchu['do_giftprice'],$jackpot_chanchu['jp_giftprice'],2);
  204. $rs_chanchu = Db::name('egg_jackpot_chanchu')->insertGetId($jackpot_chanchu);
  205. if(!$rs_chanchu){
  206. Db::rollback();
  207. $this->error('开奖失败');
  208. }
  209. }
  210. if($res1 !== false && $res2 !== false && $res3 !== false && $res4 !== false && $res5 !== false && $res7 !== false) {
  211. Db::commit();
  212. } else {
  213. Db::rollback();
  214. $this->error('开奖失败');
  215. }
  216. // 返回抽到的礼物列表
  217. $result = [
  218. 'list' => [],
  219. 'bigshow' => [],
  220. 'sumvalue' => Db::name('egg_do')->where('do_no',$do_no)->sum('price'),
  221. ];
  222. $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();
  223. $result['list'] = $list;
  224. //只弹一个大礼物,专门拿出结构
  225. if(!empty($list)){
  226. if($list[0]['is_big'] == 1){
  227. $result['bigshow'] = $list[0];
  228. }
  229. }
  230. $this->success("获取成功!",$result);
  231. }
  232. //手气榜单
  233. public function getRankList(){
  234. $type = $this->request->request("type",1);
  235. $today = strtotime(date("Y-m-d"));
  236. $where = [];
  237. $where["a.createtime"] = ["gt",$today];
  238. $where["jp.type"] = $type;
  239. $ranklist = \app\common\model\EggDo::alias("a")->field("a.user_id,a.image,a.price as money,a.createtime,u.nickname")
  240. ->join("hx_user u","u.id = a.user_id","left")
  241. ->join("egg_jackpot jp","a.Jackpot_id = jp.id","left")
  242. ->where($where)
  243. // ->group("a.user_id")
  244. ->order("money","desc")
  245. ->limit(20)
  246. ->select();
  247. $this->success("获取成功!",$ranklist);
  248. }
  249. /**
  250. * 获取排行榜
  251. */
  252. public function getRankList_old() {
  253. $time = $this->request->request("time"); // 1=今天,2=昨天
  254. if(!in_array($time,[1,2])) {
  255. $this->error("参数缺失!");
  256. }
  257. // 先根据用户抽奖总值排序 筛选
  258. $today = strtotime(date("Y-m-d 00:00:00"));
  259. $yestoday = $today - 86400;
  260. $where = [];
  261. $time == 1 && $where["a.createtime"] = ["gt",$today];
  262. $time == 2 && $where["a.createtime"] = ["between","$yestoday,$today"];
  263. $ranklist = \app\common\model\EggDo::alias("a")->field("a.user_id,sum(a.price) as money,u.avatar,u.nickname")
  264. ->join("hx_user u","u.id = a.user_id","inner")
  265. ->where($where)
  266. ->group("a.user_id")
  267. ->order("money","desc")
  268. ->limit(20)
  269. ->select();
  270. if(!$ranklist) $this->success("获取成功!",[]);
  271. foreach($ranklist as $k => $v) {
  272. // 查询
  273. $where["user_id"] = $v["user_id"];
  274. $ranklist[$k]["gifts"] = \app\common\model\EggDo::alias("a")
  275. ->field("image,count(id) as number,sum(a.price) as money")
  276. ->limit(3)
  277. ->where($where)
  278. ->group("gift_id")
  279. ->order("money","desc")
  280. ->select();
  281. }
  282. $this->success("获取成功!",$ranklist);
  283. }
  284. /**
  285. * 获取砸蛋基本信息
  286. */
  287. public function getBaseInfo() {
  288. $type = input('type',1);
  289. // 构建数据
  290. $data = [];
  291. $data["jewel"] = $this->auth->jewel;
  292. $data["playdetail"] = config("site.playdetail");
  293. $data['pay_config'] = Db::name('egg_timesprice')->field('times,price')->where('type',$type)->order('times asc')->select();
  294. $this->success("获取成功!",$data);
  295. }
  296. }