92 lines
2.6 KiB
PHP
92 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\admin\controller\BabyHealth;
|
|
|
|
use app\admin\BaseController;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use think\facade\Session;
|
|
use think\response\Json;
|
|
use think\db\exception\DbException;
|
|
use app\model\AppsBabyhealthBabys;
|
|
use app\model\AppsBabyhealthUsers;
|
|
|
|
class DashboradController extends BaseController
|
|
{
|
|
/**
|
|
* 统计宝贝数量
|
|
* @return Json
|
|
*/
|
|
public function getBabyCounts()
|
|
{
|
|
try {
|
|
// 总数
|
|
$total = AppsBabyhealthBabys::where('delete_time', null)->count();
|
|
|
|
// 男宝宝数 (sex = 1)
|
|
$maleCount = AppsBabyhealthBabys::where('delete_time', null)->where('sex', 1)->count();
|
|
|
|
// 女宝宝数 (sex = 2)
|
|
$femaleCount = AppsBabyhealthBabys::where('delete_time', null)->where('sex', 2)->count();
|
|
|
|
// 未知性别 (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
|
|
]
|
|
]);
|
|
} catch (DbException $e) {
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '统计失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 统计用户数量
|
|
* @return Json
|
|
*/
|
|
public function getUserCounts()
|
|
{
|
|
try {
|
|
// 总数
|
|
$total = AppsBabyhealthUsers::where('delete_time', null)->count();
|
|
|
|
// 父亲 (sex = 1)
|
|
$fatherCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 1)->count();
|
|
|
|
// 母亲宝数 (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
|
|
]
|
|
]);
|
|
} catch (DbException $e) {
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '统计失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
}
|