quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
 
namespace app\supplier\model\product;
 
use app\common\library\helper;
use app\common\model\product\Product as ProductModel;
use app\common\model\settings\Setting;
use app\shop\service\ProductService;
 
/**
 * 商品模型
 */
class Product extends ProductModel
{
    /**
     * 获取器:预告开启购买时间
     */
    public function getPreviewTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }
 
    /**
     * 添加商品
     */
    public function add(array $data)
    {
        if (!isset($data['image']) || empty($data['image'])) {
            $this->error = '请上传商品图片';
            return false;
        }
        //如果支持核销 by yj
        if ($data['is_virtual'] && $data['is_verify']) {
            if ($data['verify_type'] == 20) {
                if (empty($data['verify_time'])) {
                    $this->error = '请选择核销有效时间';
                    return false;
                }
                $data["verify_start_time"] = strtotime(array_shift($data['verify_time']));
                $data["verify_end_time"] = strtotime(array_pop($data['verify_time']));
            }
            if ($data['verify_store_ids']) {
                $data['verify_store_ids'] = implode(',', array_unique($data['verify_store_ids']));
            }
        }
        $data['content'] = isset($data['content']) ? $data['content'] : '';
        $data['open_coupons'] = isset($data['open_coupons']) ? $data['open_coupons'] : [];
        $data['app_id'] = $data['sku']['app_id'] = self::$app_id;
        // 设置商品重量默认值
        $data['product_weight'] = isset($data['product_weight']) ? $data['product_weight'] : 0;
        // 单规格商品,将规格重量赋值给商品重量
        if (isset($data['spec_type']) && $data['spec_type'] == '10' && isset($data['sku']['weight'])) {
            $data['product_weight'] = $data['sku']['weight'];
        }
        // 获取分类 by lyzflash 2023.06.15
        if ($data['category_ids']) {
            $data['category_id'] = $data['category_ids'][0]; // 从商品多分类中获取第一个作为主分类 by lyzflash
        }
 
        $this->processContent($data);
        // 开启事务
        $this->startTrans();
        try {
            // 添加商品
            $this->save($data);
            // 商品规格
            $this->addProductSpec($data);
            // 商品图片
            $this->addProductImages($data['image'], $data['shop_supplier_id']);
            // 商品详情图片
            if($data['is_picture'] == 1){
                $this->addProductContentImages($data['contentImage']);
            }
            // 保存商品多分类
            $data['category_ids'] && $this->saveProductCategorys($data['category_ids']);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
    /**
     * 添加商品多分类 by lzyflash 2023.06.15
     */
    private function saveProductCategorys($data)
    {
        // 先删除所有分类
        $model = new ProductCategory;
        $model->removeAll($this['product_id']);
        $model->addCategoryList($this['product_id'], $data);
    }
 
    /**
     * 添加商品图片
     */
    private function addProductImages($images, $shop_supplier_id)
    {
        $this->image()->delete();
        $data = array_map(function ($images)  use ($shop_supplier_id)  {
            isset($images['file_id']) && $image_id = $images['file_id'];
            isset($images['image_id']) && $image_id = $images['image_id'];
            if(!isset($images['file_id']) && !isset($images['image_id'])){
                $image_id = $images['file_path'];
            }
            return [
                'image_id' => $image_id,
                'app_id' => self::$app_id
            ];
        }, $images);
        return $this->image()->saveAll($data);
    }
 
    /**
     * 添加商品详情图片
     */
    private function addProductContentImages($images)
    {
        $this->contentImage()->delete();
        $data = array_map(function ($images) {
            return [
                'image_id' => isset($images['file_id']) ? $images['file_id'] : $images['image_id'],
                'image_type' => 1,
                'app_id' => self::$app_id
            ];
        }, $images);
        return $this->contentImage()->saveAll($data);
    }
 
    /**
     * 编辑商品
     */
    public function edit($data,$shop_supplier_id)
    {
        if (!isset($data['image']) || empty($data['image'])) {
            $this->error = '请上传商品图片';
            return false;
        }
        //如果支持核销 by yj
        if ($data['is_verify']) {
            if ($data['verify_type'] == 20) {
                if (empty($data['verify_time'])) {
                    $this->error = '请选择核销有效时间';
                    return false;
                }
                $data["verify_start_time"] = strtotime(array_shift($data['verify_time']));
                $data["verify_end_time"] = strtotime(array_pop($data['verify_time']));
            }
            if ($data['verify_store_ids']) {
                $data['verify_store_ids'] = implode(',', array_unique($data['verify_store_ids']));
            }
        }
        $data['spec_type'] = isset($data['spec_type']) ? $data['spec_type'] : $this['spec_type'];
        $data['content'] = isset($data['content']) ? $data['content'] : '';
        $data['open_coupons'] = isset($data['open_coupons']) ? $data['open_coupons'] : [];
        $data['app_id'] = $data['sku']['app_id'] = self::$app_id;
        // 设置商品重量默认值
        $data['product_weight'] = isset($data['product_weight']) ? $data['product_weight'] : 0;
        // 单规格商品,将规格重量赋值给商品重量
        if (isset($data['spec_type']) && $data['spec_type'] == '10' && isset($data['sku']['weight'])) {
            $data['product_weight'] = $data['sku']['weight'];
        }
        // 获取分类 by lyzflash 2023.06.15
        if ($data['category_ids']) {
            $data['category_id'] = $data['category_ids'][0]; // 从商品多分类中获取第一个作为主分类 by lyzflash
        }
        $productSkuIdList = helper::getArrayColumn(($this['sku']), 'product_sku_id');
        return $this->transaction(function () use ($data, $productSkuIdList) {
            // 商品状态,如果已审核过的,看平台配置是否需要再次审核
            $edit_audit = Setting::getItem('store')['edit_audit'];
            if($edit_audit && $data['audit_status'] == 0){
                $data['audit_status'] = 0;
            }
            // 保存商品
            $this->save($data);
            // 商品规格
            $this->addProductSpec($data, true, $productSkuIdList);
            // 商品图片
            $this->addProductImages($data['image'], $this['shop_supplier_id']);
            // 商品详情图片
            if($data['is_picture'] == 1){
                $this->addProductContentImages($data['contentImage']);
            }
            // 保存商品多分类
            $data['category_ids'] && $this->saveProductCategorys($data['category_ids']);
            return true;
        });
    }
 
    /**
     * 添加商品规格
     */
    private function addProductSpec($data, $isUpdate = false, $productSkuIdList = [])
    {
        // 更新模式: 先删除所有规格
        $model = new ProductSku;
        $isUpdate && $model->removeAll($this['product_id']);
        $stock = 0;//总库存
        $product_price = 0;
        $line_price = 0;
        // 商城设置
        $settings = Setting::getItem('store');
        // 添加规格数据
        if ($data['spec_type'] == '10') {
            // 删除多规格遗留数据
            $isUpdate && $model->removeSkuBySpec($this['product_id']);
            // 单规格
            $this->sku()->save($data['sku']);
            $stock = $data['sku']['stock_num'];
            $product_price = $data['sku']['product_price'];
            $line_price = $data['sku']['line_price'];
        } else if ($data['spec_type'] == '20') {
            // 添加商品与规格关系记录
            $model->addProductSpecRel($this['product_id'], $data['spec_many']['spec_attr']);
            // 添加商品sku
            $model->addSkuList($this['product_id'], $data['spec_many']['spec_list'], $productSkuIdList);
            $product_price = $data['spec_many']['spec_list'][0]['spec_form']['product_price'];
            foreach ($data['spec_many']['spec_list'] as &$item) {
                $stock += $item['spec_form']['stock_num'];
                if($item['spec_form']['product_price'] < $product_price){
                    $product_price = $item['spec_form']['product_price'];
                }
                if($item['spec_form']['line_price'] < $line_price){
                    $line_price = $item['spec_form']['line_price'];
                }
            }
        }
        $save_data = [
            'product_stock' => $stock,
            'product_price' => $product_price,
            'line_price' => $line_price
        ];
        // 商品价格
        $save_data['product_price'] = $product_price;
        $this->save($save_data);
    }
 
    /**
     * 修改商品状态
     */
    public function setStatus($state)
    {
        return $this->save(['product_status' => $state]) !== false;
    }
 
    /**
     * 软删除
     */
    public function setDelete()
    {
        if (ProductService::checkSpecLocked($this, 'delete')) {
            $this->error = '当前商品正在参与其他活动,不允许删除';
            return false;
        }
        //  回收站,和未审核通过的直接删
        if($this['product_status']['value'] == 30 || $this['audit_status'] != 10){
            return $this->save(['is_delete' => 1]);
        } else{
            return $this->save(['product_status' => 30]);
        }
    }
 
    /**
     * 获取当前商品总数
     */
    public function getProductTotal($where = [])
    {
        return $this->where('is_delete', '=', 0)->where($where)->count();
    }
 
    /**
     * 获取商品告急数量总数
     */
    public function getProductStockTotal($shop_supplier_id)
    {
        return $this->where('is_delete', '=', 0)->where('product_stock', '<', 10)
            ->where('shop_supplier_id', '=', $shop_supplier_id)
            ->count();
    }
 
    /**
     * 处理内容
     */
    private function processContent(&$data)
    {
        $pattern = "/src=[\"\'](.*?)[\"\']/is";
        preg_match_all($pattern, $data['content'], $match);
    }
 
    /**
     * 组件列表
     */
    public function getProductList($shop_supplier_id, $params)
    {
        return $this->with(['image.file'])->where('shop_supplier_id', '=', $shop_supplier_id)
            ->where('audit_status', '=', 10)
            ->where('product_status', '=', 10)
            ->where('is_delete', '=', 0)
            ->paginate($params);
    }
 
    /**
     * 获取数量
     */
    public function getCount($type, $shop_supplier_id)
    {
        $model = $this;
        $model = $this->buildProductType($model, $type);
        if($shop_supplier_id != 0){
            $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
        }
        return $model->where('is_delete', '=', 0)
            ->count();
    }
 
    /**
     * 查询指定商品
     * @param $value
     */
    public function getProduct($value)
    {
        return $this->where('product_id', 'in', $value)->select();
    }
 
}