diff --git a/app/crm/model/Customer.php b/app/crm/model/Customer.php index 326eb72..92c070d 100644 --- a/app/crm/model/Customer.php +++ b/app/crm/model/Customer.php @@ -3,6 +3,7 @@ namespace app\crm\model; +use app\common\model\Country; use app\crm\utils\Util; use config\constants\Constants; use DateTimeInterface; @@ -106,4 +107,9 @@ class Customer extends Model return $this->save($options); }); } + + public function countryInfo() + { + return $this->hasOne(Country::class, 'id', 'country_id'); + } } \ No newline at end of file diff --git a/app/crmAdmin/controller/StatisticsController.php b/app/crmAdmin/controller/StatisticsController.php new file mode 100644 index 0000000..77db8a0 --- /dev/null +++ b/app/crmAdmin/controller/StatisticsController.php @@ -0,0 +1,188 @@ +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个月 + var_dump($sixMonthsAgo->timestamp); + + // 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] ?? []; + } +} \ No newline at end of file diff --git a/config/route.php b/config/route.php index 05cf625..9744a36 100644 --- a/config/route.php +++ b/config/route.php @@ -194,6 +194,15 @@ Route::group('/admin', function () { Route::any('/follow', [app\crmAdmin\controller\CustomerController::class, 'follow']); Route::any('/allocation', [app\crmAdmin\controller\CustomerController::class, 'allocation']); }); + // 统计 + Route::group('/statistics', function () { + Route::any('/whole', [app\crmAdmin\controller\StatisticsController::class, 'whole']); + Route::any('/channel/count', [app\crmAdmin\controller\StatisticsController::class, 'channelCount']); + Route::any('/country/count', [app\crmAdmin\controller\StatisticsController::class, 'countryCount']); + Route::any('/track/count', [app\crmAdmin\controller\StatisticsController::class, 'trackCount']); + Route::any('/day/count', [app\crmAdmin\controller\StatisticsController::class, 'dayCount']); + Route::any('/customer/count', [app\crmAdmin\controller\StatisticsController::class, 'customerCount']); + }); }); // 车辆