Files
fdesk/app/inquiry/controller/StatisticsController.php
T
2025-08-14 20:08:57 +08:00

199 lines
5.8 KiB
PHP

<?php
namespace app\inquiry\controller;
use app\common\controller\BaseController;
use app\common\model\Country;
use app\common\model\InquiryChannel;
use app\common\model\InquiryLog;
use app\common\traits\crud\CrudControllerTrait;
use app\inquiry\service\CountryService;
use support\Request;
use support\Db;
use Illuminate\Support\Carbon;
class StatisticsController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new CountryService();
}
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
// protected function indexFormat($data)
// {
// if (isset($data['created_at']))
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
// if (isset($data['updated_at']))
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
// return $data;
// }
public function whole()
{
$totals = InquiryLog::count('id');
$channelCount = InquiryChannel::count('id');
$todayCount = InquiryLog::where('created_at', '>=', strtotime(date('Y-m-d')))->count('id');
$releaseCount = InquiryLog::groupBy('enter_user_id')->count('id');
$countryCount = Country::count('id');
$trackCount = InquiryLog::groupBy('recipient')->count('id');
$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 = InquiryLog::select('channel', Db::raw('COUNT(*) as totals'))
->groupBy('channel')
->get()->toArray();
$channelCounts = [];
foreach ($list as $vo) {
$channelCounts[] = [
"name" => $channels[$vo["channel"]],
"value" => $vo["totals"]
];
}
return $this->success($channelCounts);
}
// 各国家计数
public function countryCount()
{
$list = InquiryLog::with(["countryInfo"])
->select('country', Db::raw('COUNT(*) as totals'))
->groupBy('country')
->get();
$arr = [];
foreach ($list as $val) {
$arr[] = [
"value" => $val->totals,
"name" => $val->countryInfo->cn_addr
];
}
return $this->success($arr);
}
// 跟单人计数
public function trackCount()
{
$list = InquiryLog::with(["recipientInfo"])
->where("recipient", ">", 0)
->whereNotIn('recipient', [1,5,10,11,13,155,16,17,19,74,75,78,141,147])
->select('recipient', Db::raw('COUNT(*) as totals'))
->groupBy('recipient')
->get();
$optionType = [];
$counts = [];
foreach ($list as $val) {
if (isset($val->recipientInfo) && $val->recipientInfo) {
$optionType[] = $val->recipientInfo->name;
$counts[] = $val->totals;
}
}
$arr = [
"optionType" => $optionType,
"counts" => $counts
];
return $this->success($arr);
}
// 每日录单数
public function dayCount()
{
$list = Db::table('inquiry_log')
->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 inquiryCount(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 = InquiryLog::select([
DB::raw("DATE_FORMAT(FROM_UNIXTIME(r_time), '%Y-%m') AS month"),
DB::raw('COUNT(*) AS total_count')
])
->whereIn('channel', $channelIds)
->where('r_time', '>', 0)
->where('r_time', '>=', $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] ?? [];
}
}