Partygift.php 17 KB

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