StoreService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\GoodsModel;
  13. use App\Models\MemberModel;
  14. use App\Models\StoreCategoryModel;
  15. use App\Models\StoreModel;
  16. use App\Services\BaseService;
  17. use App\Services\RedisService;
  18. use Illuminate\Support\Facades\DB;
  19. /**
  20. * 商家管理-服务类
  21. * @author laravel开发员
  22. * @since 2020/11/11
  23. * @package App\Services\Api
  24. */
  25. class StoreService extends BaseService
  26. {
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new StoreModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $where = ['a.mark' => 1];
  55. $status = isset($params['status']) ? $params['status'] : 0;
  56. if ($status > 0) {
  57. $where['a.status'] = $status;
  58. }
  59. $list = $this->model->with(['user'])->from('stores as a')
  60. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  61. ->where($where)
  62. ->where(function ($query) use ($params) {
  63. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  64. if ($keyword) {
  65. $query->where('a.name', 'like', "%{$keyword}%");
  66. }
  67. })
  68. ->select(['a.*'])
  69. ->orderBy('a.create_time', 'desc')
  70. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  71. $list = $list ? $list->toArray() : [];
  72. if ($list) {
  73. foreach ($list['data'] as &$item) {
  74. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  75. }
  76. }
  77. return [
  78. 'pageSize' => $pageSize,
  79. 'total' => isset($list['total']) ? $list['total'] : 0,
  80. 'list' => isset($list['data']) ? $list['data'] : []
  81. ];
  82. }
  83. /**
  84. * 商品列表
  85. * @param $params
  86. * @param int $pageSize
  87. * @return array
  88. */
  89. public function getGoodsList($params, $pageSize = 7)
  90. {
  91. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  92. $cacheKey = "caches:stores:index_goods_{$userId}_{$pageSize}:".md5(json_encode($params));
  93. $datas = RedisService::get($cacheKey);
  94. if($datas){
  95. return $datas;
  96. }
  97. $storeId = $this->getStoreId($userId);
  98. if($storeId<=0){
  99. $this->error = '没有店铺信息';
  100. return false;
  101. }
  102. $storeInfo = $this->model->where(['id'=>$storeId,'status'=>1,'mark'=>1])
  103. ->select(['id','user_id','name','logo'])
  104. ->first();
  105. if(empty($storeInfo)){
  106. $this->error = '没有店铺信息';
  107. return ['id'=>$storeId];
  108. }
  109. $list = GoodsModel::where(['store_id'=>$storeId,'status'=>1,'mark'=>1])
  110. ->where(function ($query) use ($params) {
  111. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  112. if ($keyword) {
  113. $query->where('goods_name', 'like', "%{$keyword}%")
  114. ->orWhere('tags', 'like', "%{$keyword}%");
  115. }
  116. })
  117. ->where(function ($query) use ($params) {
  118. $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
  119. if ($categoryId>0) {
  120. $query->where('category_id', $categoryId);
  121. }
  122. })
  123. ->select(['id','goods_name','thumb','price','sku_type','sales','sort','status'])
  124. ->orderBy('sort', 'desc')
  125. ->orderBy('id', 'desc')
  126. ->limit($pageSize)
  127. ->get();
  128. $list = $list ? $list->toArray() : [];
  129. $datas = ['id'=>$storeId];
  130. if ($list) {
  131. $datas = [
  132. 'id'=>$storeId,
  133. 'store'=>$storeInfo,
  134. 'goods'=>$list
  135. ];
  136. RedisService::set($cacheKey, $datas, rand(10,20));
  137. }
  138. return $datas;
  139. }
  140. /**
  141. * 获取显示商家ID
  142. * @param $userId
  143. * @return array|int|mixed
  144. */
  145. public function getStoreId($userId)
  146. {
  147. $cacheKey = "caches:storeId:id_{$userId}";
  148. $data = RedisService::get($cacheKey);
  149. if($data){
  150. return $data;
  151. }
  152. // 用户信息
  153. $userInfo = MemberModel::with(['store'])->where(['id'=>$userId,'mark'=>1])
  154. ->select(['id', 'realname', 'nickname','parents', 'status'])
  155. ->first();
  156. $store = isset($userInfo['store'])? $userInfo['store'] : [];
  157. $parents = isset($userInfo['parents'])? $userInfo['parents'] : '';
  158. $parents = $parents? trim($parents,',') : '';
  159. $ids = $parents? explode(',', $parents) : [];
  160. $ids = array_reverse($ids);
  161. $storeId = isset($store['id'])? $store['id'] : 0;
  162. $storeStatus = isset($store['status'])? $store['status'] : 0;
  163. $storeMark = isset($store['mark'])? $store['mark'] : 0;
  164. if(empty($userInfo)){
  165. return 0;
  166. }
  167. // 用户自己是商家
  168. if($storeId>0 && $storeStatus==1 && $storeMark==1){
  169. RedisService::set($cacheKey, $storeId, 7 * 86400);
  170. return $storeId;
  171. }
  172. // 用户上级有商家
  173. $parentStoreInfo = $this->model->whereIn('user_id',$ids)
  174. ->where(['status'=>1,'mark'=>1])
  175. ->select(['id','name'])
  176. ->orderByRaw(DB::raw("FIND_IN_SET(id,{$parents}) asc"))
  177. ->first();
  178. $storeId = isset($parentStoreInfo['id'])?$parentStoreInfo['id'] : 0;
  179. $params = request()->all();
  180. $system = isset($params['system']) ? $params['system'] : [];
  181. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  182. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  183. $uuKey = "caches:storeId:uu_{$uuid}";
  184. if($parentStoreInfo && $storeId>0){
  185. RedisService::set($cacheKey, $storeId, 7 * 86400);
  186. return $storeId;
  187. }else if($uuid && $storeId = RedisService::get($uuKey)){
  188. return $storeId;
  189. }
  190. return 0;
  191. }
  192. /**
  193. * 行业分类
  194. * @return array|mixed
  195. */
  196. public function getCategoryList()
  197. {
  198. $cacheKey = "caches:stores:categoryList";
  199. $datas = RedisService::get($cacheKey);
  200. if($datas){
  201. return $datas;
  202. }
  203. $datas = StoreCategoryModel::where(['pid'=>0,'status'=>1,'mark'=>1])
  204. ->select(['id','name','pid','sort'])
  205. ->orderBy('sort','desc')
  206. ->orderBy('id','desc')
  207. ->get();
  208. $datas = $datas? $datas->toArray() : [];
  209. if($datas){
  210. RedisService::set($cacheKey, $datas, rand(300,600));
  211. }
  212. return $datas;
  213. }
  214. /**
  215. * 申请
  216. * @param $userId
  217. * @param $params
  218. * @return mixed
  219. */
  220. public function apply($userId, $params)
  221. {
  222. $name = isset($params['name']) ? trim($params['name']) : '';
  223. $realname = isset($params['real_name']) ? trim($params['real_name']) : '';
  224. $phone = isset($params['phone']) ? trim($params['phone']) : '';
  225. $address = isset($params['address']) ? trim($params['address']) : '';
  226. $categoryId = isset($params['category_id']) ? intval($params['category_id']) : 0;
  227. $logo = isset($params['logo']) && $params['logo']? get_image_path($params['logo']) : '';
  228. $businessLicense = isset($params['business_license']) && $params['business_license']? get_image_path($params['business_license']) : '';
  229. $otherPhoto = isset($params['other_photo']) && $params['other_photo']? get_image_path($params['other_photo']) : '';
  230. $data = [
  231. 'user_id' => $userId,
  232. 'name' => $name,
  233. 'real_name' => $realname,
  234. 'phone' => $phone,
  235. 'address' => $address,
  236. 'category_id' => $categoryId,
  237. 'logo' => $logo,
  238. 'business_license' => $businessLicense,
  239. 'other_photo' => $otherPhoto,
  240. 'order_count' => 0,
  241. 'order_total' => 0,
  242. 'confirm_remark' => '',
  243. 'create_time' => time(),
  244. 'update_time' => time(),
  245. 'status' => 2,
  246. 'mark' => 1,
  247. ];
  248. DB::beginTransaction();
  249. if($id = $this->model->where(['user_id'=>$userId])->value('id')){
  250. $this->model->where(['id'=>$id])->update($data);
  251. }else{
  252. if (!$id = $this->model->insertGetId($data)) {
  253. DB::rollBack();
  254. $this->error = '申请入驻失败';
  255. return false;
  256. }
  257. }
  258. DB::commit();
  259. RedisService::keyDel("caches:members:info_*");
  260. RedisService::keyDel("caches:stores:info*");
  261. $this->error = '申请入驻成功,请耐心等候审核~';
  262. return ['id' => $id];
  263. }
  264. /**
  265. * 入驻信息
  266. * @param $id
  267. * @param $userId
  268. * @return array|mixed
  269. */
  270. public function getApplyInfo($userId)
  271. {
  272. $cacheKey = "caches:stores:info_u{$userId}";
  273. $info = RedisService::get($cacheKey);
  274. if($info){
  275. return $info;
  276. }
  277. $where = ['user_id'=> $userId,'mark'=>1];
  278. $info = $this->model->with(['category'])->where($where)->first();
  279. $info = $info? $info->toArray() : [];
  280. if($info){
  281. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  282. RedisService::set($cacheKey, $info, rand(5, 10));
  283. }
  284. return $info;
  285. }
  286. /**
  287. * 店铺详情
  288. * @param $id
  289. * @param $userId
  290. * @return array|mixed
  291. */
  292. public function getInfo($id)
  293. {
  294. $cacheKey = "caches:stores:info_{$id}";
  295. $info = RedisService::get($cacheKey);
  296. if($info){
  297. return $info;
  298. }
  299. $where = ['id'=> $id,'mark'=>1];
  300. $info = $this->model->with(['category'])->where($where)->first();
  301. $info = $info? $info->toArray() : [];
  302. if($info){
  303. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  304. RedisService::set($cacheKey, $info, rand(5, 10));
  305. }
  306. return $info;
  307. }
  308. }