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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
 
namespace app\common\model\branch;
 
use app\common\library\helper;
use app\common\model\BaseModel;
use app\common\model\plus\table\Record as RecordModel;
use app\common\model\product\Product as ProductModel;
use app\common\model\branch\ActivityCart as ActivityCartModel;
use app\shop\service\ProductService;
 
/**
 * 参与活动的商品价格模型
 * @property int $id
 * @property int $activity_id 活动id
 * @property int $product_id 产品id
 * @property int $product_sku_id 产品的属性id
 * @property float $product_price 产品的价格
 * @property int $app_id 小程序id
 * @property int $shop_supplier_id 商户id
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 * @property int $is_delete 是否删除
 */
class ActivityProductPrice extends BaseModel
{
    protected $name = 'branch_activity_product_price';
    protected $pk = 'id';
 
    protected $type = [
        'can_user_ids'=>'json'
    ];
 
    /**
     * 关联商品规格表
     */
    public function sku()
    {
        return $this->hasMany('app\\common\\model\\product\\ProductSku')->order(['product_sku_id' => 'asc']);
    }
 
    /**
     * 关联商品规格关系表
     */
    public function specRel()
    {
        return $this->belongsToMany('app\\common\\model\\product\\SpecValue', 'ProductSpecRel', '')->order(['id' => 'asc']);
    }
 
    /**
     * 关联商品图片表
     */
    public function image()
    {
        return $this->hasMany('app\\common\\model\\product\\ProductImage')->where('image_type', '=', 0)->order(['id' => 'asc']);
    }
 
    /**
    读取活动设置的商品列表
     **/
    public function getProductPricelist($id=''){
        $model = new static();
        $list = $model
            ->where('is_delete','=',0)
            ->where(['activity_id'=>$id])
            ->hidden(['create_time','is_delete','update_time','activity_id','app_id','shop_supplier_id'])
            ->select()->toArray();
        if(empty($list)){
            return [];
        }
        foreach ($list as &$item){
            $model = ProductModel::detail($item['product_id']);
            $item['product_name'] = $model['product_name'];
            $item['image'] = $model['image'][0]['file_path'] ?? 0;
            $specData = ProductService::getSpecData($model);
            $specList = [];
            if(!empty($specData)){
               $specList = $this->transSpecData($specData);
                foreach ($specList as $otem){
                    if($item['product_sku_id']==$otem['product_sku_id']){
                        $item['product_sku_name'] = $otem['spec_name'];
                    }
                }
            }else{
                $item['product_sku_name'] = '单规格';
            }
        }
        unset($item);
        return $list;
    }
 
    /**
    读取某个活动的所有商品
     **/
    public function getProductlist($activity_id=''){
        $model = new static();
        $list = $model
            ->where('is_delete','=',0)
            ->where(['activity_id'=>$activity_id])
            ->field('*,MIN(product_new_price) as min_price')
            ->hidden(['create_time','is_delete','update_time','activity_id','app_id'])
            ->group('product_id') // 按product_id分组
            ->select();
        if(empty($list)){
            return [];
        }
        $back_column =  $model->where('is_delete','=',0)->where(['activity_id'=>$activity_id])->column('product_sku_id','spec_sku_id');
        $spec_sku_id_arr = array_keys($back_column);
        $spec_sku_id = [];
        foreach ($spec_sku_id_arr as $key=> $dd){
            if($dd!=0){
               $new_arr =  explode('_',$dd);
                $spec_sku_id[] = $new_arr[0];
            }
        }
        $product_sku_id = array_values($back_column);
        $new_list = [];
        foreach ($list as &$item){
            $products = $this->detail($item['product_id'],$product_sku_id);
            if(!$products){
               continue;
            }
            // 整理商品数据并返回
            $item = $this->setProductListData($products, false,$item);
            $item['select_spec'] = [];
            // 多规格商品sku信息
            $item['specData'] = $item['spec_type'] == 20 ? $this->getManySpecData($item['spec_rel'], $item['sku'],$spec_sku_id,$activity_id,$item['product_id']) : null;
            //获取商品最大价格
            $productPrice = helper::getArrayColumn($item['sku'], 'product_price');
            rsort($productPrice);
            $item['product_max_price'] = isset($productPrice[0]) ? $productPrice[0] : 0;
            $new_list[] = $item;
        }
        unset($item);
        return $new_list;
    }
 
    /**
     * 获取商品详情
     */
    public  function detail($product_id, $product_sku_id)
    {
        $infos = (new ProductModel())->with([
            'image.file',
            'sku.image'=>function($query) use($product_sku_id){
            $query->where(['product_sku_id'=>$product_sku_id]);
            },
            'spec_rel.spec',
        ])->where('product_id', '=', $product_id)
            ->where('product_status','=',10)
            ->find();
        if($infos){
           return  $infos->toArray();
        }
        return $infos;
    }
 
    /**
     * 商品多规格信息
     */
    public function getManySpecData($specRel, $skuData, $spec_sku_id, $activity_id = 0, $product_id)
    {
        $price_model = new static();
        // spec_attr
        $specAttrData = [];
        foreach ($specRel as $key=>$item) {
            if($key==0 && !in_array($item['spec_id'],$spec_sku_id)){
                continue;
            }
            if (!isset($specAttrData[$item['spec_id']])) {
                $specAttrData[$item['spec_id']] = [
                    'group_id' => $item['spec']['spec_id'],
                    'group_name' => $item['spec']['spec_name'],
                    'spec_items' => [],
                ];
            }
            $specAttrData[$item['spec_id']]['spec_items'][] = [
                'item_id' => $item['spec_value_id'],
                'spec_value' => $item['spec_value'],
            ];
        }
        // spec_list
        $specListData = [];
        foreach ($skuData as $item) {
            $image = (isset($item['image']) && !empty($item['image'])) ? $item['image'] : ['file_id' => 0, 'file_path' => ''];
            $product_price = $price_model->where(['spec_sku_id'=>$item['spec_sku_id'],'product_id'=>$product_id,'activity_id'=>$activity_id,'is_delete'=>0])->value('product_new_price');
            $specListData[] = [
                'product_sku_id' => $item['product_sku_id'],
                'spec_sku_id' => $item['spec_sku_id'],
                'rows' => [],
                'spec_form' => [
                    'image_id' => $image['file_id'],
                    'image_path' => $image['file_path'],
                    'product_no' => $item['product_no'],
                    'product_price' => $product_price,
                    'product_weight' => $item['product_weight'],
                    'line_price' => $item['line_price'],
                    'stock_num' => $item['stock_num']
                ],
            ];
        }
        return ['spec_attr' => array_values($specAttrData), 'spec_list' => $specListData];
    }
 
    /**
     * 设置商品展示的数据
     */
    protected function setProductListData($data, $isMultiple = true, $product_price=[])
    {
 
        if (!$isMultiple) $dataSource = [&$data]; else $dataSource = &$data;
        // 整理商品列表数据
        foreach ($dataSource as &$product) {
            // 商品主图
            $product['product_image'] = $product['image'][0]['file_path'];
            // 商品默认规格
            $product['product_sku'] = $this->getShowSku($product,$product_price);
            // 可核销门店ids by yj
            if(!is_array($product['verify_store_ids'])){
                if($product['verify_store_ids'] != ''){
                    $product['verify_store_ids'] = explode(',', $product['verify_store_ids']);
                } else {
                    $product['verify_store_ids'] = [];
                }
            }
            // 核销有效时间段
            if($product['verify_type'] == 20){
                $product['verify_time'] = [date('Y-m-d H:i:s', $product['verify_start_time']), date('Y-m-d H:i:s', $product['verify_end_time'])];
            }
        }
        return $data;
    }
 
    /**
     * 显示的sku,目前取最低价
     */
    protected  function getShowSku($product,$product_price=[])
    {
        //如果是单规格
        if($product['spec_type'] == 10){
            if(empty($product['sku'])){
                $product['sku'] = [
                    ['product_price'=>$product_price['product_new_price'],'line_price'=>$product['line_price']]
                ];
            }else{
                $product['sku'][0]['product_price'] = $product_price['product_new_price'];
            }
            return $product['sku'][0];
        }else{
            if(empty($product['sku'])){
                $product['sku'] = [
                    ['product_price'=>$product_price['product_new_price'],'line_price'=>$product['line_price']]
                ];
            }else{
                $product['sku'][0]['product_price'] = $product_price['product_new_price'];
            }
        }
        // 兼容历史数据,如果找不到返回第一个
        return isset($product['sku'][0]) ? $product['sku'][0]: $product['sku'];
    }
 
 
    /**
     * 组装前端用的数据
     */
    private function transSpecData($specData){
        $specList = [];
        foreach($specData['spec_list'] as $spec){
            $specIds = explode('_',$spec['spec_sku_id']);
            $spec['spec_name'] = '';
            foreach ($specIds as $specId){
                $spec['spec_name'] .= $this->searchSpecItem($specData['spec_attr'], $specId) . ';';
            }
            array_push($specList, $spec);
        }
        return $specList;
    }
 
    /**
     * 规格值
     */
    private function searchSpecItem($spec_attr, $item_id){
        $specValue = '';
        foreach ($spec_attr as $attr){
            foreach ($attr['spec_items'] as $item){
                if($item['item_id'] == $item_id){
                    $specValue = $attr['group_name'] . ',' . $item['spec_value'];
                    break 2;
                }
            }
        }
        return $specValue;
    }
 
    /**
     * 立即购买:获取订单商品列表
     */
    public static function getOrderProductListByNow($params)
    {
        $price_model = new static();
        // 商品详情
        $product = \app\api\model\product\Product::detail($params['product_id']);
        // 商品sku信息
        $product['product_sku'] = ProductModel::getProductSku($product, $params['product_sku_id']);
        // 自定义表单数据id,直接读取最新的那条记录 by lyzflash
        $product['table_record_id'] = 0;
        // 商品列表
        $productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
        foreach ($productList as $key=> &$item) {
            $item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
            //获取规格的图片 by yj 2024.1.16
            if(!empty($product['product_sku']) && !empty($product['product_sku']["image"])){
                $item['product_image'] = $product['product_sku']["image"]["file_path"];
            }
            $product_price = $price_model->where(['spec_sku_id'=>$item['spec_sku_id'],'product_id'=>$item['product_id'],'activity_id'=>$params['activity_id'],'is_delete'=>0])->find();
            if(!$product_price){
                unset($productList[$key]);
            }
 
            // 商品单价
            $item['product_price'] =  $product_price['product_new_price'];
            //商品服务费
            if(!empty($item["product_service"]) && isset($params["service_index"])){
                $product_service = $item["product_service"][$params["service_index"]];
                $item['service_name'] = $product_service["service_name"];
                $item['service_price'] = $product_service["price"];
            }
            // 商品购买数量
            $item['total_num'] = $params['product_num'];
            // 商品购买总金额
            $item['total_price'] = helper::bcmul($item['product_price'], $params['product_num']);
        }
        $supplierData[] = [
            'shop_supplier_id' => $product['shop_supplier_id'],
            'supplier' => $product['supplier'],
            'productList' => $productList
        ];
        unset($product['supplier']);
        return $supplierData;
    }
 
}