From 4944614d5b26eaba6b3b66bffa3847f5412a1a64 Mon Sep 17 00:00:00 2001 From: wuhui_zzw <1760308791@qq.com> Date: Fri, 8 Mar 2024 08:59:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=9A=E9=85=92=E9=81=93?= =?UTF-8?q?=E9=A6=86=E5=95=86=E5=93=81=E5=88=97=E8=A1=A8=E5=8F=8A=E8=B4=AD?= =?UTF-8?q?=E7=89=A9=E8=BD=A6=E5=A4=84=E7=90=86=E7=9B=B8=E5=85=B3=E5=86=85?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/dao/store/order/StoreOrderDao.php | 6 +- .../store/product/ProductRepository.php | 104 ++++++++++- .../product/SupplierProductRepository.php | 8 +- app/controller/api/Wine.php | 165 ++++++++++++++++++ route/api.php | 12 ++ 5 files changed, 282 insertions(+), 13 deletions(-) create mode 100644 app/controller/api/Wine.php diff --git a/app/common/dao/store/order/StoreOrderDao.php b/app/common/dao/store/order/StoreOrderDao.php index 51c21b4..f993547 100644 --- a/app/common/dao/store/order/StoreOrderDao.php +++ b/app/common/dao/store/order/StoreOrderDao.php @@ -943,9 +943,9 @@ class StoreOrderDao extends BaseDao } // 商户的某个商品购买数量 - 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){ + public function getShopMaxCountNumber(int $merId, int $productId,int $productType = 35){ + return (int)StoreOrderProduct::hasWhere('orderInfo',function($query) use($merId, $productType){ + $query->where('activity_type',$productType)->where(function($query){ $query->where('is_del',0)->whereOr(function($query){ $query->where('is_del',1)->where('paid',1); }); diff --git a/app/common/repositories/store/product/ProductRepository.php b/app/common/repositories/store/product/ProductRepository.php index 9c5ea4d..c197db2 100644 --- a/app/common/repositories/store/product/ProductRepository.php +++ b/app/common/repositories/store/product/ProductRepository.php @@ -4,7 +4,9 @@ namespace app\common\repositories\store\product; +use app\common\model\store\product\Product; use app\common\model\store\product\ProductLabel; +use app\common\model\system\merchant\Merchant; use app\common\model\user\User; use app\common\repositories\community\CommunityRepository; use app\common\repositories\store\coupon\StoreCouponRepository; @@ -1985,14 +1987,14 @@ class ProductRepository extends BaseRepository return compact('product', 'sku', 'cart'); } /** - * Common: 供应商品进入购物车检测(酒道馆进货商品) + * Common: 供应商品进入购物车检测(酒道馆进货商品) || 用户兑换酒道馆商品 * Author: wu-hui * Time: 2024/03/01 14:33 * @param array $data * @param $merId * @return array */ - public function supplyCartCheck(array $data, $merId){ + public function supplyCartCheck(array $data, $merId, $merchantType = 2){ $where = [ 'Product.is_show' => 1, 'Product.status' => 1, @@ -2002,10 +2004,8 @@ class ProductRepository extends BaseRepository $where['product_id'] = $data['source_id']; // 是否允许购买 $product = $this->dao->getSearch([]) - ->hasWhere('merchant', function ($query) { - $query->where('is_del', 0) - ->where('merchant_type', 2) - ->where('status', 1); + ->hasWhere('merchant', function ($query) use ($merchantType) { + $query->where('is_del', 0)->where('merchant_type', $merchantType)->where('status', 1); }) ->where($where) ->findOrEmpty() @@ -2020,7 +2020,10 @@ class ProductRepository extends BaseRepository if ($product['pay_limit'] == 2){ //如果长期限购 已购买数量 $storeOrderRepository = app()->make(StoreOrderRepository::class); - $count = $storeOrderRepository->getShopMaxCountNumber($merId,$product['product_id']); + // 商户类别:0=普通商户,1=酒道馆,2=供应商 + $productType = 35;// 酒道馆进货商品 + if($merchantType == 1) $productType = 36;// 用户兑换酒道馆商品 + $count = $storeOrderRepository->getShopMaxCountNumber($merId,$product['product_id'], $productType); if (($data['cart_num'] + $count) > $product['once_max_count']){ throw new ValidateException('[超出限购总数:'. $product['once_max_count'].']'.mb_substr($product['store_name'],0,10).'...'); } @@ -2441,8 +2444,6 @@ class ProductRepository extends BaseRepository return $data; } - - public function isFormatAttr(array $data, int $productId, $productType = 0) { $sukValue = []; @@ -2481,4 +2482,89 @@ class ProductRepository extends BaseRepository } return ['attr' => $data, 'value' => $valueNew]; } + /** + * Common: 获取酒道馆全部商品 + * Author: wu-hui + * Time: 2024/03/07 11:40 + * @param $search + * @param $page + * @param $limit + * @return array + */ + public function getWineList($search, $page, $limit){ + // 获取信息 + $query = (new Product())->alias('Product') + ->join('Merchant', 'Product.mer_id = Merchant.mer_id and Merchant.is_del = 0', 'LEFT') + ->where('Product.delete', 0) + ->where('Product.product_type', 0) + ->where('Product.status', 1) + ->where('Product.is_show', 1) + ->where('Product.is_used', 1) + ->where('Merchant.status', 1) + ->where('Merchant.mer_state', 1) + ->where('Merchant.merchant_type', 1) + ->when(isset($search['store_name']) && $search['store_name'] !== '', function ($query) use ($search) { + $query->whereLike('Product.store_name', "%{$search['store_name']}%"); + }); + // 查询内容 + $field = [ + 'Product.product_id', + 'Product.mer_id', + 'Product.store_name', + 'Product.price', + 'Product.unit_name', + 'Product.is_batch', + 'Product.batch_num', + 'Product.batch_unit', + 'Product.image', + 'Product.stock', + 'Product.once_min_count' + ]; + // 经纬度 + $lng = $search['lng'] ?? 0; + $lat = $search['lat'] ?? 0; + $distance = "ROUND((ST_DISTANCE(point(Merchant.long, Merchant.lat), point({$lng}, {$lat})) * 111195),2)"; + $list = $query + ->when($lng > 0 && $lat > 0,function($query) use ($distance, $field){ + $field[] = "{$distance} as distance"; + + $query->field($field); + },function($query) use ($field){ + $query->field($field); + }) + ->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', 'stock']); + } + ]) + ->when($lng > 0 && $lat > 0,function($query) use ($distance){ + $query->orderRaw("{$distance} ASC"); + },function($query){ + $query->order('Product.sort desc,Product.mer_id desc'); + }) + ->page($page, $limit) + ->select() + ->toArray(); + $count = $query->count(); + // 循环处理列表数据 + $list = array_map(function($item){ + // 规格处理 + if(count($item['attrValue']) > 1) $item['attrValue'] = array_column($item['attrValue'],null,'sku'); + // 距离处理 + if($item['distance'] > 0){ + if ($item['distance'] < 100) $item['distance_text'] = '100m以内'; + else $item['distance_text'] = sprintf("%.2f",$item['distance'] / 1000).'km'; + } + + return $item; + }, $list); + + return compact('count', 'list'); + } + + + } diff --git a/app/common/repositories/store/product/SupplierProductRepository.php b/app/common/repositories/store/product/SupplierProductRepository.php index fe592c9..a88c059 100644 --- a/app/common/repositories/store/product/SupplierProductRepository.php +++ b/app/common/repositories/store/product/SupplierProductRepository.php @@ -26,12 +26,18 @@ class SupplierProductRepository extends BaseRepository{ */ public function getList($search, $page, $limit){ // 仅显示供应商商品 - $supplierMerId = (array)Merchant::where('merchant_type', 2)->column('mer_id'); + $supplierMerId = (array)Merchant::where('merchant_type', 2) + ->where('status', 1) + ->where('mer_state', 1) + ->where('is_del', 0) + ->column('mer_id'); $query = $this->dao->getSearch([]) ->whereIn('mer_id', $supplierMerId) ->where('delete', 0) ->where('product_type', 0) ->where('status', 1) + ->where('is_show', 1) + ->where('is_used', 1) ->when(isset($search['store_name']) && $search['store_name'] !== '', function ($query) use ($search) { $query->whereLike('store_name', "%{$search['store_name']}%"); }); diff --git a/app/controller/api/Wine.php b/app/controller/api/Wine.php new file mode 100644 index 0000000..6ab6a0c --- /dev/null +++ b/app/controller/api/Wine.php @@ -0,0 +1,165 @@ +services = $services; + } + /** + * Common: 获取全部酒道馆商品 + * Author: wu-hui + * Time: 2024/03/07 11:40 + * @return mixed + */ + public function goodsList(){ + // 参数获取 + $search = $this->request->params(['store_name','lat','lng']); + [$page, $limit] = $this->getPage(); + $data = app()->make(ProductRepository::class)->getWineList($search, $page, $limit); + + return app('json')->success($data); + } + /** + * Common: 获取当前商户购物车列表 + * Author: wu-hui + * Time: 2024/03/01 15:06 + * @return mixed + */ + public function cartList(){ + // 参数获取 + $list = (array)app()->make(StoreCartRepository::class) + ->getSearch([ + 'is_pay' => 0, + 'is_del' => 0, + 'is_new' => 0, + 'is_fail' => 0, + 'product_type' => 36, + ]) + ->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(){ + // 参数获取 + $ids = (array)app()->make(StoreCartRepository::class) + ->getSearch([ + 'is_pay' => 0, + 'is_del' => 0, + 'is_new' => 0, + 'is_fail' => 0, + 'product_type' => 36, + ])->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'] = 36; + $data['source'] = $data['product_type']; + $data['source_id'] = $data['product_id']; + // 校验数据 + $result = app()->make(ProductRepository::class)->supplyCartCheck($data, 0, 1); + // 添加修改 + $cartId = (int)app()->make(StoreCartRepository::class)->getSearch([ + 'is_pay' => 0, + 'is_del' => 0, + 'is_new' => 0, + 'is_fail' => 0, + 'product_type' => 36, + 'product_id' => $data['source_id'], + '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['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'] = 36; + $data['source'] = $data['product_type']; + $data['source_id'] = $data['product_id']; + // 删除操作 + (int)app()->make(StoreCartRepository::class)->getSearch([ + 'is_pay' => 0, + 'is_del' => 0, + 'is_new' => 0, + 'is_fail' => 0, + 'product_type' => 36, + 'product_id' => $data['source_id'], + 'product_attr_unique' => $data['product_attr_unique'], + ])->delete(); + + return app('json')->success(); + } + + + + + + + +} diff --git a/route/api.php b/route/api.php index 3f04fa4..5dfb9b3 100644 --- a/route/api.php +++ b/route/api.php @@ -422,6 +422,18 @@ Route::group('api/', function () { Route::post('cart_del','cartDel');// 删除购物车商品 })->prefix('api.Supplier/')->middleware(ShopTokenMiddleware::class,TRUE); + + + // 酒道馆商品兑换相关接口 + Route::group('wine',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.Wine/'); + })->middleware(UserTokenMiddleware::class, true); //非强制登录 Route::group(function () {