AsideTicketController.php
7.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
<?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\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();
$lists = Tickets::with([
'logs.engineer:id,name',
'project',
])
->when($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');
return $query->where('status', $status);
})
->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 . '%');
});
});
})
->orderBy('id', 'desc')
->paginate($this->row, ['*'], 'page', $this->page);
$this->response('success', Code::SUCCESS, $lists);
}
/**
* @param $search
* @return void
* V5V6所有项目
*/
public function getProjects($search)
{
$projects = TicketProject::where('is_del', 0)
->where(function ($query) use ($search) {
// 查找项目名称或公司名称
$query->where('title', 'like', '%' . $search . '%')
->orWhere('company_name', 'like', '%' . $search . '%');
})
->get();
$this->response('success', Code::SUCCESS, $projects);
}
/**
* 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->save();
// A 端提工单,都是针对客户提的需求等开发任务;比如翻译,修改页面等。。。
foreach ($request->input('engineer_ids', []) as $engineer_id) {
$log = new TicketLog();
$log->engineer_id = $engineer_id;
$ticket->logs()->save($log);
}
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'))
{
// 有邀请工程师协同处理
foreach ($request->input('engineer_ids') as $engineer_id)
{
try {
// 利用唯一索引去重
$new_log = new TicketLog();
$new_log->engineer_id = $engineer_id;
$ticket->logs()->save($new_log);
}catch (\Exception $exception){}
}
}
$ticket->reply = $request->input('reply', null);
$ticket->status = $request->input('status', $ticket->status);
if ($ticket->status == Tickets::STATUS_COMPLETED)
{
// 完成工单,把子任务里面未完成的工单改为完成
$ticket->end_at = now();
$ticket->logs()->where('status', '<', TicketLog::STATUS_COMPLETED)
->update(['status' => TicketLog::STATUS_COMPLETED, 'end_at' => now()]);
}
$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)
{
//
}
}