GeoQuestionResLogic.php
8.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* @remark :
* @name :GeoQuestionResLogic.php
* @author :lyh
* @method :post
* @time :2025/7/4 9:47
*/
namespace App\Http\Logic\Bside\Geo;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Geo\GeoPlatform;
use App\Models\Geo\GeoQuestion;
use App\Models\Geo\GeoQuestionLog;
use App\Models\Geo\GeoQuestionResult;
use Illuminate\Support\Facades\DB;
class GeoQuestionResLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new GeoQuestionResult();
$this->param = $this->requestAll;
}
/**
* @remark :获取类型统计数据
* @name :getCount
* @author :lyh
* @method :post
* @time :2025/7/8 17:16
*/
public function getCount(){
$total = $this->model->counts(['project_id'=>$this->user['project_id']]);
$type_1 = $this->model->counts(['type'=>$this->model::BRAND_TYPE,'project_id'=>$this->user['project_id']]);
$type_2 = $this->model->counts(['type'=>$this->model::MARKETING_TYPE,'project_id'=>$this->user['project_id']]);
return $this->success(['total'=>$total,'type_1'=>$type_1,'type_2'=>$type_2]);
}
/**
* @remark :获取列表页数据
* @name :getResultList
* @author :lyh
* @method :post
* @time :2025/7/4 9:48
*/
public function getResultList($map = [],$page = 1,$row = 20,$order = 'created_at',$sort = 'desc'){
unset($map['sort']);
$map['project_id'] = $this->user['project_id'];
$map['hit'] = ['!=',0];
$filed = ['id','project_id','question_id','platform','is_match','question','en_question','keywords','url','label','cosine','created_at','updated_at'];
if(!empty($map['created_at'])){
$map['created_at'] = ['between',[$map['created_at'].' 00:00:00',$map['created_at'].' 23:59:59']];
$this->model = new GeoQuestionLog();
}
if(isset($map['label']) && !empty($map['label'])){
$map['label'] = ['like','%'.$map['label'].'%'];
}
if(!empty($map['keywords'])){
$map['keywords'] = ['like','%'.$map['keywords'].'%'];
}
$query = $this->model->formatQuery($map);
// $query = $query->where(function ($q) {
// $q->whereRaw('JSON_LENGTH(keywords) > 0')
// ->orWhereRaw('JSON_LENGTH(url) > 0');
// });
$data = $query->orderBy($order,$sort)->orderByRaw('CHAR_LENGTH(question) ASC')->paginate($row, $filed, 'page', $page);
return $this->success($data);
}
/**
* @remark :获取数据详情
* @name :getResultInfo
* @author :lyh
* @method :post
* @time :2025/7/4 10:19
*/
public function getResultInfo(){
if(isset($this->param['model'])){//查询记录表
$this->model = new GeoQuestionLog();
unset($this->param['model']);
}
$data = $this->model->read($this->param);
return $this->success($data);
}
/**
* @remark :统计数量
* @name :countQuantity
* @author :lyh
* @method :post
* @time :2025/7/21 11:12
*/
public function countQuantity(){
$questionModel = new GeoQuestion();
$list = $questionModel->list(['project_id'=>$this->user['project_id']],['question','keywords','url']);
$core_question_count = $questionTotalCount = $urlTotalCount = $keywordsTotalCount = 0;
$keywordArr = [];
$questionLogModel = new GeoQuestionResult();
if($this->user['project_id'] == 4533){
$keywordUrlCount = $questionLogModel->counts(['project_id'=>$this->user['project_id'],'is_match'=>1,'hit'=>['!=',0]]);
foreach ($list as $item){
$questionTotalCount += count($item['question'] ?? []);
//核心问题数
if(strpos($item['label'],'核心问题') !== false){
$core_question_count += count($item['question'] ?? []);
}
foreach ($item['keywords'] as $keyWordItem){
if (!array_key_exists($keyWordItem, $keywordArr)) {
$keywordArr[$keyWordItem] = $questionLogModel->counts(['project_id'=>$this->user['project_id'],'is_match'=>1,'keywords'=>['like','%"'.$keyWordItem.'"%']]);
}
}
}
$coreKeywordUrlCount = $questionLogModel->counts(['project_id'=>$this->user['project_id'],'label'=>['like','%核心问题%'],'is_match'=>1,'hit'=>['!=',0]]);
$data = [
'question_count'=>$questionTotalCount,//问题总数
'core_question_count'=>$core_question_count,//核心问题总数
'keywords_url_count'=>$keywordUrlCount,
'keywords_arr' => $keywordArr,
'core_keyword_url_count'=>$coreKeywordUrlCount ?? 0
];
return $this->success($data);
}else{
$keywordUrlCount = $questionLogModel->counts(['project_id'=>$this->user['project_id'],'hit'=>['!=',0]]);
foreach ($list as $item){
$questionTotalCount += count($item['question'] ?? []);
$keywordsTotalCount += count($item['keywords'] ?? []);
$urlTotalCount += count($item['url'] ?? []);
foreach ($item['keywords'] as $keyWordItem){
if (!array_key_exists($keyWordItem, $keywordArr)) {
$keywordArr[$keyWordItem] = $questionLogModel->counts(['project_id'=>$this->user['project_id'],'keywords'=>['like','%"'.$keyWordItem.'"%']]);
}
}
}
$data = [
'question_count'=>$questionTotalCount,
'keywords_count'=>$keywordsTotalCount,
'url_count'=>$urlTotalCount,
'keywords_url_count'=>$keywordUrlCount,
'keywords_arr' => $keywordArr,
];
}
//问题达标数据
$data['question_qualify_count'] = $questionLogModel->where('project_id', $this->user['project_id'])
->where('hit','!=',0)->whereIn('platform',['openai', 'gemini','google_ai_overview'])
->select(DB::raw('COUNT(DISTINCT question) as total'))
->value('total');
return $this->success($data);
}
/**
* @remark :按平台统计问题数量
* @name :platformHitCount
* @author :lyh
* @method :post
* @time :2025/7/21 15:00
*/
public function platformHitCount(){
$data = [];
$platformModel = new GeoPlatform();
$list = $platformModel->list(['status'=>1],'id',['name','en_name']);
$questionResModel = new GeoQuestionResult();
foreach ($list as $item){
if($this->user['project_id'] == 4533){
$data[$item['name']] = $questionResModel->counts(['project_id'=>$this->user['project_id'],'is_match'=>1,'hit'=>['!=',0],'platform'=>$item['en_name']]);
}else{
$data[$item['name']] = $questionResModel->counts(['project_id'=>$this->user['project_id'],'hit'=>['!=',0],'platform'=>$item['en_name']]);
}
}
return $this->success($data);
}
/**
* @remark :获取搜索时间
* @name :getSearchDate
* @author :lyh
* @method :post
* @time :2025/7/21 16:36
*/
public function getSearchDate(){
$dates = $this->model->where('project_id',$this->user['project_id'])->select(DB::raw('DATE(created_at) as date_only'))->distinct()->pluck('date_only');
return $this->success($dates);
}
/**
* @remark :获取标签列表
* @name :getLabelData
* @author :lyh
* @method :post
* @time :2025/8/19 16:42
*/
public function getLabelData(){
$questionModel = new GeoQuestion();
$labelArr = $questionModel->selectField(['project_id'=>$this->user['project_id']],'label');
$data = [];
foreach ($labelArr as $item){
$arr = explode("\r\n",$item);
$data = array_values(array_unique(array_merge($arr,$data)));
}
return $this->success($data);
}
}