diff --git a/app/common/dao/store/StoreActivityDao.php b/app/common/dao/store/StoreActivityDao.php index b2f69f3a..b3f9628b 100644 --- a/app/common/dao/store/StoreActivityDao.php +++ b/app/common/dao/store/StoreActivityDao.php @@ -14,9 +14,16 @@ namespace app\common\dao\store; use app\common\dao\BaseDao; +use app\common\dao\store\product\CloudProductDao; use app\common\model\BaseModel; +use app\common\model\store\GeoStreet; +use app\common\model\store\product\CloudProduct; use app\common\model\store\StoreActivity; +use app\common\model\store\StoreActivityOrderProduct; +use app\common\repositories\store\product\SpuRepository; use app\common\repositories\system\RelevanceRepository; +use think\exception\ValidateException; +use think\facade\Db; /** * @@ -35,4 +42,110 @@ class StoreActivityDao extends BaseDao $where['is_del'] = 0; return $this->getSearch($where); } + + /** + * 活动商品专区 + * @param $location + * @param $streetCode + * @param $activityId + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function product($location, $streetCode, $activityId) + { + if (!empty($location) && $location != ',') { + [$lat, $lng] = explode(',', $location); + } elseif (!empty($streetCode)) { + $location = GeoStreet::where('street_code', $streetCode)->field('lng,lat')->find(); + if (!empty($location)) { + [$lat, $lng] = [$location['lat'], $location['lng']]; + } + } + if (empty($lat) || empty($lng)) { + [$lat, $lng] = ['28.889137', '105.443352']; + } + $cloud_product_arr = (new CloudProductDao())->getByDistance($lat, $lng, ['activity_id' => $activityId], 4); + $cloud_product = []; + foreach ($cloud_product_arr as $key => $value) { + $cloud_product[] = $value['product_id']; + } + $where = [ + 'is_show' => 1, + 'is_used' => 1, + 'status' => 1, + 'is_del' => 0, + 'mer_status' => 1, + 'product_id' => $cloud_product + ]; + if (!$cloud_product) { + return app('json')->success(['count' => 0, 'list' => []]); + } + $query = Db::name('cloud_product')->where('status', 1); + $count = $query->where(function($query){ + $query->whereOr('mer_labels', '') + ->whereOr('mer_labels',',5,'); + })->count(); + /** @var SpuRepository $spuRep */ + $spuRep = app()->make(SpuRepository::class); + $products = $spuRep->getApiSearch($where, 1, 4, false, true); + if ($products['list']) { + $list = $products['list']; + foreach ($cloud_product_arr as $key => $value) { + foreach ($list as $k => $v) { + if ($value['product_id'] == $v['product_id']) { + if ($value['mer_labels'] == ',5,') { + $list[$k]['mer_labels_name'] = '五日达'; + } else { + $list[$k]['mer_labels_name'] = '同城'; + } + } + } + } + } + return ['count' => $count, 'list' => $list ?? []]; + } + + /** + * 是否可购买活动商品 + * @param $userId + * @param $productId + * @return bool|int + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function canBuy($userId, $productId) + { + $activityId = Db::name('cloud_product')->where('product_id', $productId)->value('activity_id'); + $find = Db::name('store_activity_order_product')->where('user_id', $userId)->where('status', 1)->find(); + if ($find && $activityId == 2) { + return false; + } + return $activityId; + } + + /** + * 保存活动订单商品 + * @param $activityId + * @param $order + * @return void + */ + public function saveOrderProduct($activityId, $order) + { + $orderProductIds = array_column($order->orderProduct->toArray(), 'product_id'); + $productIds = CloudProduct::whereIn('product_id', $orderProductIds)->where('activity_id', $activityId)->column('product_id'); + foreach ($productIds as $productId) { + $model = new StoreActivityOrderProduct(); + $model->user_id = $order['uid']; + $model->activity_id = $activityId; + $model->product_id = $productId; + $model->number = 1; + if (!$model->save()) { + throw new ValidateException('活动商品数据保存失败'); + } + } + } + } diff --git a/app/common/dao/store/product/CloudProductDao.php b/app/common/dao/store/product/CloudProductDao.php index bc0f6e8e..e648bee8 100644 --- a/app/common/dao/store/product/CloudProductDao.php +++ b/app/common/dao/store/product/CloudProductDao.php @@ -27,4 +27,27 @@ class CloudProductDao extends BaseDao { } + /** + * 按距离获取云仓商品列表 + * @param $lat + * @param $lng + * @param $limit + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function getByDistance($lat, $lng, $where = [], $limit = 10) + { + $query = CloudProduct::where('status', 1) + ->where($where) + ->whereIn('store_name', ['耶贝尔柔韧布质竹纸特惠装(10包)', '家家宜除菌洗衣液阳光清香1千克', '家家宜除菌洗衣粉阳光清香1千克-New版', '家家宜柠檬高效除油洗洁精1.12千克']) + ->whereIn('type_id', [10, 17]) + ->whereNotNull('lat') + ->whereNotNull('long') + ->field("product_id,mer_id,store_name,mer_labels,st_distance_sphere(point(`long`,`lat`), point({$lng}, {$lat})) as distance"); + $query->order('distance')->limit($limit); + return $query->select()->toArray(); + } + } diff --git a/app/common/dao/system/merchant/MerchantDao.php b/app/common/dao/system/merchant/MerchantDao.php index 19bdd512..c28fc8a8 100644 --- a/app/common/dao/system/merchant/MerchantDao.php +++ b/app/common/dao/system/merchant/MerchantDao.php @@ -15,6 +15,7 @@ namespace app\common\dao\system\merchant; use app\common\dao\BaseDao; +use app\common\model\store\product\CloudProduct; use app\common\model\system\merchant\Merchant; use crmeb\services\VicWordService; use think\db\BaseQuery; @@ -345,4 +346,33 @@ class MerchantDao extends BaseDao return []; } + /** + * 按距离获取有效的商户 + * @param $lat + * @param $lng + * @param $sort 是否按距离排序 + * @param $distance 距离,单位米 + * @return mixed + */ + public function getProductByDistance($lat, $lng, $sort = false, $distance = 10000) + { + $query = CloudProduct::where('type_id', 'IN', ["10", "17"]) + ->whereNotNull('lat') + ->whereNotNull('long') + ->field("product_id,st_distance_sphere(point(`long`,`lat`), point({$lng}, {$lat})) as distance"); + if ($sort) { + $query->order('distance')->limit(100); + } else { + $query->having("distance <= {$distance}")->limit(50); + } + $product = $query->select()->toArray(); + if (empty($product) && $distance < 50000 && !$sort) { + $product = $this->getProductByDistance($lat, $lng, $sort, 50000); + } + if (!empty($product)) { + return array_column($product, 'product_id'); + } + return []; + } + } diff --git a/app/common/model/store/StoreActivityOrderProduct.php b/app/common/model/store/StoreActivityOrderProduct.php new file mode 100644 index 00000000..561648d8 --- /dev/null +++ b/app/common/model/store/StoreActivityOrderProduct.php @@ -0,0 +1,26 @@ +canBuy($cart['uid'], $cart['product_id']); + if (!$canBuy) { + throw new ValidateException('活动商品限购1个'); + } + if ($canBuy == 2) { + $activityProductCount += $cart['cart_num']; + if ($activityProductCount > 1){ + throw new ValidateException('活动商品限购1个'); + } + } if ($cart['product_type'] == 0) { if ($cart['product']['once_min_count'] > 0 && $cart['product']['once_min_count'] > $cart['cart_num']) throw new ValidateException('[低于起购数:' . $cart['product']['once_min_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...'); diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index c9031ae7..76981766 100644 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -15,6 +15,7 @@ use app\common\dao\store\consumption\CommissionDao; use app\common\dao\store\consumption\StoreConsumptionUserDao; use app\common\dao\store\order\StoreCartDao; use app\common\dao\store\order\StoreOrderDao; +use app\common\dao\store\StoreActivityDao; use app\common\model\store\order\StoreGroupOrder; use app\common\model\store\order\StoreOrder; use app\common\model\store\order\StoreOrderInterest; @@ -228,6 +229,7 @@ class StoreOrderRepository extends BaseRepository $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); $svipDiscount = 0; foreach ($groupOrder->orderList as $_k => $order) { + (new StoreActivityDao())->saveOrderProduct(2, $order); $order->paid = 1; $order->pay_time = $time; $svipDiscount = bcadd($order->svip_discount, $svipDiscount, 2); diff --git a/app/controller/api/store/StoreActivity.php b/app/controller/api/store/StoreActivity.php index 29efd9cb..bd220d98 100644 --- a/app/controller/api/store/StoreActivity.php +++ b/app/controller/api/store/StoreActivity.php @@ -3,6 +3,7 @@ namespace app\controller\api\store; use app\common\dao\store\consumption\StoreConsumptionDao; +use app\common\dao\store\StoreActivityDao; use app\common\dao\store\StoreActivityUserDao; use crmeb\basic\BaseController; @@ -99,4 +100,21 @@ class StoreActivity extends BaseController return app('json')->success($result); } + /** + * 活动商品专区 + * @param StoreActivityDao $dao + * @return mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function product(StoreActivityDao $dao) + { + $location = $this->request->get('location'); + $streetCode = $this->request->get('street_code'); + $activityId = $this->request->get('activity_id', 2); + $result = $dao->product($location, $streetCode, $activityId); + return app('json')->success($result); + } + } \ No newline at end of file diff --git a/app/controller/api/store/product/CloudWarehouse.php b/app/controller/api/store/product/CloudWarehouse.php index 1a2ab426..fad2d374 100644 --- a/app/controller/api/store/product/CloudWarehouse.php +++ b/app/controller/api/store/product/CloudWarehouse.php @@ -46,27 +46,22 @@ class CloudWarehouse extends BaseController */ public function index($street_code, $page = 1, $category_id = 0, $cate_pid = 0,$cate_id = 0,$location = '') { - $query = Db::name('cloud_product')->where('status', 1); if (!empty($location) && $location != ',') { [$lat, $lng] = explode(',', $location); - } - if (!empty($street_code) && (empty($location) || $location == ',')) { - - + } elseif (!empty($street_code)) { $location = GeoStreet::where('street_code', $street_code)->field('lng,lat')->find(); if (!empty($location)) { [$lat, $lng] = [$location['lat'], $location['lng']]; } } - if (!empty($lat)) { - $merIds = (new MerchantDao())->getValidMerchantByDistance($lat, $lng,true,50000); - if (!empty($merIds)) { - $query->whereIn('mer_id', $merIds); - } + if (empty($lat) || empty($lng)) { + [$lat, $lng] = ['28.889137', '105.443352']; } - if (empty($merIds) && !empty($lat)) { - $merIds = (new MerchantDao())->getValidMerchantByDistance($lat, $lng, true); - $query->whereIn('mer_id', $merIds); + $query = Db::name('cloud_product')->where('status', 1); + $productIds = (new MerchantDao())->getProductByDistance($lat, $lng,false,50000); + if (empty($productIds)) { + $productIds = (new MerchantDao())->getProductByDistance($lat, $lng, true); + $query->whereIn('product_id', $productIds); } if($cate_pid!=0){ $cate_id=Db::name('store_category')->where('pid',$cate_pid)->where('is_show',1)->column('store_category_id'); diff --git a/route/api.php b/route/api.php index b01acffa..4ddeb480 100644 --- a/route/api.php +++ b/route/api.php @@ -647,6 +647,7 @@ Route::group('api/', function () { Route::resource('store/product/cloudWarehouse', 'api.store.product.CloudWarehouse'); Route::get('store/product/town_cloud', 'api.store.product.CloudWarehouse/town'); Route::get('storeActivity/consumption', 'api.store.StoreActivity/consumption'); //消费金列表 + Route::get('storeActivity/product', 'api.store.StoreActivity/product'); //活动商品专区 Route::post('open/activityCommission', 'api.open/activityCommission'); //活动佣金回调 })->middleware(UserTokenMiddleware::class, false);