/** * 获取发货状态选项 */ public function getDeliveryStatusOptions() { $options = [ ['value' => 'pending', 'label' => '待发货'], ['value' => 'delivered', 'label' => '已发货'], ]; return response()->json([ 'code' => 200, 'data' => $options, 'message' => 'success' ]); } /** * 获取平台选项 */ public function getPlatformOptions() { $platforms = Order::select('platform') ->distinct() ->orderBy('platform') ->get() ->map(function ($item) { return [ 'value' => $item->platform, 'label' => $item->platform, ]; }); return response()->json([ 'code' => 200, 'data' => $platforms, 'message' => 'success' ]); } /** * 获取店铺选项 */ public function getShopOptions() { $shops = Order::select('shop_id', 'shop_name') ->distinct() ->orderBy('shop_name') ->get() ->map(function ($item) { return [ 'value' => $item->shop_id, 'label' => $item->shop_name, ]; }); return response()->json([ 'code' => 200, 'data' => $shops, 'message' => 'success' ]); } /** * 更新订单备注 */ public function updateRemark(Request $request, string $id) { $request->validate([ 'remark' => 'required|string|max:1000', ]); $order = Order::find($id); if (!$order) { return response()->json([ 'code' => 404, 'message' => '订单不存在' ], 404); } $order->update(['remark' => $request->remark]); return response()->json([ 'code' => 200, 'message' => '备注更新成功' ]); } /** * 完成订单 */ public function completeOrder(string $id) { $order = Order::find($id); if (!$order) { return response()->json([ 'code' => 404, 'message' => '订单不存在' ], 404); } // 只有已发货的订单可以完成 if ($order->order_status !== 'shipped') { return response()->json([ 'code' => 400, 'message' => '只有已发货的订单可以完成' ], 400); } $order->update([ 'order_status' => 'completed', 'end_time' => now(), ]); return response()->json([ 'code' => 200, 'message' => '订单已完成' ]); } /** * 同步订单到平台 */ public function syncToPlatform(string $id) { $order = Order::find($id); if (!$order) { return response()->json([ 'code' => 404, 'message' => '订单不存在' ], 404); } // TODO: 实现同步到平台API // 这里应该调用对应平台的API更新订单状态 return response()->json([ 'code' => 200, 'message' => '订单已同步到平台(模拟)', 'data' => [ 'order_id' => $id, 'platform' => $order->platform, 'platform_order_sn' => $order->platform_order_sn, ] ]); } /** * 获取订单操作历史 */ public function getOperationHistory(string $id) { $order = Order::find($id); if (!$order) { return response()->json([ 'code' => 404, 'message' => '订单不存在' ], 404); } // 在实际项目中,这里应该查询操作日志表 // 暂时返回模拟数据 $history = [ [ 'id' => 1, 'operator' => '系统', 'action' => '创建', 'content' => '订单创建成功', 'created_at' => $order->created_at, ], [ 'id' => 2, 'operator' => '管理员', 'action' => '审核', 'content' => '订单审核通过', 'created_at' => $order->updated_at, ], ]; return response()->json([ 'code' => 200, 'data' => $history, 'message' => 'success' ]); } /** * 获取订单数量统计(用于仪表板) */ public function getDashboardStats() { $today = now()->startOfDay(); $yesterday = now()->subDay()->startOfDay(); $thisMonth = now()->startOfMonth(); $stats = [ 'today' => [ 'total' => Order::where('order_time', '>=', $today)->count(), 'pending' => Order::where('order_time', '>=', $today)->where('order_status', 'pending')->count(), 'auditing' => Order::where('order_time', '>=', $today)->where('order_status', 'auditing')->count(), 'shipped' => Order::where('order_time', '>=', $today)->where('order_status', 'shipped')->count(), 'amount' => Order::where('order_time', '>=', $today)->sum('total_amount'), ], 'yesterday' => [ 'total' => Order::whereBetween('order_time', [$yesterday, $today])->count(), 'amount' => Order::whereBetween('order_time', [$yesterday, $today])->sum('total_amount'), ], 'this_month' => [ 'total' => Order::where('order_time', '>=', $thisMonth)->count(), 'amount' => Order::where('order_time', '>=', $thisMonth)->sum('total_amount'), ], 'all' => [ 'total' => Order::count(), 'pending' => Order::where('order_status', 'pending')->count(), 'auditing' => Order::where('order_status', 'auditing')->count(), 'shipped' => Order::where('order_status', 'shipped')->count(), 'completed' => Order::where('order_status', 'completed')->count(), 'cancelled' => Order::where('order_status', 'cancelled')->count(), 'amount' => Order::sum('total_amount'), ], ]; return response()->json([ 'code' => 200, 'data' => $stats, 'message' => 'success' ]); }