StoreService.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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\Models\UserModel;
  15. use App\Services\BaseService;
  16. use App\Services\Common\UserRoleService;
  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\Common
  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 = 15)
  90. {
  91. $page = request()->post('page',1);
  92. $cacheKey = "caches:goods:index_{$page}_{$pageSize}:".md5(json_encode($params));
  93. $list = RedisService::get($cacheKey);
  94. if($list){
  95. return [
  96. 'pageSize' => $pageSize,
  97. 'total' => isset($list['total']) ? $list['total'] : 0,
  98. 'list' => isset($list['data']) ? $list['data'] : []
  99. ];
  100. }
  101. $where = ['a.mark' => 1];
  102. $status = isset($params['status']) && $params['status']>0 ? $params['status'] : 1;
  103. if ($status > 0) {
  104. $where['a.status'] = $status;
  105. }
  106. $list = $this->model->with(['goods'=>function($query)use($params){
  107. $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
  108. if ($categoryId>0) {
  109. $query->where('goods.category_id', $categoryId);
  110. }
  111. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  112. if ($keyword) {
  113. $query->where(function($query) use($keyword){
  114. $query->where('a.name', 'like', "%{$keyword}%")
  115. ->orWhere('goods.goods_name', 'like', "%{$keyword}%")
  116. ->orWhere('goods.tags', 'like', "%{$keyword}%");
  117. });
  118. }
  119. }])->from('stores as a')
  120. ->leftJoin('goods', function($join){
  121. $join->on('goods.store_id', '=', 'a.id')->where(['goods.status'=>1,'goods.mark'=>1]);
  122. })
  123. ->where($where)
  124. ->where('goods.id','>',0)
  125. ->where(function ($query) use ($params) {
  126. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  127. if ($keyword) {
  128. $query->where('a.name', 'like', "%{$keyword}%")
  129. ->orWhere('goods.goods_name', 'like', "%{$keyword}%")
  130. ->orWhere('goods.tags', 'like', "%{$keyword}%");
  131. }
  132. })
  133. ->where(function ($query) use ($params) {
  134. $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
  135. if ($categoryId>0) {
  136. $query->where('goods.category_id', $categoryId);
  137. }
  138. })
  139. ->select(['a.id','a.logo','a.name','a.user_id','a.status','a.sort','goods.id as goods_id'])
  140. ->groupBy('a.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. * 行业分类
  162. * @return array|mixed
  163. */
  164. public function getCategoryList()
  165. {
  166. $cacheKey = "caches:stores:categoryList";
  167. $datas = RedisService::get($cacheKey);
  168. if($datas){
  169. return $datas;
  170. }
  171. $datas = StoreCategoryModel::where(['pid'=>0,'status'=>1,'mark'=>1])
  172. ->select(['id','name','pid','sort'])
  173. ->orderBy('sort','desc')
  174. ->orderBy('id','desc')
  175. ->get();
  176. $datas = $datas? $datas->toArray() : [];
  177. if($datas){
  178. RedisService::set($cacheKey, $datas, rand(300,600));
  179. }
  180. return $datas;
  181. }
  182. /**
  183. * 申请
  184. * @param $userId
  185. * @param $params
  186. * @return mixed
  187. */
  188. public function apply($userId, $params)
  189. {
  190. $name = isset($params['name']) ? trim($params['name']) : '';
  191. $realname = isset($params['real_name']) ? trim($params['real_name']) : '';
  192. $phone = isset($params['phone']) ? trim($params['phone']) : '';
  193. $address = isset($params['address']) ? trim($params['address']) : '';
  194. $categoryId = isset($params['category_id']) ? intval($params['category_id']) : 0;
  195. $logo = isset($params['logo']) && $params['logo']? get_image_path($params['logo']) : '';
  196. $businessLicense = isset($params['business_license']) && $params['business_license']? get_image_path($params['business_license']) : '';
  197. $otherPhoto = isset($params['other_photo']) && $params['other_photo']? get_image_path($params['other_photo']) : '';
  198. $data = [
  199. 'user_id' => $userId,
  200. 'name' => $name,
  201. 'real_name' => $realname,
  202. 'phone' => $phone,
  203. 'address' => $address,
  204. 'category_id' => $categoryId,
  205. 'logo' => $logo,
  206. 'business_license' => $businessLicense,
  207. 'other_photo' => $otherPhoto,
  208. 'order_count' => 0,
  209. 'order_total' => 0,
  210. 'confirm_remark' => '',
  211. 'create_time' => time(),
  212. 'update_time' => time(),
  213. 'status' => 2,
  214. 'mark' => 1,
  215. ];
  216. DB::beginTransaction();
  217. if($id = $this->model->where(['user_id'=>$userId])->value('id')){
  218. $this->model->where(['id'=>$id])->update($data);
  219. }else{
  220. if (!$id = $this->model->insertGetId($data)) {
  221. DB::rollBack();
  222. $this->error = '申请入驻失败';
  223. return false;
  224. }
  225. }
  226. RedisService::keyDel("caches:members:info_*");
  227. RedisService::keyDel("caches:stores:info*");
  228. $this->error = '申请入驻成功,请耐心等候审核~';
  229. return ['id' => $id];
  230. }
  231. /**
  232. * 入驻信息
  233. * @param $id
  234. * @param $userId
  235. * @return array|mixed
  236. */
  237. public function getApplyInfo($userId)
  238. {
  239. $cacheKey = "caches:stores:info_u{$userId}";
  240. $info = RedisService::get($cacheKey);
  241. if($info){
  242. return $info;
  243. }
  244. $where = ['user_id'=> $userId,'mark'=>1];
  245. $info = $this->model->with(['category'])->where($where)->first();
  246. $info = $info? $info->toArray() : [];
  247. if($info){
  248. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  249. RedisService::set($cacheKey, $info, rand(5, 10));
  250. }
  251. return $info;
  252. }
  253. /**
  254. * 店铺详情
  255. * @param $id
  256. * @param $userId
  257. * @return array|mixed
  258. */
  259. public function getInfo($id)
  260. {
  261. $cacheKey = "caches:stores:info_{$id}";
  262. $info = RedisService::get($cacheKey);
  263. if($info){
  264. return $info;
  265. }
  266. $where = ['id'=> $id,'mark'=>1];
  267. $info = $this->model->with(['category'])->where($where)->first();
  268. $info = $info? $info->toArray() : [];
  269. if($info){
  270. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  271. RedisService::set($cacheKey, $info, rand(5, 10));
  272. }
  273. return $info;
  274. }
  275. }