Eggnew.php 14 KB

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