123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- <?php
- namespace app\api\controller;
- use app\api\controller\Common;
- use app\common\model\PartyJoin;
- use fast\Random;
- use app\common\controller\RedisLeaderboard;
- use Redis;
- use think\Db;
- use think\Exception;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use think\Request;
- use app\common\service\RoomService;
- class Partygift extends Common
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
-
- public function giveAllbackGiftToYou(){
-
- if (!$this->apiLimit(1, 1000)) {
- $this->error(__('Operation frequently'));
- }
- $user_ids = $this->request->request("user_id");
- $party_id = $this->request->request("party_id",0);
- $room_type = $this->request->request('room_type',1);
- $is_back = 1;
- if (!$user_ids || !in_array($room_type,[1,2]))
- {
- $this->error(__('Invalid parameters'));
- }
-
- $getMyBackGift = $this->getMyBackGift($this->auth->id);
- if(empty($getMyBackGift)){
- $this->error('背包里没有礼物了');
- }
-
- foreach($getMyBackGift as $backkey => $backval){
- $this->giveGiftToYou_back($user_ids,$backval['id'],$party_id,$room_type,$backval['number'],$is_back);
- }
- }
-
- private function getMyBackGift($userid) {
- $list = \app\common\model\GiftBack::field("id,name,sum(number) as number")
- ->where(["user_id"=>$userid,"is_use"=>0])
- ->group("name")
- ->select();
- return $list;
- }
-
- private function giveGiftToYou_back($user_ids,$gift_id,$party_id = 0,$room_type = 1,$number,$is_back = 0) {
- if (!$user_ids || !in_array($is_back,[0,1]) || !$gift_id || !$number || !in_array($room_type,[1,2])) $this->error(__('Invalid parameters'));
- $user_id_arr = explode(",",$user_ids);
- $userCount = count($user_id_arr);
- $userauthid = $this->auth->id;
- $soundCoinRate = config("site.giftCoin");
- $userModel = new \app\common\model\User();
- $backGiftId = 0;
- $boxgiftInfo = [];
- if($is_back == 1) {
-
- $giftInfo = \app\common\model\GiftBack::get($gift_id);
- if(!$giftInfo) $this->error("背包礼物获取失败");
- $backGiftId = $giftInfo->gift_id;
-
- $allCount = $number*$userCount;
- $giftbackList = \app\common\model\GiftBack::where(["name"=>$giftInfo->name,"user_id"=>$userauthid,'is_use'=>0])->limit($allCount)->select();
- $giftInfo = isset($giftbackList[0])?$giftbackList[0]:[];
- $giftcount = 0;
- $giftList = [];
- if($giftbackList) foreach($giftbackList as $k => $v) {
- $giftcount = $giftcount + $v["number"];
- $giftList[$k] = $v;
- if($giftcount >= $allCount) {
- break;
- }
- }
- if($giftcount < $allCount) $this->error("背包数量不足");
- $giftValue = $giftInfo["value"] * $number;
- $getValue = $giftInfo["value"] * $number;
- } else {
-
- $giftModel = new \app\common\model\Gift();
- $where = [];
- $where["id"] = $gift_id;
- $giftInfo = $giftModel->where($where)->find();
- if (!$giftInfo) $this->error("请选择礼物!");
- $giftValue = $giftInfo["value"] * $number;
- $giftCountValue = $giftInfo["value"] * $number * $userCount;
- $getValue = $giftValue;
-
- if($giftInfo->box_type > 0) {
- $boxgiftInfo = $this->getBoxGift($giftInfo->box_type);
- $getValue = $boxgiftInfo["price"];
- }
-
- $where = [];
- $where["id"] = $userauthid;
- $userInfo = $userModel->where($where)->lock(true)->find();
- if (!$userInfo) $this->error("用户信息查询失败!");
- if($userInfo["jewel"] < $giftCountValue) $this->error("您的钻石余额不足!");
- }
- $hotValue = $getValue;
- $getValue = round($getValue * ($soundCoinRate/100));
-
- $progetValue = $hotValue - $getValue;
- if($progetValue >0) {
- $data = [];
- $data["user_id"] = $user_ids;
- $data["party_id"] = $party_id?$party_id:0;
- $data["gift_value"] = $hotValue;
- $data["plat_value"] = $hotValue - $getValue;
- $data["createtime"] = time();
- \app\common\model\UserChangeLog::insert($data);
- }
-
- $partyInfo = null;
- if(!$party_id) {
- $platRate = 10;
- $guilderRate = 30;
- } else {
- $partyInfo = \app\common\model\Party::field("user_id,platRate,guilderRate")->where(["id"=>$party_id])->find();
-
- $platRate = $partyInfo->platRate;
- $guilderRate = $partyInfo->guilderRate;
- }
- $platValue = bcmul($platRate/100,$getValue);
- $guilderValue = bcmul($guilderRate/100,$getValue);
- $getValue = bcsub(bcsub($getValue,$platValue),$guilderValue);
- $returnData = [];
- Db::startTrans();
- try {
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"], 86400 * 31);
- if ($redisconfig['redis_pwd']) {
- $redis->auth($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['redis_selectdb']);
- }
-
- $userjewellogModel = new \app\common\model\UserJewelLog();
-
-
- $day = date("Ymd");
-
- $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
-
- $monthday = date("Ym01");
- $allVal = 0;
- $i = 0;
- foreach($user_id_arr as $user_id) {
-
- $where = [];
- $where["id"] = $user_id;
- $touserInfo = $userModel->where($where)->find();
- if($is_back == 1) {
- $b=0;
- foreach($giftList as $k => $v) {
- for($a=1;$a<=$v["number"];$a++) {
- $b++;
- $num = $v["number"] - $a;
- if($num > 0) {
- $res1 = \app\common\model\GiftBack::where(["id"=>$v["id"]])->setDec("number");
- } else {
- $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]);
- }
- if($b == $number) break;
- }
- }
- $res2 = true;
- } else {
-
- $where = [];
- $where["id"] = $userauthid;
- $res1 = $userModel->where($where)->setDec("jewel", $giftValue);
-
- $res2 = $userjewellogModel->addUserJewelLog($userauthid, $giftValue, "-", $userInfo["jewel"], "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "钻石!", 3);
- }
- $giftuserpartyModel = new \app\common\model\GiftUserParty();
-
- $data = [];
- $data["user_id"] = $userauthid;
- $data["user_to_id"] = $user_id;
- $data["party_id"] = $party_id;
- if($boxgiftInfo){
- $data["gift_id"] = $boxgiftInfo['gift_id'];
- $data["gift_give_type"] = 2;
- $data["gift_name"] = $boxgiftInfo["gift_name"];
- $data["gift_gif_image"] = $boxgiftInfo["image"];
- $data["number"] = $number;
- $data["price"] = $boxgiftInfo["price"];
- $data["value"] = $boxgiftInfo["price"] * $number;
- }else{
- $data["gift_id"] = $backGiftId > 0 ? $backGiftId : $gift_id;
- $data["gift_give_type"] = $is_back ? 1 : 2;
- $data["gift_name"] = $giftInfo["name"];
- $data["gift_gif_image"] = $giftInfo["image"];
- $data["number"] = $number;
- $data["price"] = $giftInfo["value"];
- $data["value"] = $giftValue;
- }
- $data["createtime"] = time();
- $res5 = $giftuserpartyModel->allowField(true)->save($data);
-
-
-
-
-
-
-
- if($is_back != 1){
- $res6 = Db::name('user')->where('id',$user_id)->setInc("renewcount", $giftCountValue);
- }
- if ($res1 && $res2 && $res5) {
- $i++;
- if($party_id > 0) {
-
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $hotValue, $user_id);
-
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $hotValue, $user_id);
-
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $hotValue, $user_id);
-
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid);
-
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid);
-
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid);
-
- $this->updateUserCharm($party_id, $user_id, $hotValue);
-
- if($room_type == 2) {
- $data = [];
- $data["user_id"] = $user_id;
- $data["party_id"] = $party_id;
- $data["charm"] = $hotValue;
- $data["createtime"] = time();
- \app\common\model\UserCharmRank::insert($data);
- }
- }
- $getempirical = config("site.getempirical");
- $getempirical = $getempirical * $hotValue;
-
- $noble = \app\common\model\User::getUserNoble($this->auth->id);
- if(isset($noble["noble_on"]) && $noble["noble_on"] == 1) {
- $getempirical = $getempirical + $getempirical * ($noble["explain"]/100);
- }
-
- $res = \app\common\model\User::addEmpirical($this->auth->id,$getempirical);
- if ($res){
- $this->auth->level = $res->level;
- }
-
- \app\common\model\TaskLog::tofinish($this->auth->id,"OBHqCX4g",$number);
-
-
-
- \app\common\model\Message::addMessage($user_id,"礼物通知","收到 ".$this->auth->nickname." 赠送的".$giftInfo["name"]." x".$number);
- $allVal = $allVal + $hotValue;
- }
- }
-
- $users = $redis->zRange("hourCharm_".$party_id,0,-1,true);
- $u = [];
- if($users) {
- foreach($users as $k => $v) $u[] = [
- "user_id"=>$k,
- "charm"=>$this->changeW($v)
- ];
- }
- $userCharm = $u;
-
- $partyHot = $this->updatePartyHot($party_id, $allVal, $room_type);
-
- if($room_type == 1) {
- $data = [];
- $data["party_id"] = $party_id;
- $data["hot"] = $allVal;
- $data["createtime"] = time();
- \app\common\model\PartyHot::insert($data);
- }
-
- $partyUserTop = $this->getPartyUserTop($party_id, $room_type);
- if($i == $userCount) {
- $returnData["userCharm"] = $userCharm;
- $returnData["partyHot"] = $this->changeW($partyHot);
- $returnData["partyUserTop"] = $partyUserTop;
- if($is_back != 1) {
- if($giftInfo->box_type > 0) {
- $returnData["box_type"] = $giftInfo->box_type;
- $returnData["box_image"] = $giftInfo->image;
- $returnData["image"] = $boxgiftInfo["image"];
- $returnData["name"] = $boxgiftInfo["gift_name"];
- $returnData["gif_image"] = $boxgiftInfo["special"];
- } else {
- $returnData["image"] = $giftInfo["image"];
- $returnData["gif_image"] = $giftInfo["special"];
- }
- } else {
- $returnData["image"] = $giftInfo["image"];
- $returnData["gif_image"] = $giftInfo["gif_image"];
- }
-
- $data = [];
- $data["user_id"] = $user_ids;
- $data["party_id"] = $party_id?$party_id:0;
- $data["gift_value"] = $getValue;
- $data["plat_value"] = $platValue;
- $data["guilder_value"] = $guilderValue;
- $data["createtime"] = time();
- \app\common\model\UserProfitLog::insert($data);
- Db::commit();
- $this->success("赠送成功!",$returnData);
- } else {
- $this->success("赠送失败!");
- }
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- }
- }
|