adminModel = new Admin(); $this->storeModel = new Store(); $this->massagerModel = new Massager(); $this->pBillModel = new Bill(); $this->mBillModel = new \app\api\model\massager\Bill(); $this->mWalletModel = new Wallet(); $this->userModel = new User(); $this->uBillModel = new \app\api\model\user\Bill(); $this->uWalletModel = new \app\api\model\user\Wallet(); } public function profit($order, $user, $unusual = false) { $platform = $this->adminModel->where("id", "=", 1)->find(); if (!$platform) return $this->fail("Admin不存在!"); $massager = $this->massagerModel->getMassager($order->massager_id); if ($order["massager_id"] > 0) { if (!$massager) return $this->fail("助教信息不存在!"); } $agency = $this->adminModel->findAgency($order->city_code); $redLock = new RedLock(); $agencyLock = false; $sLock = false; $mLock = false; if ($agency) { $agencyLock = $redLock->lock(Admin::AgencyKey($agency->id)); if (!is_array($agencyLock)) return $this->fail("请稍后再试!"); } // 平台抽成比例 $platform_rate = config("site.platform_profit_rate") ?? 6; // 代理商抽成比例 $agency_rate = 0; // 渠道商分佣 $parent_direct_invite_rate = 0; // 1级 $store = $this->storeModel->findById($order->store_id ?? -1); if ($order["store_id"] > 0) { // 球房分润标准 if (!$store) return $this->fail("球房信息不存在!"); $sLock = $redLock->lock(Store::SWKey($order->store_id)); if (!is_array($sLock)) return $this->fail("请稍后再试!"); $self_rate = 100 - $store["platform_profit_rate"] - $store["agency_profit_rate"]; if ($agency) { $agency_rate = $store["agency_profit_rate"]; } else { // 如果代理商不存在 则剩余部分 全部归于平台; $platform_rate = $store["agency_profit_rate"] + $store["platform_profit_rate"]; } } else { // 线上分润标准 $mLock = $redLock->lock(\app\api\model\massager\Wallet::MWKey($order->massager_id)); if (!is_array($mLock)) return $this->fail("请稍后再试!"); // 本人比例 $self_rate = (new MassagerService())->getProfitRate($massager["id"], $massager["city_code"], null, null, date("Y-m-d", $order["createtime"])); if ($agency) { $agency_rate = 100 - ($platform_rate + $self_rate); } else { // 如果代理商不存在 则剩余部分 全部归于平台; $platform_rate = 100 - $self_rate; } } $parent = null; $parent_lock = null; $parent_wallet = null; if (1 == config("site.invite_grant_switch") && !$unusual && $massager["parent_id"] > 0) { // 渠道奖励发放开启 $parent = $this->massagerModel->get($massager["parent_id"]); if ($parent) { $parent_lock = $redLock->lock(Wallet::MWKey($parent->id)); $parent_wallet = $this->mWalletModel->getWallet($parent->id); $parent_direct_invite_rate = config("site.massager_direct_invite_rate"); } } $invite_store = null; $store_direct_invite_rate = 0; if (!$unusual && $user["invite_store_id"] > 0) {// 渠道奖励发放开启 $invite_store = $this->storeModel->get($user["invite_store_id"]); $store_direct_invite_rate = config("site.store_direct_invite_rate"); } if (fixed2Float(($platform_rate + $agency_rate + $self_rate)) != 100) { return $this->fail("比例计算错误!"); } $pLock = $redLock->lock(Admin::PlatformKey()); if (!is_array($pLock)) return $this->fail("请稍后再试!"); $mWallet = $this->mWalletModel->getWallet($order->massager_id); $locks = [$pLock, $agencyLock, $sLock, $mLock, $parent_lock]; $trip_amount = $order->trip_amount; // 实际支付金额 - 出行费 = 订单分润金额 $total_profit_amount = $order->total_real_amount - $trip_amount; if ($unusual && $order["is_refund_trip"] == 1) { $trip_amount = 0; } if ($unusual) { // 管理员终止 $total_profit_amount = $order->total_real_amount - $order->refund_amount - $trip_amount; } if ($total_profit_amount <= 0) return $this->fail("分润金额小于0"); $p_profit_amount = fixed2Float($total_profit_amount * ($platform_rate / 100)); $a_profit_amount = $agency_rate > 0 ? fixed2Float($total_profit_amount * ($agency_rate / 100)) : 0; $self_profit_amount = fixed2Float($total_profit_amount * ($self_rate / 100)); // 助教直推助教奖励(%) $parent_profit_amount = $parent_direct_invite_rate > 0 ? fixed2Float($total_profit_amount * ($parent_direct_invite_rate / 100)) : 0; $total_parent_amount = $parent_profit_amount; // 球房直推用户奖励(%) $invite_store_profit_amount = $store_direct_invite_rate > 0 ? fixed2Float($total_profit_amount * ($store_direct_invite_rate / 100)) : 0; Db::startTrans(); try { $profit_bills = [ [ "identity_type" => \E_IDENTITY_TYPE::Platform, "target_id" => 1, "target_name" => "平台", "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::Profit, "order_no" => $order->no, "total_amount" => $total_profit_amount, "rate" => $platform_rate, "change" => $p_profit_amount, "before" => $platform->profit_amount, "after" => fixed2Float($platform->profit_amount + $p_profit_amount), "createtime" => time(), "city_code" => $order->city_code ] ]; if ($order->store_id > 0) { // 门店 $after = fixed2Float($store->profit_amount + $self_profit_amount); $profit_bills[] = [ "identity_type" => \E_IDENTITY_TYPE::Store, "target_id" => $store->id, "target_name" => $store->name, "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::Profit, "order_no" => $order->no, "total_amount" => $total_profit_amount, "rate" => $self_rate, "change" => $self_profit_amount, "before" => $store->profit_amount, "after" => $after, "createtime" => time(), "city_code" => $order->city_code ]; $this->storeModel->where("id", $store->id)->setInc("profit_amount", $self_profit_amount); } else { // 助教 $after = fixed2Float($mWallet->profit_amount + $self_profit_amount); $profit_bills[] = [ "identity_type" => \E_IDENTITY_TYPE::Massager, "target_id" => $massager->id, "target_name" => $massager->name, "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::Profit, "order_no" => $order->no, "total_amount" => $total_profit_amount, "rate" => $self_rate, "change" => $self_profit_amount - $total_parent_amount - $invite_store_profit_amount, "before" => $mWallet->profit_amount, "after" => $after, "createtime" => time(), "city_code" => $order->city_code ]; $m_bills = [ [ "massager_id" => $massager->id, "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_M_BILL_CHANGE_TYPE::Profit, "change" => $self_profit_amount, "before" => $mWallet->profit_amount, "after" => $after, "reason" => "服务费用", "relation_no" => $order->no, "createtime" => time() ] ]; if ($trip_amount > 0) { $m_bills[] = [ "massager_id" => $massager->id, "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_M_BILL_CHANGE_TYPE::Travel, "change" => $trip_amount, "before" => $after, "after" => $after + $trip_amount, "reason" => "出行费用", "relation_no" => $order->no, "createtime" => time() ]; } if ($total_parent_amount > 0) { $m_bills[] = [ "massager_id" => $massager->id, "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_M_BILL_CHANGE_TYPE::ChannelExpend, "change" => -$total_parent_amount, "before" => ($after + $trip_amount), "after" => ($after + $trip_amount) - $total_parent_amount, "reason" => "技师渠道抽佣", "relation_no" => $order->no, "createtime" => time() ]; } if ($invite_store_profit_amount > 0) { $m_bills[] = [ "massager_id" => $massager->id, "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_M_BILL_CHANGE_TYPE::ChannelExpend, "change" => -$invite_store_profit_amount, "before" => ($after + $trip_amount), "after" => ($after + $trip_amount) - $total_parent_amount - $invite_store_profit_amount, "reason" => "门店渠道抽佣", "relation_no" => $order->no, "createtime" => time() ]; } $this->mWalletModel->where("id", $mWallet->id)->setInc( "profit_amount", ($self_profit_amount + $trip_amount - $total_parent_amount - $invite_store_profit_amount) ); $this->mBillModel->insertAll($m_bills); } if ($agency && $a_profit_amount > 0) { $profit_bills[] = [ "identity_type" => \E_IDENTITY_TYPE::Agency, "target_id" => $agency->id, "target_name" => $agency->nickname, "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::Profit, "order_no" => $order->no, "total_amount" => $total_profit_amount, "rate" => $agency_rate, "change" => $a_profit_amount, "before" => $agency->profit_amount, "after" => fixed2Float($agency->profit_amount + $a_profit_amount), "createtime" => time(), "city_code" => $order->city_code ]; $this->adminModel->where("id", $agency->id)->setInc("profit_amount", $a_profit_amount); } $this->adminModel->where("id", 1)->setInc("profit_amount", $p_profit_amount); if ($parent && $total_parent_amount > 0) { // 技师邀请技师存在! $profit_bills[] = [ "identity_type" => \E_IDENTITY_TYPE::Massager, "target_id" => $parent->id, "target_name" => $parent->name, "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ChannelIncome, "order_no" => $order->no, "total_amount" => $total_profit_amount, "rate" => $parent_direct_invite_rate, "change" => $parent_profit_amount, "before" => fixed2Float($parent_wallet->profit_amount), "after" => fixed2Float($parent_wallet->profit_amount + $parent_profit_amount), "createtime" => time(), "city_code" => $order->city_code ]; $this->mBillModel->save([ "massager_id" => $parent->id, "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_M_BILL_CHANGE_TYPE::ChannelIncome, "change" => $total_parent_amount, "before" => $parent_wallet["profit_amount"], "after" => $parent_wallet["profit_amount"] + $total_parent_amount, "reason" => "渠道抽佣", "relation_no" => $order->no, "createtime" => time() ]); if ($parent_wallet) { $this->mWalletModel->where("id", $parent_wallet->id)->setInc("profit_amount", $parent_profit_amount); } } if ($invite_store_profit_amount > 0) { // 门店邀请技师存在 - 渠道抽佣 if ($invite_store) { $profit_bills[] = [ "identity_type" => \E_IDENTITY_TYPE::Store, "target_id" => $invite_store->id, "target_name" => $invite_store->name, "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ChannelIncome, "order_no" => $order->no, "total_amount" => $total_profit_amount, "rate" => $store_direct_invite_rate, "change" => $invite_store_profit_amount, "before" => fixed2Float($invite_store->profit_amount), "after" => fixed2Float($invite_store->profit_amount + $invite_store_profit_amount), "createtime" => time(), "city_code" => $order->city_code ]; $this->storeModel->where("id", $invite_store->id)->setInc("profit_amount", $invite_store_profit_amount); } } $this->pBillModel->insertAll($profit_bills); Db::commit(); return $this->ok(true); } catch (Exception $e) { Db::rollback(); return $this->fail($e->getMessage()); } finally { foreach ($locks as $lock) { if (is_array($lock)) $redLock->unlock($lock); } } } }