|
|
@@ -5,10 +5,8 @@ namespace app\api\service;
|
|
|
|
|
|
use app\admin\model\Admin;
|
|
|
use app\admin\model\profit\Bill;
|
|
|
-use app\api\model\Channel;
|
|
|
use app\api\model\massager\Massager;
|
|
|
use app\api\model\massager\Wallet;
|
|
|
-use app\api\model\massager\Work;
|
|
|
use app\api\model\Store;
|
|
|
use app\api\model\User;
|
|
|
use redis\RedLock;
|
|
|
@@ -338,6 +336,191 @@ class ProfitService extends BaseService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public function profitV2($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 = 10;
|
|
|
+ // 代理商抽成比例
|
|
|
+ $agency_rate = 0;
|
|
|
+ $store = $this->storeModel->findById($order->store_id ?? -1);
|
|
|
+ $self_rate = 0;
|
|
|
+ if ($order["store_id"] > 0) { // 球房分润标准
|
|
|
+ } else { // 线上分润标准
|
|
|
+ $mLock = $redLock->lock(\app\api\model\massager\Wallet::MWKey($order->massager_id));
|
|
|
+ if (!is_array($mLock))
|
|
|
+ return $this->fail("请稍后再试");
|
|
|
+ // 本人比例
|
|
|
+ $self_rate = 80;
|
|
|
+ if ($agency) {
|
|
|
+ $agency_rate = 10;
|
|
|
+ } else {
|
|
|
+ // 如果代理商不存在 则剩余部分 全部归于平台;
|
|
|
+ $platform_rate = 20;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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];
|
|
|
+ $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));
|
|
|
+
|
|
|
+ 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,
|
|
|
+ "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()
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $this->mWalletModel->where("id", $mWallet->id)->setInc(
|
|
|
+ "profit_amount",
|
|
|
+ ($self_profit_amount + $trip_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);
|
|
|
+ $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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|