| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\AccountLogModel;
- use App\Models\DeveloperModel;
- use App\Models\FinanceModel;
- use App\Models\MemberModel;
- use App\Models\PledgeOrderModel;
- use App\Services\BaseService;
- use App\Services\Common\InstitutionalService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 平台奖金制度、财务-服务类
- * @author laravel开发员
- * @since 2020/11/12
- * @package App\Services\Common\v1
- */
- class FinanceService extends BaseService
- {
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * FinanceService constructor.
- */
- public function __construct()
- {
- $this->model = new FinanceModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $where = ['a.mark' => 1];
- $status = isset($params['status']) ? $params['status'] : 0;
- $type = isset($params['type']) ? $params['type'] : 0;
- if ($status > 0) {
- $where['a.status'] = $status;
- }
- if ($type > 0) {
- $where['a.type'] = $type;
- }
- $list = $this->model->from('finance as a')
- ->where($where)
- ->where(function ($query) use ($params) {
- // 日期
- $date = isset($params['date']) ? $params['date'] : [];
- $start = isset($date[0]) ? $date[0] : '';
- $end = isset($date[1]) ? $date[1] : '';
- $end = $start >= $end ? '' : $end;
- if ($start) {
- $query->where('a.create_time', '>=', strtotime($start));
- }
- if ($end) {
- $query->where('a.create_time', '<', strtotime($end));
- }
- })
- ->select(['a.*'])
- ->orderBy('a.create_time', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- foreach ($list['data'] as &$item) {
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 平台结算
- * @param $money
- * @param int $changeType 交易类型:1-进账,2-出账
- * @param int $type 类型:1-账户流水
- * @param int $coinType 币种类型: 1-USDT
- * @return mixed
- */
- public function saveLog($userId, $money, $changeType = 1, $type = 1, $coinType = 1)
- {
- $date = date('Y-m-d');
- $info = $this->model->where(['user_id' => $userId, 'date' => $date, 'type' => $type, 'mark' => 1])->first();
- if (!$info) {
- $data = ['user_id' => $userId, 'date' => $date, 'type' => $type, 'create_time' => time(), 'update_time' => time(), 'status' => 1];
- if ($changeType == 1) {
- $data['income'] = $money;
- } else {
- $data['expend'] = $money;
- }
- return $this->model->insertGetId($data);
- } else {
- if ($changeType == 1) {
- $info->income += $money;
- } else {
- $info->expend += $money;
- }
- return $info->save();
- }
- }
- /**
- * 质押轮次奖励
- * @param $userId 结算质押用户ID
- * @param $round 当前轮次
- * @param $order 订单数据
- * @return bool
- */
- public function pledgeAward(int $userId, int $round, array $order)
- {
- // 验证是否奖励过
- $orderNo = isset($order['order_no']) ? $order['order_no'] : '';
- // 制度参数
- $roundData = InstitutionalService::make()->getAwardByRound($round);
- $awardRound = isset($roundData['round']) ? intval($roundData['round']) : 0;
- $awardUsdt = isset($roundData['value']) ? floatval($roundData['value']) : 0;
- if ($awardRound <= 0 || $awardUsdt <= 0) {
- $this->error = lang(4201, ['uid' => $userId, 'round' => $round]);
- return false;
- }
- // 奖励的轮次已经是否已结算
- if (AccountLogService::make()->checkHasByRound($userId, $awardRound, 1)) {
- $this->error = lang(4202, ['uid' => $userId, 'round' => $awardRound, 'order_no' => $orderNo]);
- return false;
- }
- // 结算用户是否存在
- $memberInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'nickname', 'usdt', 'sbt'])
- ->orderBy('id', 'asc')
- ->first();
- $userProfit = isset($memberInfo['profit']) ? $memberInfo['profit'] : 0;
- if (!$memberInfo) {
- $this->error = lang(4203, ['uid' => $userId, 'round' => $awardRound]);
- return false;
- }
- // 结算处理
- try {
- DB::beginTransaction();
- $updateData = ['profit' => DB::raw("profit + {$awardUsdt}"), 'profit_total' => DB::raw("profit_total + {$awardUsdt}"), 'update_time' => time()];
- if (!MemberModel::where(['id' => $userId])->update($updateData)) {
- DB::rollBack();
- $this->error = lang(4204, ['uid' => $userId, 'round' => $awardRound, 'money' => $awardUsdt]);
- return false;
- }
- // 奖励明细
- $data = [
- 'user_id' => $userId,
- 'order_no' => $orderNo,
- 'type' => 15,
- 'round' => $awardRound,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => $awardUsdt,
- 'before_money' => $userProfit,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => '质押轮次奖励',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = lang(4204, ['uid' => $userId, 'round' => $round, 'money' => $awardUsdt]);
- return false;
- }
- DB::commit();
- $this->error = lang(4205, ['uid' => $userId, 'round' => $round, 'money' => $awardUsdt]);
- return true;
- } catch (\Exception $exception) {
- DB::rollBack();
- $this->error = lang(4206, ['uid' => $userId, 'round' => $round, 'error' => $exception->getMessage()]);
- return false;
- }
- }
- /**
- * 团队分红
- * @param $userId 质押用户
- * @param $parentIds 推荐人ID数组
- * @param $order 订单信息
- * @return bool
- */
- public function settleTeamBonus(int $userId, array $parentIds, array $order)
- {
- $orderNo = isset($order['order_no']) ? $order['order_no'] : '';
- $pledgeUsdt = isset($order['money']) ? floatval($order['money']) : 0;
- if ($pledgeUsdt <= 0 || empty($orderNo)) {
- $this->error = lang(4610, ['uid' => $userId, 'order_no' => $orderNo]);
- return false;
- }
- // TODO 质押用户团队分红
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'usdt', 'performance', 'profit', 'bonus_rate', 'bonus_performance', 'team_bonus_performance', 'status'])
- ->first();
- $userInfo = $userInfo ? $userInfo->toArray() : [];
- $userProfit = isset($userInfo['profit']) ? floatval($userInfo['profit']) : 0; // 收益
- $userPerformance = isset($userInfo['performance']) ? floatval($userInfo['performance']) : 0;
- $bonusRate = isset($userInfo['bonus_rate']) ? floatval($userInfo['bonus_rate']) : 0;
- $bonusPerformance = isset($userInfo['bonus_performance']) ? floatval($userInfo['bonus_performance']) : 0;
- DB::beginTransaction();
- try {
- $logs = [];
- if ($bonusRate > 0 && $bonusRate < 100) {
- $teamPerformance = TeamService::make()->getTeamPerformanceByType($userId, 2, true);
- $teamPerformance = round($teamPerformance + $userPerformance, 2); // 用户团队业绩
- if ($teamPerformance > $bonusPerformance) {
- $bonusTotal = $pledgeUsdt;
- $newBonusPerformance = $teamPerformance;
- if ($bonusPerformance > 0) {
- $bonusTotal = round($teamPerformance - $bonusPerformance, 2);
- }
- // 分红金额
- $bonusTotal = $bonusTotal >= $pledgeUsdt ? $pledgeUsdt : $bonusTotal;
- $bonusAwardUsdt = moneyFormat($bonusTotal * $bonusRate / 100, 2);
- if ($bonusAwardUsdt > 0) {
- // 分红入账
- $updateData = [
- 'profit' => DB::raw("profit + {$bonusAwardUsdt}"),
- 'profit_total' => DB::raw("profit_total + {$bonusAwardUsdt}"),
- 'bonus_performance' => $newBonusPerformance,
- 'update_time' => time(),
- ];
- if (!MemberModel::where(['id' => $userId])->update($updateData)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的团队分红结算错误";
- return false;
- }
- // USDT收益账单
- $data = [
- 'user_id' => $userId,
- 'order_no' => $orderNo,
- 'type' => 14,
- 'round' => isset($order['round']) ? $order['round'] : 0,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => $bonusAwardUsdt,
- 'before_money' => $userProfit,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => "团队分红",
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的团队分红结算失败";
- return false;
- }
- $logs[$userId] = ['error'=>"质押用户[{$userId}]团队分红结算成功",'orderNo'=>$orderNo,'bonusUsdt'=>$bonusAwardUsdt,'bonusRate'=>$bonusRate,'log'=>$data];
- }
- }
- }
- // TODO 上级分红结算
- if($parentIds){
- $lastUserId = $userId;
- $lastUserInfo = [];
- $bonusTotalRate = $bonusRate;
- $parents = MemberModel::whereIn('id', $parentIds)
- ->where(['mark' => 1])
- ->select(['id', 'usdt', 'performance', 'profit', 'bonus_rate', 'bonus_performance', 'team_bonus_performance', 'status'])
- ->orderByRaw('FIELD(id, "' . implode(',', $parentIds) . '")')
- ->get()
- ->keyBy('id');
- $parents = $parents? $parents->toArray() : [];
- if($parents){
- foreach($parentIds as $pid){
- $info = isset($parents[$pid])? $parents[$pid] : [];
- if(empty($info)){
- $lastUserId = $pid;
- $logs[$pid] = ['error'=>"分红用户[{$userId}]上级用户[{$pid}]信息不存在",'orderNo'=>$orderNo,'info'=>$info];
- continue;
- }
- if($lastUserInfo == []){
- $lastUserInfo = $userInfo;
- }else{
- $lastUserInfo = isset($parents[$lastUserId])?$parents[$lastUserId] : [];
- // $lastUserId = isset($lastUserInfo['id'])? $lastUserInfo['id'] : 0;
- }
- // 当前节点线下级直推用户
- if(empty($lastUserInfo)){
- $lastUserId = $pid;
- $logs[$pid] = ['error'=>"质押用户[{$userId}]上级用户[{$pid}]直推线用户信息不存在",'orderNo'=>$orderNo,'lastUserId'=>$lastUserId,'parents'=>$parents,'bonusRateTotal'=>$bonusTotalRate,'info'=>$info];
- continue;
- }
- // 实际分红比例
- $parentProfit = isset($info['profit']) ? floatval($info['profit']) : 0; // 收益
- $parentPerformance = isset($info['performance']) ? floatval($info['performance']) : 0;
- $parentBonusRate = isset($info['bonus_rate']) ? floatval($info['bonus_rate']) : 0;
- $realBonusRate = max(0,round($parentBonusRate - $bonusTotalRate));
- if($realBonusRate <=0 || $realBonusRate >= 100){
- $lastUserId = $pid;
- $logs[$pid] = ['error'=>"质押用户[{$userId}]上级用户[{$pid}]无实际分红比例",'orderNo'=>$orderNo,'bonusRateTotal'=>$bonusTotalRate,'info'=>$info];
- continue;
- }
- // 直推线已分红业绩
- $bonusTeamPerformance = isset($lastUserInfo['team_bonus_performance']) ? floatval($lastUserInfo['team_bonus_performance']) : 0;
- $lastPerformance = isset($lastUserInfo['performance']) ? floatval($lastUserInfo['performance']) : 0;
- // 直推线团队业绩
- $teamPerformance = TeamService::make()->getTeamPerformanceByType($lastUserId, 2, true);
- $teamPerformance = round(($teamPerformance + $lastPerformance) + $parentPerformance, 2);
- if ($teamPerformance > $bonusTeamPerformance) {
- $bonusTotal = $pledgeUsdt;
- $newBonusPerformance = $teamPerformance;
- if ($bonusTeamPerformance > 0) {
- $bonusTotal = round($teamPerformance - $bonusTeamPerformance, 2);
- }
- // 上级分红金额
- $bonusTotal = $bonusTotal >= $pledgeUsdt ? $pledgeUsdt : $bonusTotal;
- $bonusAwardUsdt = moneyFormat($bonusTotal * $realBonusRate / 100, 2);
- if ($bonusAwardUsdt > 0) {
- // 分红入账
- $updateData = [
- 'profit' => DB::raw("profit + {$bonusAwardUsdt}"),
- 'profit_total' => DB::raw("profit_total + {$bonusAwardUsdt}"),
- 'update_time' => time(),
- ];
- if (!MemberModel::where(['id' => $pid])->update($updateData)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的上级[{$pid}]团队分红结算错误";
- return false;
- }
- // 直推用户记录当前节点上级已分红业绩
- $updateData = [
- 'team_bonus_performance' => $newBonusPerformance,
- 'update_time' => time(),
- ];
- if (!MemberModel::where(['id' => $lastUserId])->update($updateData)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的上级[{$pid}]团队节点已分红更新错误";
- return false;
- }
- // USDT收益账单
- $data = [
- 'user_id' => $pid,
- 'order_no' => $orderNo,
- 'source_uid' => $userId,
- 'type' => 14,
- 'round' => isset($order['round']) ? $order['round'] : 0,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => $bonusAwardUsdt,
- 'before_money' => $parentProfit,
- 'create_time' => time()+1,
- 'action_ip' => get_client_ip(),
- 'remark' => "团队分红",
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的上级[{$pid}]团队分红结算失败";
- return false;
- }
- $lastUserId = $pid;
- $logs[$pid] = ['error'=>"质押用户[{$userId}]上级用户[{$pid}]团队分红成功",'orderNo'=>$orderNo,'bonusUsdt'=>$bonusAwardUsdt,'bonusRateTotal'=>$bonusTotalRate,'info'=>$info];
- $bonusTotalRate += $realBonusRate;
- }else{
- $lastUserId = $pid;
- $logs[$pid] = ['error'=>"质押用户[{$userId}]上级用户[{$pid}]团队分红无结算金额",'orderNo'=>$orderNo,'bonusUsdt'=>0,'bonusRateTotal'=>$bonusTotalRate,'info'=>$info];
- }
- }else{
- $lastUserId = $pid;
- $logs[$pid] = ['error'=>"质押用户[{$userId}]上级用户[{$pid}]团队分红业绩不足",'orderNo'=>$orderNo,'teamPerformance'=>$teamPerformance,'bonusTeamPerformance'=>$bonusTeamPerformance,'bonusUsdt'=>0,'bonusRateTotal'=>$bonusTotalRate,'info'=>$info];
- }
- }
- }
- }
- DB::commit();
- RedisService::set("caches:pledgeOrder:team_{$userId}_{$orderNo}", ['user_id' => $userId, 'parentIds' => $parentIds, 'order' => $order, 'log' => $logs], 6 * 3600);
- return ['user_id' => $userId, 'pcount' => count($parentIds), 'orderNo' => $orderNo, 'money' => $pledgeUsdt];
- } catch (\Exception $exception) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]团队分红结算错误:" . $exception->getMessage();
- return false;
- }
- }
- /**
- * 工作室分红
- * @param $userId 质押用户
- * @param $parentIds 推荐人ID数组
- * @param $order 订单信息
- * @return bool
- */
- public function settleRoomBonus(int $userId, array $parentIds, array $order)
- {
- $orderNo = isset($order['order_no']) ? $order['order_no'] : '';
- $pledgeUsdt = isset($order['money']) ? floatval($order['money']) : 0;
- if ($pledgeUsdt <= 0 || empty($orderNo)) {
- $this->error = lang(4610, ['uid' => $userId, 'order_no' => $orderNo]);
- return false;
- }
- // TODO 质押用户工作室分红
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'usdt', 'performance', 'profit', 'room_bonus_rate', 'bonus_performance', 'room_bonus_performance', 'status'])
- ->first();
- $userInfo = $userInfo ? $userInfo->toArray() : [];
- $userProfit = isset($userInfo['profit']) ? floatval($userInfo['profit']) : 0; // 收益
- $userPerformance = isset($userInfo['performance']) ? floatval($userInfo['performance']) : 0;
- $bonusRate = isset($userInfo['room_bonus_rate']) ? floatval($userInfo['room_bonus_rate']) : 0;
- $bonusPerformance = isset($userInfo['room_bonus_performance']) ? floatval($userInfo['room_bonus_performance']) : 0;
- DB::beginTransaction();
- try {
- if ($bonusRate > 0 && $bonusRate < 100) {
- $teamPerformance = TeamService::make()->getTeamPerformanceByType($userId, 2, true);
- $teamPerformance = round($teamPerformance + $userPerformance, 2); // 用户团队业绩
- if ($teamPerformance > $bonusPerformance) {
- $bonusTotal = $pledgeUsdt;
- $newBonusPerformance = $teamPerformance;
- if ($bonusPerformance > 0) {
- $bonusTotal = round($teamPerformance - $bonusPerformance, 2);
- }
- // 分红金额
- $bonusTotal = $bonusTotal >= $pledgeUsdt ? $pledgeUsdt : $bonusTotal;
- $bonusAwardUsdt = moneyFormat($bonusTotal * $bonusRate / 100, 2);
- if ($bonusAwardUsdt > 0) {
- // 分红入账
- $updateData = [
- 'profit' => DB::raw("profit + {$bonusAwardUsdt}"),
- 'profit_total' => DB::raw("profit_total + {$bonusAwardUsdt}"),
- 'room_bonus_performance' => $newBonusPerformance,
- 'update_time' => time(),
- ];
- if (!MemberModel::where(['id' => $userId])->update($updateData)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的工作室分红结算错误";
- return false;
- }
- // USDT收益账单
- $data = [
- 'user_id' => $userId,
- 'order_no' => $orderNo,
- 'type' => 16,
- 'round' => isset($order['round']) ? $order['round'] : 0,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => $bonusAwardUsdt,
- 'before_money' => $userProfit,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => "工作室分红",
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的工作室分红结算失败";
- return false;
- }
- }
- }
- }
- // TODO 上级分红结算
- $logs = [];
- if($parentIds){
- $bonusTotalRate = $bonusRate;
- $parents = MemberModel::whereIn('id', $parentIds)
- ->where(['mark' => 1])
- ->select(['id', 'usdt', 'performance', 'profit', 'room_bonus_rate', 'room_bonus_performance', 'status'])
- ->orderByRaw('FIELD(id, "' . implode(',', $parentIds) . '")')
- ->get()
- ->keyBy('id');
- $parents = $parents? $parents->toArray() : [];
- if($parents){
- foreach($parentIds as $pid){
- $info = isset($parents[$pid])? $parents[$pid] : [];
- if(empty($info)){
- $logs[$pid] = ['error'=>"工作室分红用户[{$userId}]上级用户[{$pid}]信息不存在",'orderNo'=>$orderNo,'info'=>$info];
- continue;
- }
- // 实际分红比例
- $parentProfit = isset($info['profit']) ? floatval($info['profit']) : 0; // 收益
- $parentPerformance = isset($info['performance']) ? floatval($info['performance']) : 0;
- $parentBonusRate = isset($info['room_bonus_rate']) ? floatval($info['room_bonus_rate']) : 0;
- $bonusTeamPerformance = isset($info['room_bonus_performance']) ? floatval($info['room_bonus_performance']) : 0;
- $realBonusRate = max(0,round($parentBonusRate - $bonusTotalRate));
- if($realBonusRate <=0 || $realBonusRate >= 100){
- $logs[$pid] = ['error'=>"质押用户[{$userId}]上级用户[{$pid}]无实际工作室分红比例",'orderNo'=>$orderNo,'bonusRateTotal'=>$bonusTotalRate,'info'=>$info];
- continue;
- }
- // 当前团队业绩
- $teamPerformance = TeamService::make()->getTeamPerformanceByType($pid, 2, true);
- $teamPerformance = round($teamPerformance + $parentPerformance, 2);
- if ($teamPerformance > $bonusTeamPerformance) {
- $bonusTotal = $pledgeUsdt;
- $newBonusPerformance = $teamPerformance;
- if ($bonusTeamPerformance > 0) {
- $bonusTotal = round($teamPerformance - $bonusTeamPerformance, 2);
- }
- // 上级工作室分红金额
- $bonusTotal = $bonusTotal >= $pledgeUsdt ? $pledgeUsdt : $bonusTotal;
- $bonusAwardUsdt = moneyFormat($bonusTotal * $realBonusRate / 100, 2);
- if ($bonusAwardUsdt > 0) {
- // 工作室分红入账
- $updateData = [
- 'profit' => DB::raw("profit + {$bonusAwardUsdt}"),
- 'profit_total' => DB::raw("profit_total + {$bonusAwardUsdt}"),
- 'room_bonus_performance' => $newBonusPerformance,
- 'update_time' => time(),
- ];
- if (!MemberModel::where(['id' => $pid])->update($updateData)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的上级[{$pid}]工作室分红结算错误";
- return false;
- }
- // USDT收益账单
- $data = [
- 'user_id' => $pid,
- 'order_no' => $orderNo,
- 'source_uid' => $userId,
- 'type' => 16,
- 'round' => isset($order['round']) ? $order['round'] : 0,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => $bonusAwardUsdt,
- 'before_money' => $parentProfit,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => "工作室分红",
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的上级[{$pid}]工作室分红结算失败";
- return false;
- }
- $logs[$pid] = ['error'=>"质押用户[{$userId}]上级用户[{$pid}]工作室分红结算成功",'orderNo'=>$orderNo,'bonusUsdt'=>$bonusAwardUsdt,'bonusRateTotal'=>$bonusTotalRate,'info'=>$info];
- $bonusTotalRate += $realBonusRate;
- }
- }
- }
- }
- }
- DB::commit();
- RedisService::set("caches:pledgeOrder:room_{$userId}_{$orderNo}", ['user_id' => $userId, 'parentIds' => $parentIds, 'order' => $order, 'log' => $logs], 6 * 3600);
- return ['user_id' => $userId, 'pcount' => count($parentIds), 'orderNo' => $orderNo, 'money' => $pledgeUsdt];
- } catch (\Exception $exception) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]工作室分红结算错误:" . $exception->getMessage();
- return false;
- }
- }
- /**
- * 管理奖、平级奖结算
- * @param $userId 质押用户
- * @param $order
- * @return bool
- */
- public function settleManageAndLevelAward($userId, $parentIds, $order)
- {
- $orderNo = isset($order['order_no']) ? $order['order_no'] : '';
- $pledgeUsdt = isset($order['money']) ? floatval($order['money']) : 0;
- if ($pledgeUsdt <= 0 || empty($orderNo)) {
- $this->error = lang(4512, ['uid' => $userId, 'order_no' => $orderNo]);
- return false;
- }
- $parentIds = get_parents($parentIds);
- if (empty($parentIds)) {
- $this->error = lang(4513, ['uid' => $userId, 'order_no' => $orderNo]);
- return true;
- }
- // SBT价格
- $sbtPrice = PriceLogService::make()->getSbtPrice();
- // USDT收益分配参数
- $settleUsdtRate = ConfigService::make()->getConfigByCode('bonus_usdt_rate', 80);
- $settleUsdtRate = $settleUsdtRate > 0 && $settleUsdtRate <= 100 ? $settleUsdtRate : 80;
- $memberInfo = MemberModel::from('member as a')
- ->leftJoin('member_level as b', 'b.id', '=', 'a.member_level')
- ->where(['a.id' => $userId, 'a.mark' => 1])
- ->select(['a.id','a.member_level','a.usdt','a.profit','a.status','b.bonus_rate'])
- ->first();
- $memberInfo = $memberInfo ? $memberInfo->toArray() : [];
- $userBonusRate = isset($userInfo['bonus_rate'])? $userInfo['bonus_rate'] : 0;
- if(empty($memberInfo)){
- $this->error = lang(4413, ['uid' => $userId, 'order_no' => $orderNo]);
- return false;
- }
- // 结算处理
- try {
- // TODO 管理奖结算
- $count = 0;
- $bonusRateTotal = $userBonusRate;
- $bonusTotal = 0;
- $logs = [];
- $pjUsers = [];
- DB::beginTransaction();
- foreach ($parentIds as $pid) {
- $userInfo = MemberModel::from('member as a')
- ->leftJoin('member_level as b', 'b.id', '=', 'a.member_level')
- ->where(['a.id' => $pid, 'a.mark' => 1])
- ->select(['a.id', 'a.usdt', 'a.member_level', 'a.parent_ids', 'a.profit', 'a.sbt', 'a.profit_total', 'a.status', 'b.bonus_rate', 'b.weighting_rate'])
- ->first();
- $userInfo = $userInfo ? $userInfo->toArray() : [];
- $userProfit = isset($userInfo['profit']) ? $userInfo['profit'] : 0;
- $userUsdt = isset($userInfo['usdt']) ? $userInfo['usdt'] : 0;
- $userSbt = isset($userInfo['sbt']) ? $userInfo['sbt'] : 0;
- $level = isset($userInfo['member_level']) ? intval($userInfo['member_level']) : 0;
- if (empty($userInfo) || $level <= 0) {
- $logs[$pid] = ['error' => "上级推荐人[{$pid}]无等级,无管理奖", 'orderNo' => $orderNo, 'uid' => $userId, 'pledgeUsdt' => $pledgeUsdt];
- continue;
- }
- $bonusRate = isset($userInfo['bonus_rate']) && $userInfo['bonus_rate'] ? floatval($userInfo['bonus_rate']) : 0;
- $weightingRate = isset($userInfo['bonus_rate']) && $userInfo['weighting_rate'] ? floatval($userInfo['weighting_rate']) : 0;
- $realBonusRate = max(0, round($bonusRate - $bonusRateTotal, 2)); // 级差佣金比例,减去已分的部分
- $awardUsdt = moneyFormat($pledgeUsdt * $realBonusRate / 100, 2);
- $bonusAwardUsdt = $awardUsdt;
- // 全网加权收益
- $teamPerformance = 0;
- $globalPledgeTotal = 0;
- $weightingAwardUsdt = 0;
- $globalLevelTeamPerformance = 0;
- if ($weightingRate > 0 && $weightingRate < 100) {
- // 全网流水(质押中)
- $globalPledgeTotal = PledgeOrderService::make()->getGlobalTotal();
- if ($globalPledgeTotal > 0) {
- // 该等级全网业绩
- $globalLevelTeamPerformance = TeamService::make()->getTeamPerformanceByLevel($level);
- // 该用户团队业绩
- $teamPerformance = TeamService::make()->getTeamPerformanceByType($pid, 2, true);
- // 等级加权比例
- $levelWeightingRate = round($teamPerformance/$globalLevelTeamPerformance, 2);
- // 实际收益 = 基础收益 + 加权收益(3%全网流水 x 加权比例)
- $weightingAwardUsdt = moneyFormat($globalPledgeTotal * ($weightingRate / 100) * $levelWeightingRate, 2);
- $bonusAwardUsdt = moneyFormat($weightingAwardUsdt + $bonusAwardUsdt, 2);
- }
- }
- if ($bonusRate <= 0 || $realBonusRate <= 0 || $bonusAwardUsdt <= 0) {
- $logs[$pid] = ['error' => "上级推荐人[{$pid}]无管理奖收益", 'rate' => $bonusRate, 'realRate' => $realBonusRate, 'bonusUsdt' => $bonusAwardUsdt, 'orderNo' => $orderNo, 'uid' => $userId, 'pledgeUsdt' => $pledgeUsdt];
- continue;
- }
- // 结算划分
- $bonusRateTotal += $realBonusRate;
- $bonusUsdt = moneyFormat($bonusAwardUsdt * $settleUsdtRate / 100, 2); // 划分到USDT部分
- $bonusSbt = moneyFormat(($bonusAwardUsdt - $bonusUsdt) / $sbtPrice, 2); // 划分到SBT金本位部分
- if ($bonusUsdt <= 0 && $bonusSbt <= 0) {
- DB::rollBack();
- $logs[$pid] = ['error' => "上级推荐人[{$pid}]管理奖划分计算错误", 'rate' => $bonusRate, 'usdtRate' => $settleUsdtRate, 'sbtPrice' => $sbtPrice, 'realRate' => $realBonusRate, 'bonusUsdt' => $bonusAwardUsdt, 'orderNo' => $orderNo, 'uid' => $userId, 'pledgeUsdt' => $pledgeUsdt];
- $this->error = "上级推荐人[{$pid}]管理奖划分计算错误";
- return false;
- }
- // 验证是否结算过
- if ($logId = AccountLogModel::where(['user_id' => $pid, 'order_no' => $orderNo, 'type' => 9, 'status' => 1, 'mark' => 1])->value('id')) {
- $logs[$pid] = ['error' => "上级推荐人[{$pid}]管理奖已结算过", 'logId' => $logId, 'rate' => $bonusRate, 'realRate' => $realBonusRate, 'bonusUsdt' => $bonusAwardUsdt, 'orderNo' => $orderNo, 'uid' => $userId, 'pledgeUsdt' => $pledgeUsdt];
- continue;
- }
- // 结算入账
- $updateData = [
- 'profit' => DB::raw("profit + {$bonusUsdt}"),
- 'profit_total' => DB::raw("profit_total + {$bonusAwardUsdt}"),
- 'manage_profit' => DB::raw("manage_profit + {$bonusAwardUsdt}"),
- 'sbt' => DB::raw("sbt + {$bonusSbt}"),
- 'update_time' => time(),
- ];
- if (!MemberModel::where(['id' => $pid])->update($updateData)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的推荐人[{$pid}]管理奖收益[{$bonusAwardUsdt}]结算错误";
- return false;
- }
- // USDT收益账单
- $data = [
- 'user_id' => $pid,
- 'order_no' => $orderNo,
- 'source_uid' => $userId,
- 'type' => 9,
- 'round' => isset($order['round']) ? $order['round'] : 0,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => $bonusUsdt,
- 'before_money' => $userProfit,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => "S{$level}管理奖{$realBonusRate}%的{$settleUsdtRate}%",
- 'status' => 1,
- 'mark' => 1,
- ];
- // 有全网加权
- if($globalLevelTeamPerformance && $weightingAwardUsdt){
- $data['settle_remark'] = "S{$level}全网[{$globalLevelTeamPerformance}]团队业绩[{$teamPerformance}]全网流水[{$globalPledgeTotal}]加权收益[{$weightingAwardUsdt}]";
- }
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的推荐人[{$pid}]管理奖[{$bonusAwardUsdt}]结算错误";
- return false;
- }
- // SBT收益账单
- if ($bonusSbt > 0) {
- $data = [
- 'user_id' => $pid,
- 'order_no' => $orderNo,
- 'source_uid' => $userId,
- 'type' => 9,
- 'round' => isset($order['round']) ? $order['round'] : 0,
- 'user_type' => 1,
- 'coin_type' => 2,
- 'money' => $bonusSbt,
- 'before_money' => $userSbt,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => "S{$level}管理奖{$realBonusRate}%的" . (100 - $settleUsdtRate) . '%',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的推荐人[{$pid}]管理奖[{$bonusAwardUsdt}]结算错误";
- return false;
- }
- }
- // S1等级及以上,平级奖数据(若有上级)
- $parentIds = isset($userInfo['parent_ids']) ? $userInfo['parent_ids'] : '';
- $parentIds = get_parents($parentIds);
- if ($parentIds && $level >= 1) {
- $pjUsers[$pid] = [
- 'level' => $level, // 管理奖用户等级
- 'usdt' => $bonusAwardUsdt, // 管理奖USDT金额
- 'parent_ids' => $parentIds, // 管理奖用户上级推荐人数组
- ];
- }
- $logs[$pid] = ['error' => "推荐人[{$pid}]管理奖结算成功", 'orderNo' => $orderNo, 'uid' => $userId, 'profit' => $userProfit, 'bonusAwardUsdt' => $bonusAwardUsdt, 'awardUsdt' => $awardUsdt, 'pledgeUsdt' => $pledgeUsdt];
- $bonusTotal += $bonusAwardUsdt;
- $count++;
- }
- // TODO 平级奖结算
- $pjLogs = [];
- $pjCount = 0;
- $pjTotal = 0;
- $pjProfitRate = ConfigService::make()->getConfigByCode('pj_profit_rate', 0);
- $pjProfitRate = $pjProfitRate > 0 && $pjProfitRate < 100 ? $pjProfitRate : 0;
- if ($pjUsers && $pjProfitRate > 0) {
- foreach ($pjUsers as $uid => $item) {
- $pjLevel = isset($item['level']) ? $item['level'] : 0;
- $bonusUsdt = isset($item['usdt']) ? $item['usdt'] : 0;
- $pjParentIds = isset($item['parent_ids']) ? $item['parent_ids'] : [];
- if ($pjLevel <= 0 || empty($pjParentIds) || $bonusUsdt <= 0) {
- DB::rollBack();
- $pjLogs[$uid] = ['error' => "管理奖用户[{$uid}]平级奖参数错误", 'log' => $item];
- $this->error = "管理奖用户[{$uid}]平级奖参数错误[{$pjLevel}-{$bonusUsdt}]";
- return false;
- }
- // 平级奖金额
- $pjAwardUsdt = moneyFormat($bonusUsdt * $pjProfitRate / 100, 2);
- if ($pjAwardUsdt <= 0) {
- $pjLogs[$uid] = ['error' => "管理奖用户[{$uid}]无平级收益结算", 'rate' => $pjProfitRate, 'log' => $item];
- continue;
- }
- // 是否有平级推荐人
- $pjInfo = TeamService::make()->getUpperLevelUser($level, $pjParentIds);
- $pjUserId = isset($pjInfo['id']) ? $pjInfo['id'] : 0;
- $pjUserProfit = isset($pjInfo['profit']) ? $pjInfo['profit'] : 0;
- if (empty($pjInfo) || $pjUserId <= 0) {
- $pjLogs[$uid] = ['error' => "管理奖用户[{$uid}]无平级上级", 'profit' => $pjAwardUsdt, 'log' => $item];
- continue;
- }
- // 平级奖结算到账
- $updateData = [
- 'profit' => DB::raw("profit + {$pjAwardUsdt}"),
- 'profit_total' => DB::raw("profit_total + {$pjAwardUsdt}"),
- 'pj_profit' => DB::raw("pj_profit + {$pjAwardUsdt}"),
- 'update_time' => time()
- ];
- if (!MemberModel::where(['id' => $pjUserId])->update($updateData)) {
- DB::rollBack();
- $pjLogs[$uid] = ['error' => "管理奖用户[{$uid}]上级[{$pjUserId}]平级奖结算失败", 'profit' => $pjAwardUsdt, 'log' => $item];
- $this->error = "管理奖用户[{$uid}]上级[{$pjUserId}]平级奖结算失败";
- return false;
- }
- // USDT收益账单
- $data = [
- 'user_id' => $pjUserId,
- 'order_no' => $orderNo,
- 'source_uid' => $uid,
- 'type' => 10,
- 'round' => isset($order['round']) ? $order['round'] : 0,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => $pjAwardUsdt,
- 'before_money' => $pjUserProfit,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => "S{$pjLevel}平级奖励",
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的管理奖用户[{$uid}]平级奖用户[{$pjUserId}]的平级奖[{$pjAwardUsdt}]结算错误";
- return false;
- }
- }
- }
- DB::commit();
- RedisService::set("caches:pledgeOrder:share_{$userId}_{$orderNo}", ['user_id' => $userId, 'parentIds' => $parentIds, 'order' => $order, 'log' => $logs, 'pjRate' => $pjProfitRate, 'pjLogs' => $pjLogs], 6 * 3600);
- return ['user_id' => $userId, 'bonusTotal' => $bonusTotal, 'count' => $count, 'all' => count($parentIds), 'pjTotal' => $pjTotal, 'pjCount' => $pjCount, 'pjAll' => count($pjUsers), 'orderNo' => $orderNo, 'money' => $pledgeUsdt];
- } catch (\Exception $exception) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]管理奖和平级奖结算错误:" . $exception->getMessage();
- return false;
- }
- }
- /**
- * 推荐收益奖励结算
- * @param $userId 质押订单用户
- * @param $parentIds 质押订单用户上级推荐人ID串
- * @param $order 结算质押订单:order_no-订单号,money-质押金额,round-质押轮次
- * @return array|bool
- */
- public function settleShareAward($userId, $parentIds, $order)
- {
- $orderNo = isset($order['order_no']) ? $order['order_no'] : '';
- $pledgeUsdt = isset($order['money']) ? floatval($order['money']) : 0;
- if ($pledgeUsdt <= 0 || empty($orderNo)) {
- $this->error = lang(4510, ['uid' => $userId, 'order_no' => $orderNo]);
- return false;
- }
- $parentIds = get_parents($parentIds);
- // $parentIds = array_slice($parentIds, 0, 10);
- if (empty($parentIds)) {
- $this->error = lang(4511, ['uid' => $userId, 'order_no' => $orderNo]);
- return true;
- }
- // 推荐奖励参数
- try {
- $count = 0;
- $logs = [];
- $floor = 0;
- $shareRates = InstitutionalService::make()->getAwardByFloor();
- DB::beginTransaction();
- foreach ($parentIds as $key => $pid) {
- if($floor>7){
- $logs[$pid] = ['error' => "该推荐人[{$pid}]超过结算层级,不结算", 'orderNo' => $orderNo, 'uid' => $userId, 'pledgeUsdt' => $pledgeUsdt, 'floor' => $floor, 'rate' => $rate];
- continue;
- }
- // 验证用户是否有效
- if (!PledgeOrderService::make()->checHasPledge($pid)) {
- $logs[$pid] = ['error' => "该推荐人[{$pid}]是无效用户,不计算奖励不计算层级", 'orderNo' => $orderNo, 'uid' => $userId, 'pledgeUsdt' => $pledgeUsdt, 'floor' => $floor+1, 'rate' => $rate];
- continue;
- }
- $rateInfo = isset($shareRates[$floor+1]) ? $shareRates[$floor+1] : [];
- $rate = isset($rateInfo['value']) && $rateInfo['value'] > 0 && $rateInfo['value'] < 100 ? floatval($rateInfo['value']) : 0;
- if (empty($rateInfo) || $rate <= 0) {
- $floor++;
- $logs[$pid] = ['error' => "该层推荐人[{$pid}]推荐收益比例参数未配置,算层级不结算", 'orderNo' => $orderNo, 'uid' => $userId, 'pledgeUsdt' => $pledgeUsdt, 'floor' => $floor+1];
- continue;
- }
- $shareAwardUsdt = moneyFormat($pledgeUsdt * $rate / 100, 2);
- if ($shareAwardUsdt <= 0) {
- $floor++;
- $logs[$pid] = ['error' => "该层推荐人[{$pid}]推荐无推荐收益,算层级不结算", 'orderNo' => $orderNo, 'uid' => $userId, 'pledgeUsdt' => $pledgeUsdt, 'floor' => $floor+1, 'rate' => $rate];
- continue;
- }
- $userInfo = MemberModel::where(['id' => $pid, 'mark' => 1])
- ->select(['id', 'usdt', 'profit', 'sbt', 'profit_total', 'status'])
- ->first();
- $userInfo = $userInfo ? $userInfo->toArray() : [];
- $userProfit = isset($userInfo['profit']) ? $userInfo['profit'] : 0;
- if (empty($userInfo) || $pid <= 0) {
- $logs[$pid] = ['error' => "该推荐人[{$pid}]信息不存在,不算层级", 'orderNo' => $orderNo, 'uid' => $userId, 'pledgeUsdt' => $pledgeUsdt, 'floor' => $floor+1, 'rate' => $rate];
- continue;
- }
- // 验证是否结算过
- if ($logId = AccountLogModel::where(['user_id' => $pid, 'order_no' => $orderNo, 'type' => 8, 'status' => 1, 'mark' => 1])->value('id')) {
- $floor++;
- $logs[$pid] = ['error' => "该层推荐人[{$pid}]推荐奖励已结算过,算层级", 'logId' => $logId, 'orderNo' => $orderNo, 'uid' => $userId, 'pledgeUsdt' => $pledgeUsdt, 'floor' => $floor+1, 'rate' => $rate];
- continue;
- }
- // 结算入账
- $updateData = [
- 'profit' => DB::raw("profit + {$shareAwardUsdt}"),
- 'profit_total' => DB::raw("profit_total + {$shareAwardUsdt}"),
- 'share_profit' => DB::raw("share_profit + {$shareAwardUsdt}"),
- 'update_time' => time(),
- ];
- if (!MemberModel::where(['id' => $pid])->update($updateData)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的[{$floor}]层推荐人[{$pid}]推荐收益[{$shareAwardUsdt}]结算错误";
- return false;
- }
- // 收益账单
- $data = [
- 'user_id' => $pid,
- 'order_no' => $orderNo,
- 'source_uid' => $userId,
- 'type' => 8,
- 'round' => isset($order['round']) ? $order['round'] : 0,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => $shareAwardUsdt,
- 'before_money' => $userProfit,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => ($floor+1)."层推荐奖励",
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]的[".($floor+1)."]层推荐人[{$pid}]推荐收益[{$shareAwardUsdt}]结算错误";
- return false;
- }
- $logs[$pid] = ['error' => "推荐人[{$pid}]奖励结算成功", 'orderNo' => $orderNo, 'uid' => $userId, 'profit' => $userProfit, 'awardUsdt' => $shareAwardUsdt, 'pledgeUsdt' => $pledgeUsdt, 'floor' => $floor+1, 'rate' => $rate];
- $count++;
- $floor++;
- }
- DB::commit();
- RedisService::set("caches:pledgeOrder:share_{$userId}_{$orderNo}", ['user_id' => $userId, 'parentIds' => $parentIds, 'order' => $order, 'log' => $logs], 6 * 3600);
- return ['user_id' => $userId, 'count' => $count, 'all' => count($parentIds), 'orderNo' => $orderNo, 'money' => $pledgeUsdt];
- } catch (\Exception $exception) {
- DB::rollBack();
- $this->error = "用户[{$userId}]质押订单[{$orderNo}]推荐收益结算错误:" . $exception->getMessage();
- return false;
- }
- }
- /**
- * 开发者维护收益结算
- * @param $developerId 开发者ID
- * @param $walletUrl 开发者地址
- * @return false
- */
- public function developerSettle($developerId, $walletUrl)
- {
- if(empty($developerId) || empty($walletUrl)){
- $this->error = 2014;
- return false;
- }
- $info = DeveloperModel::from('developer as a')
- ->leftJoin('member as b','b.wallet_url','=','a.wallet_url')
- ->where(['a.id'=> $developerId])
- ->select(['a.*','b.id as user_id','b.profit'])
- ->first();
- $info = $info? $info->toArray() : [];
- $userId = isset($info['user_id'])? intval($info['user_id']) : 0;
- $userProfit = isset($info['profit'])? floatval($info['profit']) : 0;
- $profitRate = isset($info['maintenance_rate'])? floatval($info['maintenance_rate']) : 0;
- if(empty($info) || $userId<=0){
- $this->error = 2001;
- return false;
- }
- $lastSettleAt = isset($info['last_settle_at'])? $info['last_settle_at'] : '';
- if($lastSettleAt && $lastSettleAt >= date('Y-m-d', time() - 7 * 86400)){
- $this->error = 1044;
- return false;
- }
- $cacheKey = "caches:developer:lock_{$developerId}";
- if(RedisService::get($cacheKey)){
- $this->error = 1044;
- return false;
- }
- DB::beginTransaction();
- try {
- // 全网流水
- $pledgeTotal = PledgeOrderService::make()->getGlobalTotal();
- // 结算比例
- $settleRate = ConfigService::make()->getConfigByCode('developer_settle_rate', 3);
- $settleRate = $settleRate>0 && $settleRate <= 20? $settleRate : 3;
- $settleRate = $profitRate>0 && $profitRate< 50? $profitRate : $settleRate;
- $settleMoney = round($pledgeTotal * $settleRate/100, 2);
- if($settleMoney>0){
- $updateData = [
- 'profit'=> DB::raw("profit + {$settleMoney}"),
- 'profit_total'=> DB::raw("profit_total + {$settleMoney}"),
- 'update_time'=>time()
- ];
- if(!MemberModel::where(['id'=> $userId])->update($updateData)){
- DB::rollBack();
- $this->error = 1045;
- return false;
- }
- $updateData = [
- 'total'=> DB::raw("total + {$settleMoney}"),
- 'last_settle_at'=>date('Y-m-d H:i:s'),
- 'update_time'=>time()
- ];
- if(!DeveloperModel::where(['id'=> $developerId])->update($updateData)){
- DB::rollBack();
- $this->error = 1045;
- return false;
- }
- // 收益账单
- $data = [
- 'user_id' => $userId,
- 'order_no' => get_order_num('DP'),
- 'source_uid' => 0,
- 'type' => 99,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => $settleMoney,
- 'before_money' => $userProfit,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => "工作室分红",
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = 1045;
- return false;
- }
- }
- DB::commit();
- $this->error = 1046;
- return ['id'=>$developerId,'user_id'=>$userId,'money'=>$settleMoney];
- }catch (\Exception $exception){
- $this->error = $exception->getMessage();
- return false;
- }
- }
- }
|