Partygift.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\controller\Common;
  4. use app\common\model\PartyJoin;
  5. use fast\Random;
  6. use app\common\controller\RedisLeaderboard;
  7. use Redis;
  8. use think\Db;
  9. use think\Exception;
  10. use think\exception\PDOException;
  11. use think\exception\ValidateException;
  12. use think\Request;
  13. use app\common\service\RoomService;
  14. /**
  15. * 派对礼物接口
  16. */
  17. class Partygift extends Common
  18. {
  19. protected $noNeedLogin = [];
  20. protected $noNeedRight = ['*'];
  21. /**
  22. * 单独赠送礼物,一键清空背包
  23. */
  24. public function giveAllbackGiftToYou(){
  25. // 接口防并发
  26. if (!$this->apiLimit(1, 1000)) {
  27. $this->error(__('Operation frequently'));
  28. }
  29. $user_ids = $this->request->request("user_id");// 赠送对象
  30. $party_id = $this->request->request("party_id",0);// 派对ID
  31. $room_type = $this->request->request('room_type',1); // 房间类型
  32. $is_back = 1; // 是否背包赠送: 1=是,0=否
  33. if (!$user_ids || !in_array($room_type,[1,2]))
  34. {
  35. $this->error(__('Invalid parameters'));
  36. }
  37. //我所有的背包礼物
  38. $getMyBackGift = $this->getMyBackGift($this->auth->id);
  39. if(empty($getMyBackGift)){
  40. $this->error('背包里没有礼物了');
  41. }
  42. //依次走赠送礼物接口
  43. foreach($getMyBackGift as $backkey => $backval){
  44. $this->giveGiftToYou_back($user_ids,$backval['id'],$party_id,$room_type,$backval['number'],$is_back);
  45. }
  46. }
  47. /**
  48. * 获取我的背包礼物
  49. */
  50. private function getMyBackGift($userid) {
  51. $list = \app\common\model\GiftBack::field("id,name,sum(number) as number")
  52. ->where(["user_id"=>$userid,"is_use"=>0])
  53. ->group("name")
  54. ->select();
  55. return $list;
  56. }
  57. //giveGiftToYou 改的,只改了前几行传参,request接收改用变量接收,其他不变,完全复制
  58. private function giveGiftToYou_back($user_ids,$gift_id,$party_id = 0,$room_type = 1,$number,$is_back = 0) {
  59. if (!$user_ids || !in_array($is_back,[0,1]) || !$gift_id || !$number || !in_array($room_type,[1,2])) $this->error(__('Invalid parameters'));
  60. $user_id_arr = explode(",",$user_ids);
  61. $userCount = count($user_id_arr);
  62. $userauthid = $this->auth->id;
  63. $soundCoinRate = config("site.giftCoin"); // 声币兑换比例
  64. $userModel = new \app\common\model\User();
  65. $backGiftId = 0;
  66. $boxgiftInfo = [];
  67. if($is_back == 1) {
  68. // 获取背包礼物信息
  69. $giftInfo = \app\common\model\GiftBack::get($gift_id);
  70. if(!$giftInfo) $this->error("背包礼物获取失败");
  71. $backGiftId = $giftInfo->gift_id;
  72. // 随机获取一个礼物
  73. $allCount = $number*$userCount;
  74. $giftbackList = \app\common\model\GiftBack::where(["name"=>$giftInfo->name,"user_id"=>$userauthid,'is_use'=>0])->limit($allCount)->select();
  75. $giftInfo = isset($giftbackList[0])?$giftbackList[0]:[];
  76. $giftcount = 0;
  77. $giftList = [];
  78. if($giftbackList) foreach($giftbackList as $k => $v) {
  79. $giftcount = $giftcount + $v["number"];
  80. $giftList[$k] = $v;
  81. if($giftcount >= $allCount) {
  82. break;
  83. }
  84. }
  85. if($giftcount < $allCount) $this->error("背包数量不足");
  86. $giftValue = $giftInfo["value"] * $number;
  87. $getValue = $giftInfo["value"] * $number;
  88. } else {
  89. // // 不可以赠送给自己
  90. // if(in_array($userauthid,$user_id_arr)) $this->error("不可以赠送给自己!");
  91. // 获取礼物信息
  92. $giftModel = new \app\common\model\Gift();
  93. $where = [];
  94. $where["id"] = $gift_id;
  95. $giftInfo = $giftModel->where($where)->find();
  96. if (!$giftInfo) $this->error("请选择礼物!");
  97. $giftValue = $giftInfo["value"] * $number;
  98. $giftCountValue = $giftInfo["value"] * $number * $userCount;
  99. $getValue = $giftValue;
  100. // 判断如果是礼物盒则随机开礼物盒礼物
  101. if($giftInfo->box_type > 0) {
  102. $boxgiftInfo = $this->getBoxGift($giftInfo->box_type);
  103. $getValue = $boxgiftInfo["price"];
  104. }
  105. // 判断当前用户余额
  106. $where = [];
  107. $where["id"] = $userauthid;
  108. $userInfo = $userModel->where($where)->lock(true)->find();
  109. if (!$userInfo) $this->error("用户信息查询失败!");
  110. if($userInfo["jewel"] < $giftCountValue) $this->error("您的钻石余额不足!");
  111. }
  112. $hotValue = $getValue;
  113. $getValue = round($getValue * ($soundCoinRate/100));
  114. // 转换统计
  115. $progetValue = $hotValue - $getValue;
  116. if($progetValue >0) {
  117. $data = [];
  118. $data["user_id"] = $user_ids;
  119. $data["party_id"] = $party_id?$party_id:0;
  120. $data["gift_value"] = $hotValue;
  121. $data["plat_value"] = $hotValue - $getValue;
  122. $data["createtime"] = time();
  123. \app\common\model\UserChangeLog::insert($data);
  124. }
  125. // 转换声币后 再进行抽点设置
  126. $partyInfo = null;
  127. if(!$party_id) {
  128. $platRate = 10;
  129. $guilderRate = 30;
  130. } else {
  131. $partyInfo = \app\common\model\Party::field("user_id,platRate,guilderRate")->where(["id"=>$party_id])->find();
  132. // 获取系统配置信息
  133. $platRate = $partyInfo->platRate; // 平台抽成百分比
  134. $guilderRate = $partyInfo->guilderRate; // 工会长抽成百分比
  135. }
  136. $platValue = bcmul($platRate/100,$getValue);
  137. $guilderValue = bcmul($guilderRate/100,$getValue);
  138. $getValue = bcsub(bcsub($getValue,$platValue),$guilderValue);
  139. // $gif_image = $is_back==1?$giftInfo["gif_image"]:$giftInfo["special"];
  140. $returnData = [];
  141. Db::startTrans();
  142. try {
  143. $redis = new Redis();
  144. $redisconfig = config("redis");
  145. $redis->connect($redisconfig["host"], $redisconfig["port"], 86400 * 31);
  146. if ($redisconfig['redis_pwd']) {
  147. $redis->auth($redisconfig['redis_pwd']);
  148. }
  149. if($redisconfig['redis_selectdb'] > 0){
  150. $redis->select($redisconfig['redis_selectdb']);
  151. }
  152. // 事务处理余额与记录信息
  153. $userjewellogModel = new \app\common\model\UserJewelLog();
  154. //$usersoundcoinlogModel = new \app\common\model\UserSoundcoinLog();
  155. // 获取当天零点
  156. $day = date("Ymd");
  157. // 获取本周第一天
  158. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  159. // 获取本月第一天
  160. $monthday = date("Ym01");
  161. $allVal = 0;
  162. $i = 0;
  163. foreach($user_id_arr as $user_id) {
  164. // 获取赠送用户信息
  165. $where = [];
  166. $where["id"] = $user_id;
  167. $touserInfo = $userModel->where($where)->find();
  168. if($is_back == 1) {
  169. $b=0;
  170. foreach($giftList as $k => $v) {
  171. for($a=1;$a<=$v["number"];$a++) {
  172. $b++;
  173. $num = $v["number"] - $a;
  174. if($num > 0) {
  175. $res1 = \app\common\model\GiftBack::where(["id"=>$v["id"]])->setDec("number");
  176. } else {
  177. $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]);
  178. }
  179. if($b == $number) break;
  180. }
  181. }
  182. $res2 = true;
  183. } else {
  184. // 扣除当前用户钻石余额
  185. $where = [];
  186. $where["id"] = $userauthid;
  187. $res1 = $userModel->where($where)->setDec("jewel", $giftValue);
  188. // 添加当前用户钻石流水记录
  189. $res2 = $userjewellogModel->addUserJewelLog($userauthid, $giftValue, "-", $userInfo["jewel"], "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "钻石!", 3);
  190. }
  191. $giftuserpartyModel = new \app\common\model\GiftUserParty();
  192. // 添加礼物赠送记录表
  193. $data = [];
  194. $data["user_id"] = $userauthid;
  195. $data["user_to_id"] = $user_id;
  196. $data["party_id"] = $party_id;
  197. if($boxgiftInfo){
  198. $data["gift_id"] = $boxgiftInfo['gift_id'];
  199. $data["gift_give_type"] = 2;
  200. $data["gift_name"] = $boxgiftInfo["gift_name"];
  201. $data["gift_gif_image"] = $boxgiftInfo["image"];
  202. $data["number"] = $number;
  203. $data["price"] = $boxgiftInfo["price"];
  204. $data["value"] = $boxgiftInfo["price"] * $number;
  205. }else{
  206. $data["gift_id"] = $backGiftId > 0 ? $backGiftId : $gift_id;
  207. $data["gift_give_type"] = $is_back ? 1 : 2;
  208. $data["gift_name"] = $giftInfo["name"];
  209. $data["gift_gif_image"] = $giftInfo["image"];
  210. $data["number"] = $number;
  211. $data["price"] = $giftInfo["value"];
  212. $data["value"] = $giftValue;
  213. }
  214. $data["createtime"] = time();
  215. $res5 = $giftuserpartyModel->allowField(true)->save($data);
  216. // 添加赠送用户声币余额
  217. /* $where = [];
  218. $where["id"] = $user_id;
  219. $res3 = $userModel->where($where)->setInc("sound_coin", $getValue);
  220. $getValue == 0 && $res3 = true;*/
  221. // 添加赠送用户声币流水记录soundCoin
  222. // xxx送你
  223. //$res4 = $usersoundcoinlogModel->addUserSoundcoinLog($user_id, $getValue, "+", $touserInfo["sound_coin"], "{$this->auth->nickname}送你{$giftInfo['name']}x{$number}", 1, $giftuserpartyModel->id);
  224. /*if ($partyInfo && $guilderValue > 0) {
  225. // 增加房主抽成
  226. $where = [];
  227. $where["id"] = $partyInfo->user_id;
  228. $userModel->where($where)->setInc("sound_coin", $guilderValue);
  229. // 添加赠送用户声币流水记录soundCoin
  230. // xxx送礼物给xxx,房间礼物抽成
  231. $usersoundcoinlogModel->addUserSoundcoinLog($partyInfo->user_id, $guilderValue, "+", $touserInfo["sound_coin"], "{$this->auth->nickname}送礼物{$giftInfo['name']}x{$number}给{$touserInfo['nickname']},房间礼物抽成", 4, $giftuserpartyModel->id);
  232. }*/
  233. //总消费增加
  234. if($is_back != 1){
  235. $res6 = Db::name('user')->where('id',$user_id)->setInc("renewcount", $giftCountValue);
  236. }
  237. if ($res1 && $res2 && $res5) {
  238. $i++;
  239. if($party_id > 0) {
  240. // 添加redis记录做财富排行榜日榜用
  241. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $hotValue, $user_id);
  242. // 添加redis记录做财富排行榜周榜用
  243. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $hotValue, $user_id);
  244. // 添加redis记录做财富排行榜月榜用
  245. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $hotValue, $user_id);
  246. // 添加redis记录做贡献排行榜日榜用
  247. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid);
  248. // 添加redis记录做贡献排行榜周榜用
  249. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid);
  250. // 添加redis记录做贡献排行榜月榜用
  251. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid);
  252. // tcp 更新用户魅力值
  253. $this->updateUserCharm($party_id, $user_id, $hotValue);
  254. // 如果是主播,则添加魅力值记录做榜单统计
  255. if($room_type == 2) {
  256. $data = [];
  257. $data["user_id"] = $user_id;
  258. $data["party_id"] = $party_id;
  259. $data["charm"] = $hotValue;
  260. $data["createtime"] = time();
  261. \app\common\model\UserCharmRank::insert($data);
  262. }
  263. }
  264. $getempirical = config("site.getempirical");
  265. $getempirical = $getempirical * $hotValue;
  266. // 获取用户贵族信息
  267. $noble = \app\common\model\User::getUserNoble($this->auth->id);
  268. if(isset($noble["noble_on"]) && $noble["noble_on"] == 1) {
  269. $getempirical = $getempirical + $getempirical * ($noble["explain"]/100);
  270. }
  271. // 增加用户经验值
  272. $res = \app\common\model\User::addEmpirical($this->auth->id,$getempirical);
  273. if ($res){
  274. $this->auth->level = $res->level;
  275. }
  276. // +exp
  277. \app\common\model\TaskLog::tofinish($this->auth->id,"OBHqCX4g",$number);
  278. //贵族升级处理
  279. //$this->checkBeNoble($redis,$this->auth->id,$giftCountValue);
  280. // +message
  281. \app\common\model\Message::addMessage($user_id,"礼物通知","收到 ".$this->auth->nickname." 赠送的".$giftInfo["name"]." x".$number);
  282. $allVal = $allVal + $hotValue;
  283. // // 剪掉背包礼物
  284. // if($is_back == 1) {
  285. // \app\common\model\GiftBack::update(["is_use"=>1],["id"=>$gift_id]);
  286. // }
  287. }
  288. }
  289. // 获取用户魅力值
  290. $users = $redis->zRange("hourCharm_".$party_id,0,-1,true);
  291. $u = [];
  292. if($users) {
  293. foreach($users as $k => $v) $u[] = [
  294. "user_id"=>$k,
  295. "charm"=>$this->changeW($v)
  296. ];
  297. }
  298. $userCharm = $u;
  299. // tcp 更新房间热度
  300. $partyHot = $this->updatePartyHot($party_id, $allVal, $room_type);
  301. // 如果是派对,则添加派对热度值记录做榜单统计
  302. if($room_type == 1) {
  303. $data = [];
  304. $data["party_id"] = $party_id;
  305. $data["hot"] = $allVal;
  306. $data["createtime"] = time();
  307. \app\common\model\PartyHot::insert($data);
  308. }
  309. // tcp 获取房间用户周前三名
  310. $partyUserTop = $this->getPartyUserTop($party_id, $room_type);
  311. if($i == $userCount) {
  312. $returnData["userCharm"] = $userCharm;
  313. $returnData["partyHot"] = $this->changeW($partyHot);
  314. $returnData["partyUserTop"] = $partyUserTop;
  315. if($is_back != 1) {
  316. if($giftInfo->box_type > 0) { // 不是背包,宝箱中赠送
  317. $returnData["box_type"] = $giftInfo->box_type;
  318. $returnData["box_image"] = $giftInfo->image;
  319. $returnData["image"] = $boxgiftInfo["image"];
  320. $returnData["name"] = $boxgiftInfo["gift_name"];
  321. $returnData["gif_image"] = $boxgiftInfo["special"];
  322. } else {
  323. $returnData["image"] = $giftInfo["image"];
  324. $returnData["gif_image"] = $giftInfo["special"];
  325. }
  326. } else {
  327. $returnData["image"] = $giftInfo["image"];
  328. $returnData["gif_image"] = $giftInfo["gif_image"];
  329. }
  330. // 增加抽点记录
  331. $data = [];
  332. $data["user_id"] = $user_ids;
  333. $data["party_id"] = $party_id?$party_id:0;
  334. $data["gift_value"] = $getValue;
  335. $data["plat_value"] = $platValue;
  336. $data["guilder_value"] = $guilderValue;
  337. $data["createtime"] = time();
  338. \app\common\model\UserProfitLog::insert($data);
  339. Db::commit();
  340. $this->success("赠送成功!",$returnData);
  341. } else {
  342. $this->success("赠送失败!");
  343. }
  344. } catch (ValidateException $e) {
  345. Db::rollback();
  346. $this->error($e->getMessage());
  347. } catch (PDOException $e) {
  348. Db::rollback();
  349. $this->error($e->getMessage());
  350. } catch (Exception $e) {
  351. Db::rollback();
  352. $this->error($e->getMessage());
  353. }
  354. }
  355. }