Ranking.php
12.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
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
<?php
namespace app\admin\controller\facrm\analysis;
use app\admin\model\AuthGroup;
use app\common\controller\Backend;
use fast\Tree;
use think\Db;
/**
*排行榜
*/
class Ranking extends Backend
{
protected $model = null;
protected $searchFields = 'id,username,nickname';
protected $groupList = [];
public function _initialize()
{
parent::_initialize();
$this->model = model('Admin');
$this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
$this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
$this->groupList = $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
Tree::instance()->init($groupList);
$groupdata = [];
if ($this->auth->isSuperAdmin()) {
$result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
foreach ($result as $k => $v) {
$groupdata[$v['id']] = $v['name'];
}
} else {
$result = [];
$groups = $this->auth->getGroups();
foreach ($groups as $m => $n) {
$childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
foreach ($childlist as $k => $v) {
$groupdata[$v['id']] = $v['name'];
}
}
}
$this->view->assign('groupdata', $groupdata);
$this->assignconfig('groupdata', $groupdata);
$this->assignconfig("admin", ['id' => $this->auth->id]);
}
/**
* 合同排行
*/
public function contract()
{
if ($this->request->isAjax()) {
$contractModel = new \app\admin\model\facrm\Contract();
$filter = $this->request->get("filter", '');
$filter = (array)json_decode($filter, true);
$filter_c = [];
if (isset($filter['search_time'])) {
$filter['search_time'] = str_replace(' - ', ',', $filter['search_time']);
$arr = array_slice(explode(',', $filter['search_time']), 0, 2);
if (count($arr)<2)$this->error(__("时间参数有误"));
$filter_c['order_time'] = ['between time', $arr];
}
$childrenAdminIds =$this->getChildrenAdminIds($filter);
if ($childrenAdminIds){
$contractModel->where('order_admin_id', 'in', $childrenAdminIds);
}
$order = "count";
if (isset($filter['type_id'])) {
switch ($filter['type_id']) {
case 1:
$order = "count";
break;
case 2:
$order = "money";
break;
}
}
$lists = $contractModel->where('check_status', 2)->where($filter_c)
->field('COUNT(*) AS count, sum(money) as money,order_admin_id')
->group('order_admin_id')->limit(0, 10)->order($order, 'desc')->fetchSql(false)
->select();
$j = 1;
foreach ($lists as $row) {
// $row->orderAdmin;
if ($row->orderAdmin){
$row->orderAdmin->hidden(['password', 'token', 'salt']);
}
$row['index'] = $j;
$j++;
}
$result = array("total" => count($lists), "rows" => $lists);
return json($result);
}
return $this->view->fetch();
}
/**
*回款排行
*/
public function receivables()
{
if ($this->request->isAjax()) {
$receivablesModel = new \app\admin\model\facrm\contract\Receivables();
$filter = $this->request->get("filter", '');
$filter = (array)json_decode($filter, true);
$filter_c = [];
if (isset($filter['search_time'])) {
$filter['search_time'] = str_replace(' - ', ',', $filter['search_time']);
$arr = array_slice(explode(',', $filter['search_time']), 0, 2);
if (count($arr)<2)$this->error(__("时间参数有误"));
$filter_c['return_time'] = ['between time', $arr];
}
$childrenAdminIds =$this->getChildrenAdminIds($filter);
if ($childrenAdminIds){
$receivablesModel->where('order_admin_id', 'in', $childrenAdminIds);
}
$order = "money";
if (isset($filter['type_id'])) {
switch ($filter['type_id']) {
case 1:
$order = "count";
break;
case 2:
$order = "money";
break;
}
}
$lists = $receivablesModel->where('check_status', 2)->where($filter_c)
->field('COUNT(*) AS count, sum(money) as money,order_admin_id')
->group('order_admin_id')->limit(0, 10)->order($order, 'desc')->fetchSql(false)
->select();
$j = 1;
foreach ($lists as $row) {
if ($row->orderAdmin){
$row->orderAdmin->hidden(['password', 'token', 'salt']);
}
$row['index'] = $j;
$j++;
}
$result = array("total" => count($lists), "rows" => $lists);
return json($result);
}
return $this->view->fetch('contract');
}
/**
* 新客排名
* @return string|\think\response\Json|void
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function addcustomer()
{
if ($this->request->isAjax()) {
$customerModel = new \app\admin\model\facrm\Customer();
$filter = $this->request->get("filter", '');
$filter = (array)json_decode($filter, true);
$filter_c = [];
if (isset($filter['search_time'])) {
$filter['search_time'] = str_replace(' - ', ',', $filter['search_time']);
$arr = array_slice(explode(',', $filter['search_time']), 0, 2);
if (count($arr)<2)$this->error(__("时间参数有误"));
$filter_c['create_time'] = ['between time', $arr];
}
$childrenAdminIds =$this->getChildrenAdminIds($filter);
if ($childrenAdminIds){
$customerModel->where('create_user_id', 'in', $childrenAdminIds);
}
$order = "count";
$lists = $customerModel->where($filter_c)
->field('COUNT(*) AS count,create_user_id')
->group('create_user_id')->limit(0, 10)->order($order, 'desc')->fetchSql(false)
->select();
$j = 1;
foreach ($lists as $row) {
// $row->orderAdmin;
$row->createUser;
$row['index'] = $j;
$j++;
}
$result = array("total" => count($lists), "rows" => $lists);
return json($result);
}
return $this->view->fetch('contract');
}
/**
* 跟进排名
*/
public function record()
{
if ($this->request->isAjax()) {
$recordModel = new \app\admin\model\facrm\Record();
$filter = $this->request->get("filter", '');
$filter = (array)json_decode($filter, true);
$filter_c = [];
if (isset($filter['search_time'])) {
$filter['search_time'] = str_replace(' - ', ',', $filter['search_time']);
$arr = array_slice(explode(',', $filter['search_time']), 0, 2);
if (count($arr)<2)$this->error(__("时间参数有误"));
$filter_c['create_time'] = ['between time', $arr];
}
$childrenAdminIds =$this->getChildrenAdminIds($filter);
if ($childrenAdminIds){
$recordModel->where('create_user_id', 'in', $childrenAdminIds);
}
$order = "count";
$filter['type_id'] = isset($filter['type_id']) ? $filter['type_id'] : 1;
switch ($filter['type_id']) {
case 1:
$order = "count";//跟进次数
break;
case 2:
$order = "total";//客户数量select create_user_id, count(distinct types_id) as count,count(*) as aa from fa_facrm_record group by create_user_id ORDER BY count desc LIMIT 0,10
break;
default:
$this->error(__("访问有误"));
}
$lists = $recordModel->where('types', "customer")->where($filter_c)
->field("count(distinct types_id) AS total,create_user_id,types_id,count(*) as count")
->group('create_user_id')->limit(0, 10)->order($order, 'desc')->fetchSql(false)
->select();
$j = 1;
foreach ($lists as $row) {
if ($row->createUser){
$row->createUser->hidden(['password', 'token', 'salt']);
}
$row['index'] = $j;
$j++;
}
$result = array("total" => count($lists), "rows" => $lists);
return json($result);
}
return $this->view->fetch('contract');
}
/**
* 产品排名
* @return \think\response\Json|void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function product()
{
if ($this->request->isAjax()) {
$productModel = new \app\admin\model\facrm\contract\Product();
$productModel->alias('p')->join('facrm_contract c','c.id=p.contract_id');
$filter = $this->request->get("filter", '');
$filter = (array)json_decode($filter, true);
$filter_c = [];
if (isset($filter['search_time'])) {
$filter['search_time'] = str_replace(' - ', ',', $filter['search_time']);
$arr = array_slice(explode(',', $filter['search_time']), 0, 2);
if (count($arr)<2)$this->error(__("时间参数有误"));
$start_time=strtotime($arr[0]);
$end_time=strtotime($arr[1]);
$productModel->where("c.order_time BETWEEN {$start_time} AND {$end_time}");
}
$childrenAdminIds =$this->getChildrenAdminIds($filter);
if ($childrenAdminIds){
$productModel->where('order_admin_id', 'in', $childrenAdminIds);
}
$order = "count";
$filter['type_id'] = isset($filter['type_id']) ? $filter['type_id'] : 1;
switch ($filter['type_id']) {
case 1:
$order = "count";//销售数量
break;
case 2:
$order = "total";//销售金额
break;
default:
$this->error(__("访问有误"));
}
$lists = $productModel->where('check_status',2)
->field("count(*) as count,sum(subtotal) as total,product_id,p.name,product_id")
->limit(0, 10)->order($order, 'desc')->fetchSql(false)->group('product_id')
->select();
$j = 1;
foreach ($lists as $row) {
$row['index'] = $j;
$j++;
}
$result = array("total" => count($lists), "rows" => $lists);
return json($result);
}
return $this->view->fetch('contract');
}
protected function getChildrenAdminIds($filter)
{
$childrenAdminIds = [];
if (isset($filter['group_id'])) {
$childrenAdminIds = [];
if ($filter['group_id'] > 0) {
Tree::instance()->init($this->groupList);
$groupIds = Tree::instance()->getChildrenIds($filter['group_id'], true);
$authGroupList = \app\admin\model\AuthGroupAccess::
field('uid,group_id')
->where('group_id', 'in', $groupIds)
->select();
foreach ($authGroupList as $k => $v) {
if (in_array($v['uid'], $childrenAdminIds)) continue;
$childrenAdminIds[] = $v['uid'];
}
if (!$childrenAdminIds) {
$this->error("该组下面没有成员");
}
}
}
return $childrenAdminIds;
}
}