| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746 |
- <?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\MemberModel;
- use App\Models\PledgeOrderModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 租赁订单-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class PledgeOrderService extends BaseService
- {
- protected static $instance;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new PledgeOrderModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 质押订单统计
- * @param $userId 用户ID
- * @return array|mixed
- */
- public function getCountByUser($userId)
- {
- $cacheKey = "caches:pledge:counts_{$userId}";
- $counts = RedisService::get($cacheKey);
- if($counts){
- $expiredTime = isset($counts['expired_time'])? intval($counts['expired_time']) : 0;
- $settleTime = isset($counts['settle_time'])? intval($counts['settle_time']) : 0;
- $counts['pledge_end_time'] = $expiredTime>time()? intval($expiredTime - time()) : 0;
- $counts['pledge_settle_time'] = $settleTime>time()? intval($settleTime - time()) : 0;
- $counts['pledge_end_text'] = timeFormat($counts['pledge_end_time']);
- $counts['pledge_settle_data'] = timeFormat($counts['pledge_settle_time'],2);
- return $counts;
- }
- $counts = [
- "pledge_count" => 0,
- "pledge_total" => "0.00",
- "pledge_money" => "0.00",
- "pledge_profit" => "0.00",
- "pledge_end_time" => 0,
- "pledge_settle_time" => 0,
- "pledge_ent_text" => '00:00:00',
- "pledge_settle_data" => [
- 'hour'=> '00',
- 'minute'=> '00',
- 'second'=> '00',
- ],
- ];
- if($userId){
- $counts['pledge_count'] = (int)$this->model->where(['user_id'=> $userId,'mark'=>1])->count('id');
- $counts['pledge_total'] = moneyFormat($this->model->where(['user_id'=> $userId,'mark'=>1])->sum('money'),2);
- $info = $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->orderBy('create_time','desc')->orderBy('id','desc')->first();
- $info = $info? $info->toArray() : [];
- if($info){
- $counts['pledge_money'] = moneyFormat($info['money'],2);
- $counts['pledge_profit'] = moneyFormat($info['profit'], 2);
- $expiredTime = $info['expired_at']? strtotime($info['expired_at']) : 0;
- $settleTime = $info['settle_at']? strtotime($info['settle_at']) : 0;
- $counts['expired_time'] = $expiredTime;
- $counts['settle_time'] = $settleTime;
- $counts['pledge_end_time'] = $expiredTime>time()? intval($expiredTime - time()) : 0;
- $counts['pledge_settle_time'] = $settleTime>time()? intval($settleTime - time()) : 0;
- $counts['pledge_end_text'] = timeFormat($counts['pledge_end_time']);
- $counts['pledge_settle_data'] = timeFormat($counts['pledge_settle_time'],2);
- }
- }
- RedisService::set($cacheKey, $counts, rand(10, 30));
- return $counts;
- }
- /**
- * 检测是否质押过且超过了24小时退本(即是否有退本过)
- * @param $userId 质押用户ID
- * @return array|false|mixed
- */
- public function checkPledgeRefund($userId)
- {
- $cacheKey = "caches:pledge:checkRefund_{$userId}";
- $data = RedisService::get($cacheKey);
- if($data || RedisService::exists($cacheKey)){
- return $data;
- }
- $data = $this->model->where(['user_id'=> $userId,'status'=> 2,'mark'=>1])->value('id');
- if($data){
- RedisService::set($cacheKey, $data, rand(3600,7200));
- }
- return $data;
- }
- /**
- * 检测是否质押过
- * @param $userId 质押用户ID
- * @return array|false|mixed
- */
- public function checkPledge($userId)
- {
- $cacheKey = "caches:pledge:check_exists_{$userId}";
- $data = RedisService::get($cacheKey);
- if($data || RedisService::exists($cacheKey)){
- return $data;
- }
- $data = $this->model->where(['user_id'=> $userId,'mark'=>1])->value('id');
- if($data){
- RedisService::set($cacheKey, $data, 600);
- }
- return $data;
- }
- /**
- * 验证质押订单状态
- * @param $userId 质押用户
- * @return array|mixed
- */
- public function checHasPledge($userId)
- {
- $cacheKey = "caches:pledge:check_{$userId}";
- $data = RedisService::get($cacheKey);
- if($data){
- return $data;
- }
- $data = $this->model->where(['user_id'=> $userId,'mark'=>1])
- ->where(function($query){
- $query->where('expired_at','>', date('Y-m-d H:i:s'))
- ->orWhere(['status'=>1]);
- })
- ->select(['id','user_id','order_no','money','expired_at','status'])
- ->first();
- $data = $data? $data->toArray() :[];
- if($data){
- RedisService::set($cacheKey, $data, rand(300, 600));
- }
- return $data;
- }
- /**
- * 自动质押处理
- * @param $userInfo 用户信息:id-用户ID,pledge_auto-质押状态,usdt-USDT余额,sbt-sbt余额,status-状态,parent_ids-上级团队ID(升级用)
- * @return bool
- */
- public function autoMakeOrder($userInfo)
- {
- $userId = isset($userInfo['id'])? intval($userInfo['id']) : 0;
- $pledgeAuto = isset($userInfo['pledge_auto'])? intval($userInfo['pledge_auto']) : 0;
- $userUsdt = isset($userInfo['usdt'])? floatval($userInfo['usdt']) : 0;
- $userSbt = isset($userInfo['sbt'])? floatval($userInfo['sbt']) : 0;
- $tradeStatus = isset($userInfo['trade_status'])? intval($userInfo['trade_status']) : 0;
- $userStatus = isset($userInfo['status'])? intval($userInfo['status']) : 0;
- $parentIds = isset($userInfo['parent_ids'])? trim($userInfo['parent_ids']) : '';
- $settleStatus = isset($userInfo['settle_status'])? intval($userInfo['settle_status']) : 0;
- $settleStatus = $settleStatus==2? 2 : 1; // 1-正常结算,2-不结算动态和个人业绩
- if($userId<=0 || $userUsdt<=0 || $userStatus != 1){
- $this->error = lang(4101,['uid'=>$userId,'usdt'=>$userUsdt,'status'=>$userStatus]);
- return false;
- }
- // 检测是否已经开启了自动质押
- if($pledgeAuto != 1 || $tradeStatus == 2){
- $this->error = 4104;
- return false;
- }
- // 质押所需USDT验证
- $pledgeUsdt = ConfigService::make()->getConfigByCode('pledge_money', 500);
- $pledgeUsdt = $pledgeUsdt>0?$pledgeUsdt: 500;
- if($userUsdt < $pledgeUsdt){
- $this->error = lang(4102,['usdt'=> $userUsdt,'money'=>$pledgeUsdt]);
- return false;
- }
- // SBT 手续费
- $sbtPrice = PriceLogService::make()->getSbtPrice();
- $sbtUsdtFee = ConfigService::make()->getConfigByCode('pledge_sbt_usdt_fee', 0);
- $sbtUsdtFee = $sbtUsdtFee>0? floatval($sbtUsdtFee): 0;
- $sbtFee = $sbtUsdtFee && $sbtPrice>0? moneyFormat($sbtUsdtFee / $sbtPrice,2) : 0;
- if($sbtUsdtFee>0 && $sbtFee>0 && $userSbt < $sbtFee){
- $this->error = lang(4103,['sbt'=> $userSbt,'fee'=>$sbtFee]);
- return false;
- }
- // 质押收益比例参数
- $profitRate = ConfigService::make()->getConfigByCode('pledge_profit_rate',3);
- $profitRate = $profitRate>0 && $profitRate<100? floatval($profitRate) : 3;
- $bonusUsdtRate = ConfigService::make()->getConfigByCode('bonus_usdt_rate',80);
- $bonusUsdtRate = $bonusUsdtRate>0 && $bonusUsdtRate<100? floatval($bonusUsdtRate) : 80;
- // 检测是否有质押中订单
- if($info = PledgeOrderService::make()->checHasPledge($userId)){
- $orderNo = isset($info['order_no'])? $info['order_no'] : '';
- $this->error = lang(4105,['order_no'=>$orderNo]);
- return false;
- }
- // 预计收益
- $profit = moneyFormat($pledgeUsdt * $profitRate/100, 2); // 总收益
- $profitUsdt = moneyFormat($profit * $bonusUsdtRate/100, 2); // USDT余额进账部分
- // 质押有效期
- $pledgeTime = ConfigService::make()->getConfigByCode('pledge_time', 24);
- $pledgeTime = $pledgeTime>0 && $pledgeTime < 30 * 24? floatval($pledgeTime) : 24;
- $expiredAt = date('Y-m-d H:i:s', time() + intval($pledgeTime * 3600));
- // 结算时间
- $pledgeSettleTime = ConfigService::make()->getConfigByCode('pledge_settle_time', 24);
- $pledgeSettleTime = $pledgeSettleTime>0 && $pledgeSettleTime < 30 * 24? floatval($pledgeSettleTime) : 24;
- $settleAt = date('Y-m-d H:i:s', time() + intval($pledgeTime * 3600) + intval($pledgeSettleTime * 3600));
- // 质押轮次
- $lastRound = $this->model->where(['user_id'=> $userId,'mark'=>1])
- ->orderBy('round','desc')
- ->orderBy('create_time','desc')
- ->value('round');
- $round = $lastRound>0? intval($lastRound) + 1 : 1;
- // 缓存锁
- if(RedisService::get("caches:pledge:lock_{$userId}_{$round}")){
- $this->error = lang(4111,['uid'=> $userId,'round'=> $round]);
- return false;
- }
- // 若满足自动质押条件
- DB::beginTransaction();
- RedisService::set("caches:pledge:lock_{$userId}_{$round}",rand(60,120));
- try{
- // TODO 质押订单
- $order = [
- 'user_id'=> $userId,
- 'order_no'=> get_order_num('PD'),
- 'money'=> $pledgeUsdt,
- 'bonus_rate'=> $profitRate,
- 'profit'=> $profit,
- 'profit_usdt'=> $profitUsdt,
- 'sbt_price'=> $sbtPrice,
- 'time'=> $pledgeTime,
- 'round'=> $round,
- 'expired_at'=> $expiredAt,
- 'settle_at'=> $settleAt,
- 'settle_status'=> $settleStatus, // 1-正常结算,2-不结算动态和个人业绩
- 'create_time'=>time(),
- 'remark'=> '自动质押',
- 'status'=>1,
- 'mark'=>1,
- ];
- if(!$this->model->insertGetId($order)){
- DB::rollBack();
- $this->error = lang(4106,['uid'=> $userId,'round'=> $round]);
- RedisService::clear("caches:pledge:lock_{$userId}_{$round}");
- return false;
- }
- // 扣除余额和手续费
- $updateData = [
- 'usdt'=> DB::raw("usdt - {$pledgeUsdt}"),
- 'upgrade_at'=>date('Y-m-d H:i:s',time() + 2*86400),
- 'pledge_count'=>DB::raw("pledge_count + 1"),
- 'sbt'=>DB::raw("sbt - {$sbtFee}"),
- 'update_time'=>time()
- ];
- // 正常结算业绩
- if($settleStatus==1){
- $updateData['performance'] = DB::raw("performance + {$pledgeUsdt}");
- }
- if(!MemberModel::where(['id'=> $userId])->update($updateData)){
- DB::rollBack();
- $this->error = lang(4107,['uid'=> $userId,'round'=>$round,'money'=> $pledgeUsdt]);
- RedisService::clear("caches:pledge:lock_{$userId}_{$round}");
- return false;
- }
- // USDT账单
- $data = [
- 'user_id' => $userId,
- 'order_no' => $order['order_no'],
- 'type' => 1,
- 'user_type' => 1,
- 'round' => $round,
- 'coin_type' => 1,
- 'money' => -$pledgeUsdt,
- 'before_money' => $userUsdt,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => '质押交易',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = lang(4108,['uid'=> $userId,'round'=>$round,'money'=> $pledgeUsdt]);
- RedisService::clear("caches:pledge:lock_{$userId}_{$round}");
- return false;
- }
- // SBT 手续费
- if($sbtFee){
- $data = [
- 'user_id' => $userId,
- 'order_no' => $order['order_no'],
- 'type' => 1,
- 'round' => $round,
- 'user_type' => 1,
- 'coin_type' => 2,
- 'money' => -$sbtFee,
- 'before_money' => $userSbt,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => '质押交易手续费',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = lang(4108,['uid'=> $userId,'round'=>$round]);
- RedisService::clear("caches:pledge:lock_{$userId}_{$round}");
- return false;
- }
- }
- DB::commit();
- // 更新上级团队用户的等级更新时间
- $parentIds = get_parents($parentIds);
- if($parentIds){
- MemberModel::whereIn('id', $parentIds)->update(['upgrade_at'=>date('Y-m-d H:i:s',time() + 2*86400),'update_time'=>time()]);
- }
- // 质押轮次奖励
- FinanceService::make()->pledgeAward($userId, $round, $order);
- // 团队分红结算
- FinanceService::make()->settleTeamBonus($userId, $parentIds, $order);
- // 工作室分红结算
- FinanceService::make()->settleRoomBonus($userId, $parentIds, $order);
- $this->error = $this->error = lang(4109,['uid'=> $userId,'round'=>$round]);
- RedisService::clear("caches:pledge:lock_{$userId}_{$round}");
- RedisService::clear("caches:pledgeOrder:total_{$userId}");
- RedisService::clear("caches:pledge:check_{$userId}");
- RedisService::clear("caches:pledge:counts_{$userId}");
- return ['user_id'=>$userId,'round'=>$round,'money'=>$pledgeUsdt,'msg'=>'自动质押成功'];
- } catch (\Exception $exception){
- DB::rollBack();
- $this->error = lang(4110,['uid'=> $userId,'round'=>$round,'error'=> $exception->getMessage()]);
- return false;
- }
- }
- /**
- * 获取需要退本的订单
- * @return array|false
- */
- public function getRefundOrderList()
- {
- try {
- $limit = ConfigService::make()->getConfigByCode('refund_pledge_count', 500);
- $limit = $limit > 0 ? $limit : 500;
- $datas = $this->model->where(function($query){
- $query->where('expired_at', '<=', date('Y-m-d H:i:s'));
- })
- ->where(['status' => 1, 'mark' => 1])
- ->select(['id','user_id', 'order_no','money','status'])
- ->limit($limit)
- ->orderByRaw('rand()')
- ->orderBy('expired_at', 'asc')
- ->get();
- return $datas?$datas->toArray() : [];
- } catch (\Exception $exception) {
- $this->error = $exception->getMessage();
- return false;
- }
- }
- /**
- * 获取需要结算收益的订单
- * @return array|false
- */
- public function getSettleOrderList()
- {
- try {
- $limit = ConfigService::make()->getConfigByCode('settle_pledge_count', 500);
- $limit = $limit > 0 ? $limit : 500;
- $datas = $this->model->where(function($query){
- $query->where('settle_at', '<=', date('Y-m-d H:i:s'));
- })
- ->where(['status' => 2, 'mark' => 1])
- ->select(['id','user_id', 'order_no','money','status'])
- ->limit($limit)
- ->orderByRaw('rand()')
- ->orderBy('settle_at', 'asc')
- ->get();
- return $datas?$datas->toArray() : [];
- } catch (\Exception $exception) {
- $this->error = $exception->getMessage();
- return false;
- }
- }
- /**
- * 自动退本
- * @return bool
- */
- public function refund($orderId,$orderNo, $userId=0)
- {
- $cacheKey = "caches:pledgeOrder:refund_{$orderId}";
- if(RedisService::get($cacheKey)){
- $this->error = lang(4310,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // 订单信息
- $orderInfo = $this->model::where(['id'=> $orderId,'mark'=>1])
- ->select(['id','user_id','order_no','money','round','bonus_rate','profit','profit_usdt','expired_at','status'])
- ->first();
- $orderInfo = $orderInfo? $orderInfo->toArray() : [];
- if(empty($orderInfo)){
- $this->error = lang(4311,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // 状态验证
- $expiredAt = isset($orderInfo['expired_at'])? $orderInfo['expired_at'] : '';
- $status = isset($orderInfo['status'])? $orderInfo['status'] : 0;
- $pledgeUserId = isset($orderInfo['user_id'])? $orderInfo['user_id'] : 0;
- $pledgeUsdt = isset($orderInfo['money'])? floatval($orderInfo['money']) : 0;
- $pledgeRound = isset($orderInfo['round'])? intval($orderInfo['round']) : 0;
- $settleStatus = isset($orderInfo['settle_status'])? intval($orderInfo['settle_status']) : 0;
- $settleStatus = $settleStatus==2? 2 : 1; // 结算动态收益状态:1-正常,2-不结算动态
- if($expiredAt>date('Y-m-d H:i:s') || $status != 1 || $pledgeRound<=0){
- $this->error = lang(4312,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // 退本金额
- if($pledgeUsdt<=0){
- $this->error = lang(4316,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // 质押用户信息
- $memberInfo = MemberModel::where(['id'=> $pledgeUserId,'mark'=>1])
- ->select(['id','usdt','sbt','profit','performance','pledge_count','status'])
- ->first();
- $memberInfo = $memberInfo? $memberInfo->toArray() : [];
- $userUsdt = isset($memberInfo['usdt'])? $memberInfo['usdt'] : 0;
- $performance = isset($memberInfo['performance'])? $memberInfo['performance'] : 0;
- $pledgeCount = isset($memberInfo['pledge_count'])? $memberInfo['pledge_count'] : 0;
- if(empty($memberInfo) || $pledgeUserId != $userId){
- $this->error = lang(4313,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // 订单退本处理
- DB::beginTransaction();
- try{
- // 更新订单状态
- if(!$this->model->where(['id'=> $orderId])->update(['status'=>2,'refund_at'=>date('Y-m-d H:i:s'),'update_time'=>time()])){
- DB::rollBack();
- $this->error = lang(4314,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // 返还本金
- $updateData = [
- 'usdt'=>DB::raw("usdt + {$pledgeUsdt}"),
- 'pledge_count'=>max(0,($pledgeCount - 1)),
- 'update_time'=>time()
- ];
- // 正常结算业绩
- if($settleStatus == 1){
- $updateData['performance'] = max(0,($performance - $pledgeUsdt));
- }
- if(!MemberModel::where(['id'=>$pledgeUserId])->update($updateData)){
- DB::rollBack();
- $this->error = lang(4315,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // 退本账单
- $data = [
- 'user_id' => $userId,
- 'order_no' => $orderNo,
- 'type' => 12,
- 'round' => $pledgeRound,
- 'user_type' => 1,
- 'coin_type' => 1,
- 'money' => $pledgeUsdt,
- 'before_money' => $userUsdt,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => '质押到期退还',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = lang(4317,['uid'=> $userId,'order_no'=>$orderNo]);
- return false;
- }
- DB::commit();
- $this->error = lang(4318,['uid'=> $userId,'order_no'=>$orderNo,'performance'=>$pledgeUsdt]);
- $order = ['user_id'=>$userId,'round'=>$pledgeRound,'performance'=>$pledgeUsdt,'order_no'=>$orderNo];
- RedisService::set($cacheKey, $order, rand(300, 600));
- RedisService::clear("caches:pledge:check_{$userId}");
- return $order;
- }catch (\Exception $exception){
- DB::rollBack();
- $this->error = $exception->getMessage();
- return false;
- }
- }
- /**
- * 到期结算收益
- * @return bool
- */
- public function orderSettle($orderId, $orderNo, $userId=0)
- {
- $cacheKey = "caches:pledgeOrder:settle_{$orderId}";
- if(RedisService::get($cacheKey)){
- $this->error = lang(4410,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // 订单信息
- $orderInfo = $this->model::where(['id'=> $orderId,'mark'=>1])
- ->select(['id','user_id','order_no','money','round','bonus_rate','sbt_price','profit','profit_usdt','settle_at','settle_status','status'])
- ->first();
- $orderInfo = $orderInfo? $orderInfo->toArray() : [];
- if(empty($orderInfo)){
- $this->error = lang(4411,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // 状态验证
- $settleAt = isset($orderInfo['settle_at'])? $orderInfo['settle_at'] : '';
- $status = isset($orderInfo['status'])? $orderInfo['status'] : 0;
- $pledgeUserId = isset($orderInfo['user_id'])? $orderInfo['user_id'] : 0; // 质押用户
- $pledgeUsdt = isset($orderInfo['money'])? floatval($orderInfo['money']) : 0; // 质押金额
- $profit = isset($orderInfo['profit'])? floatval($orderInfo['profit']) : 0; // 总收益
- $profitUsdt = isset($orderInfo['profit_usdt'])? floatval($orderInfo['profit_usdt']) : 0; // USDT收益
- $pledgeRound = isset($orderInfo['round'])? intval($orderInfo['round']) : 0;
- $settleStatus = isset($orderInfo['settle_status'])? intval($orderInfo['settle_status']) : 0;
- $settleStatus = $settleStatus==2? 2 : 1; // 结算动态收益状态:1-正常,2-不结算动态
- if($settleAt>date('Y-m-d H:i:s') || $status != 2 || $pledgeRound<=0){
- $this->error = lang(4412,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // 质押收益金额
- if($pledgeUsdt<=0 || $profit <=0 || ($profit < $profitUsdt)){
- $this->error = lang(4416,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // 质押用户信息
- $memberInfo = MemberModel::where(['id'=> $pledgeUserId,'mark'=>1])
- ->select(['id','usdt','sbt','profit','performance','parent_ids','status'])
- ->first();
- $memberInfo = $memberInfo? $memberInfo->toArray() : [];
- $userUsdt = isset($memberInfo['usdt'])? $memberInfo['usdt'] : 0;
- $userProfit = isset($memberInfo['profit'])? $memberInfo['profit'] : 0;
- $userSbt = isset($memberInfo['sbt'])? $memberInfo['sbt'] : 0;
- $parentIds = isset($memberInfo['parent_ids'])? $memberInfo['parent_ids'] : '';
- if(empty($memberInfo) || $pledgeUserId != $userId){
- $this->error = lang(4413,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // SBT 收益金额
- $sbtPrice = PriceLogService::make()->getSbtPrice();
- $profitSbt = moneyFormat(($profit - $profitUsdt)/$sbtPrice, 2); // 金本位计算
- // 订单收益结算处理
- DB::beginTransaction();
- try{
- // 更新订单状态
- if(!$this->model->where(['id'=> $orderId])->update(['status'=>3,'bonus_at'=>date('Y-m-d H:i:s'),'update_time'=>time()])){
- DB::rollBack();
- $this->error = lang(4414,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // USDT收益和SBT收益
- $updateData = [
- 'profit'=>DB::raw("profit + {$profitUsdt}"),
- 'profit_total'=>DB::raw("profit_total + {$profit}"),
- 'pledge_profit'=>DB::raw("pledge_profit + {$profit}"),
- 'sbt'=>DB::raw("sbt + {$profitSbt}"),
- 'update_time'=>time()
- ];
- if(!MemberModel::where(['id'=>$pledgeUserId])->update($updateData)){
- DB::rollBack();
- $this->error = lang(4415,['uid'=>$userId,'order_no'=>$orderNo]);
- return false;
- }
- // USDT收益账单
- $data = [
- 'user_id' => $userId,
- 'order_no' => $orderNo,
- 'type' => 7,
- 'round' => $pledgeRound,
- 'user_type' => 1,
- 'coin_type' => 1,
- 'money' => $profitUsdt,
- 'before_money' => $userUsdt,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => '质押收益',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = lang(4417,['uid'=> $userId,'order_no'=>$orderNo]);
- return false;
- }
- // SBT收益账单
- if($profitSbt>0){
- $data = [
- 'user_id' => $userId,
- 'order_no' => $orderNo,
- 'type' => 7,
- 'round' => $pledgeRound,
- 'user_type' => 1,
- 'coin_type' => 2,
- 'money' => $profitSbt,
- 'before_money' => $userSbt,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => '质押收益',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = lang(4417,['uid'=> $userId,'order_no'=>$orderNo]);
- return false;
- }
- }
- // TODO 是否结算动态收益
- if($settleStatus==1){
- // TODO 推荐奖结算
- if(!FinanceService::make()->settleShareAward($userId, $parentIds, $orderInfo)){
- DB::rollBack();
- $this->error = FinanceService::make()->getError();
- return false;
- }
- // TODO 管理奖和平级奖结算
- if(!FinanceService::make()->settleManageAndLevelAward($userId, $parentIds, $orderInfo)){
- DB::rollBack();
- $this->error = FinanceService::make()->getError();
- return false;
- }
- }
- DB::commit();
- $this->error = lang(4418,['uid'=> $userId,'order_no'=>$orderNo]);
- $order = ['user_id'=>$userId,'round'=>$pledgeRound,'profit'=>$profit,'profit_usdt'=>$profitUsdt,'profit_sbt'=>$profitSbt,'order_no'=>$orderNo];
- RedisService::set($cacheKey, $order, rand(300, 600));
- return $order;
- }catch (\Exception $exception){
- DB::rollBack();
- $this->error = '订单结算异常:'.$exception->getMessage();
- return false;
- }
- }
- /**
- * 全网流水(质押中订单总金额)
- * @return array|mixed
- */
- public function getGlobalTotal()
- {
- $cacheKey = "caches:pledgeOrder:globalTotal";
- $data = RedisService::get($cacheKey);
- if($data){
- return $data;
- }
- $data = $this->model->where(['status'=>1,'mark'=>1])->sum('money');
- if($data){
- RedisService::set($cacheKey, $data, 600);
- }
- return $data;
- }
- }
|