StoreService.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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'])->from('stores as a')
  104. ->leftJoin('goods as b', function($join){
  105. $join->on('b.store_id', '=', 'a.id')->where(['b.status'=>1,'b.mark'=>1]);
  106. })
  107. ->where($where)
  108. ->where('b.id','>',0)
  109. ->where(function ($query) use ($params) {
  110. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  111. if ($keyword) {
  112. $query->where('a.name', 'like', "%{$keyword}%")
  113. ->orWhere('b.goods_name', 'like', "%{$keyword}%")
  114. ->orWhere('b.tags', 'like', "%{$keyword}%");
  115. }
  116. })
  117. ->select(['a.id','a.logo','a.name','a.user_id','a.status','b.id as goods_id'])
  118. ->groupBy('a.id')
  119. ->orderBy('a.sort', 'desc')
  120. ->orderBy('a.id', 'desc')
  121. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  122. $list = $list ? $list->toArray() : [];
  123. if ($list) {
  124. foreach ($list['data'] as &$item) {
  125. $item['goods'] = isset($item['goods']) && $item['goods'] ? array_slice($item['goods'],0,6) : [];
  126. }
  127. }
  128. $total = isset($list['total']) ? $list['total'] : 0;
  129. if($total){
  130. RedisService::set($cacheKey, $list, rand(5,10));
  131. }
  132. return [
  133. 'pageSize' => $pageSize,
  134. 'total' => isset($list['total']) ? $list['total'] : 0,
  135. 'list' => isset($list['data']) ? $list['data'] : []
  136. ];
  137. }
  138. /**
  139. * 行业分类
  140. * @return array|mixed
  141. */
  142. public function getCategoryList()
  143. {
  144. $cacheKey = "caches:stores:categoryList";
  145. $datas = RedisService::get($cacheKey);
  146. if($datas){
  147. return $datas;
  148. }
  149. $datas = StoreCategoryModel::where(['pid'=>0,'status'=>1,'mark'=>1])
  150. ->select(['id','name','pid','sort'])
  151. ->orderBy('sort','desc')
  152. ->orderBy('id','desc')
  153. ->get();
  154. $datas = $datas? $datas->toArray() : [];
  155. if($datas){
  156. RedisService::set($cacheKey, $datas, rand(300,600));
  157. }
  158. return $datas;
  159. }
  160. /**
  161. * 申请
  162. * @param $userId
  163. * @param $params
  164. * @return mixed
  165. */
  166. public function apply($userId, $params)
  167. {
  168. $name = isset($params['name']) ? trim($params['name']) : '';
  169. $realname = isset($params['real_name']) ? trim($params['real_name']) : '';
  170. $phone = isset($params['phone']) ? trim($params['phone']) : '';
  171. $address = isset($params['address']) ? trim($params['address']) : '';
  172. $categoryId = isset($params['category_id']) ? intval($params['category_id']) : 0;
  173. $logo = isset($params['logo']) && $params['logo']? get_image_path($params['logo']) : '';
  174. $businessLicense = isset($params['business_license']) && $params['business_license']? get_image_path($params['business_license']) : '';
  175. $otherPhoto = isset($params['other_photo']) && $params['other_photo']? get_image_path($params['other_photo']) : '';
  176. $data = [
  177. 'user_id' => $userId,
  178. 'name' => $name,
  179. 'real_name' => $realname,
  180. 'phone' => $phone,
  181. 'address' => $address,
  182. 'category_id' => $categoryId,
  183. 'logo' => $logo,
  184. 'business_license' => $businessLicense,
  185. 'other_photo' => $otherPhoto,
  186. 'order_count' => 0,
  187. 'order_total' => 0,
  188. 'confirm_remark' => '',
  189. 'create_time' => time(),
  190. 'update_time' => time(),
  191. 'status' => 2,
  192. 'mark' => 1,
  193. ];
  194. if($id = $this->model->where(['user_id'=>$userId])->value('id')){
  195. $this->model->where(['id'=>$id])->update($data);
  196. }else{
  197. if (!$id = $this->model->insertGetId($data)) {
  198. $this->error = '申请入驻失败';
  199. return false;
  200. }
  201. }
  202. RedisService::keyDel("caches:members:info_*");
  203. RedisService::keyDel("caches:stores:info*");
  204. $this->error = '申请入驻成功,请耐心等候审核~';
  205. return ['id' => $id];
  206. }
  207. /**
  208. * 入驻信息
  209. * @param $id
  210. * @param $userId
  211. * @return array|mixed
  212. */
  213. public function getApplyInfo($userId)
  214. {
  215. $cacheKey = "caches:stores:info_u{$userId}";
  216. $info = RedisService::get($cacheKey);
  217. if($info){
  218. return $info;
  219. }
  220. $where = ['user_id'=> $userId,'mark'=>1];
  221. $info = $this->model->with(['category'])->where($where)->first();
  222. $info = $info? $info->toArray() : [];
  223. if($info){
  224. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  225. RedisService::set($cacheKey, $info, rand(5, 10));
  226. }
  227. return $info;
  228. }
  229. /**
  230. * 店铺详情
  231. * @param $id
  232. * @param $userId
  233. * @return array|mixed
  234. */
  235. public function getInfo($id)
  236. {
  237. $cacheKey = "caches:stores:info_{$id}";
  238. $info = RedisService::get($cacheKey);
  239. if($info){
  240. return $info;
  241. }
  242. $where = ['id'=> $id,'mark'=>1];
  243. $info = $this->model->with(['category'])->where($where)->first();
  244. $info = $info? $info->toArray() : [];
  245. if($info){
  246. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  247. RedisService::set($cacheKey, $info, rand(5, 10));
  248. }
  249. return $info;
  250. }
  251. }