Eggnew.php 12 KB

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