添加:酒道馆商品列表及购物车处理相关内容

This commit is contained in:
wuhui_zzw 2024-03-08 08:59:33 +08:00
parent ea96c5c31d
commit 4944614d5b
5 changed files with 282 additions and 13 deletions

View File

@ -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);
});

View File

@ -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');
}
}

View File

@ -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']}%");
});

165
app/controller/api/Wine.php Normal file
View File

@ -0,0 +1,165 @@
<?php
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;
use crmeb\basic\BaseController;
use think\App;
use think\exception\ValidateException;
class Wine extends BaseController{
protected $services;
public function __construct(App $app, SystemSupplierServices $services){
parent::__construct($app);
$this->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();
}
}

View File

@ -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 () {