<?php

namespace app\system\controller\v1;

use app\BaseController;
use library\services\UtilService as Util;
use app\model\system\Attachment as AttachmentModel;
use library\services\upload\Upload as Upload;

/**
 * 图片管理类
 * Class Attachment
 * @package app\system\controller\v1
 */
class Attachment extends BaseController
{

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where = Util::getMore([
            ['page', 1],
            ['limit', 18],
            ['pid', 0]
        ]);
        return app('json')->success(AttachmentModel::getImageList($where));
    }

    /**
     * 删除指定资源
     *
     * @param string $ids
     * @return \think\Response
     */
    public function delete()
    {
        $request = app('request');
        $ids = $request->post('ids');
        $ids = explode(',', $ids);
        if (empty($ids))
            return app('json')->fail('请选择要删除的图片');
        foreach ($ids as $v) {
            self::deleteImg($v);
        }
        return app('json')->success('删除成功');
    }

    /**
     * 图片管理上传图片
     * @return \think\response\Json
     */
    public function upload()
    {
        [$pid, $file] = Util::getMore([
            ['pid', 0],
            ['file', 'file']
        ], $this->request, true);
        try {
            $path = make_path('attach', 2, true);
            $config = [
                'accessKey' => config('qiniu')['accessKeyId'],
                'secretKey' => config('qiniu')['accessKeySecret'],
                'uploadUrl' => 'imgs.boofly.cn',
                'storageName' => config('qiniu')['bucket'],
                'storageRegion' => 's3-cn-south-1.qiniucs.com',
            ];
            $upload = new Upload(2,$config);
            $res = $upload->to($path)->validate()->move($file);
            if ($res === false) {
                return app('json')->fail($upload->getError());
            } else {
                $fileInfo = $upload->getUploadInfo();
                if ($fileInfo) {
                    AttachmentModel::attachmentAdd($fileInfo['name'], $fileInfo['size'], $fileInfo['type'], $fileInfo['dir'], $fileInfo['thumb_path'], $pid, 2, $fileInfo['time'], 1, 0);
                }
                return app('json')->success('上传成功', ['src' => $res->filePath]);
            }
        } catch (\Exception $e) {
            return app('json')->fail($e->getMessage());
        }
    }

    /**删除图片和数据记录
     * @param $att_id
     */
    public function deleteImg($att_id)
    {
        $attinfo = AttachmentModel::get($att_id);
        if ($attinfo) {
            try {
                $config = [
                    'accessKey' => config('qiniu')['accessKeyId'],
                    'secretKey' => config('qiniu')['accessKeySecret'],
                    'uploadUrl' => 'imgs.boofly.cn',
                    'storageName' => config('qiniu')['bucket'],
                    'storageRegion' => 's3-cn-south-1.qiniucs.com',
                ];
                $upload = new Upload(2,$config);
                $upload->delete($attinfo['name']);
            } catch (\Throwable $e) {
            }
            AttachmentModel::where('att_id', $att_id)->delete();
        }
    }

    /**
     * 移动图片分类操作
     */
    public function moveImageCate()
    {
        $data = Util::postMore([
            'pid',
            'images'
        ]);
        if ($data['images'] == '') return app('json')->fail('请选择图片');
        if (!$data['pid']) return app('json')->fail('请选择分类');
        $res = AttachmentModel::where('att_id', 'in', $data['images'])->update(['pid' => $data['pid']]);
        if ($res)
            return app('json')->success('移动成功');
        else
            return app('json')->fail('移动失败！');
    }

}
