|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
namespace App\Services\Coach;
|
|
|
|
|
|
+use App\Models\Order;
|
|
|
use App\Models\ShopInfo;
|
|
|
use App\Models\CoachUser;
|
|
|
use App\Enums\OrderStatus;
|
|
@@ -13,6 +14,7 @@ use App\Models\CoachSchedule;
|
|
|
use App\Models\CoachStatistic;
|
|
|
use App\Enums\TechnicianStatus;
|
|
|
use App\Models\CoachInfoRecord;
|
|
|
+use App\Models\OrderGrabRecord;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use App\Enums\TechnicianAuthStatus;
|
|
|
use App\Enums\TechnicianWorkStatus;
|
|
@@ -281,7 +283,6 @@ class AccountService
|
|
|
*/
|
|
|
public function getCoachDetail(): array
|
|
|
{
|
|
|
- // 获取技师信息并预加载关联,避免 N+1 问题
|
|
|
$coach = Auth::user()->coach->load([
|
|
|
'latestInfoRecord',
|
|
|
'latestRealRecord',
|
|
@@ -294,6 +295,15 @@ class AccountService
|
|
|
// 获取基本信息
|
|
|
$baseInfo = $this->formatBaseInfo($coach->latestInfoRecord);
|
|
|
|
|
|
+ // 获取订单统计
|
|
|
+ $orderStats = $this->getOrderStatistics($coach->id);
|
|
|
+
|
|
|
+ // 获取评分统计
|
|
|
+ $statistics = $this->formatStatistics($coach->statistics);
|
|
|
+
|
|
|
+ // 合并所有统计数据
|
|
|
+ $statistics = array_merge($orderStats, $statistics);
|
|
|
+
|
|
|
// 生成技师工号
|
|
|
$coachNo = str_pad($coach->id, 8, '0', STR_PAD_LEFT);
|
|
|
|
|
@@ -312,9 +322,6 @@ class AccountService
|
|
|
// 获取店铺信息
|
|
|
$shop = $this->formatShop($coach->shop);
|
|
|
|
|
|
- // 获取统计信息
|
|
|
- $statistics = $this->formatStatistics($coach->statistics);
|
|
|
-
|
|
|
// 组装返回数据
|
|
|
return array_merge(
|
|
|
[
|
|
@@ -326,6 +333,8 @@ class AccountService
|
|
|
[
|
|
|
'state' => $coach->state,
|
|
|
'state_text' => TechnicianStatus::fromValue($coach->state)?->label(),
|
|
|
+ 'work_status' => $coach->work_status, // 工作状态
|
|
|
+ 'work_status_text' => TechnicianWorkStatus::fromValue($coach->work_status)?->label(), // 状态文本
|
|
|
],
|
|
|
[
|
|
|
'auth_status' => $authStatus,
|
|
@@ -433,26 +442,26 @@ class AccountService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 格式化统计信息
|
|
|
- * 处理技师的订单统计和等级信息
|
|
|
+ * 格式化评分统计信息
|
|
|
+ * 处理技师的评分和等级信息
|
|
|
*
|
|
|
- * 业务逻辑:
|
|
|
- * 1. 获取订单相关统计数据
|
|
|
- * 2. 获取技师等级信息
|
|
|
- * 3. 获取评分信息
|
|
|
- * 4. 处理默认值
|
|
|
- *
|
|
|
- * @param CoachStatistic|null $statistics 统计对象
|
|
|
- * @return array 格式化后的统计信息
|
|
|
+ * @param CoachStatistic|null $statistics 评分统计对象
|
|
|
+ * @return array 格式化后的评分信息
|
|
|
*/
|
|
|
private function formatStatistics(?CoachStatistic $statistics): array
|
|
|
{
|
|
|
+ if (!$statistics) {
|
|
|
+ return [
|
|
|
+ 'level' => 1, // 技师等级
|
|
|
+ 'level_text' => $this->getLevelText(1), // 等级文本
|
|
|
+ 'rating' => 5.0, // 评分
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
return [
|
|
|
- 'order_count' => $statistics?->order_count ?? 0, // 总订单数
|
|
|
- 'completed_order_count' => $statistics?->completed_order_count ?? 0, // 已完成订单数
|
|
|
- 'level' => $statistics?->level ?? 1, // 技师等级
|
|
|
- 'level_text' => $this->getLevelText($statistics?->level ?? 1), // 等级文本
|
|
|
- 'rating' => $statistics?->rating ?? 5.0, // 评分
|
|
|
+ 'level' => $statistics->level ?? 1,
|
|
|
+ 'level_text' => $this->getLevelText($statistics->level ?? 1),
|
|
|
+ 'rating' => $statistics->rating ?? 5.0,
|
|
|
];
|
|
|
}
|
|
|
|
|
@@ -513,7 +522,7 @@ class AccountService
|
|
|
*/
|
|
|
private function formatQualification($qual)
|
|
|
{
|
|
|
- // 资质类型文本映射
|
|
|
+ // 资质类型文本映
|
|
|
$qualTypeMap = [
|
|
|
1 => '初级按摩师',
|
|
|
2 => '中级按摩师',
|
|
@@ -838,7 +847,7 @@ class AccountService
|
|
|
$range['start_minutes'] <= $prevRange['end_minutes'],
|
|
|
400,
|
|
|
"时间段 {$prevRange['start_time']}-{$prevRange['end_time']} 和 " .
|
|
|
- "{$range['start_time']}-{$range['end_time']} 之间存在重叠"
|
|
|
+ "{$range['start_time']}-{$range['end_time']} 之间有重叠"
|
|
|
);
|
|
|
}
|
|
|
});
|
|
@@ -951,7 +960,7 @@ class AccountService
|
|
|
OrderStatus::ACCEPTED->value, // 已接单
|
|
|
OrderStatus::DEPARTED->value, // 已出发
|
|
|
OrderStatus::ARRIVED->value, // 已到达
|
|
|
- OrderStatus::SERVING->value, // 服务中
|
|
|
+ OrderStatus::SERVICING->value, // 服务中
|
|
|
])
|
|
|
->exists();
|
|
|
|
|
@@ -1340,7 +1349,7 @@ class AccountService
|
|
|
*/
|
|
|
private function getWalletInfo(int $coachId): array
|
|
|
{
|
|
|
- // 获取技师对象及其钱包关联
|
|
|
+ // 获取技师及其钱包关联
|
|
|
$coach = CoachUser::with('wallet')->find($coachId);
|
|
|
|
|
|
// 如果钱包不存在,返回默认值
|
|
@@ -1568,4 +1577,45 @@ class AccountService
|
|
|
'list' => $list
|
|
|
];
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取订单统计数据
|
|
|
+ *
|
|
|
+ * @param int $coachId 技师ID
|
|
|
+ * @return array 订单统计数据
|
|
|
+ */
|
|
|
+ private function getOrderStatistics(int $coachId): array
|
|
|
+ {
|
|
|
+ // 从 Order 表获取订单统计
|
|
|
+ $orderStats = Order::where('coach_id', $coachId)
|
|
|
+ ->selectRaw('
|
|
|
+ COUNT(*) as total_count,
|
|
|
+ SUM(CASE WHEN state = ? THEN 1 ELSE 0 END) as completed_count,
|
|
|
+ SUM(CASE WHEN state IN (?,?,?,?,?,?,?,?) THEN 1 ELSE 0 END) as pending_count,
|
|
|
+ SUM(CASE WHEN state = ? THEN 1 ELSE 0 END) as carrying_count
|
|
|
+ ', [
|
|
|
+ OrderStatus::COMPLETED->value, // 已完成
|
|
|
+ OrderStatus::PAID->value, // 已支付
|
|
|
+ OrderStatus::ACCEPTED->value, // 已接单
|
|
|
+ OrderStatus::DEPARTED->value, // 已出发
|
|
|
+ OrderStatus::ARRIVED->value, // 已到达
|
|
|
+ OrderStatus::SERVICE_START->value, // 开始服务
|
|
|
+ OrderStatus::SERVICING->value, // 服务中
|
|
|
+ OrderStatus::SERVICE_END->value, // 服务结束
|
|
|
+ OrderStatus::LEAVING->value, // 撤离中
|
|
|
+ OrderStatus::PAID->value // 带接单(已支付)
|
|
|
+ ])
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ // 获取抢单数量
|
|
|
+ $grabOrderCount = OrderGrabRecord::where('coach_id', $coachId)->count();
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'order_count' => $orderStats->total_count ?? 0, // 总订单数
|
|
|
+ 'completed_order_count' => $orderStats->completed_count ?? 0, // 已完成订单数
|
|
|
+ 'pending_order_count' => $orderStats->pending_count ?? 0, // 进行中订单数
|
|
|
+ 'carrying_order_count' => $orderStats->carrying_count ?? 0, // 带接单数量
|
|
|
+ 'grab_order_count' => $grabOrderCount, // 抢单数量
|
|
|
+ ];
|
|
|
+ }
|
|
|
}
|