<?php

namespace app\admin\service\sys_manage;

use app\model\SysUpload;
use support\Storage;

class UploadService
{
    /**
     * @Desc 上传文件
     * @Author Gorden
     * @Date 2024/2/26 13:22
     *
     * @return \support\Response
     */
    public static function upload($config)
    {
        try {
            $res = Storage::uploadFile($config);
            $data = [
                'fileName'=>$res[0]['origin_name'],
                'url' => getenv("STORAGE_DOMAIN").$config['uri'] . date('Ymd') . '/thumb/' . $res[0]['save_name'],
                'path' => $config['uri'] . date('Ymd') . '/' . $res[0]['save_name'],
                'size' => $res[0]['size'],
                'mime_type' => $res[0]['mime_type'],
                'src' => getenv("STORAGE_DOMAIN").$config['uri'] . date('Ymd') . '/thumb/' . $res[0]['save_name'],
            ];

            $insertData = [
                'upload_status' => 'ACTIVED',
                'upload_name' => $res[0]['origin_name'],
                'upload_ext_name' => $res[0]['extension'],
                'upload_file_path' => $data['path'],
                'upload_file_name' => $res[0]['save_name'],
                'upload_file_size' => round($data['size'] / 1024, 2),
                'upload_addtimes' => time()
            ];
            SysUpload::insert($insertData);
        } catch (\Exception $e) {
            return json_fail('上传失败：' . $e->getMessage());
        }

        return json_success('上传成功', $data);
    }

    /**
     * @Desc 修改文件状态
     * @Author Gorden
     * @Date 2024/3/7 9:29
     *
     * @param $id
     * @param $params
     * @return \support\Response
     */
    public static function updateStatus($id, $params)
    {
        try {
            SysUpload::where('upload_id', $id)->update(['upload_status' => $params['upload_status']]);

            return json_success('状态修改成功~');
        } catch (\Exception $e) {
            return json_fail('状态修改失败~');
        }
    }
}