// +---------------------------------------------------------------------- namespace App\Services\Api; 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; /** * 商家管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Common */ class StoreService extends BaseService { /** * 构造函数 * @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 = 15) { $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'] : [] ]; } $where = ['a.mark' => 1]; $status = isset($params['status']) && $params['status']>0 ? $params['status'] : 1; if ($status > 0) { $where['a.status'] = $status; } $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) ->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}%"); } }) ->where(function ($query) use ($params) { $categoryId = isset($params['category_id']) ? $params['category_id'] : 0; if ($categoryId>0) { $query->where('goods.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); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['goods'] = isset($item['goods']) && $item['goods'] ? array_slice($item['goods'],0,6) : []; } } $total = isset($list['total']) ? $list['total'] : 0; if($total){ RedisService::set($cacheKey, $list, rand(5,10)); } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 行业分类 * @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']) : ''; $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, '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; } }