AsideTicketController.php
17.3 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
<?php
namespace App\Http\Controllers\Aside\WorkOrder;
use App\Enums\Common\Code;
use App\Http\Controllers\Aside\BaseController;
use App\Http\Requests\Aside\WorkOrder\AsideTicketStoreRequest;
use App\Http\Requests\Aside\WorkOrder\AsideTicketListRequest;
use App\Http\Requests\Aside\WorkOrder\AsideTicketUpdateRequest;
use App\Http\Requests\Aside\WorkOrder\TicketProjectListRequest;
use App\Models\Manage\Manage;
use App\Models\Manage\ManageHr;
use App\Models\Ticket\TicketUploadData;
use App\Models\WorkOrder\TicketLog;
use App\Models\WorkOrder\TicketProject;
use App\Models\WorkOrder\Tickets;
use Illuminate\Support\Facades\DB;
class AsideTicketController extends BaseController
{
/**
* A端查看所有工单
*
* @return \Illuminate\Http\Response
*/
public function index(AsideTicketListRequest $request)
{
$validated = $request->validated();
$query = Tickets::with([
'logs.engineer',
'project.pm',
'project.projectV6',
])
->when(!empty($validated['engineer_id']), function ($query) use ($validated) {
// 查 gl_tickets 表 submit_user_id 或 gl_ticket_logs 表 engineer_id
$engineerId = $validated['engineer_id'];
return $query->where(function ($q) use ($engineerId) {
$q->where('submit_user_id', $engineerId)
->orWhereHas('logs', function ($q1) use ($engineerId) {
$q1->where('engineer_id', $engineerId);
});
});
})
->when($request->input('project_id') !== null, function ($query) use ($request) {
// project_id 查 gl_ticket_projects.uuid
$projectId = $request->input('project_id');
return $query->whereHas('project', function ($q) use ($projectId) {
$q->where('uuid', $projectId);
});
})
->when($request->input('status') !== null, function ($query) use ($request) {
// status 查 gl_tickets.status
$status = $request->input('status');
if($status == 10){
$newTime = date("Y-m-d H:i:s", strtotime("-120 hours"));
return $query->where('status', '!=' ,Tickets::STATUS_COMPLETED)->where('plan_end_at','<',$newTime);//超过120个小时未处理的工单
}else{
return $query->where('status', $status);
}
})
->when($request->input('timeout') !== null, function ($query) use ($request) {
// status 查 gl_tickets.status
$timeout = $request->input('timeout');
switch ($timeout) {
case 1:
$newTime = date("Y-m-d H:i:s", strtotime("-24 hours"));
break;
case 2:
$newTime = date("Y-m-d H:i:s", strtotime("-48 hours"));
break;
case 3:
$newTime = date("Y-m-d H:i:s", strtotime("-72 hours"));
break;
default:
$newTime = date("Y-m-d H:i:s");
break;
}
return $query->where('status', 0)->where('plan_end_at','<',$newTime);//超过120个小时未处理的工单
})
->when($request->input('star') !== null, function ($query) use ($request) {
$star = $request->input('star');
return $query->where('star', $star);
})
->when($request->input('search'), function ($query) use ($request) {
// search 查 gl_tickets.title 或 gl_ticket_projects.title 或 gl_ticket_projects.company_name
$search = $request->input('search');
return $query->where(function ($q) use ($search) {
$q->where('title', 'like', '%' . $search . '%')
->orWhereHas('project', function ($q1) use ($search) {
$q1->where('title', 'like', '%' . $search . '%')
->orWhere('company_name', 'like', '%' . $search . '%');
});
});
})
// 根据TicketProject的status字段筛选
->when($request->input('project_status') !== null, function ($query) use ($request) {
$projectStatus = $request->input('project_status');
return $query->whereHas('project', function ($q) use ($projectStatus) {
$q->where('status', $projectStatus);
});
})
// 根据TicketProject的project_cate字段筛选
->when($request->input('project_cate') !== null, function ($query) use ($request) {
$projectCate = $request->input('project_cate');
return $query->whereHas('project', function ($q) use ($projectCate) {
$q->where('project_cate', $projectCate);
});
});
//TODO::用户部门搜索
if(isset($this->param['dept_id']) && !empty($this->param['dept_id'])){
$manageHrModel = new ManageHr();
//售后优化+技术搜索
if($this->param['dept_id'] == 2 && !empty($this->param['entry_position'])) {//售后部
if ($this->param['entry_position'] == 1) {
$manageIdArr = $manageHrModel->selectField(['entry_position' => ['in', [44, 46, 49]], 'status' => 1], 'manage_id');
} else {
//售后技术
$manageIdArr = $manageHrModel->selectField(['entry_position' => ['in', [42, 43, 45, 48, 51]], 'status' => 1], 'manage_id');
}
$query->whereHas('logs', function ($q) use ($manageIdArr) {
$q->whereIn('engineer_id', $manageIdArr)->where('is_engineer',1);
});
}else{
$manageIdArr = $manageHrModel->selectField(['dept_id'=>$this->param['dept_id'],'status'=>1],'manage_id');
$query->whereHas('logs', function ($q) use ($manageIdArr) {
$q->whereIn('engineer_id', $manageIdArr);
});
}
}
//待审核项目列表
if(isset($this->param['pending_review']) && $this->param['pending_review'] == 1){
$ticketUploadModel = new TicketUploadData();
$projectIds = $ticketUploadModel->where('status', 0)->distinct('project_id')->pluck('project_id')->toArray();
return $query->whereHas('project', function ($q) use ($projectIds) {
$q->whereIn('post_id', $projectIds);
});
}
if(!empty($this->param['start_at']) && !empty($this->param['end_at'])){
$query->whereBetween('created_at',[$this->param['start_at'],$this->param['end_at']]);
}
//搜索提交人姓名
if(!empty($this->param['submit_username'])){
$query->where('submit_username','like','%'.$this->param['submit_username'].'%');
}
// 添加排序功能
$query->orderBy('status', 'asc');
// $query->orderBy('id', 'desc');
$sortField = $request->input('sort_field', 'plan_end_at');
$sortOrder = strtolower($request->input('sort_order', 'asc'));
$query->orderBy($sortField, $sortOrder);
if ($sortField != 'plan_end_at') $query->orderBy('plan_end_at', 'asc');
$lists = $query->paginate($this->row, ['*'], 'page', $this->page)->toArray();
if(!empty($lists) && !empty($lists['list'])){
$manageModel = new Manage();
foreach ($lists['list'] as $key => $item){
if($item['project']['version'] != 5){
$item['project']['engineer_id_name'] = $manageModel->getName($item['project']['engineer_id'] ?? 0);
}
//自动登录v6
if($item['project']['project_cate'] == 2){
$item['autologin_code'] = getAutoLoginCode($item['project']['table_id']);
}
//计算超时多少个小时
if($item['status'] == 0){
$end = date('Y-m-d H:i:s');
$start = $item['plan_end_at'];
if($start < $end){
$item['plan_ent_time'] = diffInHours($start,$end);
}
}
$lists['list'][$key] = $item;
}
}
$this->response('success', Code::SUCCESS, $lists);
}
/**
* @param $search
* @return void
* V5V6所有项目
*/
public function getProjects($search)
{
$projects = TicketProject::where(function ($query) use ($search) {
// 查找项目名称或公司名称
$query->where('title', 'like', '%' . $search . '%')
->orWhere('company_name', 'like', '%' . $search . '%');
})
->get();
$this->response('success', Code::SUCCESS, $projects);
}
/**
* @param TicketProjectListRequest $request
* @return void
* A端V5V6项目列表
*/
public function projectList(TicketProjectListRequest $request)
{
$validated = $request->validated();
$dept_id = ManageHr::where('manage_id', $this->manage['id'])
->value('dept_id');
$lists = TicketProject::when(($this->manage['role'] != 1 && $dept_id != 5), function ($query) use ($dept_id) {
/**
* 超管看所有项目 $this->manage['role']=1
* 全球搜: 技术部ID 1、售后部ID 2
* 超迹AI: AICC技术部 ID 4
* 域途:域途运营部 ID 17
*/
if (in_array($dept_id, [1, 2])) {
// V5 V6
return $query->whereIn('project_cate', [1, 2]);
}elseif ($dept_id == 4) {
// 超迹AI
return $query->where('project_cate', 3);
}elseif ($dept_id == 17){
// 域途
return $query->where('project_cate', 4);
}else{
// 其他部门,不允许看数据
return $query->where('id', 0); // 返回空结果
}
})
->when(!empty($validated['search']), function ($query) use ($validated) {
// 查找项目名称或公司名称
$search = $validated['search'];
return $query->where(function ($q) use ($search) {
$q->where('title', 'like', '%' . $search . '%')
->orWhere('company_name', 'like', '%' . $search . '%');
});
})
->when(!empty($validated['version']), function ($query) use ($validated) {
// 版本号筛选
$version = $validated['version'];
return $query->where('version', $version);
})
->when(!empty($validated['project_cate']), function ($query) use ($validated) {
// 版本号筛选
$project_cate = $validated['project_cate'];
return $query->where('project_cate', $project_cate);
})
->paginate($this->row, ['*'], 'page', $this->page);
$this->response('success', Code::SUCCESS, $lists);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(AsideTicketStoreRequest $request)
{
$request->validated();
$project = TicketProject::where('uuid', $request->input('project_id'))->first();
if ($project->is_del) $this->response('该项目状态异常', Code::USER_MODEL_NOTFOUND_ERROE);
$result = DB::transaction(function () use ($request, $project) {
$ticket = new Tickets();
$ticket->project_id = $project->id;
$ticket->title = $request->input('title');
$ticket->content = $request->input('content');
// $files = [NULL]
$files = $request->input('files');
if (empty($files) || (is_array($files) && count(array_filter($files, function($v){ return !is_null($v); })) === 0)) {
$ticket->files = null;
} else {
$ticket->files = json_encode($files);
}
$ticket->submit_side = 1; // 1 for A-side submission
$ticket->submit_user_id = $this->manage['id'];
$ticket->submit_username = $this->manage['name'];
$ticket->star = $request->input('star', 3);
$ticket->plan_end_at = $request->input('plan_end_at', null);
$ticket->close_wechat = $request->input('close_wechat', false);
$ticket->num = $request->input('num', 0);
$ticket->save();
// 分配工单参与人
$ticket->saveEngineers($request->input('engineer_ids', []));
$nickname = ManageHr::where('manage_id', $this->manage['id'])->value('nickname') ?? mb_substr($ticket->submit_username, 0, 1) . '**';
if ($project->wechat_switch && !$ticket->close_wechat)
$project->pushWechatGroupMsg("创贸({$nickname})新增了工单(ID:{$ticket->id}),请及时处理!");
return $ticket;
});
$this->response('success', Code::SUCCESS, $result->toArray());
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$ticket = Tickets::with([
'logs.engineer',
'project.projectV6:id,company,title',
])->find($id);
if (!$ticket) $this->response('工单不存在', Code::USER_MODEL_NOTFOUND_ERROE);
$this->response('success', Code::SUCCESS, $ticket->toArray());
}
/**
* A端负责人修改工单
* 1. 邀请同事
* 2. 修改工单状态
*/
public function update(AsideTicketUpdateRequest $request, $id)
{
$request->validated();
$ticket = Tickets::find($id);
if (!$ticket) $this->response('工单不存在', Code::USER_MODEL_NOTFOUND_ERROE);
// 开始修改
$result = DB::transaction(function () use ($request, $ticket) {
if ($request->input('engineer_ids'))
$ticket->saveEngineers($request->input('engineer_ids'));
// 其他字段有提交数据才修改,比如star plan_end_at
if ($request->input('title'))
$ticket->title = $request->input('title');
if ($request->input('content'))
$ticket->content = $request->input('content');
if ($request->input('star'))
$ticket->star = $request->input('star');
if ($request->input('plan_end_at'))
$ticket->plan_end_at = $request->input('plan_end_at');
if ($request->input('status'))
$ticket->status = $request->input('status');
if ($request->input('num'))
$ticket->num = $request->input('num',0);
if ($request->input('files')){
$ticket->files = json_encode($request->input('files',[]),true);
}
//同步更改工单时间
$ticket->logs()->where('status', '<', TicketLog::STATUS_COMPLETED)->where('is_engineer', 1)->update(['plan_end_at' => $ticket->plan_end_at]);
if ($ticket->status == Tickets::STATUS_COMPLETED)
{
// 完成工单,把子任务里面未完成的工单改为完成
$ticket->end_at = now();
$ticket->end_time = diffInHours($ticket->created_at,now());
$ticket->logs()->where('status', '<', TicketLog::STATUS_COMPLETED)->where('is_engineer', 1)
->update(['status' => TicketLog::STATUS_COMPLETED, 'end_at' => now(),'end_time'=>$ticket->end_time]);
// 推动微信通知
$project = $ticket->project;
if ($project->wechat_switch && !$ticket->close_wechat)
$project->pushWechatGroupMsg("工单(ID:{$ticket->id})已全部完成,请访问查看详情!");
$ticket->pushDing('finish');
}
$ticket->save();
return $ticket;
});
$this->response('success', Code::SUCCESS, $result->toArray());
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
/**
* 手动触发,推送工单到企微群
*/
public function pushNotify($id)
{
$project = TicketProject::where('uuid', $id)
->where('is_del', 0)
->first();
if (empty($project))
$this->response('工单项目不存在', Code::USER_MODEL_NOTFOUND_ERROE);
if (empty($project->wechat_group_id)) {
$this->response('该工单没有绑定的企微群', Code::USER_MODEL_NOTFOUND_ERROE);
}
if ($project->wechat_switch)
$project->pushWechatGroupMsg();
else
$this->response('请先点击开关,打开微信推送', Code::USER_MODEL_NOTFOUND_ERROE);
$this->response('success', Code::SUCCESS);
}
}