| New file |
| | |
| | | <?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; |
| | | } |
| | | |
| | | } |