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
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
<?php
 
namespace app\api\model\plus\repair;
 
use app\api\model\order\Order as OrderModel;
use think\facade\Cache;
use app\api\model\plus\repair\Project as ProjectModel;
use app\common\model\supplier\Supplier as SupplierModel;
use app\common\library\helper;
use app\common\model\plus\repair\Cart as CartModel;
 
/**
 * 购物车管理
 */
class Cart extends CartModel
{
    // 错误信息
    public $error = '';
 
    /**
     * 购物车列表 (含商品信息)
     */
    public function getList($user, $cart_ids = [])
    {
        // 获取购物车商品列表
        return $this->getOrderProjectList($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 getOrderProjectList($user, $cart_ids = [])
    {
        // 购物车列表
        $projectList = [];
        // 获取购物车列表
        $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 $projectList;
        }
        // 购物车中所有商品id集
        $projectIds = array_unique(helper::getArrayColumn($cartList, 'project_id'));
        // 获取并格式化商品数据
        $sourceData = (new ProjectModel)->getListByIds($projectIds);
        $sourceData = helper::arrayColumn2Key($sourceData, 'project_id');
        // 供应商信息
        $supplierData = [];
        // 格式化购物车数据列表
        foreach ($cartList as $key => $item) {
            // 判断商品不存在则自动删除
            if (!isset($sourceData[$item['project_id']])) {
                $this->delete($key);
                continue;
            }
            // 项目信息
            $project = clone $sourceData[$item['project_id']];
            // 判断商品是否已删除
            if (empty($project)) {
                $this->delete($key);
                continue;
            }
 
            // 购买数量
            $project['total_num'] = $item['total_num'];
            // 总价
            $project['total_price'] = bcmul($project['price'], $item['total_num'], 2);
            // 总佣金
            $project['total_money'] = bcmul($project['money'], $item['total_num'], 2);
            // 供应商
            $project['shop_supplier_id'] = $item['shop_supplier_id'];
            //购物车id
            $project['cart_id'] = $item['cart_id'];
            $projectList[] = $project;
        }
 
        $supplierIds = array_unique(helper::getArrayColumn($projectList, 'shop_supplier_id'));
        foreach($supplierIds as $supplierId){
            $supplierData[] = [
                'shop_supplier_id' => $supplierId,
                'supplier' => SupplierModel::detail($supplierId),
                'projectList' => $this->getProjectBySupplier($supplierId, $projectList)
            ];
        }
        return $supplierData;
    }
 
    private function getProjectBySupplier($supplierId, $projectList)
    {
        $result = [];
        foreach ($projectList as $project){
            if($project['shop_supplier_id'] == $supplierId){
                array_push($result, $project);
            }
        }
        return $result;
    }
 
    /**
     * 加入购物车
     */
    public function add($user, $projectId, $projectNum)
    {
        $model = $this;
        // 获取信息
        $project = ProjectModel::detail($projectId);
 
        // 获取商品购物车信息
        $cartDetail = $model->where('user_id', '=', $user['user_id'])
            ->where('project_id', '=', $projectId)
            ->find();
        // 验证能否加入
        if (!$project_price = $this->checkProject($project)) {
            return false;
        }
 
        // 记录到购物车列表
        if ($cartDetail) {
            return $cartDetail->save(['total_num' => $cartDetail['total_num'] + $projectNum]);
        } else {
            return $this->save([
                'user_id' => $user['user_id'],
                'project_id' => $projectId,
                'total_num' => $projectNum,
                'join_price' => $project_price,
                'shop_supplier_id' => $project['shop_supplier_id'],
                'app_id' => self::$app_id,
            ]);
        }
    }
 
    /**
     * 验证是否可以下单
     */
    private function checkProject($project)
    {
        // 判断项目
        if (!$project) {
            $this->setError('很抱歉,信息不存在');
            return false;
        }
 
        return $project['price'];
    }
 
    /**
     * 减少购物车中某商品数量
     */
    public function sub($user, $projectId)
    {
        $cartDetail = $this->where('user_id', '=', $user['user_id'])
            ->where('project_id', '=', $projectId)
            ->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();
    }
 
    /**
     * 设置错误信息
     */
    private function setError($error)
    {
        empty($this->error) && $this->error = $error;
    }
 
    /**
     * 获取错误信息
     */
    public function getError()
    {
        return $this->error;
    }
 
}