StoreService.php 12 KB

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