Files
fdesk/app/ticket/service/TicketService.php
T
2025-08-11 11:58:27 +08:00

163 lines
5.1 KiB
PHP

<?php
namespace app\ticket\service;
use app\common\model\User;
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);
foreach ($res as $k => &$v) {
$name = isset($v['categorie']['translation']) ? $v['categorie']['translation']['name'] : '';
if(isset($res[$k]['categorie'])){
$res[$k]['categorie']['name'] = $name;
}
if (isset($v['categorie']['translation'])){
unset($res[$k]['categorie']['translation']);
}
foreach ($v['attachments'] as &$attachments) {
$attachments['storage_path_full'] = (new \app\common\service\OssService())->generatePrivateUrl($attachments['storage_path']);
}
$user = User::where('id', $v['user_id'])->first();
$res[$k]['user_name'] = $user?->name;
$res[$k]['user_avatar'] = $user?->avatar;
}
unset($v);
unset($attachments);
return $res;
}
public function add(array $data)
{
try {
$attachment = $data['attachment']??[];
$logic = new TicketLogic();
$result = $logic->add($data);
if(!empty($attachment)){
(new AttachmentLogic())->adds($attachment, $result->id, $data['user_id']);
}
$cleanContent = trim(preg_replace('/\s+/', ' ', strip_tags($data['content'] ?? '')));
$conversation = [
'ticket_id' => $result->id,
'content' => $cleanContent,
'user_id' => $data['user_id'],
'attachment' => $data['attachment'],
];
(new ConversationService())->add($conversation);
return $result;
} catch (\Throwable $e) {
Db::rollback();
throw $e;
}
}
public function edit(array $data)
{
try {
Db::beginTransaction();
$logic = new TicketLogic();
$result = $logic->find($data['id']);
if(empty($result)){
throw new BusinessException('工单不存在');
}
if($result['user_id'] != $data['user_id']){
throw new BusinessException('只能编辑自己的工单');
}
if($result['status'] != 1){
throw new BusinessException('只能修改待受理工单');
}
$attachment = $data['attachment']??[];
$result = $logic->edit($data);
if(!empty($attachment)){
(new AttachmentLogic())->edit($attachment, $result->id, $data['user_id']);
}
Db::commit();
return $result;
} catch (\Throwable $e) {
Db::rollback();
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(trans('status_not_modified'));
}
// 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;
$status = [];
foreach ($ticket_status as $key => $value) {
$status[] = ['value' => $key, 'text' => $value];
}
if (empty($data['id'])) {
return $status;
}
$res = (new TicketLogic())->find($data['id']);
if(!empty($res) && $res->user_id == $data['user_id']){
$status = [
['value' => Constants::TICKET_STATUS['PENDING'], 'text' => $ticket_status[Constants::TICKET_STATUS['PENDING']]],
['value' => Constants::TICKET_STATUS['REJECTED'], 'text' => $ticket_status[Constants::TICKET_STATUS['REJECTED']]],
];
}
return $status;
}
}