123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\api\model;
- use app\api\model\Goods as GoodsModel;
- use app\api\model\GoodsSku as GoodsSkuModel;
- use app\api\model\shop\Shops;
- use app\api\model\User as UserModel;
- use app\api\service\User as UserService;
- use app\common\model\Cart as CartModel;
- use app\common\enum\goods\Status as GoodsStatusEnum;
- use app\common\enum\goods\GoodsType as GoodsTypeEnum;
- use app\common\exception\BaseException;
- /**
- * 购物车管理
- * Class Cart
- * @package app\api\model
- */
- class Cart extends CartModel
- {
- /**
- * 加入购物车
- * @param int $goodsId 商品ID
- * @param string $goodsSkuId 商品sku唯一标识
- * @param int $goodsNum 商品数量
- * * @param int $staffUserId 链接分享者ID
- * @return bool
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function add(int $goodsId, string $goodsSkuId, int $goodsNum, int $staffUserId = 0)
- {
- // 判断是否已存在购物车记录
- $detail = $this->getInfo($goodsId, $goodsSkuId, false);
- // 如果已存在购物车记录, 则累计商品数量
- !empty($detail) && $goodsNum += $detail['goods_num'];
- // 验证商品的状态
- $this->checkGoodsStatus($goodsId, $goodsSkuId, $goodsNum);
- // 获取链接分享者的用户ID和店铺ID
- // 获取当前用户ID
- $user = UserService::getCurrentLoginUser(true);
- $userId = $user['user_id'];
- $data = $this->getStaffUserId($staffUserId);
- // 实例化模型
- $model = $detail ?: (new static);
- // 写入商品加购数
- Goods::setIncByField($goodsId, 'cart_num', 1);
- self::where(['user_id' => $userId])->update([//当前所有的加购的商品都
- 'staff_user_id' => $data['staffUserId'],
- 'shop_id' => $data['shopId'],
- 'is_staff' => $data['isStaff'],
- ]);
- return $model->save([
- 'goods_id' => $goodsId,
- 'goods_sku_id' => $goodsSkuId,
- 'goods_num' => $goodsNum,
- 'user_id' => $userId,
- 'store_id' => self::$storeId,
- 'staff_user_id' => $data['staffUserId'],
- 'shop_id' => $data['shopId'],
- 'is_staff' => $data['isStaff'],
- ]);
- }
- /**
- * 更新购物车记录
- * @param int $goodsId 商品ID
- * @param string $goodsSkuId 商品sku唯一标识
- * @param int $goodsNum 商品数量
- * @return bool
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function sUpdate(int $goodsId, string $goodsSkuId, int $goodsNum)
- {
- // 验证商品的状态
- $this->checkGoodsStatus($goodsId, $goodsSkuId, $goodsNum);
- // 获取购物车记录
- $model = $this->getInfo($goodsId, $goodsSkuId, true);
- // 更新记录
- return $model->save(['goods_num' => $goodsNum]);
- }
- /**
- * 验证商品的状态
- * @param int $goodsId 商品ID
- * @param string $goodsSkuId 商品sku唯一标识
- * @param int $goodsNum 商品数量
- * @return bool
- * @throws BaseException
- */
- private function checkGoodsStatus(int $goodsId, string $goodsSkuId, int &$goodsNum)
- {
- if ($goodsNum<=0) {
- throwError('很抱歉, 商品数量必须大于0');
- }
- // 获取商品详情
- $goods = GoodsModel::detail($goodsId);
- // 商品不存在
- if (empty($goods) || $goods['is_delete']) {
- throwError('很抱歉, 商品信息不存在');
- }
- // 商品已下架
- if ($goods['status'] == GoodsStatusEnum::OFF_SALE) {
- throwError('很抱歉, 该商品已经下架');
- }
- // 赠品不能添加
- if ($goods['goods_type'] == GoodsTypeEnum::GIFT) {
- throwError('很抱歉, 赠品不能添加购物车');
- }
- // 获取SKU信息
- $skuInfo = GoodsSkuModel::detail($goodsId, $goodsSkuId);
- if ($skuInfo['stock_num'] < $goodsNum) {
- $goodsNum = $skuInfo['stock_num'];
- // throwError('很抱歉, 该商品库存数量不足');
- }
- return true;
- }
- /**
- * 获取分享链接的用户ID
- * @param int $staffUserId
- * @return mixed
- * @author: zjwhust
- * @Time: 2021/9/30 13:32
- */
- private function getStaffUserId( int $staffUserId)
- {
- // 获取当前用户ID
- $user = UserService::getCurrentLoginUser(true);
- $data['isStaff'] = 1;
- if($staffUserId<=0 || $staffUserId==$user['user_id']){
- if(in_array($user['role'],[1,99])){//用户角色1:普通用户 99:分销员
- $staffUserId = $user['upper_user_id'];
- $data['isStaff'] = 0;
- }else{
- $staffUserId = 0;
- }
- }
- $data['staffUserId'] = $staffUserId;
- $data['shopId'] = 0;
- if ($staffUserId>0) {
- // 获取分享链接的用户信息
- $user = User::detail($staffUserId);
- if($user){
- $data['shopId'] = !in_array($user['role'],[1,99]) ? $user['shop_id'] : $user['bind_shop_id'];
- }
- }
- return $data;
- }
- /**
- * 获取购物车记录
- * @param int $goodsId 商品ID
- * @param string $goodsSkuId 商品sku唯一标识
- * @param bool $isForce
- * @return static|bool
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private function getInfo(int $goodsId, string $goodsSkuId, bool $isForce = true)
- {
- // 获取当前用户ID
- $userId = UserService::getCurrentLoginUserId();
- // 获取购物车记录
- $model = static::detail($userId, $goodsId, $goodsSkuId);
- if (empty($model)) {
- $isForce && throwError('购物车中没有该记录');
- return false;
- }
- return $model;
- }
- /**
- * 删除购物车中指定记录
- * @param array $cartIds 购物车ID集, 如果为空删除所有
- * @return bool
- * @throws BaseException
- */
- public function clear(array $cartIds = [])
- {
- // 获取当前用户ID
- $userId = UserService::getCurrentLoginUserId();
- // 设置更新条件
- $where = [['user_id', '=', $userId]];
- // 购物车ID集
- !empty($cartIds) && $where[] = ['id', 'in', $cartIds];
- // 更新记录
- return $this->deleteAll($where);
- }
- /**
- * 获取当前用户购物车商品数量(以商品id为键 商品数量为值)
- * @return array
- */
- public function getCartGoodsIdNums()
- {
- if (!UserService::isLogin()) return [];
- $userId = UserService::getCurrentLoginUserId();
- return $this->where('user_id', '=', $userId)
- ->where('is_delete', '=', 0)
- ->column('goods_num','goods_id');
- }
- /**
- * 获取当前用户购物车商品总数量
- * @return float
- * @throws BaseException
- */
- public function getCartTotal()
- {
- if (!UserService::isLogin()) return 0;
- $userId = UserService::getCurrentLoginUserId();
- return $this->where('user_id', '=', $userId)
- ->where('is_delete', '=', 0)
- ->sum('goods_num');
- }
- /**
- * 获取当前用户购物车商品类型数量
- * @return float
- * @throws BaseException
- */
- public function getCartGoodsTotal()
- {
- if (!UserService::isLogin()) return 0;
- $userId = UserService::getCurrentLoginUserId();
- return $this->where('user_id', '=', $userId)
- ->where('is_delete', '=', 0)
- ->count();
- }
- /**
- * 更新购物车的商品选中状态
- * @param array $cartIds 购物车ID集, 如果为空删除所有
- * @return bool
- * @throws BaseException
- */
- public function updateSelected($cartIds,$selected){
- // 获取当前用户ID
- $userId = UserService::getCurrentLoginUserId();
- // 设置更新条件
- $where = [['user_id', '=', $userId]];
- // 购物车ID集
- !empty($cartIds) && $where[] = ['id', 'in', $cartIds];
- // 更新记录
- if(!empty($cartIds)){
- return $this->updateBase(['selected'=>$selected],$where);
- }else{
- return $this->updateBase(['selected'=>0],$where);
- }
- }
- /**
- * 更改购物车商品规格
- * @param $cartId
- * @param $goodsId
- * @param $goodsSkuId
- * @return mixed
- * @throws BaseException
- * @author: zjwhust
- * @Time: 2021/9/26 18:10
- */
- public function updSku($cartId, $goodsId, $goodsSkuId){
- // 获取当前用户ID
- $userId = UserService::getCurrentLoginUserId();
- $cart = $this->find($cartId);
- //如果没有更改规格,直接返回
- if($cart['goods_sku_id']==$goodsSkuId){
- return true;
- }
- // 设置更新条件
- return $this->transaction(function () use ($userId, $cartId, $goodsId, $goodsSkuId) {
- //删除商品规格相同的购物车数据
- $this->deleteAll([['user_id', '=', $userId],['goods_sku_id', '=', $goodsSkuId],['goods_id', '=', $goodsId]]);
- //更新当前的购物车属性
- $where = [['user_id', '=', $userId],['id', '=', $cartId],['goods_id', '=', $goodsId]];
- return $this->updateBase(['goods_sku_id'=>$goodsSkuId],$where);
- });
- }
- }
|