Eggnew.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. $total_jewel = Db::name('egg_timesprice')->where('type',$type)->where('times',$num)->find();
  52. if(empty($total_jewel)){
  53. $this->error('错误的次数');
  54. }
  55. $total_jewel = $total_jewel['price'];
  56. $giftdata = [];
  57. $user_id = $this->auth->id;
  58. $do_no = createUniqueNo('E',$user_id);
  59. Db::startTrans();
  60. try{
  61. // 查找正在开放的奖池
  62. $jackpot = \app\common\model\EggJackpot::where(["status"=>1,'type'=>$type])->find();
  63. if($jackpot) { // 有开放的奖池
  64. // \app\common\model\EggJackpot::order("id","asc")->find();
  65. $jackpot_id = $jackpot["id"];
  66. } else { // 没有开放的奖池
  67. $jackpotInfo = \app\common\model\EggJackpot::where('type',$type)->order('weigh asc,id asc')->find();
  68. if(!empty($jackpotInfo)){
  69. Db::rollback();
  70. $this->error('暂时还没有奖池');
  71. }
  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"=>-1],["id"=>$jackpot_id]);
  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. /*
  96. $giftArr = [];
  97. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  98. if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
  99. $giftids = array_rand($giftArr,50);
  100. // 更新礼物抽取状态
  101. \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  102. $res5 = \app\common\model\EggGift::update(["updatetime"=>time()],["id"=>["in",$giftids]]);
  103. */
  104. $res5 = true;
  105. $giftdata = $giftArr1;
  106. } else {
  107. if($newnum > count($nextjackpotGift)){
  108. Db::rollback();
  109. $this->error("奖池礼物待更新,请耐心等待!");
  110. }
  111. $giftArr = [];
  112. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  113. if(!$giftArr){
  114. Db::rollback();
  115. $this->error("奖池礼物待更新,请耐心等待!");
  116. }
  117. $giftids = array_rand($giftArr,$newnum);
  118. // 更新礼物抽取状态
  119. $res5Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  120. $res5 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  121. $giftdata = array_merge($giftArr1,$res5Info);
  122. }
  123. } else {
  124. // 随机抽取$num个礼物
  125. $giftArr = [];
  126. foreach($jackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  127. $giftids = array_rand($giftArr,$num);
  128. // 更新礼物抽取状态
  129. $res1 = \app\common\model\EggGift::update(["is_use"=>1],["id"=>["in",$giftids]]);
  130. $res1Info = \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  131. /*
  132. // ------------------ 附加逻辑开始 ---------------------//
  133. // 更新奖池
  134. $res2 = \app\common\model\EggJackpot::update(["updatetime"=>time()],["tes"=>1]);
  135. $res3 = \app\common\model\EggJackpot::update(["updatetime"=>time()],["id"=>$next_jackpot_id]);
  136. // 下个奖池所有礼物改为未使用
  137. $res4 = \app\common\model\EggGift::update(["updatetime"=>time()],["Jackpot_id"=>$next_jackpot_id]);
  138. // 获取下个奖池礼物
  139. $nextjackpotGift = \app\common\model\EggGift::where(["Jackpot_id"=>$next_jackpot_id])->select();
  140. $giftArr = [];
  141. foreach($nextjackpotGift as $k => $v) $giftArr[$v["id"]] = $v;
  142. if(!$giftArr) $this->error("奖池礼物待更新,请耐心等待!");
  143. $giftids = array_rand($giftArr,50);
  144. // 更新礼物抽取状态
  145. \app\common\model\EggGift::where(["id"=>["in",$giftids]])->select();
  146. $res5 = \app\common\model\EggGift::update(["updatetime"=>time()],["id"=>["in",$giftids]]);
  147. // ------------------ 附加逻辑结束 ---------------------//
  148. */
  149. $res2 = true;
  150. $res3 = true;
  151. $res4 = true;
  152. $res5 = true;
  153. $giftdata = $res1Info;
  154. }
  155. if(!$giftdata) {
  156. Db::rollback();
  157. $this->error('礼物为空');
  158. }
  159. $data = [];
  160. foreach($giftdata as $k => $v) {
  161. // 保存砸蛋记录
  162. $data[] = [
  163. "do_no" => $do_no,
  164. "party_id" => $party_id,
  165. "user_id" => $user_id,
  166. "gift_id" => $v["gift_id"],
  167. "egg_gift_id" => $v["id"],
  168. "image" => $v["image"],
  169. "special" => $v["special"],
  170. "gift_name" => $v["gift_name"],
  171. "Jackpot_id" => $v["Jackpot_id"],
  172. "prize_no" => $v["prize_no"],
  173. "price" => $v["price"],
  174. "createtime" => time(),
  175. ];
  176. // 添加用户背包
  177. $backdata[] = [
  178. "user_id" => $user_id,
  179. 'gift_id' => $v["gift_id"],
  180. "name" => $v["gift_name"],
  181. "image" => $v["image"],
  182. "gif_image" => $v["special"],
  183. "value" => $v["price"],
  184. "number" => 1,
  185. "is_use" => 0,
  186. "get_way" => 1,
  187. "createtime" => time(),
  188. ];
  189. }
  190. $data && $res5 = \app\common\model\EggDo::insertAll($data);
  191. // 扣除用户小锤子
  192. $res6 = true;
  193. // 添加到用户背包
  194. $backdata && $res7 = \app\common\model\GiftBack::insertAll($backdata);
  195. //扣钻石
  196. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$total_jewel,'-',0,'开箱子和大转盘',13,'jewel');
  197. if($rs_wallet['status'] === false){
  198. $this->error($rs_wallet['msg']);
  199. Db::rollback();
  200. }
  201. if($res1 !== false && $res2 !== false && $res3 !== false && $res4 !== false && $res5 !== false && $res6 && $res7) {
  202. Db::commit();
  203. /*$eggStrikeNotice = config("site.eggStrikeNotice");
  204. if($party_id){
  205. $partyInfo = \app\common\model\Party::field("id,room_type,party_name")->where(["id"=>$party_id])->find();
  206. foreach ($backdata as $backdatum) {
  207. $realMoney = $backdatum['value'] / 100;
  208. if ($realMoney >= $eggStrikeNotice){
  209. $giftUserParty = ['number'=>$backdatum['number'],'gift_name'=>$backdatum['name'],'gift_gif_image'=>$backdatum['image']];
  210. QueueApi::sendGroupMessage(73, '', $this->auth->nickname, $partyInfo, $giftUserParty);
  211. }
  212. }
  213. }*/
  214. } else {
  215. Db::rollback();
  216. $this->error('开奖失败');
  217. }
  218. }catch (ValidateException $e) {
  219. Db::rollback();
  220. $this->error($e->getMessage());
  221. } catch (PDOException $e) {
  222. Db::rollback();
  223. $this->error($e->getMessage());
  224. } catch (Exception $e) {
  225. Db::rollback();
  226. $this->error($e->getMessage());
  227. }
  228. // 返回抽到的礼物列表
  229. $result = [
  230. 'list' => [],
  231. 'bigshow' => [],
  232. ];
  233. $list = Db::name('egg_do')->alias('do')->join('gift','do.gift_id = gift.id','LEFT')->field('do.*,gift.is_big')->where('do.do_no',$do_no)->order('gift.is_big desc,gift.value desc')->select();
  234. $result['list'] = $list;
  235. //只弹一个大礼物,专门拿出结构
  236. if(!empty($list)){
  237. if($list[0]['is_big'] == 1){
  238. $result['bigshow'] = $list[0];
  239. }
  240. }
  241. $this->success("获取成功!",$result);
  242. }
  243. //手气榜单
  244. public function getRankList(){
  245. $today = strtotime(date("Y-m-d"));
  246. $where = [];
  247. $where["a.createtime"] = ["gt",$today];
  248. $ranklist = \app\common\model\EggDo::alias("a")->field("a.user_id,a.image,a.price as money,a.createtime,u.nickname")
  249. ->join("hx_user u","u.id = a.user_id","inner")
  250. ->where($where)
  251. // ->group("a.user_id")
  252. ->order("money","desc")
  253. ->limit(20)
  254. ->select();
  255. $this->success("获取成功!",$ranklist);
  256. }
  257. /**
  258. * 获取排行榜
  259. */
  260. public function getRankList_old() {
  261. $time = $this->request->request("time"); // 1=今天,2=昨天
  262. if(!in_array($time,[1,2])) {
  263. $this->error("参数缺失!");
  264. }
  265. // 先根据用户抽奖总值排序 筛选
  266. $today = strtotime(date("Y-m-d 00:00:00"));
  267. $yestoday = $today - 86400;
  268. $where = [];
  269. $time == 1 && $where["a.createtime"] = ["gt",$today];
  270. $time == 2 && $where["a.createtime"] = ["between","$yestoday,$today"];
  271. $ranklist = \app\common\model\EggDo::alias("a")->field("a.user_id,sum(a.price) as money,u.avatar,u.nickname")
  272. ->join("hx_user u","u.id = a.user_id","inner")
  273. ->where($where)
  274. ->group("a.user_id")
  275. ->order("money","desc")
  276. ->limit(20)
  277. ->select();
  278. if(!$ranklist) $this->success("获取成功!",[]);
  279. foreach($ranklist as $k => $v) {
  280. // 查询
  281. $where["user_id"] = $v["user_id"];
  282. $ranklist[$k]["gifts"] = \app\common\model\EggDo::alias("a")
  283. ->field("image,count(id) as number,sum(a.price) as money")
  284. ->limit(3)
  285. ->where($where)
  286. ->group("gift_id")
  287. ->order("money","desc")
  288. ->select();
  289. }
  290. $this->success("获取成功!",$ranklist);
  291. }
  292. /**
  293. * 获取砸蛋基本信息
  294. */
  295. public function getBaseInfo() {
  296. $type = input('type',1);
  297. // 构建数据
  298. $data = [];
  299. $data["jewel"] = $this->auth->jewel;
  300. $data["playdetail"] = config("site.playdetail");
  301. $data['pay_config'] = Db::name('egg_timesprice')->field('times,price')->where('type',$type)->order('times asc')->select();
  302. $this->success("获取成功!",$data);
  303. }
  304. }