InquiryForwardLogic.php
21.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<?php
namespace App\Http\Logic\Aside\Optimize;
use App\Helper\Common;
use App\Helper\Gpt;
use App\Helper\Translate;
use App\Http\Logic\Aside\BaseLogic;
use App\Models\Ai\AiCommand;
use App\Models\Inquiry\AreaTimezone;
use App\Models\Inquiry\ForwardCount;
use App\Models\Inquiry\InquiryInfo;
use App\Models\Inquiry\InquiryProject;
use App\Models\Inquiry\InquiryProjectRoute;
use App\Models\Inquiry\InquiryRelayAi;
use App\Models\Inquiry\InquiryRelayDetail;
use App\Models\Inquiry\InquiryRelayDetailLog;
use App\Models\Manage\Manage;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
/**
* @remark :询盘中心
* @class :InquiryForwardLogic.php
* @author :akun
* @time :2025/2/21 11:17
*/
class InquiryForwardLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new InquiryInfo();
}
/**
* 获取询盘类型
* @return array
* @author Akun
* @date 2025/02/21 11:19
*/
public function getType()
{
$type = $this->model->typeMap();
$list = [];
foreach ($type as $k => $v) {
$list[] = [
'id' => $k,
'name' => $v
];
}
return $this->success($list);
}
/**
* 获取询盘列表
* @param $map
* @param $page
* @param $row
* @param string $order
* @param string[] $filed
* @return array
* @author Akun
* @date 2025/02/21 11:19
*/
public function getInquiryLists($map, $page, $row, $order = 'id', $filed = ['*'])
{
$lists = $this->model->lists($map, $page, $row, $order, $filed);
//获取操作人
$manage_model = new Manage();
foreach ($lists['list'] as $k => $v) {
$manage_info = $manage_model->read(['id' => $v['operator_id']], ['name']);
$lists['list'][$k]['operator_name'] = $manage_info ? $manage_info['name'] : '';
}
//统计各个状态询盘总数
if (isset($map['status'])) {
unset($map['status']);
}
$all_count = $this->model->counts($map);
$map['status'] = InquiryInfo::STATUS_INIT;
$un_count = $this->model->counts($map);
$map['status'] = InquiryInfo::STATUS_FINISH;
$suc_count = $this->model->counts($map);
$map['status'] = InquiryInfo::STATUS_INVALID;
$inv_count = $this->model->counts($map);
$lists['count_stat'] = [
'all_count' => $all_count,
'un_count' => $un_count,
'suc_count' => $suc_count,
'inv_count' => $inv_count,
];
return $this->success($lists);
}
/**
* 获取询盘详情
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2025/03/01 11:20
*/
public function infoInquiry()
{
$info = $this->model->read(['id' => $this->param['id']], ['id', 'url', 'url_title', 'url_keyword', 'name', 'email', 'phone', 'ip', 'country', 'message']);
if ($info === false) {
$this->fail('获取询盘详情失败');
}
$keywords_ai = '';
$forward_url_ai = [];
$message_ai = [];
$inquiry_ai_model = new InquiryRelayAi();
$ai_info = $inquiry_ai_model->read(['form_id' => $this->param['id']], ['keywords', 'forward_url', 'message_ai']);
if ($ai_info) {
$keywords_ai = $ai_info['keywords'];
$forward_url_ai = json_decode($ai_info['forward_url'], true);
$message_ai = json_decode($ai_info['message_ai'], true);
}
$info['keywords_ai'] = $keywords_ai;
$info['forward_url_ai'] = $forward_url_ai;
$info['message_ai'] = $message_ai;
return $this->success($info);
}
/**
* 转发询盘
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2025/02/21 14:54
*/
public function inquiryForward()
{
$info = $this->model->read(['id' => $this->param['id']]);
if ($info === false) {
$this->fail('获取询盘详情失败');
}
if ($info['status'] != InquiryInfo::STATUS_INIT) {
$this->fail('当前询盘状态无法转发');
}
if (is_array($this->param['message'])) {
$message_list = $this->param['message'];
} else {
$message_list = [$this->param['message']];
}
$message_count = count($message_list);
if ($message_count == 0) {
$this->fail('内容不能为空');
}
if (is_array($this->param['forward_url'])) {
$forward_url = $this->param['forward_url'];
} else {
$forward_url = explode(',', $this->param['forward_url']);
}
DB::beginTransaction();
try {
$num = 0;
$now = date('Y-m-d H:i:s');
foreach ($forward_url as $key => $url) {
$url = trim($url);
$domain_array = parse_url($url);
$website = $domain_array['host'] ?? '';
if (!$website) {
continue;
}
//获取站点对应项目版本
$project_version = InquiryProject::select(['version'])->where('domain', 'like', '%' . $website . '%')->first();
if (!$project_version) {
continue;
}
$is_v6 = $project_version->version == 6 ? 1 : 0;
//获取国家
$country = $this->param['country'] ?? '';
if (!$country) {
$country = file_get_contents("http://ip.globalso.com?ip=" . $this->param['ip']);
}
//获取发送时间
if (isset($this->param['inquiry_date']) && $this->param['inquiry_date']) {
//填写了时间,直接使用
$start_at = $this->param['inquiry_date'];
} else {
//未填写时间
//1.根据时差获取当地随机10-18点时间
$timezoneModel = new AreaTimezone();
$country_area = $timezoneModel->read(['name' => $country], ['diff']);
$diff = $country_area ? $country_area['diff'] : 0;
if ($diff > 0) {
$area_time = date('Y-m-d H:i:s', strtotime($now . ' +' . $diff . ' hours'));
} elseif ($diff < 0) {
$area_time = date('Y-m-d H:i:s', strtotime($now . ' -' . abs($diff) . ' hours'));
} else {
$area_time = $now;
}
$area_timestamp = strtotime($area_time);
$area_hour = date('H', $area_timestamp);
if ($area_hour < 18) {
//当地时间为18点前
$area_day = date('Y-m-d', $area_timestamp);
$area_start_time = $area_hour < 10 ? '10:00:00' : date('H:i:s', $area_timestamp);
} else {
//当地时间为18点后,顺延一天
$area_day = date('Y-m-d', strtotime($area_time . ' +1 day'));
$area_start_time = '10:00:00';
}
$start_time = strtotime($area_day . ' ' . $area_start_time);
$end_time = strtotime($area_day . ' 18:00:00');
$random_timestamp = mt_rand($start_time, $end_time);
$area_date = date('Y-m-d H:i:s', $random_timestamp);
//2.再根据时差将随机生成的当地时间换算成国内时间
if ($diff > 0) {
$start_at = date('Y-m-d H:i:s', strtotime($area_date . ' -' . $diff . ' hours'));
} elseif ($diff < 0) {
$start_at = date('Y-m-d H:i:s', strtotime($area_date . ' +' . abs($diff) . ' hours'));
} else {
$start_at = $area_date;
}
}
//换算后的国内时间小于当前时间,使用当前时间
if ($start_at < $now) {
$start_at = $now;
}
//获取询盘内容
$message = $message_list[$key % $message_count] ?? $message_list[0];
InquiryRelayDetail::createInquiry($info['id'], $website, $country, $this->param['ip'], $this->param['name'], $this->param['email'], $this->param['phone'] ?? '', $message, $is_v6, json_encode([$url]), $start_at);
$num += 1;
}
//更改询盘状态及转发数量
$this->model->edit(['status' => InquiryInfo::STATUS_FINISH, 'num' => $num, 'operator_id' => $this->manager['id']], ['id' => $this->param['id']]);
DB::commit();
} catch (\Exception $e) {
DB::rollback();
@file_put_contents(storage_path('logs/lk_error.log'), $e->getMessage());
$this->fail('转发询盘失败');
}
return $this->success();
}
/**
* 获取随机ip
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2025/02/26 15:50
*/
public function getIp()
{
$ip = DB::table('gl_xunpan_ipdata')->where('ip_area', $this->param['country'])->inRandomOrder()->value('ip');
if (!$ip) {
$this->fail('当前国家无法获取随机ip');
}
return $this->success(['ip' => $ip]);
}
/**
* 关键词查询项目着陆页
* @param $keywords
* @param $type
* @param $num
* @return int|mixed
* @author Akun
* @date 2025/02/26 17:13
*/
public function searchKeywords($keywords, $type, $num)
{
$model = new InquiryProjectRoute();
if ($type == 1) {
//使用全文索引搜索
$routeQuery = $model->select(['project_id', 'route'])->whereRaw("MATCH(title) AGAINST(? IN BOOLEAN MODE)", [$keywords]);
} else {
//使用like查询
$routeQuery = $model->select(['project_id', 'route'])->where('title', 'like', '%' . $keywords . '%');
}
$re_route = $routeQuery->inRandomOrder()->take(100)->get()->toArray();
$lists = [];
if (count($re_route) > 0) {
//根据项目id查询项目,并根据询盘量排序
$project_ids = array_column($re_route, 'project_id');
$re_project = InquiryProject::select(['id', 'project', 'channel', 'domain', 'recent_inquiry', 'remark', 'version'])->whereIn('id', $project_ids)->orderBy('recent_inquiry', 'asc')->get();
if ($re_project->count() > 0) {
//根据代理商去重
$channel = [];
foreach ($re_project as $vp) {
$vp_channel = explode(',', $vp->channel);
$has_channel = 0;
foreach ($vp_channel as $vpc) {
if (in_array($vpc, $channel)) {
$has_channel = 1;
}
}
if ($has_channel == 0) {
$channel = array_merge($channel, $vp_channel);
$route = '';
foreach ($re_route as $vr) {
if ($vr['project_id'] == $vp->id) {
$route = $vr['route'];
break;
}
}
if (substr($route, -5) != '.html') {
$route = $route . '/';
}
$lists[] = [
'id' => $vp->id,
'project' => $vp->project,
'route' => $vp->domain . $route,
'recent_inquiry' => $vp->recent_inquiry,
'remark' => $vp->remark ?: '',
'version' => $vp->version,
'type' => $type
];
}
}
}
}
return $this->success(array_slice($lists, 0, $num, true));
}
/**
* AI重写询盘文案
* @param $in_content
* @param $title
* @param $keywords
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2025/02/27 10:40
*/
public function aiRewrite($in_content, $title = '', $keywords = '')
{
if ($title && $keywords) {
$command_key = 'inquiry_text_rewrite_v2';
} else {
$command_key = 'inquiry_text_rewrite';
}
$ai_info = AiCommand::select(['ai', 'not_use_probability'])->where('key', $command_key)->first();
if (!$ai_info) {
$this->fail('AI重写指令未配置');
}
$ai_command = $ai_info['ai'];
$not_use_probability = $ai_info['not_use_probability'];
// 当原始询盘内容长度大于15个字符, 60%几率直接发送原始内容。
if (strlen($in_content) >= 15) {
$randomNumber = rand(0, 100);
if ($randomNumber < $not_use_probability) {
return $this->success(['ai_message' => $in_content]);
}
}
$translateSl = Translate::translateSl($in_content);
$lang = $translateSl['texts']['sl'] ?? 'en';
if ($lang == 'en' || $lang == 'ja' || $lang == 'ko' || Str::contains($lang, 'zh')) {
$lang = 'en';
}
if ($title && $keywords) {
$ai_command = str_replace('{title}', $title, $ai_command);
$ai_command = str_replace('{keywords}', $keywords, $ai_command);
}
$ai_command = str_replace('{incontent}', Arr::random(explode("\r\n", $in_content)), $ai_command);
$text = Gpt::instance()->openai_chat_qqs($ai_command);
if ($lang != 'en' && !Str::contains($lang, 'zh')) {
$text = Translate::tran($text, $lang);
}
return $this->success(['ai_message' => str_replace('<br/>', PHP_EOL, Common::deal_str($text))]);
}
/**
* 设置询盘无效
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2025/02/27 15:36
*/
public function expiredInquiry()
{
if (isset($this->param['type']) && $this->param['type'] == 1) {
//启用
$status = InquiryInfo::STATUS_INIT;
$remark = '';
} else {
//无效
$status = InquiryInfo::STATUS_INVALID;
$remark = '手动置为无效';
}
$rs = $this->model->edit(['status' => $status, 'remark' => $remark, 'operator_id' => $this->manager['id']], ['id' => $this->param['id']]);
if ($rs === false) {
$this->fail('设置失败');
}
return $this->success();
}
/**
* 获取询盘转发详情列表
* @param $map
* @param $page
* @param $row
* @param string $order
* @param string[] $filed
* @return array
* @author Akun
* @date 2025/03/01 9:22
*/
public function getInquiryDetailLists($map, $page, $row, $order = 'id', $filed = ['*'])
{
$model = new InquiryRelayDetail();
$lists = $model->listsWith($map, $page, $row, $order, $filed, 'desc', ['detailLog']);
return $this->success($lists);
}
/**
* 停止转发
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2025/03/10 11:00
*/
public function stopInquiry()
{
$model = new InquiryRelayDetailLog();
$rs = $model->edit(['status' => InquiryRelayDetailLog::STATUS_STOP], ['detail_id' => $this->param['id'], 'status' => InquiryRelayDetailLog::STATUS_INIT]);
if ($rs === false) {
$this->fail('设置失败');
}
return $this->success();
}
/**
* 获取询盘统计数据
* @return array
* @author Akun
* @date 2025/04/14 11:42
*/
public function inquiryCount()
{
$start_date = isset($this->param['start_date']) && $this->param['start_date'] ? $this->param['start_date'] : date('Y-m-d', strtotime('-3 month'));
$end_date = isset($this->param['end_date']) && $this->param['end_date'] ? $this->param['end_date'] : date('Y-m-d');
//选择的时间跨度大于三个月,以开始时间为基准,只查询三个月的数据
$start_date_3_moth = date('Y-m-d', strtotime($start_date . ' + 3 month'));
if ($end_date > $start_date_3_moth) {
$end_date = $start_date_3_moth;
}
//构建初始化数据
$data = [];
$s_date = $start_date;
while ($s_date <= $end_date) {
$data[$s_date] = [
'total' => 0,
'invalid' => 0,
'init' => 0,
'finish' => 0
];
$s_date = date('Y-m-d', strtotime($s_date . ' + 1 day'));
}
//统计数据赋值
$result = $this->model->list(['inquiry_date' => ['between', [$start_date . ' 00:00:00', $end_date . ' 23:59:59']]], 'id', ['status', 'inquiry_date'], 'asc');
foreach ($result as $value) {
$inquiry_date = substr($value['inquiry_date'], 0, 10);
$data[$inquiry_date]['total'] += 1;
if ($value['status'] == InquiryInfo::STATUS_INVALID) {
$data[$inquiry_date]['invalid'] += 1;
} elseif ($value['status'] == InquiryInfo::STATUS_INIT) {
$data[$inquiry_date]['init'] += 1;
} else {
$data[$inquiry_date]['finish'] += 1;
}
}
return $this->success($data);
}
/**
* 根据转发人统计询盘数量
* @return array
* @author Akun
* @date 2025/10/28 15:02
*/
public function inquiryManageCount()
{
$data = Cache::get('inquiry_manage_count');
if (!$data) {
$manageModel = new Manage();
//月统计
$last_year_month = date('Y-m', strtotime('-11 months'));
$month_manage_ids = ForwardCount::select('manage_id')->where('created_at', '>', $last_year_month . '-02')->where('count', '>', 0)->orderBy('manage_id', 'asc')->distinct()->pluck('manage_id')->toArray();
$data_month = [];
$data_month_total = [];
$now_month = date('Y-m');
while ($last_year_month <= $now_month) {
$month_arr = explode('-', $last_year_month);
$year = $month_arr[0];
$month = $month_arr[1];
foreach ($month_manage_ids as $month_mid) {
$name = $manageModel->getName($month_mid);
$month_count = intval(ForwardCount::where('manage_id', $month_mid)->where('year', $year)->where('month', $month)->sum('count') ?? 0);
$data_month_total[$name] = ($data_month_total[$name] ?? 0) + $month_count;
$data_month[$last_year_month][$name] = $month_count;
}
$last_year_month = date('Y-m', strtotime($last_year_month . ' +1 month'));
}
$data_month['total'] = $data_month_total;
//周统计
$last_week_day = date('Y-m-d', strtotime('-1 week'));
$week_manage_ids = ForwardCount::select('manage_id')->where('created_at', '>', $last_week_day . ' +1 day')->where('count', '>', 0)->orderBy('manage_id', 'asc')->distinct()->pluck('manage_id')->toArray();
$data_week = [];
$data_week_total = [];
$now_day = date('Y-m-d');
while ($last_week_day < $now_day) {
$day_arr = explode('-', $last_week_day);
$year = $day_arr[0];
$month = $day_arr[1];
$day = $day_arr[2];
foreach ($week_manage_ids as $week_mid) {
$name = $manageModel->getName($week_mid);
$day_count = ForwardCount::where('manage_id', $week_mid)->where('year', $year)->where('month', $month)->where('day', $day)->value('count') ?? 0;
$data_week_total[$name] = ($data_week_total[$name] ?? 0) + $day_count;
$data_week[substr($last_week_day, 5)][$name] = $day_count;
}
$last_week_day = date('Y-m-d', strtotime($last_week_day . ' +1 day'));
}
$data_week['total'] = $data_week_total;
$data = ['data_month' => $data_month, 'data_week' => $data_week];
Cache::add('inquiry_manage_count', $data);
}
return $this->success($data);
}
}