Files
fdesk/app/ticket/service/TicketService.php
T

123 lines
3.7 KiB
PHP

<?php
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);
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']);
}
}
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']);
}
return $result;
} catch (\Throwable $e) {
Db::rollback();
throw $e;
}
}
public function edit(array $data)
{
try {
Db::beginTransaction();
$attachment = $data['attachment']??[];
$logic = new TicketLogic();
$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];
}
return $status;
}
}