quanwei
2 days ago 73b874c72ad55eb9eef21c36160ac0de58f0189e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
 
namespace app\common\model\file;
 
use app\common\model\BaseModel;
 
/**
 * 文件库模型
 */
class UploadFile extends BaseModel
{
    protected $pk = 'file_id';
    protected $name = 'upload_file';
    protected $updateTime = false;
    protected $deleteTime = false;
    protected $append = ['file_path', 'original_path'];
 
    /**
     * 关联文件库分组表
     */
    public function uploadGroup()
    {
        return $this->belongsTo('UploadGroup', 'group_id');
    }
 
    /**
     * 获取图片完整路径
     * @param $value
     * @param $data
     * @return string
     */
    public function getFilePathAttr($value, $data)
    {
        if ($data['storage'] === 'local') {
            return self::$base_url . 'uploads/' . $data['save_name'];
        }
        return $data['file_url'] . '/' . $data['save_name'];
    }
 
    /**
     * 获取原始图片完整路径 by lyzflash
     * @param $value
     * @param $data
     * @return string
     */
    public function getOriginalPathAttr($value, $data)
    {
        if ($data['storage'] === 'local') {
            return $data['is_original'] ? self::$base_url . 'uploads/' . $data['original_name'] : '';
        }
        // 网盘暂不处理
        return '';
    }
 
    /**
     * 文件详情
     */
    public static function detail($file_id)
    {
        return (new static())->find($file_id);
    }
 
    /**
     * 根据文件名查询文件id
     */
    public static function getFildIdByName($fileName)
    {
        return (new static)->where(['file_name' => $fileName])->value('file_id');
    }
 
    /**
     * 查询文件id
     */
    public static function getFileName($fileId)
    {
        return (new static)->where(['file_id' => $fileId])->value('file_name');
    }
 
    /**
     * 添加新记录
     */
    public function add($data)
    {
        return $this->save($data);
    }
 
    /**
     * 获取列表记录
     */
    public function getList($groupId = 0, $fileType = '', $pageSize = 30, $isRecycle = -1, $shop_supplier_id = 0, $branch_id = 0)
    {
        $model = $this;
        // 文件分组
        if ($groupId != 0) {
            $model = $model->where('group_id', '=', (int)$groupId);
        }
        // 文件类型
        !empty($fileType) && $model = $model->where('file_type', '=', trim($fileType));
        // 是否在回收站
        $isRecycle > -1 && $model = $model->where('is_recycle', '=', (int)$isRecycle);
        // 查询列表数据
        return $model->with(['upload_group'])
            ->where(['is_user' => 0, 'is_delete' => 0])
            ->where('shop_supplier_id', '=', $shop_supplier_id)
            ->where('branch_id', '=', $branch_id)
            ->order(['file_id' => 'desc'])
            ->paginate($pageSize);
    }
 
    /**
     * 批量移动文件分组
     */
    public function moveGroup($group_id, $fileIds)
    {
        return $this->where('file_id', 'in', $fileIds)->update(compact('group_id'));
    }
}