AsideTicketController.php
8.9 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
<?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
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(AsideTicketListRequest $request)
{
/**
* 1. 超管看所有工单
* 2. 其他查看和自己有关的工单
*/
$lists = TicketLog::with([
'ticket.project.projectV6:id,company,title',
'ticket.logs.engineer:id,name',
])
->when($this, function ($query) {
$role = $this->manage['role'];
// 超管 role = 1
if ($role == 1) {
return $query;
}
// 其他角色查自己参与的工单
return $query->where('engineer_id', $this->manage['id']);
})
->when($request->input('project_id') !== null, function ($query) use ($request) {
// project_id 查 gl_ticket_projects.uuid
return $query->whereHas('ticket.project', function ($q) use ($request) {
$q->where('uuid', $request->input('project_id'));
});
})
->when($request->input('status') !== null, function ($query) use ($request) {
// status 查 gl_tickets.status
return $query->whereHas('ticket', function ($q) use ($request) {
$q->where('status', $request->input('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');
$query->where(function ($q) use ($search) {
$q->whereHas('ticket', function ($q1) use ($search) {
$q1->where('title', 'like', '%' . $search . '%');
})
->orWhereHas('ticket.project', function ($q2) use ($search) {
$q2->where('title', 'like', '%' . $search . '%')
->orWhere('company_name', 'like', '%' . $search . '%');
});
});
})
->orderBy('id', 'desc')
->paginate($this->row, ['*'], 'page', $this->page);
$this->response('success', Code::SUCCESS, $lists);
}
public function getProjects($search)
{
$projects = TicketProject::with([
'projectV6:id,company,title',
])
->where(function ($query) use ($search) {
$query->where('title', 'like', '%' . $search . '%')
->orWhere('company_name', 'like', '%' . $search . '%')
->orWhereHas('projectV6', function ($q) use ($search) {
$q->where('company', 'like', '%' . $search . '%')
->orWhere('title', '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->version == 6){
if ($project->projectV6->delete_status) $this->response('该项目已被删除', Code::USER_MODEL_NOTFOUND_ERROE);
if ($project->projectV6->extend_type == 5) $this->response('未续费', Code::USER_MODEL_NOTFOUND_ERROE);
if ($project->projectV6->type == 8) $this->response('项目已归档', Code::USER_MODEL_NOTFOUND_ERROE);
if ($project->projectV6->site_status == 1) $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);
if ($this->manage['role'] != 1
&& $ticket->submit_user_id != $this->manage['id']
&& $ticket->logs()->where('engineer_id', $this->manage['id'])->count() == 0)
// 只能查看自己的工单
$this->response('没有权限查看该工单', Code::USER_PERMISSION_ERROE);
// TODO 判断是否有查看工单详情权限,待添加
$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);
// 检测修改权限
if ($ticket->submit_side == 1 && $ticket->submit_user_id != $this->manage['id']) {
// A端提交的工单,只有提交人可以修改
$this->response('没有权限操作该工单', Code::USER_PERMISSION_ERROE);
} elseif ($ticket->submit_side == 2)
{
// B端提交的工单,只有第一对接人可以修改
$log = $ticket->logs()->first();
if ($log->engineer_id != $this->manage['id'])
$this->response('没有权限操作该工单', Code::USER_PERMISSION_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);
$ticket->save();
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()]);
}
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)
{
//
}
}