This commit is contained in:
2026-01-05 14:33:21 +08:00
commit ee348b4925
451 changed files with 39954 additions and 0 deletions
+105
View File
@@ -0,0 +1,105 @@
<?php
namespace app\ticket\service;
use app\common\utils\translation\service\TranslationService;
use app\queue\utils\Translat;
use app\ticket\logic\CategorieLogic;
use app\ticket\model\CategorieTranslation;
use support\Db;
use support\exception\BusinessException;
class CategorieService
{
public function list($params)
{
$result = (new CategorieLogic())->list($params);
// $value=$result[0]->toArray();
foreach ($result as $key => &$value) {
$value['name'] = isset($value['translation']) ? $value['translation']['name'] : '';
unset($value['translation']);
}
unset($value);
return $result;
}
public function getAll($params)
{
$result = (new CategorieLogic())->getAll($params);
foreach ($result as $key => &$value) {
$value['name'] = isset($value['translation']) ? $value['translation']['name'] : '';
unset($value['translation']);
}
unset($value);
return $result;
}
public $translatFields = ['name'];
public function add(array $data)
{
try {
Db::beginTransaction();
$sourceLocale = locale();
$traitsourceFields = Translat::extractSourceFields($data, $this->translatFields);
$result = (new CategorieLogic())->add($data);
(new TranslationService())->queueTranslations(
'ticket_categorie',
$result->id,
$sourceLocale,
$traitsourceFields
);
Db::commit();
return $result;
} catch (\Throwable $e) {
Db::rollback();
throw $e;
}
}
public function edit(array $data)
{
try {
Db::beginTransaction();
$sourceLocale = locale();
$traitsourceFields = Translat::extractSourceFields($data, $this->translatFields);
$result = (new CategorieLogic())->edit($data);
// (new TranslationService())->queueTranslations(
// 'ticket_categorie',
// $data['id'],
// $sourceLocale,
// $traitsourceFields
// );
CategorieTranslation::updateOrCreate(
[
'categorie_id' => $data['id'],
'locale' => $sourceLocale,
],
$traitsourceFields
);
Db::commit();
return $result;
} catch (\Throwable $e) {
Db::rollback();
throw $e;
}
}
public function delete(int $id)
{
if(empty($id)){
throw new BusinessException('id不能为空');
}
try {
$result = (new CategorieLogic())->delete($id);
return $result;
} catch (\Throwable $e) {
throw $e;
}
}
}
+160
View File
@@ -0,0 +1,160 @@
<?php
namespace app\ticket\service;
use app\common\model\User;
use app\common\service\MessageNotificationsService;
use app\common\service\OssService;
use app\ticket\logic\CategorieLogic;
use app\ticket\logic\ConversationLogic;
use app\ticket\logic\TicketLogic;
use app\ticket\model\Conversation;
use config\constants\Constants;
use support\Db;
use support\exception\BusinessException;
class ConversationService
{
public function list($params)
{
$locale = locale();
// $locale = 'zh';
$ticket_status = $locale == 'zh'? Constants::TICKET_STATUS_ZH:Constants::TICKET_STATUS_EN;
// $ticket_status = Constants::TICKET_STATUS_ZH;
// $params['locale'] = locale();
$result = (new ConversationLogic())->list($params);
$status_fanyi = array_map(fn($v) => "{{{$v}}}", array_flip(Constants::TICKET_STATUS));
$ossservice = new OssService();
foreach ($result as $key => $value) {
$value=$value->toArray();
$value['content'] = str_replace($status_fanyi,$ticket_status,$value['content']);
$value['attachment'] = empty($value['attachment'])?[]:unserialize($value['attachment']);
foreach ($value['attachment'] as $k => $attachment) {
$value['attachment'][$k]['storage_path_full'] = (!empty($attachment))?$ossservice->generatePrivateUrl($attachment['storage_path']):'';
}
$user = User::where('id', $value['user_id'])->first();
$value['user_name'] = $user?->name;
$value['user_avatar'] = $user?->avatar;
$value['created_at'] = date('Y-m-d H:i:s', strtotime($value['created_at']));
$value['updated_at'] = date('Y-m-d H:i:s', strtotime($value['updated_at']));
$result[$key] = $value;
}
return $result;
}
public function add(array $data)
{
try {
$data['attachment'] = serialize($data['attachment']);
if (empty($data['content'])) {
throw new BusinessException('消息内容不能为空');
}
$conversation = (new ConversationLogic())->firstByTicket($data['ticket_id']);
$ticket = (new TicketLogic())->find($data['ticket_id']);
$categorie= (new CategorieLogic())->find($ticket['categorie_id']);
$categorieName = isset($categorie['translation']) ? $categorie['translation']['name'] : '';
$userDefault = [1, 75, 78];
$tiket_content = $ticket['content'];
if (empty($conversation)) { //没有沟通记录
$title = '新工单提醒-'.$categorieName;
$notice_user_id = $userDefault;
}else{
$title = '工单回复';
$tiket = (new TicketLogic())->find($conversation['ticket_id']);
if($data['user_id'] != $tiket['user_id']){
$notice_user_id = [$tiket['user_id']];
}else{
$conversationLast = Conversation::where('ticket_id', '=', $conversation['ticket_id'])
->where('user_id', '!=', $data['user_id'])
->orderBy('id', 'desc')
->first();
$notice_user_id = (!empty($conversationLast['user_id']))? [$conversationLast['user_id']]:$userDefault;
}
$tiket_content = $tiket['content'];
$status_fanyi = array_map(fn($v) => "{{{$v}}}", array_flip(Constants::TICKET_STATUS));
$locale = locale();
$ticket_status = $locale == 'zh'? Constants::TICKET_STATUS_ZH:Constants::TICKET_STATUS_EN;
$content = str_replace($status_fanyi,$ticket_status,$data['content']);
$reply = $content?($content."\n".'工单内容:'):'' ;
}
$notice = ['sender_id' => 0, //发送人ID 0默认系统
'sender_type' => 'system', //发送者类型 user admin system
'title' => $title, //标题
'content' => ($reply??'') . $tiket_content, //内容
'scene' => 1, //场景 0系统公共 1系统通知
'recipient' => $notice_user_id, //接收人 空为全部
'push_method' => 0 //推送方式0系统推送1手动推送
];
// 异步推送
\Webman\RedisQueue\Redis::send('notice', $notice);
unset($data['categorie_id']);
unset($data['code']);
return (new ConversationLogic())->add($data);
} catch (\Throwable $e) {
throw $e;
}
}
public function sendEmail(array $data)
{
$conversation = (new ConversationLogic())->firstByTicket($data['ticket_id']);
$ticket = (new TicketLogic())->find($data['ticket_id']);
$categorie= (new CategorieLogic())->find($ticket['categorie_id']);
$categorieName = isset($categorie['translation']) ? $categorie['translation']['name'] : '';
$notice = [
'code' => $ticket['code'],
'category' => $categorieName,
'content' => $ticket['content'],
'reply' => '',
];
//$emailDefault = 'app@hnxinyouhe.com'; //公共邮箱
//$emailDefault = 'support@hnxinyouhe.com'; //公共邮箱
$emailDefault = env('ticket_email_default');
if (empty($conversation)) {
$email = $emailDefault;
}else{
$tiket = (new TicketLogic())->find($conversation['ticket_id']);
if($conversation['user_id'] != $tiket['user_id']){
$email = User::where('id',$tiket['user_id'])->value('email');
}else{
$conversationLast = Conversation::where('ticket_id', '=', $conversation['ticket_id'])
->where('user_id', '!=', $conversation['user_id'])
->orderBy('id', 'desc')
->first();
$email = (!empty($conversationLast['user_id']))?User::where('id',$conversationLast['user_id'])->value('email',$emailDefault):$emailDefault;
}
$notice['content'] = $tiket['content'];
// $notice['reply'] = '<li>最新回复:' . $conversation['content'] . '</li>';
$notice['reply'] = '<li>最新回复:' . '卡罗拉、极石01、比亚迪唐,宋Plus、丰田荣放、捷途T2、极氪7x、(阿联酋, 亚美尼亚, 阿塞拜疆, 白俄罗斯, 哥斯达黎加, 埃塞俄比亚, 格鲁吉亚, 加纳, 吉尔吉斯斯坦, 蒙古, 巴拉圭, 沙特阿拉伯, 塔吉克斯坦, 土库曼斯坦, 乌兹别克斯坦,委内瑞拉)起亚亦跑(阿尔及利亚),基本上每个车都推中亚中东南美非洲' . '</li>';
}
\app\common\utils\email\MailSend::send($email, '【XYH】新的工单提醒', replaceTpl($notice, 'work_order'));
return $email;
}
public function edit(array $data)
{
try {
Db::beginTransaction();
$data['attachment'] = serialize($data['attachment']);
$result = (new ConversationLogic())->edit($data);
Db::commit();
return $result;
} catch (\Throwable $e) {
Db::rollback();
throw $e;
}
}
}
+167
View File
@@ -0,0 +1,167 @@
<?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;
$res[$k]['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
$res[$k]['updated_at'] = date('Y-m-d H:i:s', $v['updated_at']);
}
unset($v);
unset($attachments);
return $res;
}
public function add(array $data)
{
try {
$attachment = $data['attachment']??[];
$logic = new TicketLogic();
$data['code'] = $logic::generateSimpleTicketNo();
$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,
'categorie_id' => $data['categorie_id'],
'code' => $data['code'],
'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;
}
}