Passed
Push — master ( f22075...1dfec9 )
by Dennis
04:02
created

DynamicQuery::convertSql()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 16
rs 9.4888
cc 5
nc 5
nop 1
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
                self::createOrUpdateAttribute($rs, $dynamicUnit);
68
            }
69
        }
70
    }
71
72
    private static function createOrUpdateAttribute(array $data, DynamicUnitModel $dynamicUnitModel)
73
    {
74
        if (array_key_exists('id', $data) && $data['id']) {
75
            self::update((int) $data['id'], array_merge(['dynamic_unit_id' => $dynamicUnitModel->id], $data), 'attribute');
76
        } else {
77
            self::create(array_merge(['dynamic_unit_id' => $dynamicUnitModel->id], $data), 'attribute');
78
        }
79
    }
80
81
    private static function getFileSql(array $data, string $type = 'attribute'): array
82
    {
83
        $file = null;
84
        if ($type == 'attribute') {
85
            $sql = self::convertAttributeSql($data);
86
            $file = self::pullFile($data);
87
        } else {
88
            $sql = self::convertUnitSql($data);
89
        }
90
        return [$sql, $file];
91
    }
92
93
    /**
94
     * 修改动作
95
     *
96
     * @author Dennis Lui <[email protected]>
97
     * @param  int $id
98
     * @param  array $data
99
     * @return bool
100
     */
101
    public static function update(int $id, array $data, string $type): bool
102
    {
103
        $model = self::convertModel($type);
104
        if (!$item = $model::find($id)) {
105
            return false;
106
        }
107
        list($sql, $file) = self::getFileSql($data, $type);
108
        $item->fill($sql);
109
        $result = $item->save();
110
        if ($file) {
111
            $item->media && $item->media->each(fn($media) => $media->delete());
112
            $item->addMedia($file)->toMediaCollection($model::MEDIA_FILE);
113
        }
114
        self::addItems($data, $type, $item);
115
        return $result;
116
    }
117
118
    private static function removeUnderId(DynamicUnitModel $item, array $array): void
119
    {
120
        $collection = new Collection($array);
121
        $idList = $collection->filter(fn($n) => isset ($n['id']) && !empty ($n['id']))->values()->pluck('id')->toArray();
122
        if ($idList) {
123
            $item->items()->whereNotIn('id', $idList)->delete();
124
        }
125
    }
126
127
    private static function convertModel(string $type)
128
    {
129
        return $type == 'attribute' ? DynamicAttribute::class : DynamicUnitModel::class;
130
    }
131
132
133
    private static function convertSql(array $data): array|bool
134
    {
135
        $sql = [];
136
        $map = array_merge([
137
            'file' => null,
138
            'id' => null,
139
            'name' => null,
140
            'code' => null
141
        ], $data);
142
        if (!$map['name'] && !$map['code'])
143
            return false;
144
        if ($map['name'])
145
            $sql['name'] = $map['name'];
146
        if ($map['code'])
147
            $sql['code'] = $map['code'];
148
        return [$sql, $map['file'], $map['id']];
149
    }
150
151
    private static function pullFile(array $data): null|UploadedFile
152
    {
153
        if (array_key_exists('file', $data) && $data['file']) {
154
            return $data['file'] instanceof UploadedFile ? $data['file'] : null;
155
        }
156
        return null;
157
    }
158
159
    private static function convertAttributeSql(array $data): array
160
    {
161
        $sql = array_merge([
162
            'dynamic_unit_id' => 0,
163
            'name' => null,
164
            'code' => null
165
        ], $data);
166
        $collection = new Collection($sql);
167
        return $collection->only(['dynamic_unit_id', 'name', 'code'])->toArray();
168
    }
169
170
    private static function convertUnitSql(array $data): array
171
    {
172
        $sql = array_merge([
173
            'name' => null,
174
            'code' => null
175
        ], $data);
176
        $collection = new Collection($sql);
177
        return $collection->only(['name', 'code'])->toArray();
178
    }
179
}