StoreService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. var_dump($userId);
  98. $storeId = $this->getStoreId($userId);
  99. if($storeId<=0){
  100. $this->error = '没有店铺信息';
  101. return false;
  102. }
  103. $storeInfo = $this->model->where(['id'=>$storeId,'status'=>1,'mark'=>1])
  104. ->select(['id','user_id','name','logo'])
  105. ->first();
  106. if(empty($storeInfo)){
  107. $this->error = '没有店铺信息';
  108. return ['id'=>$storeId];
  109. }
  110. $list = GoodsModel::where(['store_id'=>$storeId,'status'=>1,'mark'=>1])
  111. ->where(function ($query) use ($params) {
  112. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  113. if ($keyword) {
  114. $query->where('goods_name', 'like', "%{$keyword}%")
  115. ->orWhere('tags', 'like', "%{$keyword}%");
  116. }
  117. })
  118. ->where(function ($query) use ($params) {
  119. $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
  120. if ($categoryId>0) {
  121. $query->where('category_id', $categoryId);
  122. }
  123. })
  124. ->select(['id','goods_name','thumb','price','sku_type','sales','sort','status'])
  125. ->orderBy('sort', 'desc')
  126. ->orderBy('id', 'desc')
  127. ->limit($pageSize)
  128. ->get();
  129. $list = $list ? $list->toArray() : [];
  130. $datas = ['id'=>$storeId];
  131. if ($list) {
  132. $datas = [
  133. 'id'=>$storeId,
  134. 'store'=>$storeInfo,
  135. 'goods'=>$list
  136. ];
  137. RedisService::set($cacheKey, $datas, rand(10,20));
  138. }
  139. return $datas;
  140. }
  141. /**
  142. * 获取显示商家ID
  143. * @param $userId
  144. * @return array|int|mixed
  145. */
  146. public function getStoreId($userId)
  147. {
  148. $cacheKey = "caches:storeId:id_{$userId}";
  149. $data = RedisService::get($cacheKey);
  150. if($data){
  151. return $data;
  152. }
  153. // 用户信息
  154. $userInfo = MemberModel::with(['store'])->where(['id'=>$userId,'mark'=>1])
  155. ->select(['id', 'realname', 'nickname','parents', 'status'])
  156. ->first();
  157. $store = isset($userInfo['store'])? $userInfo['store'] : [];
  158. $parents = isset($userInfo['parents'])? $userInfo['parents'] : '';
  159. $parents = $parents? trim($parents,',') : '';
  160. $ids = $parents? explode(',', $parents) : [];
  161. $ids = array_reverse($ids);
  162. $storeId = isset($store['id'])? $store['id'] : 0;
  163. $storeStatus = isset($store['status'])? $store['status'] : 0;
  164. $storeMark = isset($store['mark'])? $store['mark'] : 0;
  165. if(empty($userInfo)){
  166. return 0;
  167. }
  168. // 用户自己是商家
  169. if($storeId>0 && $storeStatus==1 && $storeMark==1){
  170. RedisService::set($cacheKey, $storeId, 7 * 86400);
  171. return $storeId;
  172. }
  173. // 用户上级有商家
  174. $parentStoreInfo = $this->model->whereIn('user_id',$ids)
  175. ->where(['status'=>1,'mark'=>1])
  176. ->select(['id','name'])
  177. ->orderByRaw("FIELD(user_id,".(implode(',',$ids)).") asc")
  178. ->get();
  179. dump($parentStoreInfo->toArray());
  180. $storeId = isset($parentStoreInfo['id'])?$parentStoreInfo['id'] : 0;
  181. $params = request()->all();
  182. $system = isset($params['system']) ? $params['system'] : [];
  183. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  184. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  185. $uuKey = "caches:storeId:uu_{$uuid}";
  186. if($parentStoreInfo && $storeId>0){
  187. RedisService::set($cacheKey, $storeId, 7 * 86400);
  188. return $storeId;
  189. }else if($uuid && $storeId = RedisService::get($uuKey)){
  190. return $storeId;
  191. }
  192. return 0;
  193. }
  194. /**
  195. * 行业分类
  196. * @return array|mixed
  197. */
  198. public function getCategoryList()
  199. {
  200. $cacheKey = "caches:stores:categoryList";
  201. $datas = RedisService::get($cacheKey);
  202. if($datas){
  203. return $datas;
  204. }
  205. $datas = StoreCategoryModel::where(['pid'=>0,'status'=>1,'mark'=>1])
  206. ->select(['id','name','pid','sort'])
  207. ->orderBy('sort','desc')
  208. ->orderBy('id','desc')
  209. ->get();
  210. $datas = $datas? $datas->toArray() : [];
  211. if($datas){
  212. RedisService::set($cacheKey, $datas, rand(300,600));
  213. }
  214. return $datas;
  215. }
  216. /**
  217. * 申请
  218. * @param $userId
  219. * @param $params
  220. * @return mixed
  221. */
  222. public function apply($userId, $params)
  223. {
  224. $name = isset($params['name']) ? trim($params['name']) : '';
  225. $realname = isset($params['real_name']) ? trim($params['real_name']) : '';
  226. $phone = isset($params['phone']) ? trim($params['phone']) : '';
  227. $address = isset($params['address']) ? trim($params['address']) : '';
  228. $categoryId = isset($params['category_id']) ? intval($params['category_id']) : 0;
  229. $logo = isset($params['logo']) && $params['logo']? get_image_path($params['logo']) : '';
  230. $businessLicense = isset($params['business_license']) && $params['business_license']? get_image_path($params['business_license']) : '';
  231. $otherPhoto = isset($params['other_photo']) && $params['other_photo']? get_image_path($params['other_photo']) : '';
  232. $data = [
  233. 'user_id' => $userId,
  234. 'name' => $name,
  235. 'real_name' => $realname,
  236. 'phone' => $phone,
  237. 'address' => $address,
  238. 'category_id' => $categoryId,
  239. 'logo' => $logo,
  240. 'business_license' => $businessLicense,
  241. 'other_photo' => $otherPhoto,
  242. 'order_count' => 0,
  243. 'order_total' => 0,
  244. 'confirm_remark' => '',
  245. 'create_time' => time(),
  246. 'update_time' => time(),
  247. 'status' => 2,
  248. 'mark' => 1,
  249. ];
  250. DB::beginTransaction();
  251. if($id = $this->model->where(['user_id'=>$userId])->value('id')){
  252. $this->model->where(['id'=>$id])->update($data);
  253. }else{
  254. if (!$id = $this->model->insertGetId($data)) {
  255. DB::rollBack();
  256. $this->error = '申请入驻失败';
  257. return false;
  258. }
  259. }
  260. DB::commit();
  261. RedisService::keyDel("caches:members:info_*");
  262. RedisService::keyDel("caches:stores:info*");
  263. $this->error = '申请入驻成功,请耐心等候审核~';
  264. return ['id' => $id];
  265. }
  266. /**
  267. * 入驻信息
  268. * @param $id
  269. * @param $userId
  270. * @return array|mixed
  271. */
  272. public function getApplyInfo($userId)
  273. {
  274. $cacheKey = "caches:stores:info_u{$userId}";
  275. $info = RedisService::get($cacheKey);
  276. if($info){
  277. return $info;
  278. }
  279. $where = ['user_id'=> $userId,'mark'=>1];
  280. $info = $this->model->with(['category'])->where($where)->first();
  281. $info = $info? $info->toArray() : [];
  282. if($info){
  283. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  284. RedisService::set($cacheKey, $info, rand(5, 10));
  285. }
  286. return $info;
  287. }
  288. /**
  289. * 店铺详情
  290. * @param $id
  291. * @param $userId
  292. * @return array|mixed
  293. */
  294. public function getInfo($id)
  295. {
  296. $cacheKey = "caches:stores:info_{$id}";
  297. $info = RedisService::get($cacheKey);
  298. if($info){
  299. return $info;
  300. }
  301. $where = ['id'=> $id,'mark'=>1];
  302. $info = $this->model->with(['category'])->where($where)->first();
  303. $info = $info? $info->toArray() : [];
  304. if($info){
  305. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  306. RedisService::set($cacheKey, $info, rand(5, 10));
  307. }
  308. return $info;
  309. }
  310. }