批量更新

This commit is contained in:
2026-02-24 10:12:05 +08:00
parent 0ed654b5b8
commit 0bf9b77aed
4 changed files with 157 additions and 97 deletions
@@ -16,10 +16,32 @@ use app\model\AppsBabyhealthUsers;
class DashboradController extends BaseController
{
/**
* 统计宝贝数量
* @return Json
* dashborad总体输出
*/
public function getBabyCounts()
public function getDashborad()
{
try {
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'userCounts' => $this->getUserCounts(),
'babyCounts' => $this->getBabyCounts()
]
]);
} catch (DbException $e) {
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
/**
* 统计宝贝数量
* @return array
*/
private function getBabyCounts(): array
{
try {
// 总数
@@ -34,29 +56,29 @@ class DashboradController extends BaseController
// 未知性别 (sex = 0)
$unknownCount = AppsBabyhealthBabys::where('delete_time', null)->where('sex', 0)->count();
return json([
'code' => 200,
'msg' => '统计成功',
'data' => [
'total' => $total,
'male' => $maleCount,
'female' => $femaleCount,
'unknown' => $unknownCount
]
]);
return [
'total' => $total,
'male' => $maleCount,
'female' => $femaleCount,
'unknown' => $unknownCount
];
} catch (DbException $e) {
return json([
'code' => 500,
'msg' => '统计失败:' . $e->getMessage()
]);
// 返回空数组而不是抛出异常,让外层处理
error_log('统计宝贝数量失败: ' . $e->getMessage());
return [
'total' => 0,
'male' => 0,
'female' => 0,
'unknown' => 0
];
}
}
/**
* 统计用户数量
* @return Json
* @return array
*/
public function getUserCounts()
private function getUserCounts(): array
{
try {
// 总数
@@ -65,27 +87,27 @@ class DashboradController extends BaseController
// 父亲 (sex = 1)
$fatherCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 1)->count();
// 母亲宝数 (sex = 2)
// 母亲 (sex = 2)
$motherCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 2)->count();
// 未知性别 (sex = 0)
$unknownCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 0)->count();
return json([
'code' => 200,
'msg' => '统计成功',
'data' => [
'total' => $total,
'father' => $fatherCount,
'mother' => $motherCount,
'unknown' => $unknownCount
]
]);
return [
'total' => $total,
'father' => $fatherCount,
'mother' => $motherCount,
'unknown' => $unknownCount
];
} catch (DbException $e) {
return json([
'code' => 500,
'msg' => '统计失败:' . $e->getMessage()
]);
// 返回空数组而不是抛出异常,让外层处理
error_log('统计用户数量失败: ' . $e->getMessage());
return [
'total' => 0,
'father' => 0,
'mother' => 0,
'unknown' => 0
];
}
}
}