quanwei
2025-12-09 ca425b889f3c1b5847ffc26a0229307f7f8ef43e
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
<?php
 
namespace app\api\model\plus\assemble;
 
use app\common\exception\BaseException;
use app\common\model\plus\assemble\Product as AssembleProductModel;
use app\api\model\product\Product as ProductModel;
use app\api\model\supplier\Supplier as SupplierModel;
use app\api\model\supplier\ServiceApply;
/**
 * 限时拼团模型
 */
class Product extends AssembleProductModel
{
    /**
     * 隐藏字段
     */
    protected $hidden = [
        'sales_initial',
        'total_sales',
        'is_delete',
        'app_id',
        'create_time',
        'update_time'
    ];
    /**
     * 获取首页拼团商品显示
     */
    public function getProductList($assemble_activity_id, $limit)
    {
        // 获取列表数据
        $list = $this->alias("a")
            ->with(['product.image.file', 'assembleSku'])
            ->join('product product', 'product.product_id=a.product_id')
            ->join('supplier supplier', 'product.shop_supplier_id = supplier.shop_supplier_id', 'left')
            ->where('assemble_activity_id', '=', $assemble_activity_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)
            ->where('supplier.is_recycle', '=', 0)
            ->where('product.product_status', '=', 10)
            ->where('product.audit_status', '=', 10)
            ->field('a.*')
            ->limit($limit)
            ->visible(['product.product_id','product.product_name','product.file_path'])
            ->select();
 
        foreach ($list as $key => $product) {
            $assemble_arr = array_column($product['assembleSku']->toArray(), 'assemble_price');
            $product_arr = array_column($product['assembleSku']->toArray(), 'product_price');
            sort($assemble_arr);
            sort($product_arr);
            $product['assemble_price'] = current($assemble_arr);
            $product['product_price'] = current($product_arr);
            $product['product']['file_path'] = $product['product']['image'][0]['file_path'];
            unset($product['assembleSku']);
            unset($product['product']['image']);
        }
        return $list;
    }
 
    /**
     * 获取列表页拼团数据
     * 目前未分页,后续有可能会分页
     */
    public function getActivityList($assemble_activity_id)
    {
        // 获取列表数据
        $list = $this->alias("a")
            ->with(['product.image.file', 'assembleSku'])
            ->join('product product', 'product.product_id=a.product_id')
            ->join('supplier supplier', 'product.shop_supplier_id = supplier.shop_supplier_id', 'left')
            ->where('assemble_activity_id', '=', $assemble_activity_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)
            ->where('supplier.is_recycle', '=', 0)
            ->where('product.product_status', '=', 10)
            ->where('product.audit_status', '=', 10)
            ->field('a.*')
            ->visible(['product.product_id','product.product_name','product.file_path'])
            ->select();
 
 
        foreach ($list as $product) {
            $assemble_arr = array_column($product['assembleSku']->toArray(), 'assemble_price');
            $product_arr = array_column($product['assembleSku']->toArray(), 'product_price');
            sort($assemble_arr);
            sort($product_arr);
            $product['assemble_price'] =  current($assemble_arr);
            $product['product_price'] =  current($product_arr);
            $product['product']['file_path'] = $product['product']['image'][0]['file_path'];
            unset($product['assembleSku']);
            unset($product['product']['image']);
        }
        return $list;
    }
 
    /**
     * 获取拼团商品列表
     */
    public static function getAssembleProduct($params)
    {
        // 拼团详情
        $assemble = self::detail($params['assemble_product_id'], ['assembleSku']);
 
        if (empty($assemble)) {
            throw new BaseException(['msg' => '拼团商品不存在或已结束']);
        }
        // 拼团商品详情
        $product = ProductModel::detail($assemble['product_id']);
        // 拼团商品sku信息
        $assemble_sku = null;
        if ($product['spec_type'] == 10) {
            $assemble_sku = $assemble['assembleSku'][0];
        } else {
            //多规格
            foreach ($assemble['assembleSku'] as $sku) {
                if ($sku['assemble_product_sku_id'] == $params['assemble_product_sku_id']) {
                    $assemble_sku = $sku;
                    break;
                }
            }
        }
        if ($assemble_sku == null) {
            throw new BaseException(['msg' => '拼团商品规格不存在']);
        }
 
        // 拼团商品sku信息
        $product['product_sku'] = ProductModel::getProductSku($product, $params['product_sku_id']);
        $product['assemble_sku'] = $assemble_sku;
        // 拼团商品列表
        $productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
        foreach ($productList as &$item) {
            // 商品单价
            $item['product_price'] = $assemble_sku['assemble_price'];
            // 商品购买数量
            $item['total_num'] = $params['product_num'];
            $item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
            // 商品购买总金额
            $item['total_price'] = $assemble_sku['assemble_price'] * $item['total_num'];
            $item['point_num'] = $assemble_sku['point_num'];
            $item['assemble_product_sku_id'] = $assemble_sku['assemble_product_sku_id'];
            $item['product_sku_id'] = $params['product_sku_id'];
            $item['product_source_id'] = $assemble_sku['assemble_product_id'];
            $item['sku_source_id'] = $assemble_sku['assemble_product_sku_id'];
            // 拼团活动id
            $item['activity_id'] = $assemble['assemble_activity_id'];
            // 拼团订单id
            $item['bill_source_id'] = $params['assemble_bill_id'];
            // 拼团最大购买数
            $item['assemble_product'] = [
                'limit_num' => $assemble['limit_num']
            ];
        }
        $supplierData[] = [
            'shop_supplier_id' => $product['shop_supplier_id'],
            'supplier' => $product['supplier'],
            'productList' => $productList
        ];
        unset($product['supplier']);
        return $supplierData;
    }
 
    /**
     * 拼团商品详情
     */
    public function getAssembleDetail($assemble_product_id)
    {
        $result = $this->with(['product' => ['image.file', 'contentImage.file'], 'assembleSku.productSku.image'])
            ->where('assemble_product_id', '=', $assemble_product_id)->find();
 
        if (!empty($result)) {
            $assemble_arr = array_column($result->toArray()['assembleSku'], 'assemble_price');
            $product_arr = array_column($result->toArray()['assembleSku'], 'product_price');
            sort($assemble_arr);
            sort($product_arr);
            $result['assemble_price'] =  current($assemble_arr);
            $result['line_price'] = current($product_arr);
            if (count($assemble_arr) > 1) {
                $res['assemble_high_price'] = end($assemble_arr);
                $res['line_high_price'] = end($product_arr);
            }
            $SupplierModel = new SupplierModel;
            if ($result['shop_supplier_id']) {
                $supplier = $SupplierModel::detail($result['shop_supplier_id'], ['logo', 'category']);
                $supplier['logos'] = $supplier['logo']['file_path'];
                unset($supplier['logo']);
                $supplier['category_name'] = $supplier['category']['name'];
                unset($supplier['category']);
                $supplier->visible(['logos', 'category_name', 'name', 'shop_supplier_id', 'product_sales', 'server_score', 'store_type']);
                $server = (new ServiceApply())->getList($result['shop_supplier_id']);
            } else {
                $supplier = [];
                $server = [];
            }
            $result['supplier'] = $supplier;
            $result['server'] = $server;
        }
        return $result;
    }
}