Egg.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 Egg extends Api
  11. {
  12. protected $noNeedLogin = ['getThisJackpot'];
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 获取本期奖池列表
  16. */
  17. public function getThisJackpot() {
  18. $jackId = \app\common\model\EggJackpot::where(["status"=>1])->value("id");
  19. $egggiftModel = new \app\common\model\EggGift();
  20. $where = [];
  21. $where["Jackpot_id"] = $jackId;
  22. $egggiftList = $egggiftModel->where($where)->group("gift_id")->order("price","desc")->select();
  23. return $this->success("获取成功!",$egggiftList);
  24. }
  25. /**
  26. * 购买锤子
  27. */
  28. public function buyHammer() {
  29. // 接口防并发
  30. if (!$this->apiLimit(1, 2000)) {
  31. $this->error(__('Operation frequently'));
  32. }
  33. $num = $this->request->request("num");
  34. if($num <=0) {
  35. $this->error("购买数量不能小于0!");
  36. }
  37. $user_id = $this->auth->id;
  38. $hammerPrice = config("site.hammerPrice");
  39. if($hammerPrice <=0) {
  40. $this->error("系统配置出错!");
  41. }
  42. $userModel = new \app\common\model\User;
  43. $userInfo = $userModel->where('id',$user_id)->lock(true)->find();
  44. $money = $hammerPrice*$num;
  45. // 判断用户钻石余额是否充足
  46. if($userInfo->jewel < $money) {
  47. $this->error("您的钻石余额不足!",null,100);
  48. }
  49. $userjewellogModel = new \app\common\model\UserJewelLog();
  50. Db::startTrans();
  51. try{
  52. $userjewel = $userInfo->jewel;
  53. // 添加购买订单
  54. $data = [];
  55. $data["user_id"] = $user_id;
  56. $data["price"] = $hammerPrice;
  57. $data["number"] = $num;
  58. $data["money"] = $money;
  59. $data["createtime"] = time();
  60. $res1 = \app\common\model\EggOrder::insert($data);
  61. // 为用户增加锤子
  62. $res2 = $userInfo->setInc("hammer",$num);
  63. // 扣除用户钻石并生成钻石变动记录
  64. $where = [];
  65. $where["id"] = $user_id;
  66. $res3 = $userInfo->setDec("jewel",$money);
  67. $res4 = $userjewellogModel->addUserJewelLog($user_id, $money, "-", $userjewel, "购买{$num}锤", 2);
  68. $redis = new Redis();
  69. $redisconfig = config("redis");
  70. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  71. if ($redisconfig['redis_pwd']) {
  72. $redis->auth($redisconfig['redis_pwd']);
  73. }
  74. if($redisconfig['redis_selectdb'] > 0){
  75. $redis->select($redisconfig['redis_selectdb']);
  76. }
  77. for($i=1;$i<=$num;$i++) {
  78. $val = date("YmdHis").$i;
  79. $redis->lpush("hammer_num_".$user_id,$val);
  80. }
  81. if($res1 && $res2 && $res3 && $res4) {
  82. Db::commit();
  83. $this->success("购买成功!");
  84. } else {
  85. $this->error("购买失败!");
  86. }
  87. }catch (ValidateException $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. } catch (PDOException $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. } catch (Exception $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. }
  98. /**
  99. * 查找下一个奖池
  100. */
  101. public function getNextJackpot($jackpot_id) {
  102. $jackpotIds = \app\common\model\EggJackpot::order("id","asc")->column("id");
  103. $next = 0;
  104. foreach($jackpotIds as $k => $v) {
  105. if($v == $jackpot_id) {
  106. $next = isset($jackpotIds[$k+1])?$jackpotIds[$k+1]:$jackpotIds[0];
  107. }
  108. }
  109. return $next;
  110. }
  111. /**
  112. * 砸金蛋
  113. */
  114. public function strike() {
  115. // $this->error("系统正在维护中。。。");
  116. $num = $this->request->request("num");
  117. $party_id = $this->request->request("party_id", 0);// 派对ID
  118. if($num <=0) {
  119. $this->error("参数错误");
  120. }
  121. $giftdata = [];
  122. $user_id = $this->auth->id;
  123. // 防止超卖
  124. $redis = new Redis();
  125. $redisconfig = config("redis");
  126. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  127. if ($redisconfig['redis_pwd']) {
  128. $redis->auth($redisconfig['redis_pwd']);
  129. }
  130. if($redisconfig['redis_selectdb'] > 0){
  131. $redis->select($redisconfig['redis_selectdb']);
  132. }
  133. // if($redis->lLen("hammer_num_".$user_id) < $num) $this->error("小金锤数量不足!");
  134. $do_no = date("YmdHis").rand(10000,99999);
  135. $userinfo = \app\common\model\User::lock(true)->find($this->auth->id);
  136. if($userinfo->hammer < $num) {
  137. $this->error("小金锤数量不足!");
  138. }
  139. for($i=1;$i<=$num;$i++) { // 扣除
  140. $redis->lpop("hammer_num_".$user_id);
  141. }
  142. Db::startTrans();
  143. try{
  144. // 查找正在开放的奖池
  145. $jackpot = \app\common\model\EggJackpot::where(["status"=>1])->find();
  146. if($jackpot) { // 有开放的奖池
  147. \app\common\model\EggJackpot::order("id","asc")->find();
  148. $jackpot_id = $jackpot["id"];
  149. } else { // 没有开放的奖池
  150. $jackpotInfo = \app\common\model\EggJackpot::order("id","asc")->find();
  151. $jackpot_id = $jackpotInfo["id"];
  152. \app\common\model\EggJackpot::update(["status"=>1],["id"=>$jackpot_id]);
  153. }
  154. // 查找奖池对应的奖池礼物
  155. $jackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$jackpot_id,"is_use"=>0])->select();
  156. $giftCount = count($jackpotGift);
  157. $next_jackpot_id = $this->getNextJackpot($jackpot_id);
  158. if($giftCount <= $num) {
  159. // 先获取$giftCount个礼物
  160. $giftArr1 = $jackpotGift;
  161. // 更新礼物抽取状态
  162. $giftids = array_column($giftArr1,"id");
  163. $res1 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  164. $res1Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  165. // 更新奖池
  166. $res2 = \app\common\model\EggJackpot::update(["status"=>0],["tes"=>1]);
  167. $res3 = \app\common\model\EggJackpot::update(["status"=>1],["id"=>$next_jackpot_id]);
  168. // 下个奖池所有礼物改为未使用
  169. $res4 = \app\common\model\EggGift::update(["is_use"=>0],["Jackpot_id"=>$next_jackpot_id]);
  170. // 获取下个奖池礼物
  171. $nextjackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$next_jackpot_id])->select();
  172. $newnum = $num-$giftCount;
  173. if($newnum <= 0) {
  174. $giftArr = [];
  175. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  176. if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
  177. $giftids = array_rand($giftArr,50);
  178. // 更新礼物抽取状态
  179. \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  180. $res5 = \app\common\model\EggGift::update(["updatetime"=>time()],["id"=>["in",$giftids]]);
  181. $giftdata = $giftArr1;
  182. } else {
  183. $giftArr = [];
  184. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  185. if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
  186. $giftids = array_rand($giftArr,$newnum);
  187. // 更新礼物抽取状态
  188. $res5Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  189. $res5 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  190. $giftdata = array_merge($giftArr1,$res5Info);
  191. }
  192. } else {
  193. // 随机抽取$num个礼物
  194. $giftArr = [];
  195. foreach($jackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  196. $giftids = array_rand($giftArr,$num);
  197. // 更新礼物抽取状态
  198. $res1 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  199. $res1Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  200. // ------------------ 附加逻辑开始 ---------------------//
  201. // 更新奖池
  202. $res2 = \app\common\model\EggJackpot::update(["updatetime"=>time()],["tes"=>1]);
  203. $res3 = \app\common\model\EggJackpot::update(["updatetime"=>time()],["id"=>$next_jackpot_id]);
  204. // 下个奖池所有礼物改为未使用
  205. $res4 = \app\common\model\EggGift::update(["updatetime"=>time()],["Jackpot_id"=>$next_jackpot_id]);
  206. // 获取下个奖池礼物
  207. $nextjackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$next_jackpot_id])->select();
  208. $giftArr = [];
  209. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  210. if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
  211. $giftids = array_rand($giftArr,50);
  212. // 更新礼物抽取状态
  213. \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  214. $res5 = \app\common\model\EggGift::update(["updatetime"=>time()],["id"=>["in",$giftids]]);
  215. // ------------------ 附加逻辑结束 ---------------------//
  216. $giftdata = $res1Info;
  217. }
  218. if(!$giftdata) $this->error("系统错误!");
  219. $data = [];
  220. foreach($giftdata as $k => $v) {
  221. // 保存砸蛋记录
  222. $data[] = [
  223. "do_no" => $do_no,
  224. "user_id" => $user_id,
  225. "egg_gift_id" => $v["gift_id"],
  226. "image" => $v["image"],
  227. "special" => $v["special"],
  228. "gift_name" => $v["gift_name"],
  229. "Jackpot_id" => $v["Jackpot_id"],
  230. "prize_no" => $v["prize_no"],
  231. "price" => $v["price"],
  232. "createtime" => time(),
  233. ];
  234. // 添加用户背包
  235. $backdata[] = [
  236. "user_id" => $user_id,
  237. 'gift_id' => $v["gift_id"],
  238. "name" => $v["gift_name"],
  239. "image" => $v["image"],
  240. "gif_image" => $v["special"],
  241. "value" => $v["price"],
  242. "number" => 1,
  243. "is_use" => 0,
  244. "get_way" => 1,
  245. "createtime" => time(),
  246. ];
  247. }
  248. $data && $res5 = \app\common\model\EggDo::insertAll($data);
  249. // 扣除用户小锤子
  250. $res6 = \app\common\model\User::where(["id"=>$user_id])->setDec("hammer",$num);
  251. // 添加到用户背包
  252. $backdata && $res7 = \app\common\model\GiftBack::insertAll($backdata);
  253. if($res1 !== false && $res2 && $res3 && $res4 && $res5 && $res6 && $res7) {
  254. Db::commit();
  255. $eggStrikeNotice = config("site.eggStrikeNotice");
  256. if($party_id){
  257. $partyInfo = \app\common\model\Party::field("id,room_type,party_name")->where(["id"=>$party_id])->find();
  258. foreach ($backdata as $backdatum) {
  259. $realMoney = $backdatum['value'] / 100;
  260. if ($realMoney >= $eggStrikeNotice){
  261. $giftUserParty = ['number'=>$backdatum['number'],'gift_name'=>$backdatum['name'],'gift_gif_image'=>$backdatum['image']];
  262. QueueApi::sendGroupMessage(73, '', $this->auth->nickname, $partyInfo, $giftUserParty);
  263. }
  264. }
  265. }
  266. } else {
  267. // 返还锤子
  268. for($i=1;$i<=$num;$i++) {
  269. $val = date("YmdHis").$i;
  270. $redis->lpush("hammer_num_".$user_id,$val);
  271. }
  272. }
  273. }catch (ValidateException $e) {
  274. Db::rollback();
  275. $this->error($e->getMessage());
  276. } catch (PDOException $e) {
  277. Db::rollback();
  278. $this->error($e->getMessage());
  279. } catch (Exception $e) {
  280. Db::rollback();
  281. $this->error($e->getMessage());
  282. }
  283. // 返回抽到的礼物列表
  284. $list = \app\common\model\EggDo::where(["do_no"=>$do_no])->select();
  285. $this->success("获取成功!",$list);
  286. }
  287. /**
  288. * 获取排行榜
  289. */
  290. public function getRankList() {
  291. $time = $this->request->request("time"); // 1=今天,2=昨天
  292. if(!in_array($time,[1,2])) {
  293. $this->error("参数缺失!");
  294. }
  295. // 先根据用户抽奖总值排序 筛选
  296. $today = strtotime(date("Y-m-d 00:00:00"));
  297. $yestoday = $today - 86400;
  298. $where = [];
  299. $time == 1 && $where["a.createtime"] = ["gt",$today];
  300. $time == 2 && $where["a.createtime"] = ["between","$yestoday,$today"];
  301. $ranklist = \app\common\model\EggDo::alias("a")->field("a.user_id,sum(a.price) as money,u.avatar,u.nickname")
  302. ->join("hx_user u","u.id = a.user_id","inner")
  303. ->where($where)
  304. ->group("a.user_id")
  305. ->order("money","desc")
  306. ->limit(20)
  307. ->select();
  308. if(!$ranklist) $this->success("获取成功!",[]);
  309. foreach($ranklist as $k => $v) {
  310. // 查询
  311. $where["user_id"] = $v["user_id"];
  312. $ranklist[$k]["gifts"] = \app\common\model\EggDo::alias("a")
  313. ->field("image,count(id) as number,sum(a.price) as money")
  314. ->limit(3)
  315. ->where($where)
  316. ->group("egg_gift_id")
  317. ->order("money","desc")
  318. ->select();
  319. }
  320. $this->success("获取成功!",$ranklist);
  321. }
  322. /**
  323. * 获取砸蛋基本信息
  324. */
  325. public function getBaseInfo() {
  326. $user_id = $this->auth->id;
  327. // 本人拥有锤子个数
  328. $hammerNum = \app\common\model\User::where(["id"=>$user_id])->value("hammer");
  329. // 构建数据
  330. $data = [];
  331. $data["hammerNum"] = $hammerNum;
  332. $data["hammerPrice"] = config("site.hammerPrice");
  333. $data["playdetail"] = config("site.playdetail");
  334. $this->success("获取成功!",$data);
  335. }
  336. }