quanwei
7 days ago 30563323a53b0d0260c97d08a9e8bd4cc8227a95
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
 
namespace app\api\model\order;
 
use app\api\model\order\Order as OrderModel;
use think\facade\Cache;
use app\api\model\product\Product as ProductModel;
use app\common\model\supplier\Supplier as SupplierModel;
use app\common\library\helper;
use app\common\model\order\Cart as CartModel;
 
/**
 * 购物车管理
 */
class Cart extends CartModel
{
    // 错误信息
    public $error = '';
 
    /**
     * 购物车列表 (含商品信息)
     */
    public function getList($user, $cart_ids = [])
    {
        // 获取购物车商品列表
        return $this->getOrderProductList($user, $cart_ids);
    }
 
    /**
     * 获取购物车列表
     */
    public function getCartList($cartIds = null)
    {
        if (empty($cartIds)) return static::$cart;
        $cartList = [];
        $indexArr = (strpos($cartIds, ',') !== false) ? explode(',', $cartIds) : [$cartIds];
        foreach ($indexArr as $index) {
            isset(static::$cart[$index]) && $cartList[$index] = static::$cart[$index];
        }
        return $cartList;
    }
 
    /**
     * 获取购物车中的商品列表
     */
    public function getOrderProductList($user, $cart_ids = [])
    {
        // 购物车商品列表
        $productList = [];
        // 获取购物车列表
        $model = $this;
        if ($cart_ids) {
            $model = $model->where('cart_id', 'in', explode(',', $cart_ids));
        }
        $cartList = $model->where('user_id', '=', $user['user_id'])->select();
        if (empty($cartList)) {
            $this->setError('当前购物车没有商品');
            return $productList;
        }
        // 购物车中所有商品id集
        $productIds = array_unique(helper::getArrayColumn($cartList, 'product_id'));
        // 获取并格式化商品数据
        $sourceData = (new ProductModel)->getListByIds($productIds, null);
        $sourceData = helper::arrayColumn2Key($sourceData, 'product_id');
        // 供应商信息
        $supplierData = [];
        // 格式化购物车数据列表
        foreach ($cartList as $key => $item) {
            // 判断商品不存在则自动删除
            if (!isset($sourceData[$item['product_id']])) {
                $this->delete($key);
                continue;
            }
            // 商品信息
            $product = clone $sourceData[$item['product_id']];
            // 判断商品是否已删除
            if ($product['is_delete']) {
                $this->delete($key);
                continue;
            }
            // 商品sku信息
            $product['product_sku'] = ProductModel::getProductSku($product, $item['spec_sku_id']);
            $product['spec_sku_id'] = $item['spec_sku_id'];
            // 商品sku不存在则自动删除
            if (empty($product['product_sku'])) {
                $this->delete($key);
                continue;
            }
            // 商品单价
            $product['product_price'] = $product['product_sku']['product_price'];
            // 购买数量
            $product['total_num'] = $item['total_num'];
            // 商品总价
            $product['total_price'] = bcmul($product['product_price'], $item['total_num'], 2);
            // 商品总结算价
            $product['total_settlement_price'] = bcmul($product['settlement_price'], $item['total_num'], 2);
            // 供应商
            $product['shop_supplier_id'] = $item['shop_supplier_id'];
            $product['supplier_price'] = bcmul($product['supplier_price'], $item['total_num'], 2);
            //购物车id
            $product['cart_id'] = $item['cart_id'];
            $productList[] = $product->hidden(['category', 'content', 'image']);
        }
 
        $supplierIds = array_unique(helper::getArrayColumn($productList, 'shop_supplier_id'));
        foreach($supplierIds as $supplierId){
            $supplierData[] = [
                'shop_supplier_id' => $supplierId,
                'supplier' => SupplierModel::detail($supplierId),
                'productList' => $this->getProductBySupplier($supplierId, $productList)
            ];
        }
        return $supplierData;
    }
 
    private function getProductBySupplier($supplierId, $productList)
    {
        $result = [];
        foreach ($productList as $product){
            if($product['shop_supplier_id'] == $supplierId){
                array_push($result, $product);
            }
        }
        return $result;
    }
 
    /**
     * 加入购物车
     */
    public function add($user, $productId, $productNum, $spec_sku_id)
    {
        // 获取商品购物车信息
        $cartDetail = $this->where('user_id', '=', $user['user_id'])
            ->where('product_id', '=', $productId)
            ->where('spec_sku_id', '=', $spec_sku_id)
            ->find();
        $cartProductNum = $cartDetail ? $cartDetail['total_num'] + $productNum : $productNum;
        // 获取商品信息
        $product = ProductModel::detail($productId);
        // 验证商品能否加入
        if (!$product_price = $this->checkProduct($user, $product, $spec_sku_id, $cartProductNum)) {
            return false;
        }
        // 记录到购物车列表
        if ($cartDetail) {
            return $cartDetail->save(['total_num' => $cartDetail['total_num'] + $productNum]);
        } else {
            return $this->save([
                'user_id' => $user['user_id'],
                'product_id' => $productId,
                'spec_sku_id' => $spec_sku_id,
                'total_num' => $productNum,
                'join_price' => $product_price,
                'shop_supplier_id' => $product['shop_supplier_id'],
                'app_id' => self::$app_id,
            ]);
        }
    }
 
    /**
     * 验证商品是否可以购买
     */
    private function checkProduct($user, $product, $spec_sku_id, $cartProductNum)
    {
        // 判断商品是否下架
        if (!$product || $product['is_delete'] || $product['product_status']['value'] != 10) {
            $this->setError('很抱歉,商品信息不存在或已下架');
            return false;
        }
        // 商品sku信息
        $product['product_sku'] = ProductModel::getProductSku($product, $spec_sku_id);
        // 判断商品库存
        if ($cartProductNum > $product['product_sku']['stock_num']) {
            $this->setError('很抱歉,商品库存不足');
            return false;
        }
        // 是否是会员商品
        if (count($product['grade_ids']) > 0 && $product['grade_ids'][0] != '') {
            if (!in_array($user['grade_id'], $product['grade_ids'])) {
                $this->setError('很抱歉,此商品仅特定会员可购买');
                return false;
            }
        }
        // 是否超过最大购买数
        if ($product['limit_num'] > 0) {
            $hasNum = OrderModel::getHasBuyOrderNum($user['user_id'], $product['product_id']);
            if ($hasNum + $product['total_num'] > $product['limit_num']) {
                $this->error = "很抱歉,购买超过此商品最大限购数量";
                return false;
            }
        }
        return $product['product_sku']['product_price'];
    }
 
    /**
     * 减少购物车中某商品数量
     */
    public function sub($user, $productId, $spec_sku_id)
    {
        $cartDetail = $this->where('user_id', '=', $user['user_id'])
            ->where('product_id', '=', $productId)
            ->where('spec_sku_id', '=', $spec_sku_id)
            ->find();
        if ($cartDetail['total_num'] <= 1) {
            return $cartDetail->delete();
        } else {
            $cartDetail->save(['total_num' => $cartDetail['total_num'] - 1]);
        }
    }
 
    /**
     * 删除购物车中指定商品
     * @param string $cartIds (支持字符串ID集)
     */
    public function setDelete($user, $cart_id)
    {
        return $this->where('user_id', '=', $user['user_id'])->where('cart_id', 'in', explode(',', $cart_id))->delete();
    }
 
    /**
     * 获取当前用户购物车商品总数量(含件数)
     */
    public function getTotalNum($user)
    {
        $num = $this->where('user_id', '=', $user['user_id'])->sum('total_num');
        return $num ? $num : 0;
    }
 
    /**
     * 获取当前用户购物车商品总数量(不含件数)
     */
    public function getProductNum($user)
    {
        return $this->where('user_id', '=', $user['user_id'])->count();
    }
 
    /**
     * 清空当前用户购物车
     */
    public function clearAll($user, $cartIds)
    {
        return $this->where('user_id', '=', $user['user_id'])
            ->where('cart_id', 'in', explode(',', $cartIds))
            ->delete();
    }
    public function getCartCount($user_id, $product_id)
    {
 
        $cart=$this->where('user_id',$user_id)->where('product_id',$product_id)->find();
        $total_num=$this->where('user_id',$user_id)->where('product_id',$product_id)->sum('total_num');
        $cart['total_num']=$total_num;
        return $cart;
    }
    /**
     * 设置错误信息
     */
    private function setError($error)
    {
        empty($this->error) && $this->error = $error;
    }
 
    /**
     * 获取错误信息
     */
    public function getError()
    {
        return $this->error;
    }
 
}