Eggnew.php 14 KB

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