Eggnew.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 = createUniqueNo('E',$user_id);
  64. Db::startTrans();
  65. try{
  66. // 查找正在开放的奖池
  67. $jackpot = \app\common\model\EggJackpot::where(["status"=>1,'type'=>$type])->find();
  68. if($jackpot) { // 有开放的奖池
  69. // \app\common\model\EggJackpot::order("id","asc")->find();
  70. $jackpot_id = $jackpot["id"];
  71. } else { // 没有开放的奖池
  72. $jackpotInfo = \app\common\model\EggJackpot::where('type',$type)->order('weigh asc,id asc')->find();
  73. if(!empty($jackpotInfo)){
  74. Db::rollback();
  75. $this->error('暂时还没有奖池');
  76. }
  77. $jackpot_id = $jackpotInfo["id"];
  78. \app\common\model\EggJackpot::update(["status"=>1],["id"=>$jackpot_id]);//打开
  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. // $res1Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  91. // 更新奖池
  92. $res2 = \app\common\model\EggJackpot::update(["status"=>0],["id"=>$jackpot_id]);
  93. $res3 = \app\common\model\EggJackpot::update(["status"=>1],["id"=>$next_jackpot_id]);
  94. // 下个奖池所有礼物改为未使用
  95. // $res4 = \app\common\model\EggGift::update(["is_use"=>0],["Jackpot_id"=>$next_jackpot_id]);
  96. $res4 = true;
  97. // 获取下个奖池礼物
  98. $nextjackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$next_jackpot_id])->select();
  99. $newnum = $num-$giftCount;
  100. if($newnum == 0) {
  101. /*
  102. $giftArr = [];
  103. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  104. if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
  105. $giftids = array_rand($giftArr,50);
  106. // 更新礼物抽取状态
  107. \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  108. $res5 = \app\common\model\EggGift::update(["updatetime"=>time()],["id"=>["in",$giftids]]);
  109. */
  110. $res5 = true;
  111. $giftdata = $giftArr1;
  112. } else {
  113. if($newnum > count($nextjackpotGift)){
  114. Db::rollback();
  115. $this->error("奖池礼物待更新,请耐心等待!");
  116. }
  117. $giftArr = [];
  118. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  119. if(!$giftArr){
  120. Db::rollback();
  121. $this->error("奖池礼物待更新,请耐心等待!");
  122. }
  123. $giftids = array_rand($giftArr,$newnum);
  124. // 更新礼物抽取状态
  125. $res5Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  126. $res5 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  127. $giftdata = array_merge($giftArr1,$res5Info);
  128. }
  129. } else {
  130. // 随机抽取$num个礼物
  131. $giftArr = [];
  132. foreach($jackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  133. $giftids = array_rand($giftArr,$num);
  134. // 更新礼物抽取状态
  135. $res1 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  136. $res1Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  137. /*
  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. */
  155. $res2 = true;
  156. $res3 = true;
  157. $res4 = true;
  158. $res5 = true;
  159. $giftdata = $res1Info;
  160. }
  161. if(!$giftdata) {
  162. Db::rollback();
  163. $this->error('礼物为空');
  164. }
  165. $data = [];
  166. foreach($giftdata as $k => $v) {
  167. // 保存砸蛋记录
  168. $data[] = [
  169. "do_no" => $do_no,
  170. "user_id" => $user_id,
  171. "gift_id" => $v["gift_id"],
  172. "egg_gift_id" => $v["id"],
  173. "image" => $v["image"],
  174. "special" => $v["special"],
  175. "gift_name" => $v["gift_name"],
  176. "Jackpot_id" => $v["Jackpot_id"],
  177. "prize_no" => $v["prize_no"],
  178. "price" => $v["price"],
  179. "createtime" => time(),
  180. ];
  181. // 添加用户背包
  182. $backdata[] = [
  183. "user_id" => $user_id,
  184. 'gift_id' => $v["gift_id"],
  185. "name" => $v["gift_name"],
  186. "image" => $v["image"],
  187. "gif_image" => $v["special"],
  188. "value" => $v["price"],
  189. "number" => 1,
  190. "is_use" => 0,
  191. "get_way" => 1,
  192. "createtime" => time(),
  193. ];
  194. }
  195. $data && $res5 = \app\common\model\EggDo::insertAll($data);
  196. // 扣除用户小锤子
  197. $res6 = true;
  198. // 添加到用户背包
  199. $backdata && $res7 = \app\common\model\GiftBack::insertAll($backdata);
  200. if($res1 !== false && $res2 !== false && $res3 !== false && $res4 !== false && $res5 !== false && $res6 && $res7) {
  201. Db::commit();
  202. /*$eggStrikeNotice = config("site.eggStrikeNotice");
  203. if($party_id){
  204. $partyInfo = \app\common\model\Party::field("id,room_type,party_name")->where(["id"=>$party_id])->find();
  205. foreach ($backdata as $backdatum) {
  206. $realMoney = $backdatum['value'] / 100;
  207. if ($realMoney >= $eggStrikeNotice){
  208. $giftUserParty = ['number'=>$backdatum['number'],'gift_name'=>$backdatum['name'],'gift_gif_image'=>$backdatum['image']];
  209. QueueApi::sendGroupMessage(73, '', $this->auth->nickname, $partyInfo, $giftUserParty);
  210. }
  211. }
  212. }*/
  213. } else {
  214. Db::rollback();
  215. $this->error('开奖失败');
  216. }
  217. }catch (ValidateException $e) {
  218. Db::rollback();
  219. $this->error($e->getMessage());
  220. } catch (PDOException $e) {
  221. Db::rollback();
  222. $this->error($e->getMessage());
  223. } catch (Exception $e) {
  224. Db::rollback();
  225. $this->error($e->getMessage());
  226. }
  227. // 返回抽到的礼物列表
  228. $list = \app\common\model\EggDo::where(["do_no"=>$do_no])->select();
  229. $this->success("获取成功!",$list);
  230. }
  231. /**
  232. * 获取排行榜
  233. */
  234. public function getRankList() {
  235. $time = $this->request->request("time"); // 1=今天,2=昨天
  236. if(!in_array($time,[1,2])) {
  237. $this->error("参数缺失!");
  238. }
  239. // 先根据用户抽奖总值排序 筛选
  240. $today = strtotime(date("Y-m-d 00:00:00"));
  241. $yestoday = $today - 86400;
  242. $where = [];
  243. $time == 1 && $where["a.createtime"] = ["gt",$today];
  244. $time == 2 && $where["a.createtime"] = ["between","$yestoday,$today"];
  245. $ranklist = \app\common\model\EggDo::alias("a")->field("a.user_id,sum(a.price) as money,u.avatar,u.nickname")
  246. ->join("hx_user u","u.id = a.user_id","inner")
  247. ->where($where)
  248. ->group("a.user_id")
  249. ->order("money","desc")
  250. ->limit(20)
  251. ->select();
  252. if(!$ranklist) $this->success("获取成功!",[]);
  253. foreach($ranklist as $k => $v) {
  254. // 查询
  255. $where["user_id"] = $v["user_id"];
  256. $ranklist[$k]["gifts"] = \app\common\model\EggDo::alias("a")
  257. ->field("image,count(id) as number,sum(a.price) as money")
  258. ->limit(3)
  259. ->where($where)
  260. ->group("gift_id")
  261. ->order("money","desc")
  262. ->select();
  263. }
  264. $this->success("获取成功!",$ranklist);
  265. }
  266. /**
  267. * 获取砸蛋基本信息
  268. */
  269. public function getBaseInfo() {
  270. $user_id = $this->auth->id;
  271. // 构建数据
  272. $data = [];
  273. $data["jewel"] = $this->auth->jewel;
  274. $data["playdetail"] = config("site.playdetail");
  275. $this->success("获取成功!",$data);
  276. }
  277. }