From 8d36ffe13baebd0d85daba7a30ede7815f5eda1c Mon Sep 17 00:00:00 2001 From: "HESU\\HESU" <1696136552@qq.com> Date: Thu, 3 Jul 2025 15:28:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E5=8D=95=E6=B2=9F=E9=80=9A=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ConversationController.php | 46 +++++++++++++++++ app/ticket/controller/TicketController.php | 25 +++++++++- app/ticket/logic/ConversationLogic.php | 46 +++++++++++++++++ app/ticket/logic/TicketLogic.php | 6 +++ app/ticket/model/Conversation.php | 33 ++++++++++++ app/ticket/service/ConversationService.php | 50 +++++++++++++++++++ app/ticket/service/TicketService.php | 48 ++++++++++++++++++ config/constants/Constants.php | 37 ++++++++++++++ config/route.php | 10 ++++ 9 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 app/ticket/controller/ConversationController.php create mode 100644 app/ticket/logic/ConversationLogic.php create mode 100644 app/ticket/model/Conversation.php create mode 100644 app/ticket/service/ConversationService.php create mode 100644 config/constants/Constants.php diff --git a/app/ticket/controller/ConversationController.php b/app/ticket/controller/ConversationController.php new file mode 100644 index 0000000..0e1b7ec --- /dev/null +++ b/app/ticket/controller/ConversationController.php @@ -0,0 +1,46 @@ +input('ticket_id'); + if(empty($ticket_id)){ + return $this->error('ticket_id不能为空'); + } + $result = (new ConversationService())->list($request->all()); + return $this->success($this->page($result)); + } catch (\Throwable $e) { + return $this->error($e->getMessage().$e->getLine().$e->getFile()); + } + } + public function add(Request $request) + { + try { + $request->setPost('user_id', 1); + return (new ConversationService())->add($request->all()) + ? $this->success() + : $this->error(); + } catch (\Throwable $th) { + return $this->error($th->getMessage().' '.$th->getLine().' '.$th->getFile()); + } + } + public function edit(Request $request) + { + try { + $request->setPost('user_id', 1); + $result = (new ConversationService())->edit($request->all()); + return $result + ? $this->success($result) + : $this->error(); + } catch (\Throwable $th) { + return $this->error($th->getMessage().' '.$th->getLine().' '.$th->getFile()); + } + } +} \ No newline at end of file diff --git a/app/ticket/controller/TicketController.php b/app/ticket/controller/TicketController.php index ff3defa..cc33459 100644 --- a/app/ticket/controller/TicketController.php +++ b/app/ticket/controller/TicketController.php @@ -39,7 +39,30 @@ class TicketController extends BaseController ? $this->success($result) : $this->error(); } catch (\Throwable $th) { - return $this->error($th->getMessage().' '.$th->getLine().' '.$th->getFile()); + return $this->error($th->getMessage()); + } + } + public function status(Request $request) + { + try { + $request->setPost('user_id', 1); + $result = (new TicketService())->status($request->all()); + return $result + ? $this->success($result) + : $this->error(); + } catch (\Throwable $th) { + return $this->error($th->getMessage()); + } + } + public function getStatus(Request $request) + { + try { + $result = (new TicketService())->getStatus($request->all()); + return $result + ? $this->success($result) + : $this->error(); + } catch (\Throwable $th) { + return $this->error($th->getMessage()); } } diff --git a/app/ticket/logic/ConversationLogic.php b/app/ticket/logic/ConversationLogic.php new file mode 100644 index 0000000..6ce3cbf --- /dev/null +++ b/app/ticket/logic/ConversationLogic.php @@ -0,0 +1,46 @@ +paginate($size, ['*'], 'page', $current); + } + public function add(array $data) + { + try { + return Conversation::create($data); + } catch (\Throwable $e) { + Log::error('会话创建失败', [ + 'data' => $data, + 'error' => $e->getMessage() + ]); + throw new BusinessException('会话创建失败,请稍后重试', 500); + } + } + public function edit(array $data) + { + try { + $conversation = Conversation::updateOrCreate(['id' => $data['id']], $data); + return $conversation; + } catch (\Throwable $e) { + Log::error('会话编辑失败', [ + 'data' => $data, + 'error' => $e->getMessage() + ]); + throw new BusinessException($e->getMessage().'会话编辑失败,请稍后重试', 500); + } + } +} \ No newline at end of file diff --git a/app/ticket/logic/TicketLogic.php b/app/ticket/logic/TicketLogic.php index 0037879..16fb5ce 100644 --- a/app/ticket/logic/TicketLogic.php +++ b/app/ticket/logic/TicketLogic.php @@ -50,6 +50,12 @@ class TicketLogic } } + public function find(int $id) + { + $ticket = Ticket::find($id); + return $ticket; + } + public static function generateSimpleTicketNo(): string { $date = date('Ymd'); // 日期 diff --git a/app/ticket/model/Conversation.php b/app/ticket/model/Conversation.php new file mode 100644 index 0000000..8839455 --- /dev/null +++ b/app/ticket/model/Conversation.php @@ -0,0 +1,33 @@ + 'datetime:Y-m-d H:i:s', + 'created_at' => 'datetime:Y-m-d H:i:s', + ]; + protected $fillable = [ + 'ticket_id', + 'user_id', + 'content', + ]; +} \ No newline at end of file diff --git a/app/ticket/service/ConversationService.php b/app/ticket/service/ConversationService.php new file mode 100644 index 0000000..cc3ef84 --- /dev/null +++ b/app/ticket/service/ConversationService.php @@ -0,0 +1,50 @@ +list($params); + + $status_fanyi = array_map(fn($v) => "{{{$v}}}", array_flip(Constants::TICKET_STATUS)); + foreach ($result as $key => $value) { + $result[$key]['content'] = str_replace($status_fanyi,$ticket_status,$value['content']); + } + return $result; + } + + public function add(array $data) + { + try { + return (new ConversationLogic())->add($data); + } catch (\Throwable $e) { + Db::rollback(); + throw $e; + } + } + + public function edit(array $data) + { + try { + Db::beginTransaction(); + $result = (new ConversationLogic())->edit($data); + Db::commit(); + return $result; + } catch (\Throwable $e) { + Db::rollback(); + throw $e; + } + } +} \ No newline at end of file diff --git a/app/ticket/service/TicketService.php b/app/ticket/service/TicketService.php index 650e163..f01d9c2 100644 --- a/app/ticket/service/TicketService.php +++ b/app/ticket/service/TicketService.php @@ -3,14 +3,17 @@ namespace app\ticket\service; use app\ticket\logic\AttachmentLogic; +use app\ticket\logic\ConversationLogic; use app\ticket\logic\TicketLogic; use app\ticket\model\Ticket; +use config\constants\Constants; use support\Db; use support\exception\BusinessException; use support\Log; class TicketService { + public function list($params) { $res = (new TicketLogic())->list($params); @@ -51,4 +54,49 @@ class TicketService throw $e; } } + + public function status(array $data) + { + try { + Db::beginTransaction(); + $msg = $data['msg']??[]; + $ticket = (new TicketLogic())->find($data['id']); + if(empty($ticket)){ + throw new BusinessException('工单不存在'); + } + if($data['status'] == $ticket->status){ + throw new BusinessException('状态未修改'); + } + +// if(!in_array($ticket->status, [Constants::TICKET_STATUS['PENDING'], Constants::TICKET_STATUS['CONFIRM']]) +// && empty($msg)){ +// throw new BusinessException('提交失败'); +// } + + $ticket->status = $data['status']; + $result = $ticket->save(); + + if(!empty($msg)){ + $conversation = [ + 'ticket_id' => $ticket->id, + 'user_id' => $data['user_id'], + 'content' => '状态更新为{{'.array_search($data['status'],Constants::TICKET_STATUS).'}}:'.$msg + ]; + $res = (new ConversationLogic())->add($conversation); + } + + Db::commit(); + return $result; + } catch (\Throwable $e) { + Db::rollback(); + throw $e; + } + } + + public function getStatus(array $data) + { + $locale = locale(); + $ticket_status = $locale == 'zh'? Constants::TICKET_STATUS_ZH:Constants::TICKET_STATUS_EN; + return $ticket_status; + } } diff --git a/config/constants/Constants.php b/config/constants/Constants.php new file mode 100644 index 0000000..e4c9284 --- /dev/null +++ b/config/constants/Constants.php @@ -0,0 +1,37 @@ + 1, // 待受理 + 'PROCESS' => 2, //处理中 + 'CONFIRM' => 3, // 待确认 + 'FINISH' => 4, // 已完成 + 'REJECTED' => 5, // 已驳回 + ]; + + public const TICKET_STATUS_ZH = [ + self::TICKET_STATUS['PENDING'] => '待受理', + self::TICKET_STATUS['PROCESS'] => '处理中', + self::TICKET_STATUS['CONFIRM'] => '待确认', + self::TICKET_STATUS['FINISH'] => '已完成', + self::TICKET_STATUS['REJECTED'] => '已驳回', + ]; + public const TICKET_STATUS_EN = [ + self::TICKET_STATUS['PENDING'] => 'Pending acceptance', + self::TICKET_STATUS['PROCESS'] => 'Processing', + self::TICKET_STATUS['CONFIRM'] => 'To be confirmed', + self::TICKET_STATUS['FINISH'] => 'Completed', + self::TICKET_STATUS['REJECTED'] => 'Rejected', + ]; + public const TICKET_STATUS_RU = [ + self::TICKET_STATUS['PENDING'] => 'Подлежит рассмотрению', + self::TICKET_STATUS['PROCESS'] => 'В процессе обработки', + self::TICKET_STATUS['CONFIRM'] => 'Подлежит подтверждению', + self::TICKET_STATUS['FINISH'] => 'Завершено', + self::TICKET_STATUS['REJECTED'] => 'Отклонено', + ]; + +} \ No newline at end of file diff --git a/config/route.php b/config/route.php index a1b8c64..6a53a53 100644 --- a/config/route.php +++ b/config/route.php @@ -35,9 +35,19 @@ Route::group('/common', function () { Route::group('/ticket', function () { + //工单 Route::any('/ticket/add', [app\ticket\controller\TicketController::class, 'add']); Route::any('/ticket/edit', [app\ticket\controller\TicketController::class, 'edit']); Route::any('/ticket/list', [app\ticket\controller\TicketController::class, 'index']); + Route::any('/ticket/status', [app\ticket\controller\TicketController::class, 'status']); + Route::any('/ticket/getStatus', [app\ticket\controller\TicketController::class, 'getStatus']); + Route::any('/upload', [app\common\controller\UploadController::class, 'uploadFile']); + + //工单沟通记录 + Route::any('/conversation/add', [app\ticket\controller\ConversationController::class, 'add']); + Route::any('/conversation/edit', [app\ticket\controller\ConversationController::class, 'edit']); + Route::any('/conversation/list', [app\ticket\controller\ConversationController::class, 'index']); + });