Browse Source

社区服务点分账

master
weigang 6 years ago
parent
commit
7452ec5167
  1. 5
      app/Constants/LogLabel.php
  2. 33
      app/Controller/NotifyController.php
  3. 3
      app/Controller/PaymentController.php
  4. 49
      app/JsonRpc/OrderService.php
  5. 2
      app/Model/OrderMain.php
  6. 33
      app/Model/ServiceReward.php
  7. 6
      app/Service/FinancialRecordService.php
  8. 48
      app/Service/OrderService.php
  9. 6
      app/Service/OrderServiceInterface.php
  10. 32
      app/Service/SeparateAccountsService.php

5
app/Constants/LogLabel.php

@ -38,4 +38,9 @@ class LogLabel extends AbstractConstants
* @Message("Separate Accounts Log Label") * @Message("Separate Accounts Log Label")
*/ */
const SEPARATE_ACCOUNTS_LOG = 'separate_accounts_log'; const SEPARATE_ACCOUNTS_LOG = 'separate_accounts_log';
/**
* @Message("Online Order Complete Log Label")
*/
const ONLINE_COMPLETE_LOG = 'online_complete_log';
} }

33
app/Controller/NotifyController.php

@ -187,27 +187,14 @@ class NotifyController extends BaseController
// 喇叭通知,兼容旧音响,MQTT+IOT // 喇叭通知,兼容旧音响,MQTT+IOT
$res = $this->mqttSpeakerService->speakToStore($orderMain->id); $res = $this->mqttSpeakerService->speakToStore($orderMain->id);
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
['fail_mqtt' => json_encode($res)]
);
$res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->id); $res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->id);
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
['fail_device' => json_encode($res)]
);
// 公众号模板消息 // 公众号模板消息
$res = $this->miniprogramService->sendTemMsgForOnlineOrder($orderMain->id); $res = $this->miniprogramService->sendTemMsgForOnlineOrder($orderMain->id);
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
['fail_mini' => json_encode($res)]
);
// 打印订单,自动打印 TODO 后续优化调用逻辑 // 打印订单,自动打印 TODO 后续优化调用逻辑
$res = $this->feiePrintService->feiePrint($orderMain->global_order_id); $res = $this->feiePrintService->feiePrint($orderMain->global_order_id);
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
['fail_feie' => json_encode($res)]
);
Db::commit(); Db::commit();
return true; return true;
@ -375,21 +362,11 @@ class NotifyController extends BaseController
// 喇叭通知,兼容旧音响,MQTT+IOT // 喇叭通知,兼容旧音响,MQTT+IOT
$res = $this->mqttSpeakerService->speakToStore($orderMain->id); $res = $this->mqttSpeakerService->speakToStore($orderMain->id);
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
['fail_mqtt' => json_encode($res)]
);
$res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->id); $res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->id);
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
['fail_device' => json_encode($res)]
);
// 公众号模板消息 // 公众号模板消息
$res = $this->miniprogramService->sendTemMsgForOfflineOrder($orderMain->id); $res = $this->miniprogramService->sendTemMsgForOfflineOrder($orderMain->id);
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
['fail_mini' => json_encode($res)]
);
Db::commit(); Db::commit();
return true; return true;

3
app/Controller/PaymentController.php

@ -33,7 +33,8 @@ class PaymentController extends BaseController
$result = $app->order->unify([ $result = $app->order->unify([
'body' => '懒族生活 - 外卖下单', 'body' => '懒族生活 - 外卖下单',
'out_trade_no' => $orderMain->global_order_id, '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', 'notify_url' => config('site_host') . '/wechat/notify/wxminionline',
'trade_type' => 'JSAPI', 'trade_type' => 'JSAPI',
'openid' => $data['openid'], 'openid' => $data['openid'],

49
app/JsonRpc/OrderService.php

@ -2,10 +2,13 @@
namespace App\JsonRpc; namespace App\JsonRpc;
use App\Commons\Log;
use App\Constants\ErrorCode; use App\Constants\ErrorCode;
use App\Service\SeparateAccountsServiceInterface; use App\Service\SeparateAccountsServiceInterface;
use Hyperf\DbConnection\Db;
use Hyperf\RpcServer\Annotation\RpcService; use Hyperf\RpcServer\Annotation\RpcService;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use App\Constants\LogLabel;
/** /**
* @RpcService(name="OrderService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="") * @RpcService(name="OrderService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="")
@ -13,21 +16,51 @@ use Hyperf\Di\Annotation\Inject;
class OrderService implements OrderServiceInterface class OrderService implements OrderServiceInterface
{ {
/**
* @Inject
* @var Log
*/
protected $log;
/**
* @Inject
* @var \App\Service\OrderServiceInterface
*/
protected $orderService;
/** /**
* @Inject * @Inject
* @var SeparateAccountsServiceInterface * @var SeparateAccountsServiceInterface
*/ */
protected $separateAccService;
protected $separateAccountsService;
public function onlineComplete($global_order_id) 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)
];
}
} }
} }

2
app/Model/OrderMain.php

@ -32,6 +32,8 @@ class OrderMain extends Model
const ORDER_STATE_REFUNDED = 9; const ORDER_STATE_REFUNDED = 9;
// 拒绝退款 // 拒绝退款
const ORDER_STATE_UNREFUND = 10; const ORDER_STATE_UNREFUND = 10;
// 完成状态组合
const ORDER_STATE_FINISH = [self::ORDER_STATE_COMPLETE, self::ORDER_STATE_EVALUATED, self::ORDER_STATE_UNREFUND];
// 订单支付方式 // 订单支付方式
// 微信支付 // 微信支付

33
app/Model/ServiceReward.php

@ -0,0 +1,33 @@
<?php
declare (strict_types=1);
namespace App\Model;
class ServiceReward extends Model
{
/**
* 社区服务点
*/
const TYPE_COMMUNITY = 1;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'lanzu_service_reward';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'set_reward' => 'array'
];
}

6
app/Service/FinancialRecordService.php

@ -10,7 +10,7 @@ class FinancialRecordService implements FinancialRecordServiceInterface
/** /**
* @inheritDoc * @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( $this->record(
$user_id, $user_id,
@ -33,7 +33,7 @@ class FinancialRecordService implements FinancialRecordServiceInterface
/** /**
* @inheritDoc * @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( $this->record(
$user_id, $user_id,
@ -56,7 +56,7 @@ class FinancialRecordService implements FinancialRecordServiceInterface
/** /**
* @inheritDoc * @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( $this->record(
$user_id, $user_id,

48
app/Service/OrderService.php

@ -12,7 +12,6 @@ use App\Model\Order;
use App\Model\OrderGoods; use App\Model\OrderGoods;
use App\Model\OrderMain; use App\Model\OrderMain;
use App\Model\SpecCombination; use App\Model\SpecCombination;
use App\Model\Users;
use Exception; use Exception;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
use Hyperf\Snowflake\IdGeneratorInterface; use Hyperf\Snowflake\IdGeneratorInterface;
@ -89,10 +88,11 @@ class OrderService implements OrderServiceInterface
// 统计订单中所有店铺当日订单数,做店铺订单序号 // 统计订单中所有店铺当日订单数,做店铺订单序号
$countsArr = Order::query() $countsArr = Order::query()
->selectRaw('id, COUNT(*) AS count')
->selectRaw('store_id, COUNT(*) AS count')
->whereIn('store_id', explode(',', $dataMain['store_ids'])) ->whereIn('store_id', explode(',', $dataMain['store_ids']))
->where(['type' => OrderMain::ORDER_TYPE_ONLINE]) ->where(['type' => OrderMain::ORDER_TYPE_ONLINE])
->whereBetween('time', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')]) ->whereBetween('time', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')])
->groupBy('store_id')
->get() ->get()
->toArray(); ->toArray();
@ -462,14 +462,13 @@ class OrderService implements OrderServiceInterface
} }
} }
/** /**
* 计算和校验当前订单可用红包及金额 * 计算和校验当前订单可用红包及金额
* @param $couponIds * @param $couponIds
* @param $orderAmount * @param $orderAmount
* @param $userId * @param $userId
* @param $marketId * @param $marketId
* @return int|string
* @throws Exception * @throws Exception
*/ */
protected function getCouponAmount($couponIds, $orderAmount, $userId, $marketId) 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) public function existsByGlobalOrderId($global_order_id)
{ {
return OrderMain::query()->where(['order_num' => $global_order_id])->value('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;
}
}
} }

6
app/Service/OrderServiceInterface.php

@ -27,4 +27,10 @@ interface OrderServiceInterface
*/ */
public function existsByGlobalOrderId($global_order_id); public function existsByGlobalOrderId($global_order_id);
/**
* @param $global_order_id
* @return mixed
*/
public function onlineCompleted($global_order_id);
} }

32
app/Service/SeparateAccountsService.php

@ -5,6 +5,7 @@ namespace App\Service;
use App\Commons\Log; use App\Commons\Log;
use App\Constants\LogLabel; use App\Constants\LogLabel;
use App\Model\OrderMain; use App\Model\OrderMain;
use App\Model\ServiceReward;
use App\Model\UserRelationBind; use App\Model\UserRelationBind;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
@ -62,23 +63,32 @@ class SeparateAccountsService implements SeparateAccountsServiceInterface
// 奖励规则B:用户是非新用户,奖励社区服务点订单实际支付金额z%的分成 // 奖励规则B:用户是非新用户,奖励社区服务点订单实际支付金额z%的分成
// =======社区服务点分账 / Start======= // =======社区服务点分账 / Start=======
// 奖励/分账金额 TODO 届时查询出来一个配置
$award = ['new_user' => 1, 'first_order' => 2, 'separate_rate' => 2];
// 当前用户的社区服务点绑定关系 // 当前用户的社区服务点绑定关系
$communityBind = UserRelationBind::query() $communityBind = UserRelationBind::query()
->where(['bind_type' => UserRelationBind::BIND_TYPE_COMMUNITY, 'user_id' => $orderMain->user_id]) ->where(['bind_type' => UserRelationBind::BIND_TYPE_COMMUNITY, 'user_id' => $orderMain->user_id])
->first(); ->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======= // =======社区服务点分账 / End=======

Loading…
Cancel
Save