StoreService.php 9.9 KB

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