Passed
Push — master ( f72b76...f22075 )
by Dennis
08:27 queued 04:27
created

DynamicQuery::update()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 11
c 2
b 1
f 0
dl 0
loc 15
rs 9.9
cc 4
nc 4
nop 3
1
<?php
2
namespace SimpleCMS\DynamicUnit\Packages;
3
4
use Illuminate\Http\UploadedFile;
5
use Illuminate\Support\Collection;
6
use SimpleCMS\DynamicUnit\Models\DynamicAttribute;
7
use SimpleCMS\DynamicUnit\Models\DynamicUnit as DynamicUnitModel;
8
9
/**
10
 * 操作类
11
 *
12
 * @author Dennis Lui <[email protected]>
13
 */
14
class DynamicQuery
15
{
16
17
    /**
18
     * 删除动作
19
     *
20
     * @author Dennis Lui <[email protected]>
21
     * @param  int $id
22
     * @param  string $type
23
     * @return bool
24
     */
25
    public static function delete(int $id, string $type): bool
26
    {
27
        $model = self::convertModel($type);
28
        if (!$item = $model::find($id)) {
29
            return false;
30
        }
31
        return $item->delete();
32
    }
33
34
    /**
35
     * 添加动作
36
     *
37
     * @author Dennis Lui <[email protected]>
38
     * @param  array $data
39
     * @return bool
40
     */
41
    public static function create(array $data, string $type): bool
42
    {
43
        $model = self::convertModel($type);
44
        list($sql, $file) = self::getFileSql($data, $type);
45
        $item = $model::create($sql);
46
        if ($file) {
47
            $item->addMedia($file)->toMediaCollection($model::MEDIA_FILE);
48
        }
49
        self::addItems($data, $type, $item);
50
        return !empty($item);
51
    }
52
53
    private static function addItems(array $data, string $type = 'attribute', DynamicUnitModel $dynamicUnit): void
54
    {
55
        if ($type == 'unit' && array_key_exists('items', $data) && $data['items']) {
56
            foreach ($data['items'] as $rs) {
57
                self::create(array_merge(['dynamic_unit_id' => $dynamicUnit->id], $rs), 'attribute');
58
            }
59
        }
60
    }
61
62
    private static function updateItems(array $data, string $type = 'attribute', DynamicUnitModel $dynamicUnit): void
63
    {
64
        if ($type == 'unit' && array_key_exists('items', $data) && $data['items']) {
65
            self::removeUnderId($dynamicUnit, $data['items']);
66
            foreach ($data['items'] as $rs) {
67
                if (array_key_exists('id', $rs) && $rs['id']) {
68
                    self::update((int) $rs['id'], array_merge(['dynamic_unit_id' => $dynamicUnit->id], $rs), 'attribute');
69
                } else {
70
                    self::create(array_merge(['dynamic_unit_id' => $dynamicUnit->id], $rs), 'attribute');
71
                }
72
            }
73
        }
74
    }
75
76
    private static function getFileSql(array $data, string $type = 'attribute'): array
77
    {
78
        $file = null;
79
        if ($type == 'attribute') {
80
            $sql = self::convertAttributeSql($data);
81
            $file = self::pullFile($data);
82
        } else {
83
            $sql = self::convertUnitSql($data);
84
        }
85
        return [$sql, $file];
86
    }
87
88
    /**
89
     * 修改动作
90
     *
91
     * @author Dennis Lui <[email protected]>
92
     * @param  int $id
93
     * @param  array $data
94
     * @return bool
95
     */
96
    public static function update(int $id, array $data, string $type): bool
97
    {
98
        $model = self::convertModel($type);
99
        if (!$item = $model::find($id)) {
100
            return false;
101
        }
102
        list($sql, $file) = self::getFileSql($data, $type);
103
        $item->fill($sql);
104
        $result = $item->save();
105
        if ($file) {
106
            $item->media && $item->media->each(fn($media) => $media->delete());
107
            $item->addMedia($file)->toMediaCollection($model::MEDIA_FILE);
108
        }
109
        self::addItems($data, $type, $item);
110
        return $result;
111
    }
112
113
    private static function removeUnderId(DynamicUnitModel $item, array $array): void
114
    {
115
        $collection = new Collection($array);
116
        $idList = $collection->filter(fn($n) => isset ($n['id']) && !empty ($n['id']))->values()->pluck('id')->toArray();
117
        if ($idList) {
118
            $item->items()->whereNotIn('id', $idList)->delete();
119
        }
120
    }
121
122
    private static function convertModel(string $type)
123
    {
124
        return $type == 'attribute' ? DynamicAttribute::class : DynamicUnitModel::class;
125
    }
126
127
128
    private static function convertSql(array $data): array|bool
129
    {
130
        $sql = [];
131
        if (array_key_exists('name', $data) && trim($data['name'])) {
132
            $sql['name'] = trim($data['name']);
133
        }
134
        if (array_key_exists('code', $data) && trim($data['code'])) {
135
            $sql['code'] = trim($data['code']);
136
        }
137
        if (!$sql)
138
            return false;
139
        $file = array_key_exists("file", $data) ? $data['file'] : null;
140
        $id = array_key_exists("id", $data) ? $data['id'] : null;
141
        return [$sql, $file, $id];
142
    }
143
144
    private static function pullFile(array $data): null|UploadedFile
145
    {
146
        if (array_key_exists('file', $data) && $data['file']) {
147
            return $data['file'] instanceof UploadedFile ? $data['file'] : null;
148
        }
149
        return null;
150
    }
151
152
    private static function convertAttributeSql(array $data): array
153
    {
154
        $sql = array_merge([
155
            'dynamic_unit_id' => 0,
156
            'name' => null,
157
            'code' => null
158
        ], $data);
159
        $collection = new Collection($sql);
160
        return $collection->only(['dynamic_unit_id', 'name', 'code'])->toArray();
161
    }
162
163
    private static function convertUnitSql(array $data): array
164
    {
165
        $sql = array_merge([
166
            'name' => null,
167
            'code' => null
168
        ], $data);
169
        $collection = new Collection($sql);
170
        return $collection->only(['name', 'code'])->toArray();
171
    }
172
}