Eggnew.php 13 KB

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