quanwei
2026-01-17 e1e2fe5710a5b5cd9c19bd3aa99c998a1a613ca8
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
<?php
 
namespace app\supplier\model\plus\product;
 
use app\common\model\plus\product\Reduce as ReduceModel;
use app\common\model\product\Category;
use app\supplier\model\product\Product as ProductModel;
use app\common\model\shop\FullReduce as FullReduceModel;
 
/**
 * 商品满减
 */
class Reduce extends ReduceModel
{
    public function getList($data){
        $model = new ProductModel();
        if(isset($data['is_join']) && $data['is_join'] != -1){
            if($data['is_join'] == 1){
                $model = $model->where('reduce.product_id', '>', 0);
            }else if($data['is_join'] == 0){
                $model = $model->where('reduce.product_id', '=', null);
            }
        }
        if ($data['category_id'] > 0) {
            $arr = Category::getSubCategoryId($data['category_id']);
            $model = $model->where('category_id', 'IN', $arr);
        }
        if (!empty($data['product_name'])) {
            $model = $model->where('product_name', 'like', '%' . trim($data['product_name']) . '%');
        }
        $list = $model->field(['product.product_id,product.product_name,product.product_price,product.product_stock,product.sales_actual,product.sales_initial,reduce.product_id as reduce_pid'])->alias('product')->with(['image.file'])
            ->join('product_reduce reduce', 'reduce.product_id = product.product_id','left')
            ->order(['reduce.create_time' => 'desc', 'product.create_time' => 'desc'])
            ->paginate($data);
        $full_model = new FullReduceModel();
        foreach ($list as &$item){
            if($item['reduce_pid'] != null){
                $item['reduce_list'] = $full_model->where('product_id', '=', $item['reduce_pid'])->select();
            }else{
                $item['reduce_list'] = [];
            }
        }
        return $list;
    }
 
    public function edit($data, $shop_supplier_id){
        $this->startTrans();
        try {
            $full_model = new FullReduceModel();
            $this->where('product_id','=', $data['product_id'])->delete();
            $full_model->where('product_id','=', $data['product_id'])->delete();
            if($data['is_join'] == 1){
                $this->save([
                    'product_id' => $data['product_id'],
                    'app_id' => self::$app_id
                ]);
                $save_data = [];
                foreach ($data['reduce_list'] as $item){
                    $save_data[] = [
                        'active_name' => '商品满减',
                        'full_type' => $data['full_type'],
                        'full_value' => $item['full_value'],
                        'reduce_type' => $data['reduce_type'],
                        'reduce_value' => $item['reduce_value'],
                        'product_id' => $data['product_id'],
                        'shop_supplier_id' => $shop_supplier_id,
                        'app_id' => self::$app_id
                    ];
                }
                $full_model->saveAll($save_data);
            }
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
}