1. 协议查询
This commit is contained in:
73
common/services/SalespersonOrderBindService.php
Normal file
73
common/services/SalespersonOrderBindService.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* 推广员与订单/挂号绑定解析(与 Laravel SalespersonOrderBindService 规则一致)。
|
||||
*/
|
||||
class SalespersonOrderBindService
|
||||
{
|
||||
/**
|
||||
* @return array{salesperson_id: int, user_salesperson_id: int}
|
||||
*/
|
||||
public function resolveForOrder(int $userId, int $storeId, int $orderCreatedAt): array
|
||||
{
|
||||
$empty = ['salesperson_id' => 0, 'user_salesperson_id' => 0];
|
||||
|
||||
if ($userId <= 0 || $storeId <= 0) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
$bind = (new \yii\db\Query())
|
||||
->from('xk_user_salesperson')
|
||||
->where(['user_id' => $userId, 'status' => 0])
|
||||
->orderBy(['id' => SORT_DESC])
|
||||
->one();
|
||||
|
||||
if (!$bind) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
$salespersonId = (int) ($bind['salesperson_id'] ?? 0);
|
||||
if ($salespersonId <= 0) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
$storeIds = (new \yii\db\Query())
|
||||
->select('store_id')
|
||||
->from('xk_salesperson_qr_code')
|
||||
->where(['salesperson_id' => $salespersonId])
|
||||
->column();
|
||||
|
||||
$storeIds = array_map('intval', $storeIds);
|
||||
if (!in_array($storeId, $storeIds, true)) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
$bindCreatedAt = $bind['created_at'] ?? 0;
|
||||
if (!is_numeric($bindCreatedAt)) {
|
||||
$bindCreatedAt = strtotime((string) $bindCreatedAt);
|
||||
}
|
||||
$bindCreatedAt = (int) $bindCreatedAt;
|
||||
|
||||
if ($orderCreatedAt <= $bindCreatedAt) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
$salesperson = (new \yii\db\Query())
|
||||
->from('xk_salesperson')
|
||||
->where(['id' => $salespersonId])
|
||||
->one();
|
||||
|
||||
if (!$salesperson) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
return [
|
||||
'salesperson_id' => (int) $salesperson['id'],
|
||||
'user_salesperson_id' => (int) ($bind['id'] ?? 0),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user