2019-12-12 19:47:32 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Http\Requests\TicketSave;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Ticket;
|
|
|
|
use App\Models\TicketMessage;
|
|
|
|
use App\Utils\Helper;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
class TicketController extends Controller
|
|
|
|
{
|
2019-12-23 16:12:35 +08:00
|
|
|
public function fetch (Request $request) {
|
2019-12-12 20:08:20 +08:00
|
|
|
if ($request->input('id')) {
|
|
|
|
$ticket = Ticket::where('id', $request->input('id'))
|
|
|
|
->where('user_id', $request->session()->get('id'))
|
|
|
|
->first();
|
|
|
|
if (!$ticket) {
|
|
|
|
abort(500, '工单不存在');
|
|
|
|
}
|
|
|
|
$ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
|
2019-12-12 20:27:24 +08:00
|
|
|
for ($i = 0; $i < count($ticket['message']); $i++) {
|
2019-12-13 22:30:45 +08:00
|
|
|
if ($ticket['message'][$i]['user_id'] == $ticket->user_id) {
|
2019-12-12 20:26:56 +08:00
|
|
|
$ticket['message'][$i]['is_me'] = true;
|
|
|
|
} else {
|
|
|
|
$ticket['message'][$i]['is_me'] = false;
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 20:08:20 +08:00
|
|
|
return response([
|
|
|
|
'data' => $ticket
|
|
|
|
]);
|
|
|
|
}
|
2019-12-13 17:13:10 +08:00
|
|
|
$ticket = Ticket::where('user_id', $request->session()->get('id'))
|
|
|
|
->orderBy('created_at', 'DESC')
|
|
|
|
->get();
|
|
|
|
for ($i = 0; $i < count($ticket); $i++) {
|
2019-12-13 17:16:53 +08:00
|
|
|
if ($ticket[$i]['last_reply_user_id'] == $request->session()->get('id')) {
|
2019-12-13 17:13:10 +08:00
|
|
|
$ticket[$i]['reply_status'] = 0;
|
|
|
|
} else {
|
|
|
|
$ticket[$i]['reply_status'] = 1;
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 19:47:32 +08:00
|
|
|
return response([
|
2019-12-13 17:13:10 +08:00
|
|
|
'data' => $ticket
|
2019-12-12 19:47:32 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save (TicketSave $request) {
|
|
|
|
DB::beginTransaction();
|
|
|
|
$ticket = Ticket::create(array_merge($request->only([
|
|
|
|
'subject',
|
|
|
|
'level'
|
|
|
|
]), [
|
2019-12-13 17:07:46 +08:00
|
|
|
'user_id' => $request->session()->get('id'),
|
|
|
|
'last_reply_user_id' => $request->session()->get('id')
|
2019-12-12 19:47:32 +08:00
|
|
|
]));
|
|
|
|
if (!$ticket) {
|
|
|
|
DB::rollback();
|
|
|
|
abort(500, '工单创建失败');
|
|
|
|
}
|
|
|
|
$ticketMessage = TicketMessage::create([
|
|
|
|
'user_id' => $request->session()->get('id'),
|
|
|
|
'ticket_id' => $ticket->id,
|
|
|
|
'message' => $request->input('message')
|
|
|
|
]);
|
|
|
|
if (!$ticketMessage) {
|
|
|
|
DB::rollback();
|
|
|
|
abort(500, '工单创建失败');
|
|
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2019-12-12 20:33:14 +08:00
|
|
|
|
|
|
|
public function reply (Request $request) {
|
2019-12-13 15:16:44 +08:00
|
|
|
if (empty($request->input('id'))) {
|
2019-12-12 20:33:14 +08:00
|
|
|
abort(500, '参数错误');
|
|
|
|
}
|
2019-12-13 15:16:44 +08:00
|
|
|
if (empty($request->input('message'))) {
|
|
|
|
abort(500, '消息不能为空');
|
|
|
|
}
|
2019-12-12 20:33:14 +08:00
|
|
|
$ticket = Ticket::where('id', $request->input('id'))
|
|
|
|
->where('user_id', $request->session()->get('id'))
|
|
|
|
->first();
|
|
|
|
if (!$ticket) {
|
|
|
|
abort(500, '工单不存在');
|
|
|
|
}
|
2019-12-13 17:37:51 +08:00
|
|
|
if ($ticket->status) {
|
|
|
|
abort(500, '工单已关闭,无法回复');
|
|
|
|
}
|
2019-12-13 16:17:01 +08:00
|
|
|
if ($request->session()->get('id') == $this->getLastMessage($ticket->id)->user_id) {
|
2019-12-13 16:16:20 +08:00
|
|
|
abort(500, '请等待技术支持回复');
|
2019-12-13 16:16:09 +08:00
|
|
|
}
|
2019-12-13 17:07:46 +08:00
|
|
|
DB::beginTransaction();
|
2019-12-12 20:33:14 +08:00
|
|
|
$ticketMessage = TicketMessage::create([
|
|
|
|
'user_id' => $request->session()->get('id'),
|
|
|
|
'ticket_id' => $ticket->id,
|
|
|
|
'message' => $request->input('message')
|
|
|
|
]);
|
2019-12-13 17:07:46 +08:00
|
|
|
$ticket->last_reply_user_id = $request->session()->get('id');
|
|
|
|
if (!$ticketMessage || !$ticket->save()) {
|
|
|
|
DB::rollback();
|
2019-12-12 20:33:14 +08:00
|
|
|
abort(500, '工单回复失败');
|
|
|
|
}
|
2019-12-13 17:07:46 +08:00
|
|
|
DB::commit();
|
2019-12-12 20:33:14 +08:00
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2019-12-13 16:16:09 +08:00
|
|
|
|
2019-12-13 17:29:55 +08:00
|
|
|
|
|
|
|
public function close (Request $request) {
|
|
|
|
if (empty($request->input('id'))) {
|
|
|
|
abort(500, '参数错误');
|
|
|
|
}
|
|
|
|
$ticket = Ticket::where('id', $request->input('id'))
|
|
|
|
->where('user_id', $request->session()->get('id'))
|
|
|
|
->first();
|
|
|
|
if (!$ticket) {
|
|
|
|
abort(500, '工单不存在');
|
|
|
|
}
|
|
|
|
$ticket->status = 1;
|
|
|
|
if (!$ticket->save()) {
|
|
|
|
abort(500, '关闭失败');
|
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:16:09 +08:00
|
|
|
private function getLastMessage ($ticketId) {
|
|
|
|
return TicketMessage::where('ticket_id', $ticketId)
|
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
->first();
|
|
|
|
}
|
2019-12-12 19:47:32 +08:00
|
|
|
}
|