// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\GoodsModel; use App\Models\MemberModel; use App\Models\StoreCategoryModel; use App\Models\StoreModel; use App\Services\BaseService; use App\Services\RedisService; use Illuminate\Support\Facades\DB; /** * 商家管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Api */ class StoreService extends BaseService { protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new StoreModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['a.mark' => 1]; $status = isset($params['status']) ? $params['status'] : 0; if ($status > 0) { $where['a.status'] = $status; } $list = $this->model->with(['user'])->from('stores as a') ->leftJoin('member as b', 'b.id', '=', 'a.user_id') ->where($where) ->where(function ($query) use ($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; if ($keyword) { $query->where('a.name', 'like', "%{$keyword}%"); } }) ->select(['a.*']) ->orderBy('a.create_time', 'desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : ''; } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 商品列表 * @param $params * @param int $pageSize * @return array */ public function getGoodsList($params, $pageSize = 3) { $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; } $storeId = $this->getStoreId($userId); if($storeId<=0){ $this->error = $this->error? '没有店铺信息:'.$this->error: '没有店铺信息'; return false; } $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('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('category_id', $categoryId); } }) ->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) { $datas = [ 'id'=>$storeId, 'store'=>$storeInfo, 'goods'=>$list ]; RedisService::set($cacheKey, $datas, rand(10,20)); } 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; } $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:uuid_{$uuid}"; $ipKey = "caches:storeId:ip_".get_client_ip(); if($userId<=0){ if($storeId = RedisService::get($ipKey)){ $this->error = "未登录缓存的店铺"; return $storeId; }else if($uuid && $storeId = RedisService::get($uuKey)){ $this->error = "未登录缓存的店铺"; return $storeId; } } // 用户信息 $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'] : ''; $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($userId && empty($userInfo)){ $this->error = '用户账户不存在'; return 0; } // 用户自己是商家 if($storeId>0 && $storeStatus==1 && $storeMark==1){ $this->error = '用户自己的店铺'; RedisService::set($cacheKey, $storeId, 7 * 86400); return $storeId; } // 用户上级有商家 $parentStoreInfo = []; if($ids){ $parentStoreInfo = $this->model->whereIn('user_id',$ids) ->where(['status'=>1,'mark'=>1]) ->select(['id','user_id','name']) ->orderByRaw("FIELD(user_id,".(implode(',',$ids)).") asc") ->first(); } $storeId = isset($parentStoreInfo['id'])?$parentStoreInfo['id'] : 0; $parentUserId = isset($parentStoreInfo['user_id'])?$parentStoreInfo['user_id'] : 0; if($parentStoreInfo && $storeId>0){ RedisService::set($cacheKey, $storeId, 7 * 86400); RedisService::set($ipKey, $storeId, 7 * 86400); RedisService::set($uuKey, $storeId, 7 * 86400); $this->error = "上级[{$parentUserId}]的店铺"; return $storeId; } $this->error = ""; return 0; } /** * 行业分类 * @return array|mixed */ public function getCategoryList() { $cacheKey = "caches:stores:categoryList"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = StoreCategoryModel::where(['pid'=>0,'status'=>1,'mark'=>1]) ->select(['id','name','pid','sort']) ->orderBy('sort','desc') ->orderBy('id','desc') ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, rand(300,600)); } return $datas; } /** * 申请 * @param $userId * @param $params * @return mixed */ public function apply($userId, $params) { $name = isset($params['name']) ? trim($params['name']) : ''; $realname = isset($params['real_name']) ? trim($params['real_name']) : ''; $phone = isset($params['phone']) ? trim($params['phone']) : ''; $address = isset($params['address']) ? trim($params['address']) : ''; $intro = isset($params['intro']) ? trim($params['intro']) : ''; $categoryId = isset($params['category_id']) ? intval($params['category_id']) : 0; $logo = isset($params['logo']) && $params['logo']? get_image_path($params['logo']) : ''; $businessLicense = isset($params['business_license']) && $params['business_license']? get_image_path($params['business_license']) : ''; $otherPhoto = isset($params['other_photo']) && $params['other_photo']? get_image_path($params['other_photo']) : ''; $data = [ 'user_id' => $userId, 'name' => $name, 'real_name' => $realname, 'phone' => $phone, 'address' => $address, 'category_id' => $categoryId, 'logo' => $logo, 'business_license' => $businessLicense, 'other_photo' => $otherPhoto, 'intro' => $intro, 'order_count' => 0, 'order_total' => 0, 'confirm_remark' => '', 'create_time' => time(), 'update_time' => time(), 'status' => 2, 'mark' => 1, ]; DB::beginTransaction(); if($id = $this->model->where(['user_id'=>$userId])->value('id')){ $this->model->where(['id'=>$id])->update($data); }else{ if (!$id = $this->model->insertGetId($data)) { DB::rollBack(); $this->error = '申请入驻失败'; return false; } } DB::commit(); RedisService::keyDel("caches:members:info_*"); RedisService::keyDel("caches:stores:info*"); $this->error = '申请入驻成功,请耐心等候审核~'; return ['id' => $id]; } /** * 入驻信息 * @param $id * @param $userId * @return array|mixed */ public function getApplyInfo($userId) { $cacheKey = "caches:stores:info_u{$userId}"; $info = RedisService::get($cacheKey); if($info){ return $info; } $where = ['user_id'=> $userId,'mark'=>1]; $info = $this->model->with(['category'])->where($where)->first(); $info = $info? $info->toArray() : []; if($info){ $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : ''; RedisService::set($cacheKey, $info, rand(5, 10)); } return $info; } /** * 店铺详情 * @param $id * @param $userId * @return array|mixed */ public function getInfo($id) { $cacheKey = "caches:stores:info_{$id}"; $info = RedisService::get($cacheKey); if($info){ return $info; } $where = ['id'=> $id,'mark'=>1]; $info = $this->model->with(['category'])->where($where)->first(); $info = $info? $info->toArray() : []; if($info){ $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : ''; RedisService::set($cacheKey, $info, rand(5, 10)); } return $info; } }