29 changed files with 1502 additions and 107 deletions
-
29app/Constants/v3/LogLabel.php
-
42app/Constants/v3/UserType.php
-
152app/Controller/TestController.php
-
46app/Controller/v3/NotifyController.php
-
29app/Model/v3/CsInfo.php
-
2app/Model/v3/Goods.php
-
2app/Model/v3/GoodsActivity.php
-
33app/Model/v3/OrderSalesStatistic.php
-
33app/Model/v3/ServiceReward.php
-
2app/Model/v3/Store.php
-
9app/Model/v3/StoreAccount.php
-
11app/Model/v3/SystemConfig.php
-
38app/Model/v3/UserBalance.php
-
40app/Model/v3/UserRelationBind.php
-
4app/Service/v3/Implementations/DeviceService.php
-
26app/Service/v3/Implementations/FeiePrintService.php
-
315app/Service/v3/Implementations/FinancialRecordService.php
-
36app/Service/v3/Implementations/MiniprogramService.php
-
13app/Service/v3/Implementations/MqttService.php
-
35app/Service/v3/Implementations/OrderOfflineService.php
-
60app/Service/v3/Implementations/OrderOnlineService.php
-
278app/Service/v3/Implementations/SeparateAccountsService.php
-
69app/Service/v3/Implementations/SmsAliService.php
-
4app/Service/v3/Implementations/UserService.php
-
253app/Service/v3/Interfaces/FinancialRecordServiceInterface.php
-
1app/Service/v3/Interfaces/OrderOfflineServiceInterface.php
-
28app/Service/v3/Interfaces/SeparateAccountsServiceInterface.php
-
9app/Service/v3/Interfaces/SmsServiceInterface.php
-
10config/autoload/dependencies.php
@ -0,0 +1,42 @@ |
|||
<?php |
|||
|
|||
namespace App\Constants\v3; |
|||
|
|||
use Hyperf\Constants\AbstractConstants; |
|||
use Hyperf\Constants\Annotation\Constants; |
|||
|
|||
/** |
|||
* @Constants |
|||
*/ |
|||
class UserType extends AbstractConstants |
|||
{ |
|||
/** |
|||
* @Message("平台总账") |
|||
*/ |
|||
const LEDGER = -1; |
|||
|
|||
/** |
|||
* @Message("用户") |
|||
*/ |
|||
const USER = 1; |
|||
|
|||
/** |
|||
* @Message("服务商") |
|||
*/ |
|||
const MP = 2; |
|||
|
|||
/** |
|||
* @Message("市场经理") |
|||
*/ |
|||
const MM = 3; |
|||
|
|||
/** |
|||
* @Message("代理服务点") |
|||
*/ |
|||
const CS = 4; |
|||
|
|||
/** |
|||
* @Message("商户") |
|||
*/ |
|||
const STORE = 5; |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
<?php |
|||
|
|||
declare (strict_types=1); |
|||
namespace App\Model\v3; |
|||
|
|||
use Hyperf\DbConnection\Model\Model; |
|||
/** |
|||
*/ |
|||
class CsInfo extends Model |
|||
{ |
|||
/** |
|||
* The table associated with the model. |
|||
* |
|||
* @var string |
|||
*/ |
|||
protected $table = 'lanzu_cs_info'; |
|||
/** |
|||
* The attributes that are mass assignable. |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $fillable = []; |
|||
/** |
|||
* The attributes that should be cast to native types. |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $casts = []; |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
<?php |
|||
|
|||
declare (strict_types=1); |
|||
|
|||
namespace App\Model\v3; |
|||
|
|||
use App\Model\Model; |
|||
|
|||
/** |
|||
*/ |
|||
class OrderSalesStatistic extends Model |
|||
{ |
|||
/** |
|||
* The table associated with the model. |
|||
* |
|||
* @var string |
|||
*/ |
|||
protected $table = 'lanzu_order_sales_statistics'; |
|||
/** |
|||
* The attributes that are mass assignable. |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $fillable = []; |
|||
/** |
|||
* The attributes that should be cast to native types. |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $casts = []; |
|||
|
|||
public $timestamps = false; |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
<?php |
|||
|
|||
declare (strict_types=1); |
|||
namespace App\Model\v3; |
|||
|
|||
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' |
|||
]; |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
<?php |
|||
|
|||
namespace App\Model\v3; |
|||
|
|||
class StoreAccount extends Model |
|||
{ |
|||
protected $table = 'ims_cjdc_store_account'; |
|||
public $timestamps = false; |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace App\Model\v3; |
|||
|
|||
|
|||
class SystemConfig extends Model |
|||
{ |
|||
protected $table = 'ims_cjdc_system_config'; |
|||
public $timestamps = false; |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
<?php |
|||
|
|||
declare (strict_types=1); |
|||
namespace App\Model\v3; |
|||
|
|||
use App\Model\Model; |
|||
|
|||
/** |
|||
*/ |
|||
class UserBalance extends Model |
|||
{ |
|||
const USER_TYPE_MP = 1; |
|||
const USER_TYPE_MM = 2; |
|||
const USER_TYPE_CS = 3; |
|||
|
|||
/** |
|||
* The table associated with the model. |
|||
* |
|||
* @var string |
|||
*/ |
|||
protected $table = 'lanzu_user_balance'; |
|||
/** |
|||
* The attributes that are mass assignable. |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $fillable = [ |
|||
'source_id', |
|||
'user_type', |
|||
'balance' |
|||
]; |
|||
/** |
|||
* The attributes that should be cast to native types. |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $casts = []; |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
<?php |
|||
|
|||
declare (strict_types=1); |
|||
namespace App\Model\v3; |
|||
|
|||
/** |
|||
*/ |
|||
class UserRelationBind extends Model |
|||
{ |
|||
|
|||
/** |
|||
* 社区服务点类型 |
|||
*/ |
|||
const BIND_TYPE_COMMUNITY = 1; |
|||
|
|||
/** |
|||
* The table associated with the model. |
|||
* |
|||
* @var string |
|||
*/ |
|||
protected $table = 'lanzu_user_relation_bind'; |
|||
/** |
|||
* The attributes that are mass assignable. |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $fillable = ['bind_type', 'source_id', 'user_id', 'json_data']; |
|||
/** |
|||
* The attributes that should be cast to native types. |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $casts = []; |
|||
|
|||
public function setJsonDataAttribute($value) |
|||
{ |
|||
$this->attributes['json_data'] = $value ? json_encode(json_decode($value, true)) : ''; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,315 @@ |
|||
<?php |
|||
|
|||
namespace App\Service\v3\Implementations; |
|||
|
|||
use App\Constants\v3\UserType; |
|||
use App\Model\v3\FinancialRecord; |
|||
use App\Model\v3\Store; |
|||
use App\Model\v3\UserBalance; |
|||
use App\Service\v3\Interfaces\FinancialRecordServiceInterface; |
|||
use App\Service\v3\Interfaces\SmsServiceInterface; |
|||
use Hyperf\Di\Annotation\Inject; |
|||
|
|||
class FinancialRecordService implements FinancialRecordServiceInterface |
|||
{ |
|||
|
|||
/** |
|||
* @Inject |
|||
* @var SmsServiceInterface |
|||
*/ |
|||
protected $smsAliService; |
|||
|
|||
public function ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment='') |
|||
{ |
|||
return $this->record( |
|||
-1, |
|||
[ |
|||
'user_id' => -1, |
|||
'user_type' => UserType::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(); |
|||
|
|||
} |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function communityAwardByPlatNewUser( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::CS, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_CS_PLAT_NEW_USER, |
|||
$desc='新用户奖励', |
|||
$comment='社区服务点' |
|||
) |
|||
{ |
|||
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); |
|||
// 维护社区服务点余额
|
|||
$balance = UserBalance::query()->firstOrNew([ |
|||
'user_type' => UserType::CS, |
|||
'source_id' => $user_id |
|||
]); |
|||
$balance->balance = bcadd($balance->balance, $money, 2); |
|||
$balance->save(); |
|||
|
|||
// 发送短信
|
|||
$this->smsAliService->sendForCommunityFinancial($user_id, $money); |
|||
|
|||
} |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function communityAwardByPlatNewUserFirstOLOrder( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::CS, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_CS_FIRST_ORDER, |
|||
$desc='新用户首单奖励', |
|||
$comment='社区服务点' |
|||
) |
|||
{ |
|||
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); |
|||
// 维护社区服务点余额
|
|||
$balance = UserBalance::query()->firstOrNew([ |
|||
'user_type' => UserType::CS, |
|||
'source_id' => $user_id |
|||
]); |
|||
$balance->balance = bcadd($balance->balance, $money, 2); |
|||
$balance->save(); |
|||
|
|||
// 发送短信
|
|||
$this->smsAliService->sendForCommunityFinancial($user_id, $money); |
|||
} |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function communitySeparateAccountsByOrderComp( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::CS, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_CS_OL_ORDER, |
|||
$desc='用户订单分成', |
|||
$comment='社区服务点' |
|||
) |
|||
{ |
|||
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); |
|||
// 维护社区服务点余额
|
|||
$balance = UserBalance::query()->firstOrNew([ |
|||
'user_type' => UserType::CS, |
|||
'source_id' => $user_id |
|||
]); |
|||
$balance->balance = bcadd($balance->balance, $money,2); |
|||
$balance->save(); |
|||
|
|||
// 发送短信
|
|||
$this->smsAliService->sendForCommunityFinancial($user_id, $money); |
|||
} |
|||
|
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function storeAwardByPlatNewUserOFLOrder( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::STORE, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_STORE_PLAT_NEW_USER, |
|||
$desc='新用户下单奖励', |
|||
$comment='用户当面付商户奖励' |
|||
) |
|||
{ |
|||
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); |
|||
// 维护余额
|
|||
$balance = UserBalance::query()->firstOrNew([ |
|||
'user_type' => UserType::STORE, |
|||
'source_id' => $user_id |
|||
]); |
|||
$balance->balance = bcadd($balance->balance, $money, 2); |
|||
$balance->save(); |
|||
} |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function storeAwardByTodayFirstOFLOrder( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::STORE, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_STORE_FIRST_ORDER, |
|||
$desc='用户店铺首单奖励', |
|||
$comment='用户当面付商户奖励' |
|||
) |
|||
{ |
|||
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); |
|||
// 维护余额
|
|||
$balance = UserBalance::query()->firstOrNew([ |
|||
'user_type' => UserType::STORE, |
|||
'source_id' => $user_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='') { |
|||
$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 userByOFLOrderPaid( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::USER, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_USER_OFL_ORDER, |
|||
$desc='用户下单(线下)', |
|||
$comment='用户下单' |
|||
) |
|||
{ |
|||
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); |
|||
} |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function userByOLOrderPaid( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::USER, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_USER_OL_ORDER, |
|||
$desc='用户下单(线上)', |
|||
$comment='用户下单' |
|||
) |
|||
{ |
|||
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); |
|||
} |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function storeByOLOrderComp( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type = UserType::STORE, |
|||
$source_type = FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type = FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP, |
|||
$desc = '线上外卖订单收入', |
|||
$comment = '用户订单完成' |
|||
) |
|||
{ |
|||
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); |
|||
// 维护余额
|
|||
$balance = UserBalance::query()->firstOrNew([ |
|||
'user_type' => UserType::STORE, |
|||
'source_id' => $user_id |
|||
]); |
|||
$balance->balance = bcadd($balance->balance, $money, 2); |
|||
$balance->save(); |
|||
} |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function storeByOFLOrderComp( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type = UserType::STORE, |
|||
$source_type = FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type = FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP, |
|||
$desc = '线下当面付订单收入', |
|||
$comment = '用户订单完成' |
|||
) |
|||
{ |
|||
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); |
|||
// 维护余额
|
|||
$balance = UserBalance::query()->firstOrNew([ |
|||
'user_type' => UserType::STORE, |
|||
'source_id' => $user_id |
|||
]); |
|||
$balance->balance = bcadd($balance->balance, $money, 2); |
|||
$balance->save(); |
|||
} |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
* 订单退款(线上) |
|||
*/ |
|||
public function userByOLOrderRefund( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type = UserType::USER, |
|||
$source_type = FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type = FinancialRecord::MONEY_TYPE_USER_OL_ORDER_REFUND, |
|||
$desc = '线上订单退款', |
|||
$comment = '线上订单退款到微信' |
|||
) |
|||
{ |
|||
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment); |
|||
} |
|||
} |
|||
@ -0,0 +1,278 @@ |
|||
<?php |
|||
|
|||
namespace App\Service\v3\Implementations; |
|||
|
|||
use App\Commons\Log; |
|||
use App\Constants\v3\LogLabel; |
|||
use App\Model\v3\FinancialRecord; |
|||
use App\Model\v3\Order; |
|||
use App\Model\v3\OrderMain; |
|||
use App\Model\v3\ServiceReward; |
|||
use App\Model\v3\Store; |
|||
use App\Model\v3\StoreAccount; |
|||
use App\Model\v3\SystemConfig; |
|||
use App\Model\v3\UserBalance; |
|||
use App\Model\v3\UserRelationBind; |
|||
use App\Model\v3\User; |
|||
use App\Service\v3\Interfaces\FinancialRecordServiceInterface; |
|||
use App\Service\v3\Interfaces\MiniprogramServiceInterface; |
|||
use App\Service\v3\Interfaces\SeparateAccountsServiceInterface; |
|||
use App\Service\v3\Interfaces\UserServiceInterface; |
|||
use Hyperf\DbConnection\Db; |
|||
use Hyperf\Di\Annotation\Inject; |
|||
|
|||
class SeparateAccountsService implements SeparateAccountsServiceInterface |
|||
{ |
|||
/** |
|||
* @Inject |
|||
* @var Log |
|||
*/ |
|||
protected $log; |
|||
|
|||
/** |
|||
* @Inject |
|||
* @var UserServiceInterface |
|||
*/ |
|||
protected $userService; |
|||
|
|||
/** |
|||
* @Inject |
|||
* @var FinancialRecordServiceInterface |
|||
*/ |
|||
protected $financialRecordService; |
|||
|
|||
/** |
|||
* @Inject |
|||
* @var MiniprogramServiceInterface |
|||
*/ |
|||
protected $miniprogramService; |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function orderOnlinePaid($orderMainId) |
|||
{ |
|||
try { |
|||
// 线上订单支付完成
|
|||
// 订单
|
|||
$orderMain = OrderMain::query()->find($orderMainId); |
|||
|
|||
if (empty($orderMain)) { |
|||
return false; |
|||
} |
|||
|
|||
// =======用户支付流水 / Start=======
|
|||
$this->financialRecordService->userByOLOrderPaid($orderMain->user_id, $orderMain->global_order_id, $orderMain->money); |
|||
// =======用户支付流水 / End=======
|
|||
|
|||
return true; |
|||
} catch (\Exception $e) { |
|||
$this->log->event(LogLabel::SEPARATE_ACCOUNTS_LOG, ['exception' => $e->getMessage()]); |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function orderOnlineCompleted($global_order_id) |
|||
{ |
|||
// 线上订单完成(用户点击确认收货完成/管理后台点击完成/配送员点击完成/自动收货等),进行相关分账
|
|||
// 订单
|
|||
$orderMain = OrderMain::query() |
|||
->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 false; |
|||
} |
|||
|
|||
$currentTime = time(); |
|||
Db::beginTransaction(); |
|||
try { |
|||
|
|||
// =======商户订单收入流水 / Start=======
|
|||
// 查询子订单
|
|||
$orders = Order::query()->select(['id', 'money', 'user_id', 'store_id', 'pay_time']) |
|||
->where(['order_main_id' => $orderMain->id]) |
|||
->get()->toArray(); |
|||
|
|||
foreach ($orders as $key => &$order) { |
|||
|
|||
// 商户
|
|||
$store = Store::find($order['store_id']); |
|||
|
|||
// 旧商户流水基础数据 TODO 直接移除或后续考虑移除
|
|||
$storeAccountBase = [ |
|||
'user_id' => $order['user_id'], |
|||
'order_id' => $order['id'], |
|||
'store_id' => $order['store_id'], |
|||
'type' => 1, |
|||
'time' => date('Y-m-d H:i:s', $currentTime), |
|||
'add_time' => $currentTime, |
|||
]; |
|||
|
|||
// 旧商户流水 TODO 直接移除或后续考虑移除
|
|||
$storeAccount = [ |
|||
'money' => $order['money'], |
|||
'note' => '线上订单', |
|||
'category' => 1, |
|||
]; |
|||
StoreAccount::query()->insert(array_merge($storeAccountBase, $storeAccount)); |
|||
|
|||
// 新商户流水
|
|||
$this->financialRecordService->storeByOLOrderComp($store->user_id, $global_order_id ,$order['money']); |
|||
} |
|||
// =======商户订单收入流水 / End=======
|
|||
|
|||
// =======社区服务点分账 / Start=======
|
|||
// 前提:用户线上下单并且订单完成
|
|||
// 奖励规则A:用户是平台新用户,奖励社区服务点平台新用户奖励x元+平台新用户首单奖励y元+订单商品金额z%的分成
|
|||
// 奖励规则B:用户是非新用户,奖励社区服务点订单实际支付金额z%的分成
|
|||
// =======社区服务点分账 / Start=======
|
|||
|
|||
// 当前用户的社区服务点绑定关系
|
|||
$communityBind = UserRelationBind::query() |
|||
->where(['bind_type' => UserRelationBind::BIND_TYPE_COMMUNITY, 'user_id' => $orderMain->user_id]) |
|||
->first(); |
|||
|
|||
if ($communityBind) { |
|||
|
|||
// 奖励/分账金额
|
|||
$award = ServiceReward::query()->where(['type' => ServiceReward::TYPE_COMMUNITY])->first(); |
|||
if (empty($award)) { |
|||
Db::rollBack(); |
|||
return false; |
|||
} |
|||
|
|||
$award = $award->set_reward; |
|||
|
|||
// 平台新用户
|
|||
if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->id)) { |
|||
$this->financialRecordService->communityAwardByPlatNewUser($communityBind->source_id, $global_order_id, $award['new_user_reward']); |
|||
$this->financialRecordService->communityAwardByPlatNewUserFirstOLOrder($communityBind->source_id, $global_order_id, $award['first_reward']); |
|||
} |
|||
|
|||
// 账单分成
|
|||
$money = bcmul($orderMain->money, bcdiv($award['flow_reward'], 100, 6), 2); |
|||
$this->financialRecordService->communitySeparateAccountsByOrderComp($communityBind->source_id, $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($orderMainId) |
|||
{ |
|||
// 线下订单支付完成
|
|||
// 订单
|
|||
$orderMain = OrderMain::query()->find($orderMainId); |
|||
$global_order_id = $orderMain->global_order_id; |
|||
|
|||
if (empty($orderMain)) { |
|||
return false; |
|||
} |
|||
|
|||
// 查询子订单,当面付目前实际上只有一个子订单
|
|||
$order = Order::query() |
|||
->where(['order_main_id' => $orderMain->id]) |
|||
->first(); |
|||
|
|||
if (empty($order)) { |
|||
return false; |
|||
} |
|||
|
|||
$currentTime = time(); |
|||
Db::beginTransaction(); |
|||
try { |
|||
|
|||
// =======用户支付流水 / Start=======
|
|||
$this->financialRecordService->userByOFLOrderPaid($orderMain->user_id, $global_order_id, $orderMain->money); |
|||
// =======用户支付流水 / End=======
|
|||
|
|||
// =======线下订单支付完成商户分账 / Start=======
|
|||
// 前提:用户线上下单并且支付完成
|
|||
// 奖励规则A:用户是平台新用户,奖励商户2元
|
|||
// 奖励规则B:用户是非新用户,但是是商户当日首单,奖励商户0.05元
|
|||
// =======线下订单支付完成商户分账 / Start=======
|
|||
|
|||
$message = []; |
|||
|
|||
// 商户
|
|||
$store = Store::find($order->store_id); |
|||
|
|||
// 新商户订单流水
|
|||
$this->financialRecordService->storeByOFLOrderComp($store->user_id, $global_order_id, $order->money); |
|||
|
|||
$needAward = false; |
|||
$awardAmount = 0; |
|||
// 新用户商户奖励
|
|||
if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->id)) { |
|||
|
|||
$awardAmount = 2; |
|||
// 旧商户流水 TODO 直接移除或后续考虑移除
|
|||
$message = [ |
|||
'money' => $awardAmount, |
|||
'note' => '新用户下单成功,平台奖励', |
|||
]; |
|||
// 新商户流水
|
|||
$this->financialRecordService->storeAwardByPlatNewUserOFLOrder($store->user_id, $global_order_id, $awardAmount); |
|||
$needAward = true; |
|||
|
|||
} else { |
|||
// 商户当日首单奖励
|
|||
if ( |
|||
$this->userService->isStoreFirstOrderToday( |
|||
$order->user_id, |
|||
$order->store_id, |
|||
$order->id, |
|||
FinancialRecord::OFL_FIRST_AWARD_LIMIT_AMOUNT |
|||
) |
|||
&& $order->money >= FinancialRecord::OFL_FIRST_AWARD_LIMIT_AMOUNT |
|||
) { |
|||
|
|||
$awardAmount = 0.05; |
|||
// 旧商户流水 TODO 直接移除或后续考虑移除
|
|||
$message = [ |
|||
'money' => $awardAmount, |
|||
'note' => '用户下单成功,平台奖励', |
|||
]; |
|||
// 新商户流水
|
|||
$this->financialRecordService->storeAwardByTodayFirstOFLOrder($store->user_id, $global_order_id, $awardAmount); |
|||
$needAward = true; |
|||
|
|||
} |
|||
} |
|||
|
|||
if ($needAward && $awardAmount) { |
|||
// 发模板消息
|
|||
$openid = User::query()->where(['id' => $store['user_id']])->value('openid'); |
|||
$res = $this->miniprogramService->sendTemMsgForAward($message['money'], $message['note'], $openid, date('Y-m-d H:i:s', $currentTime)); |
|||
} |
|||
|
|||
// =======线下订单支付完成商户分账 / 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; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,69 @@ |
|||
<?php |
|||
|
|||
namespace App\Service\v3\Implementations; |
|||
|
|||
use AlibabaCloud\Client\AlibabaCloud; |
|||
use AlibabaCloud\Client\Exception\ClientException; |
|||
use AlibabaCloud\Client\Exception\ServerException; |
|||
use App\Commons\Log; |
|||
use App\Model\v3\CsInfo; |
|||
use App\Model\v3\Market; |
|||
use App\Service\v3\Interfaces\SmsServiceInterface; |
|||
use Hyperf\Di\Annotation\Inject; |
|||
|
|||
class SmsAliService implements SmsServiceInterface |
|||
{ |
|||
const TEMPLATE_COMMUNITY_FINANCIAL = 'SMS_200690862'; |
|||
|
|||
/** |
|||
* @Inject |
|||
* @var Log |
|||
*/ |
|||
protected $log; |
|||
|
|||
public function send($phone, $template, $templateParams, $signName='懒族生活') |
|||
{ |
|||
|
|||
$alisms = config('alisms'); |
|||
AlibabaCloud::accessKeyClient($alisms['app_key'], $alisms['app_secret']) |
|||
->regionId($alisms['regionid']) |
|||
->asDefaultClient(); |
|||
|
|||
try { |
|||
$result = AlibabaCloud::rpc() |
|||
->product($alisms['product']) |
|||
// ->scheme('https') // https | http
|
|||
->version('2017-05-25') |
|||
->action('SendSms') |
|||
->method('POST') |
|||
->host($alisms['host']) |
|||
->options([ |
|||
'query' => [ |
|||
'RegionId' => $alisms['regionid'], |
|||
'PhoneNumbers' => $phone, |
|||
'SignName' => $signName, |
|||
'TemplateCode' => $template, |
|||
'TemplateParam' => $templateParams, |
|||
], |
|||
]) |
|||
->request(); |
|||
return $result->toArray(); |
|||
} catch (ClientException $e) { |
|||
$this->log->event('alisms', ['alisms_error_ClientException' => $e->getErrorMessage()]); |
|||
return false; |
|||
} catch (ServerException $e) { |
|||
$this->log->event('alisms', ['alisms_error_ServerException' => $e->getErrorMessage()]); |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
public function sendForCommunityFinancial($userId, $money) |
|||
{ |
|||
|
|||
$csInfo = CsInfo::query()->where(['admin_user_id' => $userId])->first(); |
|||
$market = Market::query()->where(['id' => $csInfo->market_id])->first(); |
|||
|
|||
$params = ['user_name' => $csInfo->name, 'market_name' => $market->name, 'money' => $money]; |
|||
return $this->send($csInfo->phone, self::TEMPLATE_COMMUNITY_FINANCIAL, json_encode($params)); |
|||
} |
|||
} |
|||
@ -0,0 +1,253 @@ |
|||
<?php |
|||
|
|||
namespace App\Service\v3\Interfaces; |
|||
|
|||
use App\Constants\v3\UserType; |
|||
use App\Model\FinancialRecord; |
|||
|
|||
interface FinancialRecordServiceInterface |
|||
{ |
|||
|
|||
/** |
|||
* 社区服务点新用户奖励 |
|||
* @param $user_id |
|||
* @param $source_id |
|||
* @param $money |
|||
* @param int $user_type |
|||
* @param int $source_type |
|||
* @param int $money_type |
|||
* @param string $comment |
|||
* @param string $desc |
|||
* @return mixed |
|||
*/ |
|||
public function communityAwardByPlatNewUser( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::CS, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_CS_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 $comment |
|||
* @param string $desc |
|||
* @return mixed |
|||
*/ |
|||
public function communityAwardByPlatNewUserFirstOLOrder( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::CS, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_CS_FIRST_ORDER, |
|||
$desc='新用户首单奖励', |
|||
$comment='' |
|||
); |
|||
|
|||
/** |
|||
* 社区服务点用户订单完成分账 |
|||
* @param $user_id |
|||
* @param $source_id |
|||
* @param $money |
|||
* @param int $user_type |
|||
* @param int $source_type |
|||
* @param int $money_type |
|||
* @param string $comment |
|||
* @param string $desc |
|||
* @return mixed |
|||
*/ |
|||
public function communitySeparateAccountsByOrderComp( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::CS, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_CS_OL_ORDER, |
|||
$desc='用户订单分成', |
|||
$comment='' |
|||
); |
|||
|
|||
/** |
|||
* 收支总账 |
|||
* @param $source_id |
|||
* @param $money |
|||
* @param $source_type |
|||
* @param $money_type |
|||
* @param $desc |
|||
* @param string $comment |
|||
* @return mixed |
|||
*/ |
|||
public function ledgerAccounts($source_id, $money, $source_type, $money_type, $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 storeAwardByPlatNewUserOFLOrder( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::STORE, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_STORE_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 storeAwardByTodayFirstOFLOrder( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::STORE, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_STORE_FIRST_ORDER, |
|||
$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 userByOFLOrderPaid( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::USER, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_USER_OFL_ORDER, |
|||
$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 userByOLOrderPaid( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type=UserType::USER, |
|||
$source_type=FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type=FinancialRecord::MONEY_TYPE_USER_OL_ORDER, |
|||
$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 storeByOLOrderComp( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type = UserType::STORE, |
|||
$source_type = FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type = FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP, |
|||
$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 storeByOFLOrderComp( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type = UserType::STORE, |
|||
$source_type = FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type = FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP, |
|||
$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 userByOLOrderRefund( |
|||
$user_id, |
|||
$source_id, |
|||
$money, |
|||
$user_type = UserType::STORE, |
|||
$source_type = FinancialRecord::SOURCE_TYPE_ORDER, |
|||
$money_type = FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP, |
|||
$desc='线上订单退款', |
|||
$comment='' |
|||
); |
|||
|
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
<?php |
|||
|
|||
namespace App\Service\v3\Interfaces; |
|||
|
|||
interface SeparateAccountsServiceInterface |
|||
{ |
|||
/** |
|||
* (线上)订单支付完成分账 |
|||
* @param $orderMainId |
|||
* @return mixed |
|||
*/ |
|||
public function orderOnlinePaid($orderMainId); |
|||
|
|||
/** |
|||
* (线上)订单确认完成分账 |
|||
* 用户确认或服务商(服务站确认) |
|||
* @param $global_order_id |
|||
* @return mixed |
|||
*/ |
|||
public function orderOnlineCompleted($global_order_id); |
|||
|
|||
/** |
|||
* (线下)订单支付完成分账 |
|||
* @param $orderMainId |
|||
* @return mixed |
|||
*/ |
|||
public function orderOfflinePaid($orderMainId); |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
<?php |
|||
|
|||
namespace App\Service\v3\Interfaces; |
|||
|
|||
interface SmsServiceInterface |
|||
{ |
|||
public function send($phone, $template, $templateParams); |
|||
public function sendForCommunityFinancial($userId, $money); |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue