StoreService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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\StoreCategoryModel;
  13. use App\Models\StoreModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. /**
  17. * 商家管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * @package App\Services\Common
  21. */
  22. class StoreService extends BaseService
  23. {
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new StoreModel();
  32. }
  33. /**
  34. * 静态入口
  35. * @return static|null
  36. */
  37. public static function make()
  38. {
  39. if (!self::$instance) {
  40. self::$instance = (new static());
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * @param $params
  46. * @param int $pageSize
  47. * @return array
  48. */
  49. public function getDataList($params, $pageSize = 15)
  50. {
  51. $where = ['a.mark' => 1];
  52. $status = isset($params['status']) ? $params['status'] : 0;
  53. if ($status > 0) {
  54. $where['a.status'] = $status;
  55. }
  56. $list = $this->model->with(['user'])->from('stores as a')
  57. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  58. ->where($where)
  59. ->where(function ($query) use ($params) {
  60. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  61. if ($keyword) {
  62. $query->where('a.name', 'like', "%{$keyword}%");
  63. }
  64. })
  65. ->select(['a.*'])
  66. ->orderBy('a.create_time', 'desc')
  67. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  68. $list = $list ? $list->toArray() : [];
  69. if ($list) {
  70. foreach ($list['data'] as &$item) {
  71. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  72. }
  73. }
  74. return [
  75. 'pageSize' => $pageSize,
  76. 'total' => isset($list['total']) ? $list['total'] : 0,
  77. 'list' => isset($list['data']) ? $list['data'] : []
  78. ];
  79. }
  80. /**
  81. * 商品列表
  82. * @param $params
  83. * @param int $pageSize
  84. * @return array
  85. */
  86. public function getGoodsList($params, $pageSize = 15)
  87. {
  88. $page = request()->post('page',1);
  89. $cacheKey = "caches:goods:index_{$page}_{$pageSize}:".md5(json_encode($params));
  90. $list = RedisService::get($cacheKey);
  91. if($list){
  92. return [
  93. 'pageSize' => $pageSize,
  94. 'total' => isset($list['total']) ? $list['total'] : 0,
  95. 'list' => isset($list['data']) ? $list['data'] : []
  96. ];
  97. }
  98. $where = ['a.mark' => 1];
  99. $status = isset($params['status']) && $params['status']>0 ? $params['status'] : 1;
  100. if ($status > 0) {
  101. $where['a.status'] = $status;
  102. }
  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) {
  131. $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
  132. if ($categoryId>0) {
  133. $query->where('goods.category_id', $categoryId);
  134. }
  135. })
  136. ->select(['a.id','a.logo','a.name','a.user_id','a.status','a.sort','goods.id as goods_id'])
  137. ->groupBy('a.id')
  138. ->orderBy('a.sort', 'desc')
  139. ->orderBy('a.id', 'desc')
  140. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  141. $list = $list ? $list->toArray() : [];
  142. if ($list) {
  143. foreach ($list['data'] as &$item) {
  144. $item['goods'] = isset($item['goods']) && $item['goods'] ? array_slice($item['goods'],0,6) : [];
  145. }
  146. }
  147. $total = isset($list['total']) ? $list['total'] : 0;
  148. if($total){
  149. RedisService::set($cacheKey, $list, rand(5,10));
  150. }
  151. return [
  152. 'pageSize' => $pageSize,
  153. 'total' => isset($list['total']) ? $list['total'] : 0,
  154. 'list' => isset($list['data']) ? $list['data'] : []
  155. ];
  156. }
  157. /**
  158. * 行业分类
  159. * @return array|mixed
  160. */
  161. public function getCategoryList()
  162. {
  163. $cacheKey = "caches:stores:categoryList";
  164. $datas = RedisService::get($cacheKey);
  165. if($datas){
  166. return $datas;
  167. }
  168. $datas = StoreCategoryModel::where(['pid'=>0,'status'=>1,'mark'=>1])
  169. ->select(['id','name','pid','sort'])
  170. ->orderBy('sort','desc')
  171. ->orderBy('id','desc')
  172. ->get();
  173. $datas = $datas? $datas->toArray() : [];
  174. if($datas){
  175. RedisService::set($cacheKey, $datas, rand(300,600));
  176. }
  177. return $datas;
  178. }
  179. /**
  180. * 申请
  181. * @param $userId
  182. * @param $params
  183. * @return mixed
  184. */
  185. public function apply($userId, $params)
  186. {
  187. $name = isset($params['name']) ? trim($params['name']) : '';
  188. $realname = isset($params['real_name']) ? trim($params['real_name']) : '';
  189. $phone = isset($params['phone']) ? trim($params['phone']) : '';
  190. $address = isset($params['address']) ? trim($params['address']) : '';
  191. $categoryId = isset($params['category_id']) ? intval($params['category_id']) : 0;
  192. $logo = isset($params['logo']) && $params['logo']? get_image_path($params['logo']) : '';
  193. $businessLicense = isset($params['business_license']) && $params['business_license']? get_image_path($params['business_license']) : '';
  194. $otherPhoto = isset($params['other_photo']) && $params['other_photo']? get_image_path($params['other_photo']) : '';
  195. $data = [
  196. 'user_id' => $userId,
  197. 'name' => $name,
  198. 'real_name' => $realname,
  199. 'phone' => $phone,
  200. 'address' => $address,
  201. 'category_id' => $categoryId,
  202. 'logo' => $logo,
  203. 'business_license' => $businessLicense,
  204. 'other_photo' => $otherPhoto,
  205. 'order_count' => 0,
  206. 'order_total' => 0,
  207. 'confirm_remark' => '',
  208. 'create_time' => time(),
  209. 'update_time' => time(),
  210. 'status' => 2,
  211. 'mark' => 1,
  212. ];
  213. if($id = $this->model->where(['user_id'=>$userId])->value('id')){
  214. $this->model->where(['id'=>$id])->update($data);
  215. }else{
  216. if (!$id = $this->model->insertGetId($data)) {
  217. $this->error = '申请入驻失败';
  218. return false;
  219. }
  220. }
  221. RedisService::keyDel("caches:members:info_*");
  222. RedisService::keyDel("caches:stores:info*");
  223. $this->error = '申请入驻成功,请耐心等候审核~';
  224. return ['id' => $id];
  225. }
  226. /**
  227. * 入驻信息
  228. * @param $id
  229. * @param $userId
  230. * @return array|mixed
  231. */
  232. public function getApplyInfo($userId)
  233. {
  234. $cacheKey = "caches:stores:info_u{$userId}";
  235. $info = RedisService::get($cacheKey);
  236. if($info){
  237. return $info;
  238. }
  239. $where = ['user_id'=> $userId,'mark'=>1];
  240. $info = $this->model->with(['category'])->where($where)->first();
  241. $info = $info? $info->toArray() : [];
  242. if($info){
  243. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  244. RedisService::set($cacheKey, $info, rand(5, 10));
  245. }
  246. return $info;
  247. }
  248. /**
  249. * 店铺详情
  250. * @param $id
  251. * @param $userId
  252. * @return array|mixed
  253. */
  254. public function getInfo($id)
  255. {
  256. $cacheKey = "caches:stores:info_{$id}";
  257. $info = RedisService::get($cacheKey);
  258. if($info){
  259. return $info;
  260. }
  261. $where = ['id'=> $id,'mark'=>1];
  262. $info = $this->model->with(['category'])->where($where)->first();
  263. $info = $info? $info->toArray() : [];
  264. if($info){
  265. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  266. RedisService::set($cacheKey, $info, rand(5, 10));
  267. }
  268. return $info;
  269. }
  270. }