service = new CountryService(); } public function whole() { $totals = Customer::count('id'); $channelCount = InquiryChannel::count('id'); $todayCount = Customer::where('created_at', '>=', strtotime(date('Y-m-d')))->count('id'); $releaseCount = Customer::distinct('enter_user_id')->count('id'); $countryCount = Country::count('id'); $trackCount = Customer::distinct('user_id')->count(); $data = [ 'totals' => $totals, 'channelCount' => $channelCount, 'todayCount' => $todayCount, 'releaseCount' => $releaseCount, 'countryCount' => $countryCount, 'trackCount' => $trackCount ]; return $this->success($data); } // 各渠道计数 public function channelCount() { $inquiryChannelList = InquiryChannel::all(['id', 'name']); $channels = []; foreach ($inquiryChannelList as $key => $val) { $channels[$val->id] = $val->name; } $list = Customer::select('channel_id', Db::raw('COUNT(*) as totals')) ->groupBy('channel_id') ->get()->toArray(); $channelCounts = []; foreach ($list as $vo) { $channelCounts[] = [ "name" => $channels[$vo["channel_id"]], "value" => $vo["totals"] ]; } return $this->success($channelCounts); } // 各国家计数 public function countryCount() { $list = Customer::with("countryInfo") ->select('country_id', Db::raw('COUNT(*) as totals')) ->where('country_id','<>',0) ->groupBy('country_id') ->get(); $arr = []; foreach ($list as $val) { $arr[] = [ "value" => $val->totals, "name" => $val->countryInfo->cn_addr ]; } return $this->success($arr); } // 跟单人计数 public function trackCount() { $list = Customer::with(["user"]) ->where("user_id", ">", 0) ->whereNotIn('user_id', [1,5,10,11,13,155,16,17,19,74,75,78,141,147]) ->select('user_id', Db::raw('COUNT(*) as totals')) ->groupBy('user_id') ->get(); $optionType = []; $counts = []; foreach ($list as $val) { if (isset($val->user) && $val->user) { $optionType[] = $val->user->name; $counts[] = $val->totals; } } $arr = [ "optionType" => $optionType, "counts" => $counts ]; return $this->success($arr); } // 每日录单数 public function dayCount() { $list = Db::table('crm_customer') ->where('created_at', '>=', Carbon::now()->subDay(7)->timestamp) ->select( Db::raw('DATE(FROM_UNIXTIME(created_at)) as date'), Db::raw('COUNT(*) as order_count') ) ->groupBy('date') ->pluck('order_count', 'date')->toArray(); $optionType = []; $counts = []; foreach ($list as $key=>$val) { $optionType[] = $key; $counts[] = $val; } $arr = [ "optionType" => $optionType, "counts" => $counts ]; return $this->success($arr); } public function customerCount(Request $request) { $companyId = $request->get('company_id') ?? 0; // 1. 计算时间范围 $now = Carbon::now(); $sixMonthsAgo = $now->copy()->subMonths(5)->startOfMonth(); // 包含6个月 // 2. 获取关联渠道ID $channelIds = $this->getChannelIdsByCompany($companyId); if (empty($channelIds)) { return collect(); } // 3. 执行查询 $list = Customer::select([ DB::raw("DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y-%m') AS month"), DB::raw('COUNT(*) AS total_count') ]) ->whereIn('channel_id', $channelIds) ->where('created_at', '>', 0) ->where('created_at', '>=', $sixMonthsAgo->timestamp) // 添加时间限制 ->groupBy('month') ->orderBy('month') ->get(); $optionType = []; $counts = []; foreach ($list as $item) { $optionType[] = $item->month; $counts[] = $item->total_count; } $arr = [ "optionType" => $optionType, "counts" => $counts ]; return $this->success($arr); } /** * 获取公司与渠道的映射 */ private function getChannelIdsByCompany($companyId) { $data = [ 0 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 1 => [1, 2, 3, 4, 5, 6, 7, 8], 3 => [9, 10, 11] ]; return $data[$companyId] ?? []; } }