Eggnew.php 13 KB

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