diff --git a/app/Constants/SsdbKeysPrefix.php b/app/Constants/SsdbKeysPrefix.php index cd94476..624ec09 100644 --- a/app/Constants/SsdbKeysPrefix.php +++ b/app/Constants/SsdbKeysPrefix.php @@ -52,10 +52,5 @@ class SsdbKeysPrefix extends AbstractConstants * */ const PURCHASE_RECORD = 'purchase_record_'; - - /** - * @Message("Store New User") - */ - const STORE_NEW_USER = 'store_new_user_'; } diff --git a/app/Model/FinancialRecord.php b/app/Model/FinancialRecord.php index 2068055..e0c4636 100644 --- a/app/Model/FinancialRecord.php +++ b/app/Model/FinancialRecord.php @@ -60,6 +60,12 @@ class FinancialRecord extends Model const MONEY_TYPE_STORE_OL_ORDER_COMP = 6; // 商户线上订单完成收入 const MONEY_TYPE_STORE_OFL_ORDER_COMP = 7; // 商户线下订单完成收入 const MONEY_TYPE_USER_OL_ORDER_REFUND = 8; // 用户线上订单退款 + const MONEY_TYPE_MM_PLAT_NEW_USER = 9; // 市场经理发展新用户 + const MONEY_TYPE_MM_PLAT_NEW_STORE = 10; // 市场经理发展新商户 + const MONEY_TYPE_MP_PLAT_NEW_USER = 11; // 服务商发展新用户 + const MONEY_TYPE_MP_PLAT_NEW_STORE = 12; // 服务商发展新商户 + const MONEY_TYPE_MP_OL_ORDER = 13; // 服务商线上订单分账(线上订单完成) + const MONEY_TYPE_USER_OFL_ORDER = 100; // 用户线下支付订单 const MONEY_TYPE_USER_OL_ORDER = 101; // 用户线上支付订单 diff --git a/app/Model/MmInfo.php b/app/Model/MmInfo.php new file mode 100644 index 0000000..42dd8f6 --- /dev/null +++ b/app/Model/MmInfo.php @@ -0,0 +1,8 @@ +where(['user_id' => $user_id])->first(); $store->award_money = bcadd($store->award_money, $money, 2); $store->save(); + + // 维护余额 + $balance = UserBalance::firstOrNew([ + 'user_type' => UserBalance::USER_TYPE_STORE, + 'source_id' => $store->id + ]); + $balance->balance = bcadd($balance->balance, $money, 2); + $balance->save(); } /** @@ -183,6 +191,14 @@ class FinancialRecordService implements FinancialRecordServiceInterface $store = Store::query()->where(['user_id' => $user_id])->first(); $store->award_money = bcadd($store->award_money, $money, 2); $store->save(); + + // 维护余额 + $balance = UserBalance::firstOrNew([ + 'user_type' => UserBalance::USER_TYPE_STORE, + 'source_id' => $store->id + ]); + $balance->balance = bcadd($balance->balance, $money, 2); + $balance->save(); } public function recordAll($user_id, $source_id, $money, $user_type=1, $source_type=0, $money_type=0, $desc='', $comment='') { @@ -257,6 +273,14 @@ class FinancialRecordService implements FinancialRecordServiceInterface $store = Store::query()->where(['user_id' => $user_id])->first(); $store->store_wallet = bcadd($store->store_wallet, $money, 2); $store->save(); + + // 维护余额 + $balance = UserBalance::firstOrNew([ + 'user_type' => UserBalance::USER_TYPE_STORE, + 'source_id' => $store->id + ]); + $balance->balance = bcadd($balance->balance, $money, 2); + $balance->save(); } /** @@ -274,6 +298,19 @@ class FinancialRecordService implements FinancialRecordServiceInterface ) { $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); + + // 同时维护钱包 + $store = Store::query()->where(['user_id' => $user_id])->first(); + $store->store_wallet = bcadd($store->store_wallet, $money, 2); + $store->save(); + + // 维护余额 + $balance = UserBalance::firstOrNew([ + 'user_type' => UserBalance::USER_TYPE_STORE, + 'source_id' => $store->id + ]); + $balance->balance = bcadd($balance->balance, $money, 2); + $balance->save(); } /** @@ -293,4 +330,129 @@ class FinancialRecordService implements FinancialRecordServiceInterface { $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); } + + /** + * @inheritDoc + */ + public function mmAwardByPlatNewUser( + $user_id, + $source_id, + $money, + $user_type = FinancialRecord::USER_TYPE_MM, + $source_type = FinancialRecord::SOURCE_TYPE_ORDER, + $money_type = FinancialRecord::MONEY_TYPE_MM_PLAT_NEW_USER, + $desc = '市场经理发展新用户', + $comment = '市场经理发展新用户奖励' + ) + { + $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); + + // 维护余额 + $balance = UserBalance::firstOrNew([ + 'user_type' => UserBalance::USER_TYPE_MM, + 'source_id' => $user_id + ]); + $balance->balance = bcadd($balance->balance, $money, 2); + $balance->save(); + } + + /** + * @inheritDoc + */ + public function mmAwardByNewStore( + $user_id, + $source_id, + $money, + $user_type = FinancialRecord::USER_TYPE_MM, + $source_type = FinancialRecord::SOURCE_TYPE_ORDER, + $money_type = FinancialRecord::MONEY_TYPE_MM_PLAT_NEW_STORE, + $desc = '市场经理发展新商户', + $comment = '市场经理发展新商户奖励' + ) + { + $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); + + // 维护余额 + $balance = UserBalance::firstOrNew([ + 'user_type' => UserBalance::USER_TYPE_MM, + 'source_id' => $user_id + ]); + $balance->balance = bcadd($balance->balance, $money, 2); + $balance->save(); + } + + /** + * @inheritDoc + */ + public function mpAwardByPlatNewUser( + $user_id, + $source_id, + $money, + $user_type = FinancialRecord::USER_TYPE_MP, + $source_type = FinancialRecord::SOURCE_TYPE_ORDER, + $money_type = FinancialRecord::MONEY_TYPE_MP_PLAT_NEW_USER, + $desc = '服务商发展新用户', + $comment = '服务商发展新用户奖励' + ) + { + $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); + + // 维护余额 + $balance = UserBalance::firstOrNew([ + 'user_type' => UserBalance::USER_TYPE_MP, + 'source_id' => $user_id + ]); + $balance->balance = bcadd($balance->balance, $money, 2); + $balance->save(); + } + + /** + * @inheritDoc + */ + public function mpAwardByNewStore( + $user_id, + $source_id, + $money, + $user_type = FinancialRecord::USER_TYPE_MP, + $source_type = FinancialRecord::SOURCE_TYPE_ORDER, + $money_type = FinancialRecord::MONEY_TYPE_MP_PLAT_NEW_STORE, + $desc = '服务商发展新商户', + $comment = '服务商发展新商户奖励' + ) + { + $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); + + // 维护余额 + $balance = UserBalance::firstOrNew([ + 'user_type' => UserBalance::USER_TYPE_MP, + 'source_id' => $user_id + ]); + $balance->balance = bcadd($balance->balance, $money, 2); + $balance->save(); + } + + /** + * @inheritDoc + */ + public function mpSeparateAccountByOLOrderComp( + $user_id, + $source_id, + $money, + $user_type = FinancialRecord::USER_TYPE_MP, + $source_type = FinancialRecord::SOURCE_TYPE_ORDER, + $money_type = FinancialRecord::MONEY_TYPE_MP_OL_ORDER, + $desc = '服务商线上订单分账', + $comment = '服务商用户线上订单完成后分账' + ) + { + $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); + + // 维护余额 + $balance = UserBalance::firstOrNew([ + 'user_type' => UserBalance::USER_TYPE_MP, + 'source_id' => $user_id + ]); + $balance->balance = bcadd($balance->balance, $money, 2); + $balance->save(); + } } \ No newline at end of file diff --git a/app/Service/FinancialRecordServiceInterface.php b/app/Service/FinancialRecordServiceInterface.php index 6c71e5b..01255cb 100644 --- a/app/Service/FinancialRecordServiceInterface.php +++ b/app/Service/FinancialRecordServiceInterface.php @@ -249,4 +249,117 @@ interface FinancialRecordServiceInterface $comment='' ); + /** + * 市场经理新用户 + * @param $user_id + * @param $source_id + * @param $money + * @param int $user_type + * @param int $source_type + * @param int $money_type + * @param string $desc + * @param string $comment + */ + public function mmAwardByPlatNewUser( + $user_id, + $source_id, + $money, + $user_type = FinancialRecord::USER_TYPE_MM, + $source_type = FinancialRecord::SOURCE_TYPE_ORDER, + $money_type = FinancialRecord::MONEY_TYPE_MM_PLAT_NEW_USER, + $desc = '市场经理发展新用户', + $comment = '' + ); + + /** + * 市场经理新商户 + * @param $user_id + * @param $source_id + * @param $money + * @param int $user_type + * @param int $source_type + * @param int $money_type + * @param string $desc + * @param string $comment + * @return mixed + */ + public function mmAwardByNewStore( + $user_id, + $source_id, + $money, + $user_type = FinancialRecord::USER_TYPE_MM, + $source_type = FinancialRecord::SOURCE_TYPE_ORDER, + $money_type = FinancialRecord::MONEY_TYPE_MM_PLAT_NEW_STORE, + $desc = '市场经理发展新商户', + $comment = '市场经理发展新商户奖励' + ); + + /** + * 服务商新用户 + * @param $user_id + * @param $source_id + * @param $money + * @param int $user_type + * @param int $source_type + * @param int $money_type + * @param string $desc + * @param string $comment + * @return mixed + */ + public function mpAwardByPlatNewUser( + $user_id, + $source_id, + $money, + $user_type = FinancialRecord::USER_TYPE_MP, + $source_type = FinancialRecord::SOURCE_TYPE_ORDER, + $money_type = FinancialRecord::MONEY_TYPE_MP_PLAT_NEW_USER, + $desc = '服务商发展新用户', + $comment = '服务商发展新用户奖励' + ); + + /** + * 服务商新商户 + * @param $user_id + * @param $source_id + * @param $money + * @param int $user_type + * @param int $source_type + * @param int $money_type + * @param string $desc + * @param string $comment + * @return mixed + */ + public function mpAwardByNewStore( + $user_id, + $source_id, + $money, + $user_type = FinancialRecord::USER_TYPE_MP, + $source_type = FinancialRecord::SOURCE_TYPE_ORDER, + $money_type = FinancialRecord::MONEY_TYPE_MP_PLAT_NEW_STORE, + $desc = '服务商发展新商户', + $comment = '服务商发展新商户奖励' + ); + + /** + * 服务商线上订单分账 + * @param $user_id + * @param $source_id + * @param $money + * @param int $user_type + * @param int $source_type + * @param int $money_type + * @param string $desc + * @param string $comment + * @return mixed + */ + public function mpSeparateAccountByOLOrderComp( + $user_id, + $source_id, + $money, + $user_type = FinancialRecord::USER_TYPE_MP, + $source_type = FinancialRecord::SOURCE_TYPE_ORDER, + $money_type = FinancialRecord::MONEY_TYPE_MP_OL_ORDER, + $desc = '服务商线上订单分账', + $comment = '服务商用户线上订单完成后分账' + ); } \ No newline at end of file diff --git a/app/Service/SeparateAccountsService.php b/app/Service/SeparateAccountsService.php index 8cd5266..59acb42 100644 --- a/app/Service/SeparateAccountsService.php +++ b/app/Service/SeparateAccountsService.php @@ -5,6 +5,9 @@ namespace App\Service; use App\Commons\Log; use App\Constants\LogLabel; use App\Model\FinancialRecord; +use App\Model\Market; +use App\Model\MmInfo; +use App\Model\MpInfo; use App\Model\Order; use App\Model\OrderMain; use App\Model\ServiceReward; @@ -14,8 +17,10 @@ use App\Model\SystemConfig; use App\Model\UserBalance; use App\Model\UserRelationBind; use App\Model\Users; +use App\TaskWorker\SSDBTask; use Hyperf\DbConnection\Db; use Hyperf\Di\Annotation\Inject; +use Hyperf\Utils\ApplicationContext; class SeparateAccountsService implements SeparateAccountsServiceInterface { @@ -68,6 +73,9 @@ class SeparateAccountsService implements SeparateAccountsServiceInterface */ public function orderOnlineCompleted($global_order_id) { + + $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); + // 线上订单完成(用户点击确认收货完成/管理后台点击完成/配送员点击完成/自动收货等),进行相关分账 // 订单 $orderMain = OrderMain::query() @@ -146,12 +154,70 @@ class SeparateAccountsService implements SeparateAccountsServiceInterface } // 账单分成 - $money = bcmul($orderMain->money, bcdiv($award['flow_reward'], 100, 6), 2); - $this->financialRecordService->communitySeparateAccountsByOrderComp($communityBind->source_id, $global_order_id, $money); + if ($orderMain->type == OrderMain::ORDER_TYPE_ONLINE) { + $money = bcmul($orderMain->money, bcdiv($award['flow_reward'], 100, 6), 2); + $this->financialRecordService->communitySeparateAccountsByOrderComp($communityBind->source_id, $global_order_id, $money); + } } // =======社区服务点分账 / End======= + // =======服务商、市场经理奖励分账 / Start======= + // 前提A:新用户下单并且订单完成(线上、线下都行) + // 奖励规则A:用户是平台新用户,奖励市场经理 1 元,服务商 0.5 元,如果是线上订单,服务商有6%的订单分成 + // 前提B:新商户旗下产生 10 个新用户 + // 奖励规则B:奖励市场经理 25 元,服务商 10 元(仅仅奖励一次) + // 前提C:用户线上下单并且订单完成 + // 奖励规则C:奖励服务商账单 6% 分成 + // 判断是新商户:入驻绑定的时候把关系存在SSDB + // =======服务商、市场经理奖励分账 / Start======= + + $MmMpAwardConfig = [ + 'mm_new_user' => 1, + 'mm_new_store' => 25, + 'mp_new_user' => 0.5, + 'mp_new_store' => 10, + 'separate_rate' => 6, + 'limit_new_user_number' => 10, + ]; + foreach ($orders as $key => &$order) { + + // 当前订单(子)对应商户是否有市场经理绑定关系 + $store = Store::query()->find($order['store_id']); // 商户 + $mmInfo = MmInfo::query()->where(['user_id' => $store->mm_user_id])->first(); // 市场经理 + $market = Market::query()->find($mmInfo->market_id); // 市场 + $mpInfo = MpInfo::query()->find($market->mp_id); // 服务商 + + $ssdbName = 'mm_'.$mmInfo->id.'_award_'.$store->id; + $record = $ssdb->exec('hgetall', $ssdbName); + + // 平台新用户 + if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->id)) { + $ssdb->exec('hset', $ssdbName, 'new_user_number', bcadd($record['new_user_number'],1)); + $this->financialRecordService->mmAwardByPlatNewUser($mmInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mm_new_user']); // 市场经理新用户奖励 + $this->financialRecordService->mpAwardByPlatNewUser($mpInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mp_new_user']); // 服务商新用户奖励 + } + + // 判断是否已经奖励过新拓展商户的奖励,没有的话判断新用户个数是否达到要求,进行奖励 + if ( + !empty($record) + &&$record['is_awarded']==0 + &&$record['new_user_number']>=$MmMpAwardConfig['limit_new_user_number'] + ) { // 存在记录且未发放奖励,同时新用户数已经超过10 + $this->financialRecordService->mmAwardByNewStore($mmInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mm_new_store']); // 市场经理新商户奖励 + $this->financialRecordService->mpAwardByNewStore($mpInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mp_new_store']); // 服务商新商户奖励 + } + + } + + // 线上订单服务商分账 + if ($orderMain->type == OrderMain::ORDER_TYPE_ONLINE) { + $money = bcmul($orderMain->money, bcdiv($MmMpAwardConfig['separate_rate'], 100, 6), 2); + $this->financialRecordService->mpSeparateAccountByOLOrderComp($mpInfo->admin_user_id, $global_order_id, $money); + } + + // =======服务商、市场经理奖励分账 / End======= + Db::commit(); return true; diff --git a/config/routes.php b/config/routes.php index 9a1d4f4..659680f 100644 --- a/config/routes.php +++ b/config/routes.php @@ -66,7 +66,6 @@ Router::addGroup('/v1/',function (){ Router::post('PurchaseLimit/getStoreIdByMarketId', 'App\Controller\PurchaseLimitController@getStoreIdByMarketId'); Router::post('OrderList/storeOrderList', 'App\Controller\OrderListController@storeOrderList'); Router::post('OrderList/userOrderList', 'App\Controller\OrderListController@userOrderList'); - Router::post('Store/applyEntry', 'App\Controller\StoreController@applyEntry'); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]);