完善版本
This commit is contained in:
@@ -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
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user