quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
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
<?php
 
namespace app\shop\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', 'active'])
            ->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();
        $data['app_id'] = $this->app_id;
        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 batchAdd($productsData)
    {
        $results = [];
        foreach ($productsData as $productData) {
            $results[] = $this->add($productData);
        }
        return array_filter($results); // 返回成功的数量
    }
 
    /**
     * 编辑团购商品
     */
    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\shop\model\plus\groupbuy\ProductSku();
        
        // 先获取现有的SKU ID 列表
        $existingSkus = $skuModel->where('groupbuy_product_id', $this->groupbuy_product_id)->select();
        $existingSkuIds = [];
        foreach ($existingSkus as $existingSku) {
            $existingSkuIds[$existingSku['product_sku_id']] = $existingSku['groupbuy_product_sku_id'];
        }
        
        // 添加或更新团购SKU
        foreach ($skuList as $sku) {
            $sku['groupbuy_product_id'] = $this->groupbuy_product_id;
            $sku['app_id'] = $this->app_id;
            
            // 检查是否是现有SKU
            if (isset($existingSkuIds[$sku['product_sku_id']])) {
                // 更新现有SKU
                $skuModel->save($sku, ['groupbuy_product_sku_id' => $existingSkuIds[$sku['product_sku_id']]]);
            } else {
                // 创建新SKU
                $skuModel->create($sku);
            }
        }
    }
 
    /**
     * 删除团购商品
     */
    public function remove()
    {
        $this->startTrans();
        try {
            // 删除团购SKU
            $skuModel = new \app\shop\model\plus\groupbuy\ProductSku();
            $skuModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete();
            
            // 删除团购订单用户记录
            $billUserModel = new \app\shop\model\plus\groupbuy\BillUser();
            $billUserModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete();
            
            // 删除团购订单
            $billModel = new \app\shop\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;
        }
    }
}