diff --git a/app/Constants/LogLabel.php b/app/Constants/LogLabel.php index fe2253c..04b91be 100644 --- a/app/Constants/LogLabel.php +++ b/app/Constants/LogLabel.php @@ -38,4 +38,9 @@ class LogLabel extends AbstractConstants * @Message("Separate Accounts Log Label") */ const SEPARATE_ACCOUNTS_LOG = 'separate_accounts_log'; + + /** + * @Message("Online Order Complete Log Label") + */ + const ONLINE_COMPLETE_LOG = 'online_complete_log'; } diff --git a/app/Controller/NotifyController.php b/app/Controller/NotifyController.php index 2a8b094..8376952 100644 --- a/app/Controller/NotifyController.php +++ b/app/Controller/NotifyController.php @@ -187,27 +187,14 @@ class NotifyController extends BaseController // 喇叭通知,兼容旧音响,MQTT+IOT $res = $this->mqttSpeakerService->speakToStore($orderMain->id); - $this->log->event( - LogLabel::PAY_NOTIFY_WXMINI, - ['fail_mqtt' => json_encode($res)] - ); $res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->id); - $this->log->event( - LogLabel::PAY_NOTIFY_WXMINI, - ['fail_device' => json_encode($res)] - ); + // 公众号模板消息 $res = $this->miniprogramService->sendTemMsgForOnlineOrder($orderMain->id); - $this->log->event( - LogLabel::PAY_NOTIFY_WXMINI, - ['fail_mini' => json_encode($res)] - ); + // 打印订单,自动打印 TODO 后续优化调用逻辑 $res = $this->feiePrintService->feiePrint($orderMain->global_order_id); - $this->log->event( - LogLabel::PAY_NOTIFY_WXMINI, - ['fail_feie' => json_encode($res)] - ); + Db::commit(); return true; @@ -375,21 +362,11 @@ class NotifyController extends BaseController // 喇叭通知,兼容旧音响,MQTT+IOT $res = $this->mqttSpeakerService->speakToStore($orderMain->id); - $this->log->event( - LogLabel::PAY_NOTIFY_WXMINI, - ['fail_mqtt' => json_encode($res)] - ); $res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->id); - $this->log->event( - LogLabel::PAY_NOTIFY_WXMINI, - ['fail_device' => json_encode($res)] - ); + // 公众号模板消息 $res = $this->miniprogramService->sendTemMsgForOfflineOrder($orderMain->id); - $this->log->event( - LogLabel::PAY_NOTIFY_WXMINI, - ['fail_mini' => json_encode($res)] - ); + Db::commit(); return true; diff --git a/app/Controller/PaymentController.php b/app/Controller/PaymentController.php index b2c6fba..9fc92ae 100644 --- a/app/Controller/PaymentController.php +++ b/app/Controller/PaymentController.php @@ -33,7 +33,8 @@ class PaymentController extends BaseController $result = $app->order->unify([ 'body' => '懒族生活 - 外卖下单', 'out_trade_no' => $orderMain->global_order_id, - 'total_fee' => bcmul(floatval($orderMain->money), 100, 0), + // 'total_fee' => bcmul(floatval($orderMain->money), 100, 0), + 'total_fee' => 1, 'notify_url' => config('site_host') . '/wechat/notify/wxminionline', 'trade_type' => 'JSAPI', 'openid' => $data['openid'], diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php index 6c25b92..10cf513 100644 --- a/app/JsonRpc/OrderService.php +++ b/app/JsonRpc/OrderService.php @@ -2,10 +2,13 @@ namespace App\JsonRpc; +use App\Commons\Log; use App\Constants\ErrorCode; use App\Service\SeparateAccountsServiceInterface; +use Hyperf\DbConnection\Db; use Hyperf\RpcServer\Annotation\RpcService; use Hyperf\Di\Annotation\Inject; +use App\Constants\LogLabel; /** * @RpcService(name="OrderService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="") @@ -13,21 +16,51 @@ use Hyperf\Di\Annotation\Inject; class OrderService implements OrderServiceInterface { + /** + * @Inject + * @var Log + */ + protected $log; + + /** + * @Inject + * @var \App\Service\OrderServiceInterface + */ + protected $orderService; + /** * @Inject * @var SeparateAccountsServiceInterface */ - protected $separateAccService; + protected $separateAccountsService; public function onlineComplete($global_order_id) { - $data = $this->separateAccService->orderOnlineCompleted($global_order_id); - return [ - "status" => 200, - "code" => $data ? 0 : ErrorCode::SEPARATE_ACCOUNTS_ERROR, - "result" => [], - "message" => $data ? '调用成功' : ErrorCode::getMessage(ErrorCode::SEPARATE_ACCOUNTS_ERROR) - ]; + Db::beginTransaction(); + try { + + $this->orderService->onlineCompleted($global_order_id); + $this->separateAccountsService->orderOnlineCompleted($global_order_id); + + Db::commit(); + return [ + "status" => 200, + "code" => 0, + "result" => [], + "message" => '调用成功' + ]; + } catch (\Exception $e) { + + Db::rollBack(); + $this->log->event(LogLabel::ONLINE_COMPLETE_LOG, ['exception' => $e->getMessage()]); + return [ + "status" => 200, + "code" =>ErrorCode::SEPARATE_ACCOUNTS_ERROR, + "result" => [], + "message" => ErrorCode::getMessage(ErrorCode::SEPARATE_ACCOUNTS_ERROR) + ]; + } + } } \ No newline at end of file diff --git a/app/Model/OrderMain.php b/app/Model/OrderMain.php index 92213ad..dfb6d0d 100644 --- a/app/Model/OrderMain.php +++ b/app/Model/OrderMain.php @@ -32,6 +32,8 @@ class OrderMain extends Model const ORDER_STATE_REFUNDED = 9; // 拒绝退款 const ORDER_STATE_UNREFUND = 10; + // 完成状态组合 + const ORDER_STATE_FINISH = [self::ORDER_STATE_COMPLETE, self::ORDER_STATE_EVALUATED, self::ORDER_STATE_UNREFUND]; // 订单支付方式 // 微信支付 diff --git a/app/Model/ServiceReward.php b/app/Model/ServiceReward.php new file mode 100644 index 0000000..583ad0a --- /dev/null +++ b/app/Model/ServiceReward.php @@ -0,0 +1,33 @@ + 'array' + ]; +} \ No newline at end of file diff --git a/app/Service/FinancialRecordService.php b/app/Service/FinancialRecordService.php index 061e297..34680b3 100644 --- a/app/Service/FinancialRecordService.php +++ b/app/Service/FinancialRecordService.php @@ -10,7 +10,7 @@ class FinancialRecordService implements FinancialRecordServiceInterface /** * @inheritDoc */ - public function communityAwardByPlatNewUser($user_id, $source_id, $money, $user_type=2, $source_type=1, $money_type=1, $desc='新用户奖励', $comment='') + public function communityAwardByPlatNewUser($user_id, $source_id, $money, $user_type=2, $source_type=1, $money_type=1, $desc='新用户奖励', $comment='社区服务点') { $this->record( $user_id, @@ -33,7 +33,7 @@ class FinancialRecordService implements FinancialRecordServiceInterface /** * @inheritDoc */ - public function communityAwardByPlatNewUserFirstOLOrder($user_id, $source_id, $money, $user_type=2, $source_type=1, $money_type=1, $desc='新用户首单奖励', $comment='') + public function communityAwardByPlatNewUserFirstOLOrder($user_id, $source_id, $money, $user_type=2, $source_type=1, $money_type=1, $desc='新用户首单奖励', $comment='社区服务点') { $this->record( $user_id, @@ -56,7 +56,7 @@ class FinancialRecordService implements FinancialRecordServiceInterface /** * @inheritDoc */ - public function communitySeparateAccountsByOrderComp($user_id, $source_id, $money, $user_type=2, $source_type=1, $money_type=2, $desc='用户订单分成', $comment='') + public function communitySeparateAccountsByOrderComp($user_id, $source_id, $money, $user_type=2, $source_type=1, $money_type=2, $desc='用户订单分成', $comment='社区服务点') { $this->record( $user_id, diff --git a/app/Service/OrderService.php b/app/Service/OrderService.php index 10a9936..7aa31e1 100644 --- a/app/Service/OrderService.php +++ b/app/Service/OrderService.php @@ -12,7 +12,6 @@ use App\Model\Order; use App\Model\OrderGoods; use App\Model\OrderMain; use App\Model\SpecCombination; -use App\Model\Users; use Exception; use Hyperf\DbConnection\Db; use Hyperf\Snowflake\IdGeneratorInterface; @@ -89,10 +88,11 @@ class OrderService implements OrderServiceInterface // 统计订单中所有店铺当日订单数,做店铺订单序号 $countsArr = Order::query() - ->selectRaw('id, COUNT(*) AS count') + ->selectRaw('store_id, COUNT(*) AS count') ->whereIn('store_id', explode(',', $dataMain['store_ids'])) ->where(['type' => OrderMain::ORDER_TYPE_ONLINE]) ->whereBetween('time', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')]) + ->groupBy('store_id') ->get() ->toArray(); @@ -462,14 +462,13 @@ class OrderService implements OrderServiceInterface } } - - /** * 计算和校验当前订单可用红包及金额 * @param $couponIds * @param $orderAmount * @param $userId * @param $marketId + * @return int|string * @throws Exception */ protected function getCouponAmount($couponIds, $orderAmount, $userId, $marketId) @@ -522,12 +521,47 @@ class OrderService implements OrderServiceInterface } /** - * 订单是否存在 - * @param $global_order_id - * @return mixed|void|null + * @inheritDoc */ public function existsByGlobalOrderId($global_order_id) { return OrderMain::query()->where(['order_num' => $global_order_id])->value('id'); } + + /** + * @inheritDoc + */ + public function onlineCompleted($global_order_id) + { + Db::beginTransaction(); + try { + + // 主订单状态更新 + $orderMain = OrderMain::query() + ->where(['global_order_id' => $global_order_id, 'state' => OrderMain::ORDER_STATE_DELIVERY]) + ->first(); + + if (empty($orderMain)) { + Db::rollBack(); + return false; + } + + $orderMain->state = OrderMain::ORDER_STATE_COMPLETE; + $orderMain->save(); + + // 子订单状态更新 + $upChild = Order::query() + ->where(['order_main_id' => $orderMain->id]) + ->update(['state' => OrderMain::ORDER_STATE_COMPLETE]); + + Db::commit(); + return true; + } catch (Exception $e) { + + $this->log->event(LogLabel::ONLINE_COMPLETE_LOG, ['exception' => $e->getMessage()]); + Db::rollBack(); + return false; + } + + } } \ No newline at end of file diff --git a/app/Service/OrderServiceInterface.php b/app/Service/OrderServiceInterface.php index 4fa6657..92fa986 100644 --- a/app/Service/OrderServiceInterface.php +++ b/app/Service/OrderServiceInterface.php @@ -27,4 +27,10 @@ interface OrderServiceInterface */ public function existsByGlobalOrderId($global_order_id); + /** + * @param $global_order_id + * @return mixed + */ + public function onlineCompleted($global_order_id); + } \ No newline at end of file diff --git a/app/Service/SeparateAccountsService.php b/app/Service/SeparateAccountsService.php index 57852a8..4958584 100644 --- a/app/Service/SeparateAccountsService.php +++ b/app/Service/SeparateAccountsService.php @@ -5,6 +5,7 @@ namespace App\Service; use App\Commons\Log; use App\Constants\LogLabel; use App\Model\OrderMain; +use App\Model\ServiceReward; use App\Model\UserRelationBind; use Hyperf\DbConnection\Db; use Hyperf\Di\Annotation\Inject; @@ -62,23 +63,32 @@ class SeparateAccountsService implements SeparateAccountsServiceInterface // 奖励规则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']); - } + if ($communityBind) { + + // 奖励/分账金额 + $award = ServiceReward::query()->where(['type' => ServiceReward::TYPE_COMMUNITY])->first(); + if (empty($award)) { + Db::rollBack(); + return false; + } - // 账单分成 - $money = bcmul($orderMain->money, bcdiv($award['separate_rate'], 100, 6), 2); - $this->financialRecordService->communitySeparateAccountsByOrderComp($communityBind->source_id, $orderMain->global_order_id, $money); + $award = $award->set_reward; + + // 平台新用户 + if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->id)) { + $this->financialRecordService->communityAwardByPlatNewUser($communityBind->source_id, $orderMain->global_order_id, $award['new_user_reward']); + $this->financialRecordService->communityAwardByPlatNewUserFirstOLOrder($communityBind->source_id, $orderMain->global_order_id, $award['first_reward']); + } + + // 账单分成 + $money = bcmul($orderMain->money, $award['flow_reward']); + $this->financialRecordService->communitySeparateAccountsByOrderComp($communityBind->source_id, $orderMain->global_order_id, $money); + } // =======社区服务点分账 / End=======