<?php


namespace App\Http\Services;


//后台用户相关信息服务
use App\Model\SupplierAttachmentsModel;

//用于判断是否已经查看的服务
class SupplierAttachmentService
{

    public function getAttachment($supplierId)
    {
        $attachmentModel = new SupplierAttachmentsModel();
        return $attachmentModel->where('supplier_id', $supplierId)->get()->toArray();
    }

    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'];
        if (empty($attachment['description'])) {
            unset($attachment['description']);
        }
        $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)?$attachment['supplier_id']:0;
        }
    }

    //新增附件来源于新增页面,数据要转换
    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) {
            $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_time' => time(),
                'create_name' => request()->user->name,
                'description' => !empty($attachmentData['description'][$key]) ? $attachmentData['description'][$key] : ' ',
                'type_name' => array_get(config('fixed.FileNameMapping'), $attachmentData['field_name'][$key]),
            ];
            if (empty($attachment['file_name'])) {
                continue;
            }
            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]));
            } else {
                $attachment['validity_start'] = 0;
                $attachment['validity_end'] = 0;
            }
            $attachments[] = $attachment;
        }
        if ($attachments) {
            return SupplierAttachmentsModel::insert($attachments);
        }
        return true;
    }
}