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
<?php
 
namespace app\api\model\plus\groupbuy;
 
use app\common\exception\BaseException;
use app\common\model\plus\groupbuy\Product as ProductModel;
use app\api\model\product\Product as ProductModelApi;
use app\api\model\supplier\Supplier as SupplierModel;
 
/**
 * 团购商品模型(API端)
 */
class Product extends ProductModel
{
    /**
     * 隐藏字段
     */
    protected $hidden = [
        'sales_initial',
        'total_sales',
        'is_delete',
        'app_id',
        'create_time',
        'update_time'
    ];
 
    /**
     * 获取团购商品详情
     */
    public static function detail($groupbuy_product_id, $with = [])
    {
        return (new static())->with($with)->where('groupbuy_product_id', '=', $groupbuy_product_id)->find();
    }
 
    /**
     * 获取团购商品列表
     */
    public function getProductList($groupbuy_active_id, $limit)
    {
        // 获取列表数据
        $list = $this->alias("a")
            ->with(['product.image.file', 'groupbuySku'])
            ->join('product product', 'product.product_id=a.product_id')
            ->join('supplier supplier', 'product.shop_supplier_id = supplier.shop_supplier_id', 'left')
            ->where('groupbuy_active_id', '=', $groupbuy_active_id)
            ->where('a.is_delete', '=', 0)
            ->where('a.status', '=', 10)
            ->where('a.state', '=', 10)
            ->where('product.is_delete', '=', 0)
            ->where('supplier.is_delete', '=', 0)
            ->where('supplier.status', '=', 0)
            ->limit($limit)
            ->select();
 
        $data = [];
        foreach ($list as $item) {
            $data[] = [
                'product' => $item['product'],
                'groupbuySku' => $item['groupbuySku'],
                'groupbuy_price' => $item['groupbuySku'][0]['groupbuy_price'],
                'product_price' => $item['groupbuySku'][0]['product_price'],
                'groupbuy_num' => $item['groupbuy_num'],
                'limit_num' => $item['limit_num'],
                'stock' => $item['groupbuySku'][0]['groupbuy_stock'],
                'product_sales' => $item['product']['product_sales'] + $item['sales_initial'],
            ];
        }
        return $data;
    }
 
    /**
     * 获取团购商品规格详情
     */
    public function getProductSku($groupbuy_product_id, $product_sku_id)
    {
        $product = $this->alias('gp')
            ->field([
                'gp.groupbuy_product_id',
                'gp.product_id',
                'gp.limit_num',
                'gp.groupbuy_num',
                'gp.stock',
                'gp.sales_initial',
                'gp.total_sales',
                'p.product_name',
                'p.product_no',
                'p.product_status',
                'p.product_image',
                'p.product_price',
                'p.line_price',
                'p.product_sales',
                'p.content',
                'p.spec_type',
                'ps.product_sku_id',
                'ps.spec_sku_id',
                'ps.product_attr',
                'ps.product_no as sku_product_no',
                'ps.product_price as sku_product_price',
                'ps.line_price as sku_line_price',
                'ps.stock_num as stock_num',
                'gps.groupbuy_price',
                'gps.groupbuy_stock',
            ])
            ->join('product p', 'p.product_id = gp.product_id')
            ->join('product_sku ps', 'ps.product_id = p.product_id')
            ->join('groupbuy_product_sku gps', 'gps.product_sku_id = ps.product_sku_id AND gps.groupbuy_product_id = gp.groupbuy_product_id')
            ->where('gp.groupbuy_product_id', '=', $groupbuy_product_id)
            ->where('ps.product_sku_id', '=', $product_sku_id)
            ->find();
 
        if (!$product) {
            throw new BaseException(['msg' => '未找到该团购商品']);
        }
 
        return $product;
    }
 
    /**
     * 获取团购商品列表(用于API)
     */
    public function getProductListByApi($params = [])
    {
        $model = $this->alias('gp')
            ->field([
                'gp.groupbuy_product_id',
                'gp.product_id',
                'gp.limit_num',
                'gp.groupbuy_num',
                'gp.stock',
                'gp.sales_initial',
                'gp.total_sales',
                'gp.sort',
                'p.product_name',
                'p.product_no',
                'p.product_status',
                'p.product_image',
                'p.product_price',
                'p.line_price',
                'p.product_sales',
                'p.content',
                'p.spec_type',
                'gps.groupbuy_price',
                'gps.groupbuy_stock',
                's.name as supplier_name',
                's.server_score',
            ])
            ->join('product p', 'p.product_id = gp.product_id')
            ->join('supplier s', 's.shop_supplier_id = p.shop_supplier_id')
            ->join('groupbuy_product_sku gps', 'gps.groupbuy_product_id = gp.groupbuy_product_id')
            ->where('gp.is_delete', '=', 0)
            ->where('gp.status', '=', 10)
            ->where('gp.state', '=', 10)
            ->where('p.is_delete', '=', 0)
            ->where('s.is_delete', '=', 0)
            ->where('s.status', '=', 0);
 
        // 筛选条件
        if (isset($params['groupbuy_active_id']) && $params['groupbuy_active_id']) {
            $model = $model->where('gp.groupbuy_active_id', '=', $params['groupbuy_active_id']);
        }
 
        if (isset($params['keyword']) && $params['keyword']) {
            $model = $model->where('p.product_name', 'like', '%' . $params['keyword'] . '%');
        }
 
        if (isset($params['shop_supplier_id']) && $params['shop_supplier_id']) {
            $model = $model->where('p.shop_supplier_id', '=', $params['shop_supplier_id']);
        }
 
        $list = $model->order(['gp.sort' => 'asc', 'gp.create_time' => 'desc'])
            ->paginate($params);
 
        return $list;
    }
}