quanwei
2 days ago 04102f7237efefa744090ed7c25f7b5d0807b679
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
<?php
 
namespace app\api\model\plus\groupbuy;
 
use app\common\model\plus\groupbuy\Active as ActiveModel;
 
/**
 * 团购活动模型(API端)
 */
class Active extends ActiveModel
{
    /**
     * 获取活动列表
     */
    public function getList($param)
    {
        $model = $this;
        if (isset($param['status']) && $param['status'] > -1) {
            $model = $model->where('status', '=', $param['status']);
        }
        if (isset($param['title']) && !empty($param['title'])) {
            // 修改为正确的字段名 active_name
            $model = $model->where('active_name', 'like', '%' . trim($param['title']) . '%');
        }
        $res = $model->with(['file'])
            ->order('create_time', 'desc')
            ->paginate($param);
 
        foreach ($res as $key => $val) {
            $res[$key]['start_time'] = format_time($val['start_time']);
            $res[$key]['end_time'] = format_time($val['end_time']);
        }
        return $res;
    }
 
    /**
     * 获取有效的团购活动
     */
    public static function getValidActive()
    {
        return (new static())
            ->where('start_time', '<=', time())
            ->where('end_time', '>', time())
            ->where('status', '=', 1)
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->find();
    }
 
    /**
     * 获取团购商品列表
     */
    public function getGroupbuyProductList($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;
    }
}