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
<?php
 
namespace app\operations\model\plus\groupbuy;
 
use app\common\model\plus\groupbuy\Active as ActiveModel;
 
/**
 * 商家端团购活动模型
 */
class Active extends ActiveModel
{
    // 定义全局的查询范围
    protected $globalScope = ['status'];
    public function scopeStatus($query)
    {
        $shop_supplier_ids = \app\operations\model\supplier\Supplier::getSupplierIdsByUser(session('jjjshop_operations')['user']);
        if (empty($shop_supplier_ids)){
            $query->where($query->getTable() . '.shop_supplier_id', -1);
        }else{
            $query->where($query->getTable() . '.shop_supplier_id', 'in', $shop_supplier_ids);
        }
    }
    /**
     * 获取团购活动列表
     */
    public function getList($param)
    {
        $filter = [];
        if (!empty($param['search'])) {
            $filter[] = ['active_name', 'like', '%' . trim($param['search']) . '%'];
        }
        
        if (isset($param['status']) && $param['status'] !== '') {
            $filter[] = ['status', '=', (int)$param['status']];
        }
        
        // 店铺ID筛选 - 如果提供了shop_supplier_id参数,则按供应商筛选
        if (isset($param['shop_supplier_id']) && $param['shop_supplier_id'] !== '') {
            $filter[] = ['shop_supplier_id', '=', (int)$param['shop_supplier_id']];
        }
        
        // 时间范围筛选
        if (!empty($param['start_time']) && !empty($param['end_time'])) {
            $filter[] = ['start_time', '>=', strtotime($param['start_time'])];
            $filter[] = ['end_time', '<=', strtotime($param['end_time'])];
        }
        
        $order = ['create_time' => 'desc'];
        if (!empty($param['order'])) {
            $order = [$param['order_field'] => $param['order']];
        }
        
        return $this->with(['file'])
            ->where($filter)
            ->order($order)
            ->paginate([
                'list_rows' => $param['list_rows'] ?? 15,
                'query' => $param
            ]);
    }
 
    /**
     * 添加团购活动
     */
    public function add($data)
    {
        $this->startTrans();
        try {
            // 格式化数据
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            $data['app_id'] = self::$app_id;
            
            $result = $this->save($data);
            $this->commit();
            return $result !== false;
        } catch (\Exception $e) {
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }
 
    /**
     * 编辑团购活动
     */
    public function edit($data)
    {
        $this->startTrans();
        try {
            // 格式化数据
            if (isset($data['start_time'])) {
                $data['start_time'] = strtotime($data['start_time']);
            }
            if (isset($data['end_time'])) {
                $data['end_time'] = strtotime($data['end_time']);
            }
            
            $result = $this->save($data);
            $this->commit();
            return $result !== false;
        } catch (\Exception $e) {
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }
 
    /**
     * 更新状态
     */
    public function updateStatus($status)
    {
        return $this->save(['status' => $status]) !== false;
    }
 
    /**
     * 删除团购活动
     */
    public function remove()
    {
        // 检查是否有团购商品关联
        $productModel = new \app\operations\model\plus\groupbuy\Product();
        $count = $productModel->where('groupbuy_active_id', $this->groupbuy_active_id)->count();
        if ($count > 0) {
            $this->error = '该活动下还有团购商品,无法删除';
            return false;
        }
        
        return $this->delete();
    }
}