MessagePush.php
3.4 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
<?php
namespace App\Models\Workchat;
use App\Models\Base;
use App\Models\Inquiry\InquiryFormData;
use App\Models\ProjectAssociation\ProjectAssociation;
use App\Services\ProjectServer;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class MessagePush
* @package App\Models\Workchat
* @author zbj
* @date 2024/9/7
*/
class MessagePush extends Base
{
use SoftDeletes;
const STATUS_PENDING = 0;
const STATUS_SUCCESS = 1;
const STATUS_ERROR = 9;
const TYPE_INQUIRY = 'inquiry';
//设置关联表名
/**
* @var mixed
*/
protected $table = 'gl_workchat_message_push';
/**
* @param $id
* @param $project_id
* @param $country
* @param $name
* @param $submit_at
* @return bool
*/
public static function addInquiryMessage($id, $project_id, $country, $name, $submit_at){
if(!ProjectServer::useProject($project_id)){
return false;
}
//项目是否有绑定群
$friend_id = ProjectAssociation::where('project_id', $project_id)
->where('status', ProjectAssociation::STATUS_NORMAL)
->where('binding_app', ProjectAssociation::ENTERPRISE_WECHAT)
->value('friend_id');
if(!$friend_id){
return false;
}
//9-21 点,每条消息及时通知
//21-第二天 9 点,整合一起通知
$hour = date('H', strtotime($submit_at));
if($hour >= 9 && $hour < 21) {
$model = new self();
$model->project_id = $project_id;
$model->friend_id = $friend_id;
$model->type = self::TYPE_INQUIRY;
$model->ref_ids = $id;
$model->content = '[' . date('H:i', strtotime($submit_at)) . '] 您的全球搜网站收到来自【' . $country . $name . '】的询盘信息,请登录后台或APP进行查看!';
}else{
//定时发送时间
$send_time = $hour >= 9 ? date('Y-m-d 09:00:00', strtotime('+1 day')) : date('Y-m-d 09:00:00');
$model = self::where('project_id', $project_id)->where('type', self::TYPE_INQUIRY)->where('send_time', $send_time)->first();
if(!$model){
$model = new self();
$model->project_id = $project_id;
$model->friend_id = $friend_id;
$model->type = self::TYPE_INQUIRY;
$model->ref_ids = $id;
$model->send_time = $send_time;
$model->content = '[09:00] 您的全球搜网站收到来自【' . $country . $name . '】的询盘信息,请登录后台或APP进行查看!';
}else{
$ref_ids = explode(',', $model->ref_ids);
$ref_ids[] = $id;
$model->ref_ids = implode(',', $ref_ids);
$countries = InquiryFormData::whereIn('id', $ref_ids)->pluck('country')->toArray();
$count = count($countries);
$countries = array_unique($countries);
if(count($countries) > 3){
$country = implode(',', array_slice($countries, 0, 3)) . '...';
}else{
$country = implode(',', $countries);
}
$model->content = '[09:00] 您的全球搜网站收到来自【' . $country . '】'.$count.'条询盘信息,请登录后台或APP进行查看!';
}
}
$model->save();
}
}