|
|
@@ -11,11 +11,11 @@
|
|
|
|
|
|
namespace App\Services\Api;
|
|
|
|
|
|
+use App\Models\GoodsModel;
|
|
|
+use App\Models\MemberModel;
|
|
|
use App\Models\StoreCategoryModel;
|
|
|
use App\Models\StoreModel;
|
|
|
-use App\Models\UserModel;
|
|
|
use App\Services\BaseService;
|
|
|
-use App\Services\Common\UserRoleService;
|
|
|
use App\Services\RedisService;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
@@ -23,7 +23,7 @@ use Illuminate\Support\Facades\DB;
|
|
|
* 商家管理-服务类
|
|
|
* @author laravel开发员
|
|
|
* @since 2020/11/11
|
|
|
- * @package App\Services\Common
|
|
|
+ * @package App\Services\Api
|
|
|
*/
|
|
|
class StoreService extends BaseService
|
|
|
{
|
|
|
@@ -93,77 +93,124 @@ class StoreService extends BaseService
|
|
|
* @param int $pageSize
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function getGoodsList($params, $pageSize = 15)
|
|
|
+ public function getGoodsList($params, $pageSize = 7)
|
|
|
{
|
|
|
- $page = request()->post('page',1);
|
|
|
- $cacheKey = "caches:goods:index_{$page}_{$pageSize}:".md5(json_encode($params));
|
|
|
- $list = RedisService::get($cacheKey);
|
|
|
- if($list){
|
|
|
- return [
|
|
|
- 'pageSize' => $pageSize,
|
|
|
- 'total' => isset($list['total']) ? $list['total'] : 0,
|
|
|
- 'list' => isset($list['data']) ? $list['data'] : []
|
|
|
- ];
|
|
|
+ $userId = isset($params['user_id'])? $params['user_id'] : 0;
|
|
|
+ $cacheKey = "caches:stores:index_goods_{$userId}_{$pageSize}:".md5(json_encode($params));
|
|
|
+ $datas = RedisService::get($cacheKey);
|
|
|
+ if($datas){
|
|
|
+ return $datas;
|
|
|
}
|
|
|
- $where = ['a.mark' => 1];
|
|
|
- $status = isset($params['status']) && $params['status']>0 ? $params['status'] : 1;
|
|
|
- if ($status > 0) {
|
|
|
- $where['a.status'] = $status;
|
|
|
+
|
|
|
+ $storeId = $this->getStoreId($userId);
|
|
|
+ if($storeId<=0){
|
|
|
+ $this->error = '没有店铺信息';
|
|
|
+ return false;
|
|
|
}
|
|
|
- $list = $this->model->with(['goods'=>function($query)use($params){
|
|
|
- $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
|
|
|
- if ($categoryId>0) {
|
|
|
- $query->where('goods.category_id', $categoryId);
|
|
|
- }
|
|
|
|
|
|
- $keyword = isset($params['keyword']) ? $params['keyword'] : '';
|
|
|
- if ($keyword) {
|
|
|
- $query->where(function($query) use($keyword){
|
|
|
- $query->where('a.name', 'like', "%{$keyword}%")
|
|
|
- ->orWhere('goods.goods_name', 'like', "%{$keyword}%")
|
|
|
- ->orWhere('goods.tags', 'like', "%{$keyword}%");
|
|
|
- });
|
|
|
- }
|
|
|
- }])->from('stores as a')
|
|
|
- ->leftJoin('goods', function($join){
|
|
|
- $join->on('goods.store_id', '=', 'a.id')->where(['goods.status'=>1,'goods.mark'=>1]);
|
|
|
- })
|
|
|
- ->where($where)
|
|
|
- ->where('goods.id','>',0)
|
|
|
+ $storeInfo = $this->model->where(['id'=>$storeId,'status'=>1,'mark'=>1])
|
|
|
+ ->select(['id','user_id','name','logo'])
|
|
|
+ ->first();
|
|
|
+ if(empty($storeInfo)){
|
|
|
+ $this->error = '没有店铺信息';
|
|
|
+ return ['id'=>$storeId];
|
|
|
+ }
|
|
|
+
|
|
|
+ $list = GoodsModel::where(['store_id'=>$storeId,'status'=>1,'mark'=>1])
|
|
|
->where(function ($query) use ($params) {
|
|
|
$keyword = isset($params['keyword']) ? $params['keyword'] : '';
|
|
|
if ($keyword) {
|
|
|
- $query->where('a.name', 'like', "%{$keyword}%")
|
|
|
- ->orWhere('goods.goods_name', 'like', "%{$keyword}%")
|
|
|
- ->orWhere('goods.tags', 'like', "%{$keyword}%");
|
|
|
+ $query->where('goods_name', 'like', "%{$keyword}%")
|
|
|
+ ->orWhere('tags', 'like', "%{$keyword}%");
|
|
|
}
|
|
|
})
|
|
|
->where(function ($query) use ($params) {
|
|
|
$categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
|
|
|
if ($categoryId>0) {
|
|
|
- $query->where('goods.category_id', $categoryId);
|
|
|
+ $query->where('category_id', $categoryId);
|
|
|
}
|
|
|
})
|
|
|
- ->select(['a.id','a.logo','a.name','a.user_id','a.status','a.sort','goods.id as goods_id'])
|
|
|
- ->groupBy('a.id')
|
|
|
- ->orderBy('a.sort', 'desc')
|
|
|
- ->orderBy('a.id', 'desc')
|
|
|
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
|
|
|
+ ->select(['id','goods_name','thumb','price','sku_type','sales','sort','status'])
|
|
|
+ ->orderBy('sort', 'desc')
|
|
|
+ ->orderBy('id', 'desc')
|
|
|
+ ->limit($pageSize)
|
|
|
+ ->get();
|
|
|
$list = $list ? $list->toArray() : [];
|
|
|
+ $datas = ['id'=>$storeId];
|
|
|
if ($list) {
|
|
|
- foreach ($list['data'] as &$item) {
|
|
|
- $item['goods'] = isset($item['goods']) && $item['goods'] ? array_slice($item['goods'],0,6) : [];
|
|
|
- }
|
|
|
+ $datas = [
|
|
|
+ 'id'=>$storeId,
|
|
|
+ 'store'=>$storeInfo,
|
|
|
+ 'goods'=>$list
|
|
|
+ ];
|
|
|
+ RedisService::set($cacheKey, $datas, rand(10,20));
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = '获取成功';
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取显示商家ID
|
|
|
+ * @param $userId
|
|
|
+ * @return array|int|mixed
|
|
|
+ */
|
|
|
+ public function getStoreId($userId)
|
|
|
+ {
|
|
|
+ $cacheKey = "caches:storeId:id_{$userId}";
|
|
|
+ $data = RedisService::get($cacheKey);
|
|
|
+ if($data){
|
|
|
+ return $data;
|
|
|
}
|
|
|
- $total = isset($list['total']) ? $list['total'] : 0;
|
|
|
- if($total){
|
|
|
- RedisService::set($cacheKey, $list, rand(5,10));
|
|
|
+
|
|
|
+ // 用户信息
|
|
|
+ $userInfo = MemberModel::with(['store'])
|
|
|
+ ->where(['id'=>$userId,'mark'=>1])
|
|
|
+ ->select(['id', 'realname', 'nickname','parents', 'status'])
|
|
|
+ ->first();
|
|
|
+ $store = isset($userInfo['store'])? $userInfo['store'] : [];
|
|
|
+ $parents = isset($userInfo['parents'])? $userInfo['parents'] : '';
|
|
|
+ var_dump($parents);
|
|
|
+ $ids = $parents? explode(',', $parents) : [];
|
|
|
+ $ids = array_filter($ids);
|
|
|
+ $ids = array_reverse($ids);
|
|
|
+ $storeId = isset($store['id'])? $store['id'] : 0;
|
|
|
+ $storeStatus = isset($store['status'])? $store['status'] : 0;
|
|
|
+ $storeMark = isset($store['mark'])? $store['mark'] : 0;
|
|
|
+ if(empty($userInfo)){
|
|
|
+ return 0;
|
|
|
}
|
|
|
- return [
|
|
|
- 'pageSize' => $pageSize,
|
|
|
- 'total' => isset($list['total']) ? $list['total'] : 0,
|
|
|
- 'list' => isset($list['data']) ? $list['data'] : []
|
|
|
- ];
|
|
|
+
|
|
|
+ // 用户自己是商家
|
|
|
+ if($storeId>0 && $storeStatus==1 && $storeMark==1){
|
|
|
+ RedisService::set($cacheKey, $storeId, 7 * 86400);
|
|
|
+ return $storeId;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 用户上级有商家
|
|
|
+ $parentStoreInfo = $this->model->whereIn('user_id',$ids)
|
|
|
+ ->where(['status'=>1,'mark'=>1])
|
|
|
+ ->select(['id','user_id','name'])
|
|
|
+ ->orderByRaw("FIELD(user_id,".(implode(',',$ids)).") asc")
|
|
|
+ ->get();
|
|
|
+ dump($ids);
|
|
|
+ dump($parentStoreInfo->toArray());
|
|
|
+ $storeId = isset($parentStoreInfo['id'])?$parentStoreInfo['id'] : 0;
|
|
|
+
|
|
|
+ $params = request()->all();
|
|
|
+ $system = isset($params['system']) ? $params['system'] : [];
|
|
|
+ $system = $system && !is_array($system) ? json_decode($system, true) : $system;
|
|
|
+ $uuid = isset($system['uuid']) ? $system['uuid'] : '';
|
|
|
+ $uuKey = "caches:storeId:uu_{$uuid}";
|
|
|
+ if($parentStoreInfo && $storeId>0){
|
|
|
+ RedisService::set($cacheKey, $storeId, 7 * 86400);
|
|
|
+ return $storeId;
|
|
|
+ }else if($uuid && $storeId = RedisService::get($uuKey)){
|
|
|
+ return $storeId;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|