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
<?php
 
namespace app\operations\controller\plus\groupbuy;
 
use app\operations\controller\Controller;
use app\operations\model\plus\groupbuy\Product as ProductModel;
use app\common\model\product\Product as CommonProductModel;
 
/**
 * 商家端团购商品控制器
 */
class Product extends Controller
{
    /**
     * 团购商品列表
     */
    public function index()
    {
        $model = new ProductModel();
        $list = $model->getList($this->postData());
        return $this->renderSuccess('', compact('list'));
    }
 
    /**
     * 添加团购商品
     */
    public function add()
    {
        if ($this->request->isGet()) {
            // 获取店铺的商品列表用于选择
            $commonProductModel = new CommonProductModel();
            $postData = $this->postData();
            $postData['shop_supplier_id'] = $this->getSupplierId();
            $productList = $commonProductModel->getList($postData);
            return $this->renderSuccess('', compact('productList'));
        }
        
        $model = new ProductModel();
        $data = $this->postData();
        
        // 如果传递的是批量数据,则处理多个商品
        if (isset($data['product_list']) && is_array($data['product_list']) && count($data['product_list']) > 0) {
            // 处理批量添加
            $successCount = 0;
            $errors = [];
            
            foreach ($data['product_list'] as $productData) {
                $productData['shop_supplier_id'] = $this->getSupplierId();
                if ($model->add($productData)) {
                    $successCount++;
                } else {
                    $errors[] = "商品ID {$productData['product_id']} 添加失败: " . $model->getError();
                }
            }
            
            if ($successCount > 0) {
                $message = "成功添加 {$successCount} 个商品";
                if (!empty($errors)) {
                    $message .= ", 部分商品添加失败: " . implode(', ', $errors);
                    return $this->renderSuccess($message);
                }
                return $this->renderSuccess($message);
            } else {
                return $this->renderError('所有商品添加失败: ' . implode(', ', $errors));
            }
        } else {
            // 单个商品添加
            $data['shop_supplier_id'] = $this->getSupplierId();
            if ($model->add($data)) {
                return $this->renderSuccess('添加成功');
            }
            return $this->renderError($model->getError() ?: '添加失败');
        }
    }
 
    /**
     * 编辑团购商品
     */
    public function edit()
    {
        $groupbuy_product_id = $this->request->param('groupbuy_product_id');
        if (empty($groupbuy_product_id)) {
            return $this->renderError('缺少必要参数:groupbuy_product_id');
        }
        
        $model = ProductModel::detail($groupbuy_product_id, [
            'product', 'groupbuySku'
        ]);
        if (!$model) {
            return $this->renderError('团购商品不存在');
        }
        
        // 验证权限 - 商家端可以编辑所有商品,不需要权限验证
        /*
        if ($model['shop_supplier_id'] != $this->getStoreId()) {
            return $this->renderError('没有权限操作此商品');
        }
        */
        
        if ($this->request->isGet()) {
            return $this->renderSuccess('', compact('model'));
        }
        
        $data = $this->postData();
        if ($model->edit($data)) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
 
    /**
     * 删除团购商品
     */
    public function delete()
    {
        $groupbuy_product_id = $this->request->param('groupbuy_product_id');
        if (empty($groupbuy_product_id)) {
            return $this->renderError('缺少必要参数:groupbuy_product_id');
        }
        
        $model = ProductModel::detail($groupbuy_product_id);
        if (!$model) {
            return $this->renderError('团购商品不存在');
        }
        
        // 验证权限 - 商家端可以删除所有商品,不需要权限验证
        /*
        if ($model['shop_supplier_id'] != $this->getStoreId()) {
            return $this->renderError('没有权限操作此商品');
        }
        */
        
        if ($model->remove()) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError('删除失败');
    }
 
    /**
     * 获取团购商品详情
     */
    public function detail()
    {
        $groupbuy_product_id = $this->request->param('groupbuy_product_id');
        if (empty($groupbuy_product_id)) {
            return $this->renderError('缺少必要参数:groupbuy_product_id');
        }
        
        $model = ProductModel::detail($groupbuy_product_id, [
            'product', 'groupbuySku', 'active'
        ]);
        if (!$model) {
            return $this->renderError('团购商品不存在');
        }
        
        // 验证权限 - 商家端可以查看所有商品,不需要权限验证
        /*
        if ($model['shop_supplier_id'] != $this->getStoreId()) {
            return $this->renderError('没有权限查看此商品');
        }
        */
        
        return $this->renderSuccess('', compact('model'));
    }
}