Files
fdesk/app/ticket/logic/AttachmentLogic.php
T
2025-07-04 14:38:00 +08:00

82 lines
2.7 KiB
PHP

<?php
namespace app\ticket\logic;
use app\common\model\UserTokenLog;
use app\common\model\Systems;
use app\common\model\User;
use app\common\model\UserSn;
use app\common\utils\Jwt;
use app\ticket\model\Attachment;
use app\ticket\model\Ticket;
use support\Redis;
use support\exception\BusinessException;
use support\Log;
class AttachmentLogic
{
public function adds(array $data,int $ticket_id,int $user_id)
{
try {
foreach ($data as $k=>$v){
$v['ticket_id'] = $ticket_id;
$v['user_id'] = $user_id;
$res = Attachment::create($v);
}
return $res;
} catch (\Throwable $e) {
Log::error('工单创建失败', [
'data' => $data,
'error' => $e->getMessage()
]);
throw new BusinessException('工单创建失败,请稍后重试', 500);
}
}
public function edit(array $data,int $ticket_id,int $user_id)
{
try {
$attachmentsId = [];
foreach ($data as $k=>$v){
$v['ticket_id'] = $ticket_id;
$v['user_id'] = $user_id;
if(!empty($v['id'])){ //修改附件
$attachmentId = $v['id'];
$attachmentsId[] = $attachmentId;
// $v_res = Attachment::update($attachmentId, $v);
$v_res = Attachment::updateOrCreate(['id' => $v['id']],$v);
if (!$v_res) {
throw new BusinessException('附件保存失败');
}
} else { //新增附件
unset($v['id']);
$v_res = Attachment::create($v);
if (!$v_res) {
throw new BusinessException('附件创建失败');
}
$vId = $v_res->id;
$attachmentsId[] = $vId;
}
}
//删除本次没有出现的附件
$res = Attachment::whereNotIn('id', $attachmentsId)->where('ticket_id',$ticket_id)->delete();
return $res;
} catch (\Throwable $e) {
Log::error('工单创建失败', [
'data' => $data,
'error' => $e->getMessage()
]);
throw new BusinessException('工单创建失败,请稍后重试', 500);
}
}
public static function generateSimpleTicketNo(): string
{
$date = date('Ymd'); // 日期
$prefix = 'GD'; // 前缀
$random = strtoupper(substr(md5(uniqid()), 0, 5)); // 5位随机字母数字
return $prefix . $date . '-' . $random; // 如 GD20240702-AB3X9Z
}
}