diff --git a/app/common/dao/store/order/StoreOrderDao.php b/app/common/dao/store/order/StoreOrderDao.php index 79d62ec..469f512 100644 --- a/app/common/dao/store/order/StoreOrderDao.php +++ b/app/common/dao/store/order/StoreOrderDao.php @@ -935,6 +935,17 @@ class StoreOrderDao extends BaseDao })->where('product_id',$productId)->sum('product_num'); } + // 商户的某个商品购买数量 + public function getShopMaxCountNumber(int $merId, int $productId){ + return (int)StoreOrderProduct::hasWhere('orderInfo',function($query) use($merId){ + $query->where('buy_mer_id',$merId)->where(function($query){ + $query->where('is_del',0)->whereOr(function($query){ + $query->where('is_del',1)->where('paid',1); + }); + }); + })->where('product_id',$productId)->sum('product_num'); + } + public function getOrderSn($order_id) { return StoreOrder::getDB()->where($this->getPk(), $order_id)->value('order_sn', ''); diff --git a/app/common/repositories/store/product/ProductRepository.php b/app/common/repositories/store/product/ProductRepository.php index 5978f30..8dcac65 100644 --- a/app/common/repositories/store/product/ProductRepository.php +++ b/app/common/repositories/store/product/ProductRepository.php @@ -1984,6 +1984,65 @@ class ProductRepository extends BaseRepository return compact('product', 'sku', 'cart'); } + /** + * Common: 供应商品进入购物车检测(酒道馆进货商品) + * Author: wu-hui + * Time: 2024/03/01 14:33 + * @param array $data + * @param $merId + * @return array + */ + public function supplyCartCheck(array $data, $merId){ + $where = [ + 'is_show' => 1, + 'status' => 1, + 'is_used' => 1, + 'product_type' => 0, + 'is_gift_bag' => 0, + ]; + $where['product_id'] = $data['source_id']; + unset($where['is_gift_bag']); + // 是否允许购买 + $product = $this->dao->search(null, $where) + ->hasWhere('merchant', function ($query) use ($where) { + $query->where('is_del', 0) + ->where('mer_state', 1) + ->where('merchant_type', 2) + ->where('status', 1); + }) + ->findOrEmpty() + ->toArray(); + if (!$product) throw new ValidateException('商品已下架'); + if ($product['once_min_count'] > 0 && $product['once_min_count'] > $data['cart_num']){ + throw new ValidateException('[低于起购数:'.$product['once_min_count'].']'.mb_substr($product['store_name'],0,10).'...'); + } + if ($product['pay_limit'] == 1 && $product['once_max_count'] < $data['cart_num']){ + throw new ValidateException('[超出单次限购数:'.$product['once_max_count'].']'.mb_substr($product['store_name'],0,10).'...'); + } + if ($product['pay_limit'] == 2){ + //如果长期限购 已购买数量 + $storeOrderRepository = app()->make(StoreOrderRepository::class); + $count = $storeOrderRepository->getShopMaxCountNumber($merId,$product['product_id']); + if (($data['cart_num'] + $count) > $product['once_max_count']){ + throw new ValidateException('[超出限购总数:'. $product['once_max_count'].']'.mb_substr($product['store_name'],0,10).'...'); + } + } + if ($product['type'] && !$data['is_new']) throw new ValidateException('虚拟商品不可加入购物车'); + // 规格是否存在 + $value_make = app()->make(ProductAttrValueRepository::class); + $sku = $value_make->getOptionByUnique($data['product_attr_unique']); + if (!$sku) throw new ValidateException('SKU不存在'); + //分销礼包 + if ($product['is_gift_bag']) throw new ValidateException('当前商品禁止购买!'); + // 判断库存 + $isBatch = $data['is_batch'] ?? 0;//购买类型:0=单个购买,1=批量购买 + $stock = $sku['stock'] ?? 0; + if($isBatch == 1) $stock = floor($stock / $data['batch_num']);// 批量购买时 库存 = 购买数量 / 每批的数量 + if ($stock < $data['cart_num']) throw new ValidateException('库存不足'); + + return compact('product', 'sku'); + } + /** * TODO 购物车单商品数量 diff --git a/app/common/repositories/store/product/SupplierProductRepository.php b/app/common/repositories/store/product/SupplierProductRepository.php index f7b9c0c..fe592c9 100644 --- a/app/common/repositories/store/product/SupplierProductRepository.php +++ b/app/common/repositories/store/product/SupplierProductRepository.php @@ -46,14 +46,15 @@ class SupplierProductRepository extends BaseRepository{ 'is_batch', 'batch_num', 'batch_unit', - 'image' + 'image', + 'stock' ]) ->with([ 'attr' => function($query){ $query->field(['product_id','attr_name','attr_values']); }, 'attrValue' => function($query){ - $query->field(['product_id','detail','image','price','sku','unique','value_id']); + $query->field(['product_id','detail','image','price','sku','unique','value_id', 'stock']); } ]) ->order('sort desc,mer_id desc') diff --git a/app/controller/api/Supplier.php b/app/controller/api/Supplier.php index cebecef..a835c27 100644 --- a/app/controller/api/Supplier.php +++ b/app/controller/api/Supplier.php @@ -2,6 +2,8 @@ namespace app\controller\api; +use app\common\repositories\store\order\StoreCartRepository; +use app\common\repositories\store\product\ProductRepository; use app\common\repositories\store\product\SupplierProductRepository; use app\services\supplier\SystemSupplierApplyServices; use app\services\supplier\SystemSupplierServices; @@ -153,6 +155,139 @@ class Supplier extends BaseController{ return app('json')->success($data); } + /** + * Common: 获取当前商户购物车列表 + * Author: wu-hui + * Time: 2024/03/01 15:06 + * @return mixed + */ + public function cartList(){ + // 参数获取 + $merId = $this->request->merId(); + $list = (array)app()->make(StoreCartRepository::class) + ->getSearch([ + 'is_pay' => 0, + 'is_del' => 0, + 'is_new' => 0, + 'is_fail' => 0, + 'product_type' => 30, + 'wine_mer_id' => $merId, + ]) + ->field(['cart_id','product_type','product_id','product_attr_unique','cart_num','is_batch','batch_num']) + ->with([ + 'productAttr' => function($query){ + $query->field(['product_id','detail','image','price','sku','unique','value_id', 'stock']); + }, + 'product' => function($query){ + $query->field(['product_id','store_name','unit_name','is_batch','batch_num','batch_unit']); + } + ]) + ->select() + ->toArray(); + + return app('json')->success($list); + } + /** + * Common: 获取购物车id列表 + * Author: wu-hui + * Time: 2024/03/01 15:39 + * @return mixed + */ + public function cartIds(){ + // 参数获取 + $merId = $this->request->merId(); + $ids = (array)app()->make(StoreCartRepository::class) + ->getSearch([ + 'is_pay' => 0, + 'is_del' => 0, + 'is_new' => 0, + 'is_fail' => 0, + 'product_type' => 30, + 'wine_mer_id' => $merId, + ])->column('cart_id'); + + return app('json')->success($ids); + } + /** + * Common: 商户购物车 - 新增购买商品&修改购买商品数量 + * Author: wu-hui + * Time: 2024/03/01 14:24 + * @return mixed + */ + public function cartAdd(){ + // 参数获取 + $data = $this->request->params([ + 'cart_num', + 'product_attr_unique', + 'product_id', + 'is_batch', + 'batch_num', + ]); + $data['product_type'] = 30; + $data['source'] = $data['product_type']; + $data['source_id'] = $data['product_id']; + $merId = $this->request->merId(); + // 校验数据 + $result = app()->make(ProductRepository::class)->supplyCartCheck($data,$merId); + // 添加修改 + $cartId = (int)app()->make(StoreCartRepository::class)->getSearch([ + 'is_pay' => 0, + 'is_del' => 0, + 'is_new' => 0, + 'is_fail' => 0, + 'product_type' => 30, + 'product_id' => $data['source_id'], + 'wine_mer_id' => $merId, + 'product_attr_unique' => $data['product_attr_unique'], + ])->value('cart_id'); + if($cartId > 0){ + // 已经存在 修改数量 + app()->make(StoreCartRepository::class)->update($cartId,[ + 'cart_num' => $data['cart_num'], + 'is_batch' => $data['is_batch'], + 'batch_num' => $data['batch_num'] + ]); + }else{ + // 不存在 添加信息 + $data['wine_mer_id'] = $merId; + $data['mer_id'] = $result['product']['mer_id']; + app()->make(StoreCartRepository::class)->create($data); + } + + return app('json')->success(); + } + /** + * Common: 商户购物车 - 删除购买商品 + * Author: wu-hui + * Time: 2024/03/01 14:49 + * @return mixed + */ + public function cartDel(){ + // 参数获取 + $data = $this->request->params([ + 'product_attr_unique', + 'product_id', + ]); + $data['product_type'] = 30; + $data['source'] = $data['product_type']; + $data['source_id'] = $data['product_id']; + $merId = $this->request->merId(); + // 删除操作 + $cartId = (int)app()->make(StoreCartRepository::class)->getSearch([ + 'is_pay' => 0, + 'is_del' => 0, + 'is_new' => 0, + 'is_fail' => 0, + 'product_type' => 30, + 'product_id' => $data['source_id'], + 'wine_mer_id' => $merId, + 'product_attr_unique' => $data['product_attr_unique'], + ])->delete(); + + return app('json')->success(); + } + + diff --git a/route/api.php b/route/api.php index 7d4e780..db32ffd 100644 --- a/route/api.php +++ b/route/api.php @@ -411,6 +411,15 @@ Route::group('api/', function () { Route::group('supplier',function(){ // 进货相关 Route::get('goods_list','goodsList');// 供应商全部商品 + // 进货 - 购物车 + Route::get('cart_list','cartList');// 商品购物车列表 + Route::get('cart_ids','cartIds');// 商品购物车IDS + Route::post('cart_add','cartAdd');// 添加购物车 + Route::post('cart_del','cartDel');// 删除购物车商品 + + + + })->prefix('api.Supplier/')->middleware(ShopTokenMiddleware::class,TRUE);