完善版本
This commit is contained in:
@@ -89,4 +89,19 @@ class StatisticsController extends BaseController
|
||||
|
||||
return response()->json($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取个人中心统计数据
|
||||
*/
|
||||
public function dashboard(Request $request)
|
||||
{
|
||||
try {
|
||||
$this->data['data'] = $this->statisticsService->getDashboardStats($request);
|
||||
} catch (\Throwable $th) {
|
||||
$this->data['code'] = 0;
|
||||
$this->data['message'] = $th->getMessage();
|
||||
}
|
||||
|
||||
return response()->json($this->data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,21 +21,22 @@ class BillService
|
||||
$month = $request->input('month');
|
||||
$startDate = $request->input('start_date');
|
||||
$endDate = $request->input('end_date');
|
||||
$familyId = $request->input('family_id');
|
||||
$dataType = $request->input('data_type', 'family'); // 默认为家庭数据
|
||||
|
||||
// 获取用户所在的家庭ID
|
||||
$userFamilyId = $this->getUserFamilyId($userId);
|
||||
|
||||
$query = Bill::with(['user:uid,nickname,username', 'family:id,name']);
|
||||
|
||||
// 如果用户加入了家庭,且没有指定family_id,则显示家庭账单
|
||||
if ($userFamilyId && !$familyId) {
|
||||
// 根据data_type参数判断数据类型
|
||||
if ($dataType === 'personal') {
|
||||
// 个人数据:只显示当前用户的账单
|
||||
$query->where('user_id', $userId);
|
||||
} elseif ($dataType === 'family' && $userFamilyId) {
|
||||
// 家庭数据:显示用户所在家庭的账单
|
||||
$query->where('family_id', $userFamilyId);
|
||||
} elseif ($familyId) {
|
||||
// 如果指定了family_id,则显示该家庭的账单
|
||||
$query->where('family_id', $familyId);
|
||||
} else {
|
||||
// 否则显示个人账单
|
||||
// 默认显示个人数据
|
||||
$query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
@@ -66,13 +67,13 @@ class BillService
|
||||
$monthExpense = Bill::query();
|
||||
$monthIncome = Bill::query();
|
||||
|
||||
// 同样的家庭查询逻辑
|
||||
if ($userFamilyId && !$familyId) {
|
||||
// 同样的数据类型查询逻辑
|
||||
if ($dataType === 'personal') {
|
||||
$monthExpense->where('user_id', $userId);
|
||||
$monthIncome->where('user_id', $userId);
|
||||
} elseif ($dataType === 'family' && $userFamilyId) {
|
||||
$monthExpense->where('family_id', $userFamilyId);
|
||||
$monthIncome->where('family_id', $userFamilyId);
|
||||
} elseif ($familyId) {
|
||||
$monthExpense->where('family_id', $familyId);
|
||||
$monthIncome->where('family_id', $familyId);
|
||||
} else {
|
||||
$monthExpense->where('user_id', $userId);
|
||||
$monthIncome->where('user_id', $userId);
|
||||
@@ -175,8 +176,14 @@ class BillService
|
||||
$data['category'] = $categoryMap[$data['category_id']];
|
||||
$data['bill_date'] = $data['date'];
|
||||
|
||||
// 检查家庭权限
|
||||
if (!empty($data['family_id'])) {
|
||||
// 如果前端没有指定family_id,自动获取用户所在的家庭
|
||||
if (empty($data['family_id'])) {
|
||||
$userFamilyId = $this->getUserFamilyId($userId);
|
||||
if ($userFamilyId) {
|
||||
$data['family_id'] = $userFamilyId;
|
||||
}
|
||||
} else {
|
||||
// 检查家庭权限
|
||||
$this->checkFamilyAccess($userId, $data['family_id']);
|
||||
}
|
||||
|
||||
@@ -233,17 +240,29 @@ class BillService
|
||||
$data = $request->validate([
|
||||
'type' => 'required|in:income,expense',
|
||||
'amount' => 'required|numeric|min:0.01',
|
||||
'category' => 'required|string|max:50',
|
||||
'category_id' => 'required|integer',
|
||||
'remark' => 'nullable|string|max:255',
|
||||
'bill_date' => 'required|date',
|
||||
'date' => 'required|date',
|
||||
'family_id' => 'nullable|integer|exists:account_families,id'
|
||||
]);
|
||||
|
||||
// 将category_id转换为category字符串
|
||||
$categoryMap = $this->getCategoryMap($data['type']);
|
||||
if (!isset($categoryMap[$data['category_id']])) {
|
||||
throw new \Exception('分类不存在');
|
||||
}
|
||||
|
||||
$data['category'] = $categoryMap[$data['category_id']];
|
||||
$data['bill_date'] = $data['date'];
|
||||
|
||||
// 检查家庭权限
|
||||
if (!empty($data['family_id'])) {
|
||||
$this->checkFamilyAccess($userId, $data['family_id']);
|
||||
}
|
||||
|
||||
// 移除前端字段
|
||||
unset($data['category_id'], $data['date']);
|
||||
|
||||
$bill->update($data);
|
||||
|
||||
return $bill;
|
||||
@@ -278,12 +297,25 @@ class BillService
|
||||
$bill = Bill::with(['user:uid,nickname,username', 'family:id,name,owner_id'])
|
||||
->findOrFail($id);
|
||||
|
||||
// 验证权限
|
||||
if ($bill->user_id != $userId && !in_array($userId, $bill->family->members->pluck('uid')->toArray())) {
|
||||
// 验证权限:只有账单创建者才能查看和编辑
|
||||
if ($bill->user_id != $userId) {
|
||||
throw new \Exception('无权查看此账单');
|
||||
}
|
||||
|
||||
return $bill;
|
||||
// 转换数据格式以匹配前端
|
||||
return [
|
||||
'id' => $bill->id,
|
||||
'type' => $bill->type,
|
||||
'amount' => (float)$bill->amount,
|
||||
'category' => $bill->category,
|
||||
'category_id' => $this->getCategoryId($bill->category, $bill->type),
|
||||
'remark' => $bill->remark,
|
||||
'date' => $bill->bill_date,
|
||||
'bill_date' => $bill->bill_date,
|
||||
'created_at' => $bill->created_at->format('Y-m-d H:i:s'),
|
||||
'user' => $bill->user,
|
||||
'family' => $bill->family
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,9 +17,10 @@ class StatisticsService
|
||||
$userId = auth('api')->user()['uid'];
|
||||
$year = $request->input('year');
|
||||
$month = $request->input('month');
|
||||
$dataType = $request->input('data_type', 'family'); // 默认为家庭数据
|
||||
|
||||
// 获取用户所在的家庭ID
|
||||
$familyId = $this->getUserFamilyId($userId);
|
||||
$userFamilyId = $this->getUserFamilyId($userId);
|
||||
|
||||
// 查询当月的账单
|
||||
$query = Bill::query();
|
||||
@@ -32,9 +33,15 @@ class StatisticsService
|
||||
->whereYear('bill_date', date('Y'));
|
||||
}
|
||||
|
||||
if ($familyId) {
|
||||
$query->where('family_id', $familyId);
|
||||
// 根据data_type参数判断数据类型
|
||||
if ($dataType === 'personal') {
|
||||
// 个人数据:只显示当前用户的账单
|
||||
$query->where('user_id', $userId);
|
||||
} elseif ($dataType === 'family' && $userFamilyId) {
|
||||
// 家庭数据:显示用户所在家庭的账单
|
||||
$query->where('family_id', $userFamilyId);
|
||||
} else {
|
||||
// 默认显示个人数据
|
||||
$query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
@@ -59,9 +66,10 @@ class StatisticsService
|
||||
$year = $request->input('year');
|
||||
$month = $request->input('month');
|
||||
$days = 7; // 最近7天
|
||||
$dataType = $request->input('data_type', 'family'); // 默认为家庭数据
|
||||
|
||||
// 获取用户所在的家庭ID
|
||||
$familyId = $this->getUserFamilyId($userId);
|
||||
$userFamilyId = $this->getUserFamilyId($userId);
|
||||
|
||||
$data = [];
|
||||
for ($i = $days - 1; $i >= 0; $i--) {
|
||||
@@ -69,14 +77,19 @@ class StatisticsService
|
||||
|
||||
$query = Bill::whereDate('bill_date', $date);
|
||||
|
||||
if ($familyId) {
|
||||
$query->where('family_id', $familyId);
|
||||
// 根据data_type参数判断数据类型
|
||||
if ($dataType === 'personal') {
|
||||
// 个人数据:只显示当前用户的账单
|
||||
$query->where('user_id', $userId);
|
||||
} elseif ($dataType === 'family' && $userFamilyId) {
|
||||
// 家庭数据:显示用户所在家庭的账单
|
||||
$query->where('family_id', $userFamilyId);
|
||||
} else {
|
||||
// 默认显示个人数据
|
||||
$query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
$bills = $query->get();
|
||||
|
||||
$data[] = [
|
||||
'date' => $date,
|
||||
'income' => (float) $bills->where('type', 'income')->sum('amount'),
|
||||
@@ -96,9 +109,10 @@ class StatisticsService
|
||||
$type = $request->input('type', 'expense');
|
||||
$year = $request->input('year');
|
||||
$month = $request->input('month');
|
||||
$dataType = $request->input('data_type', 'family'); // 默认为家庭数据
|
||||
|
||||
// 获取用户所在的家庭ID
|
||||
$familyId = $this->getUserFamilyId($userId);
|
||||
$userFamilyId = $this->getUserFamilyId($userId);
|
||||
|
||||
$query = Bill::where('type', $type);
|
||||
|
||||
@@ -110,16 +124,22 @@ class StatisticsService
|
||||
->whereYear('bill_date', date('Y'));
|
||||
}
|
||||
|
||||
if ($familyId) {
|
||||
$query->where('family_id', $familyId);
|
||||
// 根据data_type参数判断数据类型
|
||||
if ($dataType === 'personal') {
|
||||
// 个人数据:只显示当前用户的账单
|
||||
$query->where('user_id', $userId);
|
||||
} elseif ($dataType === 'family' && $userFamilyId) {
|
||||
// 家庭数据:显示用户所在家庭的账单
|
||||
$query->where('family_id', $userFamilyId);
|
||||
} else {
|
||||
// 默认显示个人数据
|
||||
$query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
$bills = $query->get();
|
||||
|
||||
// 按分类汇总
|
||||
$data = $bills->groupBy('category')->map(function ($items) {
|
||||
$data = $bills->groupBy('category')->map(function ($items) use ($type) {
|
||||
return [
|
||||
'category_id' => $this->getCategoryId($items->first()->category, $type),
|
||||
'amount' => (float) $items->sum('amount')
|
||||
@@ -153,18 +173,25 @@ class StatisticsService
|
||||
{
|
||||
$userId = auth('api')->user()['uid'];
|
||||
$year = $request->input('year', date('Y'));
|
||||
$dataType = $request->input('data_type', 'family'); // 默认为家庭数据
|
||||
|
||||
// 获取用户所在的家庭ID
|
||||
$familyId = $this->getUserFamilyId($userId);
|
||||
$userFamilyId = $this->getUserFamilyId($userId);
|
||||
|
||||
$data = [];
|
||||
for ($month = 1; $month <= 12; $month++) {
|
||||
$query = Bill::whereMonth('bill_date', $month)
|
||||
->whereYear('bill_date', $year);
|
||||
|
||||
if ($familyId) {
|
||||
$query->where('family_id', $familyId);
|
||||
// 根据data_type参数判断数据类型
|
||||
if ($dataType === 'personal') {
|
||||
// 个人数据:只显示当前用户的账单
|
||||
$query->where('user_id', $userId);
|
||||
} elseif ($dataType === 'family' && $userFamilyId) {
|
||||
// 家庭数据:显示用户所在家庭的账单
|
||||
$query->where('family_id', $userFamilyId);
|
||||
} else {
|
||||
// 默认显示个人数据
|
||||
$query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
@@ -191,9 +218,10 @@ class StatisticsService
|
||||
{
|
||||
$userId = auth('api')->user()['uid'];
|
||||
$years = $request->input('years', 5);
|
||||
$dataType = $request->input('data_type', 'family'); // 默认为家庭数据
|
||||
|
||||
// 获取用户所在的家庭ID
|
||||
$familyId = $this->getUserFamilyId($userId);
|
||||
$userFamilyId = $this->getUserFamilyId($userId);
|
||||
|
||||
$data = [];
|
||||
for ($i = $years - 1; $i >= 0; $i--) {
|
||||
@@ -201,9 +229,15 @@ class StatisticsService
|
||||
|
||||
$query = Bill::whereYear('bill_date', $year);
|
||||
|
||||
if ($familyId) {
|
||||
$query->where('family_id', $familyId);
|
||||
// 根据data_type参数判断数据类型
|
||||
if ($dataType === 'personal') {
|
||||
// 个人数据:只显示当前用户的账单
|
||||
$query->where('user_id', $userId);
|
||||
} elseif ($dataType === 'family' && $userFamilyId) {
|
||||
// 家庭数据:显示用户所在家庭的账单
|
||||
$query->where('family_id', $userFamilyId);
|
||||
} else {
|
||||
// 默认显示个人数据
|
||||
$query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
@@ -228,4 +262,55 @@ class StatisticsService
|
||||
$familyMember = FamilyMember::where('user_id', $userId)->first();
|
||||
return $familyMember ? $familyMember->family_id : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取个人中心统计数据
|
||||
*/
|
||||
public function getDashboardStats(Request $request)
|
||||
{
|
||||
$userId = auth('api')->user()['uid'];
|
||||
$dataType = $request->input('data_type', 'family'); // 默认为家庭数据
|
||||
|
||||
// 获取用户所在的家庭ID
|
||||
$userFamilyId = $this->getUserFamilyId($userId);
|
||||
|
||||
// 统计账单数
|
||||
$billQuery = Bill::query();
|
||||
|
||||
// 根据data_type参数判断数据类型
|
||||
if ($dataType === 'personal') {
|
||||
// 个人数据:只统计当前用户的账单
|
||||
$billQuery->where('user_id', $userId);
|
||||
} elseif ($dataType === 'family' && $userFamilyId) {
|
||||
// 家庭数据:统计用户所在家庭的账单
|
||||
$billQuery->where('family_id', $userFamilyId);
|
||||
} else {
|
||||
// 默认统计个人数据
|
||||
$billQuery->where('user_id', $userId);
|
||||
}
|
||||
|
||||
$billCount = $billQuery->count();
|
||||
|
||||
// 统计记账天数(计算从第一个账单到现在的天数)
|
||||
$days = 0;
|
||||
$firstBill = $billQuery->orderBy('bill_date', 'asc')->first();
|
||||
if ($firstBill) {
|
||||
$firstDate = new \DateTime($firstBill->bill_date);
|
||||
$now = new \DateTime();
|
||||
$interval = $firstDate->diff($now);
|
||||
$days = $interval->days + 1; // +1 包含当天
|
||||
}
|
||||
|
||||
// 统计家庭成员数
|
||||
$familyMembers = 0;
|
||||
if ($userFamilyId) {
|
||||
$familyMembers = FamilyMember::where('family_id', $userFamilyId)->count();
|
||||
}
|
||||
|
||||
return [
|
||||
'bill_count' => $billCount,
|
||||
'days' => $days,
|
||||
'family_members' => $familyMembers
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,5 +44,6 @@ Route::middleware(['auth:api'])->group(function () {
|
||||
Route::get('category', [StatisticsController::class, 'category']);
|
||||
Route::get('monthly', [StatisticsController::class, 'monthly']);
|
||||
Route::get('yearly', [StatisticsController::class, 'yearly']);
|
||||
Route::get('dashboard', [StatisticsController::class, 'dashboard']);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user