| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- <?php
- namespace app\api\service;
- use app\admin\model\Admin;
- use app\admin\model\profit\Bill;
- use app\api\model\massager\Massager;
- use app\api\model\massager\Wallet;
- use app\api\model\Store;
- use app\api\model\User;
- use redis\RedLock;
- use think\Db;
- use think\Exception;
- class ProfitService extends BaseService
- {
- private $adminModel;
- private $storeModel;
- private $massagerModel;
- private $pBillModel;
- private $mBillModel;
- private $mWalletModel;
- private $userModel;
- private $uBillModel;
- private $uWalletModel;
- public function __construct()
- {
- $this->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);
- }
- }
- }
- 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);
- }
- }
- }
- }
|