diff --git a/app/Http/Controllers/Admin/ChartController.php b/app/Http/Controllers/Admin/ChartController.php index a4951a5..3135a71 100644 --- a/app/Http/Controllers/Admin/ChartController.php +++ b/app/Http/Controllers/Admin/ChartController.php @@ -36,65 +36,53 @@ class ChartController extends CommonController $startDate = $all['start_date'] ?? null; $endDate = $all['end_date'] ?? null; - // 构建查询条件 - $buildQuery = function ($type) use ($startDate, $endDate) { - $query = Visit::where('type', $type); - if ($startDate && $endDate) { - $query->whereBetween('date', [$startDate, $endDate]); - } elseif ($startDate) { - $query->where('date', '>=', $startDate); - } elseif ($endDate) { - $query->where('date', '<=', $endDate); - } - return $query; - }; - + $today = date('Y-m-d'); $list = [ 'common_visit' => [ // 总人数 - 'total' => $buildQuery(1)->count(), + 'total' => Visit::visitHome(1, $startDate, $endDate, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), // 总入场人数 - 'enter_visit' => $buildQuery(1)->whereNotNull('accept_admin_sign')->count(), + 'enter_visit' => Visit::visitHome(1, $startDate, $endDate, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), // 离厂统计 - 'leave_total' => $buildQuery(1)->where('audit_status', Visit::AUDIT_STATUS_LEFT)->count(), - // 总随访人数 - 'follow_total' => $buildQuery(1)->get()->sum('follw_people_total'), + 'leave_total' => Visit::visitHome(1, $startDate, $endDate, [Visit::AUDIT_STATUS_LEFT]), + // 今日总人数 - 'today_total' => $buildQuery(1)->where('date', date('Y-m-d'))->count(), + 'today_total' => Visit::visitHome(1, $today, $today, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), // 今日入场人数 - 'today_enter_visit' => $buildQuery(1)->where('date', date('Y-m-d'))->whereNotNull('accept_admin_sign')->count(), + 'today_enter_visit' => Visit::visitHome(1, $today, $today, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), // 今日离厂 - 'today_leave_total' => $buildQuery(1)->where('date', date('Y-m-d'))->where('audit_status', Visit::AUDIT_STATUS_LEFT)->count(), - // 今日随访人数 - 'today_follow_total' => $buildQuery(1)->where('date', date('Y-m-d'))->get()->sum('follw_people_total'), + 'today_leave_total' => Visit::visitHome(1, $today, $today, [Visit::AUDIT_STATUS_LEFT]), + ], 'work_visit' => [ - 'total' => $buildQuery(2)->count(), - 'enter_visit' => $buildQuery(2)->whereNotNull('accept_admin_sign')->count(), - 'today_total' => Visit::where('type', 2)->where('date', date('Y-m-d'))->count(), - 'today_enter_visit' => Visit::where('type', 2)->where('date', date('Y-m-d'))->whereNotNull('accept_admin_sign')->count(), + // 总人数 + 'total' => Visit::visitHome(2, $startDate, $endDate, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), + // 总入场人数 + 'enter_visit' => Visit::visitHome(2, $startDate, $endDate, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), // 离厂统计 - 'leave_total' => $buildQuery(2)->where('audit_status', Visit::AUDIT_STATUS_LEFT)->count(), + 'leave_total' => Visit::visitHome(2, $startDate, $endDate, [Visit::AUDIT_STATUS_LEFT]), + + // 今日总人数 + 'today_total' => Visit::visitHome(2, $today, $today, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), + // 今日入场人数 + 'today_enter_visit' => Visit::visitHome(2, $today, $today, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), // 今日离厂 - 'today_leave_total' => Visit::where('type', 2)->where('date', date('Y-m-d'))->where('audit_status', Visit::AUDIT_STATUS_LEFT)->count(), - // 总随访人数 - 'follow_total' => $buildQuery(2)->get()->sum('follw_people_total'), - // 今日随访人数 - 'today_follow_total' => $buildQuery(2)->where('date', date('Y-m-d'))->get()->sum('follw_people_total'), + 'today_leave_total' => Visit::visitHome(2, $today, $today, [Visit::AUDIT_STATUS_LEFT]), ], 'car_visit' => [ - 'total' => $buildQuery(3)->count(), - 'enter_visit' => $buildQuery(3)->whereNotNull('accept_admin_sign')->count(), - 'today_total' => Visit::where('type', 3)->where('date', date('Y-m-d'))->count(), - 'today_enter_visit' => Visit::where('type', 3)->where('date', date('Y-m-d'))->whereNotNull('accept_admin_sign')->count(), + // 总人数 + 'total' => Visit::visitHome(3, $startDate, $endDate, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), + // 总入场人数 + 'enter_visit' => Visit::visitHome(3, $startDate, $endDate, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), // 离厂统计 - 'leave_total' => $buildQuery(3)->where('audit_status', Visit::AUDIT_STATUS_LEFT)->count(), + 'leave_total' => Visit::visitHome(3, $startDate, $endDate, [Visit::AUDIT_STATUS_LEFT]), + + // 今日总人数 + 'today_total' => Visit::visitHome(3, $today, $today, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), + // 今日入场人数 + 'today_enter_visit' => Visit::visitHome(3, $today, $today, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]), // 今日离厂 - 'today_leave_total' => Visit::where('type', 3)->where('date', date('Y-m-d'))->where('audit_status', Visit::AUDIT_STATUS_LEFT)->count(), - // 总随访人数 - 'follow_total' => $buildQuery(3)->get()->sum('follw_people_total'), - // 今日随访人数 - 'today_follow_total' => $buildQuery(3)->where('date', date('Y-m-d'))->get()->sum('follw_people_total'), + 'today_leave_total' => Visit::visitHome(3, $today, $today, [Visit::AUDIT_STATUS_LEFT]), ], ]; diff --git a/app/Models/Visit.php b/app/Models/Visit.php index ba3505a..9fc2822 100644 --- a/app/Models/Visit.php +++ b/app/Models/Visit.php @@ -146,4 +146,38 @@ class Visit extends SoftDeletesModel return $app->subscribe_message->send($data); } + /** + * 首页统计 + */ + public static function visitHome($type, $startDate, $endDate, $auditStatus) + { + $visit = self::visitQuery($type, $startDate, $endDate, $auditStatus); + $visitTotal = $visit->count(); + $follwPeopleTotal = $visit->sum('follw_people_total'); + return $visitTotal + $follwPeopleTotal; + } + + /** + * 首页统计 + */ + public static function visitQuery($type, $startDate, $endDate, $auditStatus) + { + $visit = Visit::where(function ($query) use ($type, $startDate, $endDate, $auditStatus) { + if ($type) { + $query->where('type', $type); + } + if ($auditStatus) { + $query->whereIn('audit_status', $auditStatus); + } + if ($startDate) { + $query->where('date', '>=', $startDate); + } + if ($endDate) { + $query->where('date', '<=', $endDate); + } + })->get(); + return $visit; + } + + }