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
<?php
 
namespace app\supplier\model\plus\groupbuy;
 
use app\common\model\plus\groupbuy\Product as ProductModel;
 
/**
 * 供应商端团购商品模型
 */
class Product extends ProductModel
{
    /**
     * 获取团购商品列表
     */
    public function getList($param)
    {
        $filter = [];
        if (!empty($param['search'])) {
            $filter[] = ['p.product_name', 'like', '%' . trim($param['search']) . '%'];
        }
        
        if (!empty($param['groupbuy_active_id'])) {
            $filter[] = ['gp.groupbuy_active_id', '=', (int)$param['groupbuy_active_id']];
        }
        
        // 供应商ID筛选 - 使用正确的字段名
        $filter[] = ['gp.shop_supplier_id', '=', $param['shop_supplier_id'] ?? 0];
        
        $order = ['gp.create_time' => 'desc'];
        if (!empty($param['order'])) {
            $order = [$param['order_field'] => $param['order']];
        }
        
        return $this->with(['product', 'groupbuySku'])
            ->alias('gp')
            ->join('product p', 'p.product_id = gp.product_id')
            ->where($filter)
            ->order($order)
            ->paginate([
                'list_rows' => $param['list_rows'] ?? 15,
                'query' => $param
            ]);
    }
 
    /**
     * 添加团购商品
     */
    public function add($data)
    {
        $this->startTrans();
        try {
            // 检查商品是否已存在该活动中
            $exist = $this->where([
                'product_id' => $data['product_id'],
                'groupbuy_active_id' => $data['groupbuy_active_id']
            ])->find();
            if ($exist) {
                $this->error = '该商品已在当前活动中';
                return false;
            }
            
            // 保存团购商品基本信息
            $result = $this->save($data);
            
            if ($result !== false) {
                // 处理团购SKU
                if (!empty($data['sku_list'])) {
                    $this->handleGroupbuySku($data['sku_list']);
                }
            }
            
            $this->commit();
            return $result !== false;
        } catch (\Exception $e) {
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }
 
    /**
     * 编辑团购商品
     */
    public function edit($data)
    {
        $this->startTrans();
        try {
            $result = $this->save($data);
            
            if ($result !== false && !empty($data['sku_list'])) {
                // 更新团购SKU
                $this->handleGroupbuySku($data['sku_list']);
            }
            
            $this->commit();
            return $result !== false;
        } catch (\Exception $e) {
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }
 
    /**
     * 处理团购SKU数据
     */
    private function handleGroupbuySku($skuList)
    {
        $skuModel = new \app\supplier\model\plus\groupbuy\ProductSku();
        
        // 先删除原有SKU
        $skuModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete();
        
        // 添加新的团购SKU
        foreach ($skuList as $sku) {
            $sku['groupbuy_product_id'] = $this->groupbuy_product_id;
            $skuModel->create($sku);
        }
    }
 
    /**
     * 删除团购商品
     */
    public function remove()
    {
        $this->startTrans();
        try {
            // 删除团购SKU
            $skuModel = new \app\supplier\model\plus\groupbuy\ProductSku();
            $skuModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete();
            
            // 删除团购订单用户记录
            $billUserModel = new \app\supplier\model\plus\groupbuy\BillUser();
            $billUserModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete();
            
            // 删除团购订单
            $billModel = new \app\supplier\model\plus\groupbuy\Bill();
            $billModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete();
            
            // 最后删除团购商品
            $result = $this->delete();
            
            $this->commit();
            return $result !== false;
        } catch (\Exception $e) {
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }
}