<?php
namespace app\controller\manager;

use app\model\{File as MFile, Article, Block, Category, Link, LinkProduct, Slide, Log};
use app\service\Tool;

class File extends Base
{
    //删除磁盘上的文件
    public function delPath()
    {
        if ($this->request->isPost()) {
            $paths = input('post.paths/a');
            if(!empty($paths)){
                foreach($paths as $path){
                    Tool::delFile($path);
                }
                Log::write('file', 'delPath', '批量删除了磁盘文件，涉及到的文件路径为：' . implode(',', $paths));
                return $this->json();
            }
            return $this->json(2, '待删除文件列表为空');
        }
        return $this->json(1, '非法请求!');
    }
    //删除文件记录
    public function del()
    {
        if ($this->request->isPost()) {
            $ids = input('post.ids/a');
            if(empty($ids) || !is_array($ids)) {
                return $this->json(2, '参数错误,请核对之后再操作!');
            }
            $items = MFile::getListByIds($ids);
            if(!empty($items)){
                $delIds = [];
                foreach($items as $item){
                    $delIds[] = $item['id'];
                    if($item['type'] == MFile::IMG){
                        Tool::delFile($item['src'], 1);
                    }else{
                        Tool::delFile($item['src']);
                    }
                }
                MFile::destroy($delIds);
                Log::write('file', 'del', '批量删除了文件，涉及到的文件ID为：' . implode(',', $delIds));
                return $this->json();
            }else{
                return $this->json(3, '待删除文件列表为空');
            }
        }
        return $this->json(1, '非法请求!');
    }
    /**
     * 未使用文件列表，
     * 1. 遍历数据库中使用的图片视频及文件路径
     * 2. 遍历上传目录中的文件
     * 3. 数据对比，找出存在目录中的文件&不在数据库中的文件
     * 4. 页面上显示查找出来的文件
    */
    public function unuse()
    {
        $filesInUse = $this->getAllFilesInUse();    //数据库中在使用的文件
        $rootPath = app()->getRootPath();
        $uploadPath = $rootPath . 'storage';
        $uploadedFiles = getAllFilesByPath($uploadPath, $rootPath); //磁盘上上传的文件
        $files = MFile::getAll();
        $dbUploadedFiles = [];  //数据库中上传的文件
        foreach($files as $file){
            $src = trim($file['src']);
            if(!empty($src)){
                $key = getKeyByPath($src);
                $dbUploadedFiles[$key] = $file;
            }
        }

        $uploadedNotInUseFiles = array_diff_key($uploadedFiles, $filesInUse);   //磁盘上上传未使用的文件
        $dbUploadedNotInUseFiles = array_diff_key($dbUploadedFiles, $filesInUse);    //数据库中上传未使用的文件
        $bothNotInUseFiles = array_intersect_key($uploadedNotInUseFiles, $dbUploadedNotInUseFiles);  //磁盘和数据库中，两边都未使用
        $this->data['uploadedNotInUseFiles'] = $uploadedNotInUseFiles;
        $this->data['dbUploadedNotInUseFiles'] = $dbUploadedNotInUseFiles;
        $this->data['bothNotInUseFilesKey'] = array_keys($bothNotInUseFiles);
        return $this->view();
    }

    //获取所有在使用的文件
    private function getAllFilesInUse()
    {
        $files = [];
        $blockFiles = Block::getFilesInUse();
        if(!empty($blockFiles)){
            $files = array_merge($files, $blockFiles);
        }
        $slideFiles = Slide::getFilesInUse();
        if(!empty($slideFiles)){
            $files = array_merge($files, $slideFiles);
        }
        $linkFiles = Link::getFilesInUse();
        if(!empty($linkFiles)){
            $files = array_merge($files, $linkFiles);
        }
        $linkProductFiles = LinkProduct::getFilesInUse();
        if(!empty($linkProductFiles)){
            $files = array_merge($files, $linkProductFiles);
        }
        $categoryFiles = Category::getFilesInUse();
        if(!empty($categoryFiles)){
            $files = array_merge($files, $categoryFiles);
        }
        $articleFiles = Article::getFilesInUse();
        if(!empty($articleFiles)){
            $files = array_merge($files, $articleFiles);
        }
        return $files;
    }


    //ajax获取文件列表
    public function ajaxList()
    {
        if($this->request->isAjax()){
            $page = input('param.page/d', 1);
            $size = input('param.size/d', 20);
            if(!is_integer($page) || $page < 1){
                $page = 1;
            }
            if (!is_integer($size) || $size < 1) {
                $size = 20;
            }
            $type = input('param.type', '');
            if(!in_array($type, array_keys(MFile::getTypes()))){
                $type = '';
            }
            $items = MFile::getList($type, $page, $size);
            return $this->json(0, 'ok', $items);
        }
        return $this->json(1, '无此操作');
    }
    //列表
    public function index()
    {
        $items = MFile::getListPage();
        $this->data['items'] = $items;
        $this->data['types'] = MFile::getTypes();
        return $this->view();
    }
}
