Record.php
5.6 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
<?php
namespace app\admin\model\facrm;
use think\Db;
use think\Model;
use traits\model\SoftDelete;
class Record extends Model
{
use SoftDelete;
// 表名
protected $name = 'facrm_record';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
protected $deleteTime = 'delete_time';
/**
* 创建者
*/
public function createUser()
{
return $this->hasOne('\app\admin\model\Admin', 'id', 'create_user_id');
}
/**
* 客户对象
* @return \think\model\relation\BelongsTo
*/
public function customer()
{
return $this->belongsTo('\app\admin\model\facrm\Customer', 'types_id', 'id');
}
/**
* 线索对象
* @return \think\model\relation\BelongsTo
*/
public function clues()
{
return $this->belongsTo('\app\admin\model\facrm\Clues', 'types_id', 'id');
}
/**
* 商机需求对象
* @return \think\model\relation\BelongsTo
*/
public function business()
{
return $this->belongsTo('\app\admin\model\facrm\Business', 'types_id', 'id');
}
/**
* 附件
* @return \think\model\relation\HasMany
*/
public function files(){
return $this->hasMany('\app\admin\model\facrm\record\Files','record_id','id');
}
/**
* 跟进趋势Echart
* @return bool
*/
public function getRecordEchartData($startDate,$endDate,$owner_user_ids,$types="customer")
{
$recordModel = $this;
// 生成查询的开始和结束时间,默认取30日
!is_numeric($startDate) && $starttime = strtotime($startDate);
!is_numeric($endDate) && $endtime = strtotime($endDate);
$isnotrangeDate = empty($starttime) && empty($endtime);
$nearly = '30';
if ($isnotrangeDate) {
$endtime = time();
$nearly -= 1;
$starttime = strtotime("-{$nearly} day"); // 最近30天日期
} elseif ($starttime > $endtime) {
$this->error = '起始时间要小于终止时间';
return false;
}
$totalseconds = $endtime - $starttime;;
if ($totalseconds > 86400 * 30 * 2) {
$format = '%Y-%m';
} else {
if ($totalseconds > 86400) {
$format = '%Y-%m-%d';
} else {
$format = '%H:00';
}
}
if ($owner_user_ids&&is_numeric($owner_user_ids)){
$recordModel->where('create_user_id', $owner_user_ids);
}elseif($owner_user_ids&&is_array($owner_user_ids)){
$recordModel->where('create_user_id','in', $owner_user_ids);
}
//跟进次数
$lists = $recordModel->where('types', $types)->where('create_time', 'between time', [$starttime, $endtime])
->field('COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(create_time), "' . $format . '") AS add_date')
->group('add_date')
->select();
//客户数量
if ($owner_user_ids&&is_numeric($owner_user_ids)){
$recordModel->where('create_user_id', $owner_user_ids);
}elseif($owner_user_ids&&is_array($owner_user_ids)){
$recordModel->where('create_user_id','in', $owner_user_ids);
}
$tempsql = $recordModel->field('max(id) id,types,max(create_time) create_time,types_id')->where('types', $types)->where('create_time', 'between time', [$starttime, $endtime])->group('types_id')->buildSql();
$listsd = Db::query("SELECT COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(create_time), \"%Y-%m-%d\") AS add_date FROM ($tempsql) as r GROUP BY `add_date`");
if ($totalseconds > 84600 * 30 * 2) {
$starttime = strtotime('last month', $starttime);
while (($starttime = strtotime('next month', $starttime)) <= $endtime) {
$column[] = date('Y-m', $starttime);
}
} else {
if ($totalseconds > 86400) {
for ($time = $starttime; $time <= $endtime;) {
$column[] = date("Y-m-d", $time);
$time += 86400;
}
} else {
for ($time = $starttime; $time <= $endtime;) {
$column[] = date("H:00", $time);
$time += 3600;
}
}
}
$c_count = $d_count = array_fill_keys($column, 0);
foreach ($lists as $k => $v) {
$c_count[$v['add_date']] = $v['nums'];//
}
foreach ($listsd as $k => $v) {
$d_count[$v['add_date']] = $v['nums'];
}
$result = [
'date' => array_keys($c_count),
'd_count' => array_values($d_count),
'c_count' => array_values($c_count),
];
$data = [
'date' => $result['date'],
'data' => [
"跟进客户" => $result['d_count'],
"跟进次数" => $result['c_count'],
],
];
return $data;
}
/**
* 获取跟进类型文本
* @param $value
* @param $data
* @return string
*/
public function getRecordTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['record_type']) ? $data['record_type'] : '');
$list = $this->getRecordTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
/**
* 获取跟进类型
* @ApiInternal
*/
public function getRecordTypeList()
{
$config = get_addon_config('facrm');
return $config['record_type'];
}
}