From cbb94cc7141250aa325d914b6645e57acb81c1ae Mon Sep 17 00:00:00 2001 From: weigang Date: Thu, 20 Aug 2020 19:07:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BE=E5=8C=BA=E6=9C=8D=E5=8A=A1=E7=82=B9?= =?UTF-8?q?=E5=88=86=E8=B4=A6=E4=BB=A5=E5=8F=8A=E5=88=86=E8=B4=A6=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Amqp/Consumer/couponRebateConsumer.php | 3 + app/Constants/ErrorCode.php | 5 + app/Constants/LogLabel.php | 5 + app/Controller/NotifyController.php | 2 +- app/JsonRpc/OrderService.php | 33 +++++ app/JsonRpc/OrderServiceInterface.php | 8 ++ app/Model/FinancialRecord.php | 89 +++++++++++++ app/Model/Model.php | 12 ++ app/Service/FinancialRecordService.php | 122 ++++++++++++++++++ .../FinancialRecordServiceInterface.php | 62 +++++++++ app/Service/SeparateAccountsService.php | 104 +++++++++++++++ .../SeparateAccountsServiceInterface.php | 28 ++++ app/Service/UserService.php | 2 +- app/Service/UserServiceInterface.php | 2 +- composer.json | 6 +- config/autoload/dependencies.php | 4 + config/autoload/server.php | 10 ++ 17 files changed, 493 insertions(+), 4 deletions(-) create mode 100644 app/JsonRpc/OrderService.php create mode 100644 app/JsonRpc/OrderServiceInterface.php create mode 100644 app/Model/FinancialRecord.php create mode 100644 app/Service/FinancialRecordService.php create mode 100644 app/Service/FinancialRecordServiceInterface.php create mode 100644 app/Service/SeparateAccountsService.php create mode 100644 app/Service/SeparateAccountsServiceInterface.php diff --git a/app/Amqp/Consumer/couponRebateConsumer.php b/app/Amqp/Consumer/couponRebateConsumer.php index a76f046..3417e2a 100644 --- a/app/Amqp/Consumer/couponRebateConsumer.php +++ b/app/Amqp/Consumer/couponRebateConsumer.php @@ -37,6 +37,9 @@ class couponRebateConsumer extends ConsumerMessage public function isEnable(): bool { + if(env('APP_ENV') == 'local') { + return false; + } return parent::isEnable(); } } diff --git a/app/Constants/ErrorCode.php b/app/Constants/ErrorCode.php index ca97510..62811c0 100644 --- a/app/Constants/ErrorCode.php +++ b/app/Constants/ErrorCode.php @@ -54,4 +54,9 @@ class ErrorCode extends AbstractConstants */ const PAY_FAILURE = 400; + /** + * @Message("Separate Accounts Error!") + */ + const SEPARATE_ACCOUNTS_ERROR = 700; + } diff --git a/app/Constants/LogLabel.php b/app/Constants/LogLabel.php index edcb57c..fe2253c 100644 --- a/app/Constants/LogLabel.php +++ b/app/Constants/LogLabel.php @@ -33,4 +33,9 @@ class LogLabel extends AbstractConstants * @Message("Order Log Label") */ const ORDER_LOG = 'order_log'; + + /** + * @Message("Separate Accounts Log Label") + */ + const SEPARATE_ACCOUNTS_LOG = 'separate_accounts_log'; } diff --git a/app/Controller/NotifyController.php b/app/Controller/NotifyController.php index 955db3f..2a8b094 100644 --- a/app/Controller/NotifyController.php +++ b/app/Controller/NotifyController.php @@ -332,7 +332,7 @@ class NotifyController extends BaseController StoreAccount::query()->insert(array_merge($recordBase, $record)); // 平台新用户奖励给商户 - $isStageNewUser = $this->userService->isStageNewUser($orderItem['user_id'], $orderMain->id); + $isStageNewUser = $this->userService->isPlatformNewUser($orderItem['user_id'], $orderMain->id); $needAward = false; $awardAmount = 0; if ($isStageNewUser) { diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php new file mode 100644 index 0000000..6c25b92 --- /dev/null +++ b/app/JsonRpc/OrderService.php @@ -0,0 +1,33 @@ +separateAccService->orderOnlineCompleted($global_order_id); + return [ + "status" => 200, + "code" => $data ? 0 : ErrorCode::SEPARATE_ACCOUNTS_ERROR, + "result" => [], + "message" => $data ? '调用成功' : ErrorCode::getMessage(ErrorCode::SEPARATE_ACCOUNTS_ERROR) + ]; + } + +} \ No newline at end of file diff --git a/app/JsonRpc/OrderServiceInterface.php b/app/JsonRpc/OrderServiceInterface.php new file mode 100644 index 0000000..6292ffe --- /dev/null +++ b/app/JsonRpc/OrderServiceInterface.php @@ -0,0 +1,8 @@ +50是分账 + * + * MONEY_TYPE_PLAT_NEW_USER + * 社区服务点新用户奖励(线上订单完成) / 1 + * + * MONEY_TYPE_FIRST_ORDER + * 社区服务点新用户线上首单奖励(线上订单完成) / 2 + * + * MONEY_TYPE_OL_ORDER + * 社区服务点用户线上订单分账(线上订单完成) / 51 + */ + const MONEY_TYPE_PLAT_NEW_USER = 1; + const MONEY_TYPE_FIRST_ORDER = 2; + const MONEY_TYPE_OL_ORDER = 51; + + /** + * 状态 + */ + const STATUS_NORMAL = 1; + const STATUS_ABNORMAL = 2; + + /** + * The table associated with the model. + * + * @var string + */ + protected $table = 'lanzu_financial_record'; + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'user_id', + 'user_type', + 'money', + 'money_type', + 'source_id', + 'source_type', + 'desc', + 'comment', + 'status', + ]; + /** + * The attributes that should be cast to native types. + * + * @var array + */ + protected $casts = []; +} \ No newline at end of file diff --git a/app/Model/Model.php b/app/Model/Model.php index f606652..fa03395 100644 --- a/app/Model/Model.php +++ b/app/Model/Model.php @@ -19,4 +19,16 @@ abstract class Model extends BaseModel implements CacheableInterface { use Cacheable; protected $dateFormat = 'U'; + + /** + * 设置表后缀 + * @param string $suffix + * @return $this + */ + public function suffix($suffix = ''): Model + { + $tableName = $this->getTable(); + $this->setTable($tableName.'_'.$suffix); + return $this; + } } diff --git a/app/Service/FinancialRecordService.php b/app/Service/FinancialRecordService.php new file mode 100644 index 0000000..061e297 --- /dev/null +++ b/app/Service/FinancialRecordService.php @@ -0,0 +1,122 @@ +record( + $user_id, + [ + 'user_id' => $user_id, + 'user_type' => $user_type, + 'money' => $money, + 'money_type' => $money_type, + 'source_id' => $source_id, + 'source_type' => $source_type, + 'desc' => $desc, + 'comment' => $comment, + 'status' => FinancialRecord::STATUS_NORMAL, + ] + ); + + $this->ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment); + } + + /** + * @inheritDoc + */ + public function communityAwardByPlatNewUserFirstOLOrder($user_id, $source_id, $money, $user_type=2, $source_type=1, $money_type=1, $desc='新用户首单奖励', $comment='') + { + $this->record( + $user_id, + [ + 'user_id' => $user_id, + 'user_type' => $user_type, + 'money' => $money, + 'money_type' => $money_type, + 'source_id' => $source_id, + 'source_type' => $source_type, + 'desc' => $desc, + 'comment' => $comment, + 'status' => FinancialRecord::STATUS_NORMAL, + ] + ); + + $this->ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment); + } + + /** + * @inheritDoc + */ + public function communitySeparateAccountsByOrderComp($user_id, $source_id, $money, $user_type=2, $source_type=1, $money_type=2, $desc='用户订单分成', $comment='') + { + $this->record( + $user_id, + [ + 'user_id' => $user_id, + 'user_type' => $user_type, + 'money' => $money, + 'money_type' => $money_type, + 'source_id' => $source_id, + 'source_type' => $source_type, + 'desc' => $desc, + 'comment' => $comment, + 'status' => FinancialRecord::STATUS_NORMAL, + ] + ); + + $this->ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment); + } + + public function ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment='') + { + return $this->record( + FinancialRecord::ACCOUNT_LEDGER, + [ + 'user_id' => FinancialRecord::ACCOUNT_LEDGER, + 'user_type' => FinancialRecord::USER_TYPE_LEDGER, + 'money' => $money, + 'money_type' => $money_type, + 'source_id' => $source_id, + 'source_type' => $source_type, + 'desc' => $desc, + 'comment' => $comment, + 'status' => FinancialRecord::STATUS_NORMAL, + ], + true + ); + } + + public function record($user_id, $record, $isLedger=false) + { + $financialRecord = new FinancialRecord(); + + if (!$isLedger) { + $mod = bcmod((string)$user_id, '5', 0); + $financialRecord->suffix($mod); + } + + return $financialRecord->fill( + [ + 'user_id' => $user_id, + 'user_type' => $record['user_type'], + 'money' => $record['money'], + 'money_type' => $record['money_type'], + 'source_id' => $record['source_id'], + 'source_type' => $record['source_type'], + 'desc' => $record['desc'], + 'comment' => $record['comment'], + 'status' => $record['status'], + ] + )->save(); + + } +} \ No newline at end of file diff --git a/app/Service/FinancialRecordServiceInterface.php b/app/Service/FinancialRecordServiceInterface.php new file mode 100644 index 0000000..4078043 --- /dev/null +++ b/app/Service/FinancialRecordServiceInterface.php @@ -0,0 +1,62 @@ +where(['global_order_id' => $global_order_id]) + ->whereIn('state', [OrderMain::ORDER_STATE_COMPLETE,OrderMain::ORDER_STATE_EVALUATED,OrderMain::ORDER_STATE_UNREFUND]) + ->first(); + + if (empty($orderMain)) { + return; + } + + Db::beginTransaction(); + try { + + // =======社区服务点分账 / Start======= + // 前提:用户线上下单并且订单完成 + // 奖励规则A:用户是平台新用户,奖励社区服务点平台新用户奖励x元+平台新用户首单奖励y元+订单商品金额z%的分成 + // 奖励规则B:用户是非新用户,奖励社区服务点订单实际支付金额z%的分成 + // =======社区服务点分账 / Start======= + + // 奖励/分账金额 TODO 届时查询出来一个配置 + $award = ['new_user' => 1, 'first_order' => 2, 'separate_rate' => 2]; + + // 当前用户的社区服务点绑定关系 + $communityBind = UserRelationBind::query() + ->where(['bind_type' => UserRelationBind::BIND_TYPE_COMMUNITY, 'user_id' => $orderMain->user_id]) + ->first(); + + // 平台新用户 + if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->id)) { + $this->financialRecordService->communityAwardByPlatNewUser($communityBind->source_id, $orderMain->global_order_id, $award['new_user']); + $this->financialRecordService->communityAwardByPlatNewUserFirstOLOrder($communityBind->source_id, $orderMain->global_order_id, $award['first_order']); + } + + // 账单分成 + $money = bcmul($orderMain->money, bcdiv($award['separate_rate'], 100, 6), 2); + $this->financialRecordService->communitySeparateAccountsByOrderComp($communityBind->source_id, $orderMain->global_order_id, $money); + + // =======社区服务点分账 / End======= + + Db::commit(); + return true; + + } catch (\Exception $e) { + + $this->log->event(LogLabel::SEPARATE_ACCOUNTS_LOG, ['exception' => $e->getMessage(), 'order_main' => json_encode($orderMain)]); + Db::rollBack(); + return false; + } + + } + + /** + * @inheritDoc + */ + public function orderOfflinePaid($global_order_id) + { + // TODO: Implement orderOfflinePaid() method. + } +} \ No newline at end of file diff --git a/app/Service/SeparateAccountsServiceInterface.php b/app/Service/SeparateAccountsServiceInterface.php new file mode 100644 index 0000000..c574735 --- /dev/null +++ b/app/Service/SeparateAccountsServiceInterface.php @@ -0,0 +1,28 @@ +where(['user_id' => $user_id]) diff --git a/app/Service/UserServiceInterface.php b/app/Service/UserServiceInterface.php index 2920d53..1e8f0fb 100644 --- a/app/Service/UserServiceInterface.php +++ b/app/Service/UserServiceInterface.php @@ -12,7 +12,7 @@ interface UserServiceInterface * @param $order_main_id * @return mixed */ - public function isStageNewUser($user_id, $order_main_id): bool; + public function isPlatformNewUser($user_id, $order_main_id): bool; public function saveUserUnionid($openid,$unionid); diff --git a/composer.json b/composer.json index 284593c..4cfe0dd 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,11 @@ "alibabacloud/iot": "^1.8", "hyperf/snowflake": "^2.0", "ext-bcmath": "*", - "overtrue/wechat": "~4.0" + "overtrue/wechat": "~4.0", + "hyperf/json-rpc": "^2.0", + "hyperf/rpc-server": "^2.0", + "hyperf/rpc-client": "^2.0", + "hyperf/consul": "^2.0" }, "require-dev": { "swoole/ide-helper": "^4.5", diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php index a9ee1fb..760f9d0 100644 --- a/config/autoload/dependencies.php +++ b/config/autoload/dependencies.php @@ -27,4 +27,8 @@ return [ \App\Service\MiniprogramServiceInterface::class => \App\Service\MiniprogramService::class, \App\Service\UserServiceInterface::class => \App\Service\UserService::class, \App\Service\UserRelationBindServiceInterface::class => \App\Service\UserCommunityBindService::class, + \Hyperf\JsonRpc\JsonRpcTransporter::class => \Hyperf\JsonRpc\JsonRpcPoolTransporter::class, + \App\JsonRpc\OrderServiceInterface::class => \App\JsonRpc\OrderService::class, + \App\Service\FinancialRecordServiceInterface::class => \App\Service\FinancialRecordService::class, + \App\Service\SeparateAccountsServiceInterface::class => \App\Service\SeparateAccountsService::class, ]; diff --git a/config/autoload/server.php b/config/autoload/server.php index f44b083..1418f3a 100644 --- a/config/autoload/server.php +++ b/config/autoload/server.php @@ -25,6 +25,16 @@ return [ SwooleEvent::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'], ], ], + [ + 'name' => 'jsonrpc-http', + 'type' => Server::SERVER_HTTP, + 'host' => '0.0.0.0', + 'port' => 9505, + 'sock_type' => SWOOLE_SOCK_TCP, + 'callbacks' => [ + SwooleEvent::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'], + ], + ], ], 'settings' => [ 'enable_coroutine' => true,