$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 } }