<?php namespace App\Http\Services; //后台用户相关信息服务 use App\Model\RedisModel; use App\Model\SupplierAttachmentModel; use App\Model\SupplierAttachmentsModel; use Illuminate\Support\Facades\DB; //用于判断是否已经查看的服务 class SupplierAttachmentService { public function getAttachment($supplierId) { $attachmentModel = new SupplierAttachmentModel(); $attachmentData = $attachmentModel->where('supplier_id', $supplierId)->first(); $attachment = []; if (!empty($attachmentData)) { $attachment = json_decode($attachmentData, true); $attachment = array_map(function ($item) { return json_decode($item, true); }, $attachment); } return $attachment; } public function saveAttachment($attachment) { if ($attachment['validity_period']) { $validityPeriod = explode('~', $attachment['validity_period']); $attachment['validity_start'] = strtotime(trim($validityPeriod[0])); $attachment['validity_end'] = strtotime(trim($validityPeriod[1])); } if ($attachment['validity_type'] == 1) { $attachment['validity_start'] = $attachment['validity_end'] = 0; } $attachment['type_name'] = array_get(config('fixed.FileNameMapping'), $attachment['field_name']); unset($attachment['validity_period']); $attachmentId = $attachment['attachment_id']; $attachmentModel = new SupplierAttachmentsModel(); if (empty($attachmentId)) { $attachment['create_uid'] = request()->user->userId; $attachment['create_name'] = request()->user->name; $attachment['create_time'] = time(); return $attachmentModel->insertGetId($attachment); } else { $attachment['update_time'] = time(); return $attachmentModel->where('attachment_id', $attachmentId)->update($attachment); } } //新增附件来源于新增页面,数据要转换 public static function addAttachmentFromAddPage($supplierId, $attachmentData) { //恢复正常的坐标 $attachmentData = array_map(function ($item) { if (is_array($item)) { $item = array_values($item); } return $item; },$attachmentData); if (empty($attachmentData)) { return true; } $attachments = []; foreach ($attachmentData['file_name'] as $key => $value) { //最后一个跳过,因为是模板里的数据 if ($key == (count($attachmentData['file_name']) - 1)) { continue; } $attachment = [ 'supplier_id' => $supplierId, 'file_name' => $attachmentData['file_name'][$key], 'file_url' => $attachmentData['file_url'][$key], 'field_name' => $attachmentData['field_name'][$key], 'validity_type' => $attachmentData['validity_type'][$key], 'create_uid' => request()->user->userId, 'create_name' => request()->user->name, 'type_name' => array_get(config('fixed.FileNameMapping'), $attachmentData['field_name'][$key]), ]; if (!empty($attachmentData['validity_period'][$key])) { $validityPeriod = explode('~', $attachmentData['validity_period'][$key]); $attachment['validity_start'] = strtotime(trim($validityPeriod[0])); $attachment['validity_end'] = strtotime(trim($validityPeriod[1])); } $attachments[] = $attachment; } if ($attachments) { return SupplierAttachmentsModel::insert($attachments); } return true; } }