Eggnew.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. * 查找下一个奖池
  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. $num = $this->request->request("num");
  46. $type = $this->request->request("type",1);
  47. $party_id = $this->request->request("party_id", 0);// 派对ID
  48. if($num <=0) {
  49. $this->error("参数错误");
  50. }
  51. $giftdata = [];
  52. $user_id = $this->auth->id;
  53. // 防止超卖
  54. $redis = new Redis();
  55. $redisconfig = config("redis");
  56. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  57. if ($redisconfig['redis_pwd']) {
  58. $redis->auth($redisconfig['redis_pwd']);
  59. }
  60. if($redisconfig['redis_selectdb'] > 0){
  61. $redis->select($redisconfig['redis_selectdb']);
  62. }
  63. $do_no = date("YmdHis").rand(10000,99999);
  64. Db::startTrans();
  65. try{
  66. // 查找正在开放的奖池
  67. $jackpot = \app\common\model\EggJackpot::where(["status"=>1,'type'=>$type])->find();
  68. if($jackpot) { // 有开放的奖池
  69. $jackpot_id = $jackpot["id"];
  70. } else { // 没有开放的奖池
  71. $jackpotInfo = \app\common\model\EggJackpot::where('type',$type)->order('weigh asc,id asc')->find();
  72. $jackpot_id = $jackpotInfo["id"];
  73. \app\common\model\EggJackpot::update(["status"=>1],["id"=>$jackpot_id]);//打开
  74. }
  75. // 查找奖池对应的奖池礼物
  76. $jackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$jackpot_id,"is_use"=>0])->select();
  77. $giftCount = count($jackpotGift);
  78. $next_jackpot_id = $this->getNextJackpot($jackpot_id);
  79. if($giftCount <= $num) {
  80. // 先获取$giftCount个礼物
  81. $giftArr1 = $jackpotGift;
  82. // 更新礼物抽取状态
  83. $giftids = array_column($giftArr1,"id");
  84. $res1 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  85. $res1Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  86. // 更新奖池
  87. $res2 = \app\common\model\EggJackpot::update(["status"=>0],["tes"=>1]);
  88. $res3 = \app\common\model\EggJackpot::update(["status"=>1],["id"=>$next_jackpot_id]);
  89. // 下个奖池所有礼物改为未使用
  90. $res4 = \app\common\model\EggGift::update(["is_use"=>0],["Jackpot_id"=>$next_jackpot_id]);
  91. // 获取下个奖池礼物
  92. $nextjackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$next_jackpot_id])->select();
  93. $newnum = $num-$giftCount;
  94. if($newnum <= 0) {
  95. $giftArr = [];
  96. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  97. if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
  98. $giftids = array_rand($giftArr,50);
  99. // 更新礼物抽取状态
  100. \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  101. $res5 = \app\common\model\EggGift::update(["updatetime"=>time()],["id"=>["in",$giftids]]);
  102. $giftdata = $giftArr1;
  103. } else {
  104. $giftArr = [];
  105. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  106. if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
  107. $giftids = array_rand($giftArr,$newnum);
  108. // 更新礼物抽取状态
  109. $res5Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  110. $res5 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  111. $giftdata = array_merge($giftArr1,$res5Info);
  112. }
  113. } else {
  114. // 随机抽取$num个礼物
  115. $giftArr = [];
  116. foreach($jackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  117. $giftids = array_rand($giftArr,$num);
  118. // 更新礼物抽取状态
  119. $res1 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  120. $res1Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  121. // ------------------ 附加逻辑开始 ---------------------//
  122. // 更新奖池
  123. $res2 = \app\common\model\EggJackpot::update(["updatetime"=>time()],["tes"=>1]);
  124. $res3 = \app\common\model\EggJackpot::update(["updatetime"=>time()],["id"=>$next_jackpot_id]);
  125. // 下个奖池所有礼物改为未使用
  126. $res4 = \app\common\model\EggGift::update(["updatetime"=>time()],["Jackpot_id"=>$next_jackpot_id]);
  127. // 获取下个奖池礼物
  128. $nextjackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$next_jackpot_id])->select();
  129. $giftArr = [];
  130. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  131. if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
  132. $giftids = array_rand($giftArr,50);
  133. // 更新礼物抽取状态
  134. \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  135. $res5 = \app\common\model\EggGift::update(["updatetime"=>time()],["id"=>["in",$giftids]]);
  136. // ------------------ 附加逻辑结束 ---------------------//
  137. $giftdata = $res1Info;
  138. }
  139. if(!$giftdata) $this->error("系统错误!");
  140. $data = [];
  141. foreach($giftdata as $k => $v) {
  142. // 保存砸蛋记录
  143. $data[] = [
  144. "do_no" => $do_no,
  145. "user_id" => $user_id,
  146. "egg_gift_id" => $v["gift_id"],
  147. "image" => $v["image"],
  148. "special" => $v["special"],
  149. "gift_name" => $v["gift_name"],
  150. "Jackpot_id" => $v["Jackpot_id"],
  151. "prize_no" => $v["prize_no"],
  152. "price" => $v["price"],
  153. "createtime" => time(),
  154. ];
  155. // 添加用户背包
  156. $backdata[] = [
  157. "user_id" => $user_id,
  158. 'gift_id' => $v["gift_id"],
  159. "name" => $v["gift_name"],
  160. "image" => $v["image"],
  161. "gif_image" => $v["special"],
  162. "value" => $v["price"],
  163. "number" => 1,
  164. "is_use" => 0,
  165. "get_way" => 1,
  166. "createtime" => time(),
  167. ];
  168. }
  169. $data && $res5 = \app\common\model\EggDo::insertAll($data);
  170. // 扣除用户小锤子
  171. $res6 = \app\common\model\User::where(["id"=>$user_id])->setDec("hammer",$num);
  172. // 添加到用户背包
  173. $backdata && $res7 = \app\common\model\GiftBack::insertAll($backdata);
  174. if($res1 !== false && $res2 && $res3 && $res4 && $res5 && $res6 && $res7) {
  175. Db::commit();
  176. $eggStrikeNotice = config("site.eggStrikeNotice");
  177. if($party_id){
  178. $partyInfo = \app\common\model\Party::field("id,room_type,party_name")->where(["id"=>$party_id])->find();
  179. foreach ($backdata as $backdatum) {
  180. $realMoney = $backdatum['value'] / 100;
  181. if ($realMoney >= $eggStrikeNotice){
  182. $giftUserParty = ['number'=>$backdatum['number'],'gift_name'=>$backdatum['name'],'gift_gif_image'=>$backdatum['image']];
  183. QueueApi::sendGroupMessage(73, '', $this->auth->nickname, $partyInfo, $giftUserParty);
  184. }
  185. }
  186. }
  187. } else {
  188. // 返还锤子
  189. for($i=1;$i<=$num;$i++) {
  190. $val = date("YmdHis").$i;
  191. $redis->lpush("hammer_num_".$user_id,$val);
  192. }
  193. }
  194. }catch (ValidateException $e) {
  195. Db::rollback();
  196. $this->error($e->getMessage());
  197. } catch (PDOException $e) {
  198. Db::rollback();
  199. $this->error($e->getMessage());
  200. } catch (Exception $e) {
  201. Db::rollback();
  202. $this->error($e->getMessage());
  203. }
  204. // 返回抽到的礼物列表
  205. $list = \app\common\model\EggDo::where(["do_no"=>$do_no])->select();
  206. $this->success("获取成功!",$list);
  207. }
  208. /**
  209. * 获取排行榜
  210. */
  211. public function getRankList() {
  212. $time = $this->request->request("time"); // 1=今天,2=昨天
  213. if(!in_array($time,[1,2])) {
  214. $this->error("参数缺失!");
  215. }
  216. // 先根据用户抽奖总值排序 筛选
  217. $today = strtotime(date("Y-m-d 00:00:00"));
  218. $yestoday = $today - 86400;
  219. $where = [];
  220. $time == 1 && $where["a.createtime"] = ["gt",$today];
  221. $time == 2 && $where["a.createtime"] = ["between","$yestoday,$today"];
  222. $ranklist = \app\common\model\EggDo::alias("a")->field("a.user_id,sum(a.price) as money,u.avatar,u.nickname")
  223. ->join("hx_user u","u.id = a.user_id","inner")
  224. ->where($where)
  225. ->group("a.user_id")
  226. ->order("money","desc")
  227. ->limit(20)
  228. ->select();
  229. if(!$ranklist) $this->success("获取成功!",[]);
  230. foreach($ranklist as $k => $v) {
  231. // 查询
  232. $where["user_id"] = $v["user_id"];
  233. $ranklist[$k]["gifts"] = \app\common\model\EggDo::alias("a")
  234. ->field("image,count(id) as number,sum(a.price) as money")
  235. ->limit(3)
  236. ->where($where)
  237. ->group("egg_gift_id")
  238. ->order("money","desc")
  239. ->select();
  240. }
  241. $this->success("获取成功!",$ranklist);
  242. }
  243. /**
  244. * 获取砸蛋基本信息
  245. */
  246. public function getBaseInfo() {
  247. $user_id = $this->auth->id;
  248. // 构建数据
  249. $data = [];
  250. $data["jewel"] = $this->auth->jewel;
  251. $data["playdetail"] = config("site.playdetail");
  252. $this->success("获取成功!",$data);
  253. }
  254. }