GoodsCategoryService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\GoodsCategoryModel;
  13. use App\Models\GoodsModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. /**
  17. * 商品分类管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class GoodsCategoryService
  21. * @package App\Services\Api
  22. */
  23. class GoodsCategoryService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. * GoodsCategoryService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new GoodsCategoryModel();
  36. }
  37. /**
  38. * 静态入口
  39. * @return static|null
  40. */
  41. public static function make()
  42. {
  43. if (!self::$instance) {
  44. self::$instance = (new static());
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 列表数据
  50. * @param $params
  51. * @param int $pageSize
  52. * @return array
  53. */
  54. public function getDataList($params, $pageSize = 15, $field = '')
  55. {
  56. $cacheKey = "caches:goodsCategorys:{$pageSize}_".md5(json_encode($params));
  57. $datas = RedisService::get($cacheKey);
  58. // var_dump($datas);
  59. if($datas){
  60. $datas['cache'] = true;
  61. return $datas;
  62. }
  63. $where = ['a.mark' => 1];
  64. $field = $field? $field : 'lev_a.id,lev_a.merch_id,lev_a.cate_id,lev_a.pid,lev_a.name,lev_a.icon,lev_a.status';
  65. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
  66. $order = 'id desc';
  67. if($sortType == 1){
  68. $order = 'lev_a.is_recommend asc,lev_a.sort desc, lev_a.id asc';
  69. }
  70. $list = $this->model->with(['subList'])->from('goods_category as a')
  71. ->where($where)
  72. ->where(function ($query) use ($params) {
  73. $status = isset($params['status']) ? $params['status'] : 0;
  74. $status = $status > 0 ? $status : 1;
  75. if ($status > 0) {
  76. $query->where('a.status', $status);
  77. }
  78. $pid = isset($params['pid']) ? $params['pid'] : -1;
  79. if ($pid >= 0) {
  80. $query->where('a.pid', $pid);
  81. }
  82. if (isset($params['is_recommend']) && $params['is_recommend']) {
  83. $query->where('a.is_recommend', $params['is_recommend']);
  84. }
  85. // 商户
  86. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  87. if ($merchId > 0) {
  88. $query->where('a.merch_id', $merchId);
  89. }
  90. // 商户
  91. $ids = isset($params['ids']) ? $params['ids'] : [];
  92. if (count($ids) > 0) {
  93. $query->whereIn('a.id', $ids);
  94. }
  95. })
  96. ->where(function ($query) use ($params) {
  97. $keyword = isset($params['kw']) ? $params['kw'] : '';
  98. if ($keyword) {
  99. $query->where('a.name', 'like', "%{$keyword}%");
  100. }
  101. })
  102. ->selectRaw($field)
  103. ->orderByRaw($order)
  104. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  105. $list = $list ? $list->toArray() : [];
  106. if ($list) {
  107. foreach ($list['data'] as &$item) {
  108. $item['icon'] = isset($item['icon']) && $item['icon'] ? get_image_url($item['icon']) : '';
  109. $item['sub_list'] = isset($item['sub_list']) && $item['sub_list']? $item['sub_list'] : [];
  110. if($item['sub_list']){
  111. foreach ($item['sub_list'] as &$v){
  112. $v['icon'] = isset($v['icon']) && $v['icon'] ? get_image_url($v['icon']) : get_image_url('/images/member/logo.png');
  113. }
  114. unset($v);
  115. }
  116. }
  117. }
  118. $total = isset($list['total']) ? $list['total'] : 0;
  119. $datas = [
  120. 'pageSize' => $pageSize,
  121. 'total' => $total,
  122. 'list' => isset($list['data']) ? $list['data'] : []
  123. ];
  124. if($total>0){
  125. RedisService::set($cacheKey, $datas, rand(5,10));
  126. }
  127. return $datas;
  128. }
  129. /**
  130. * 获取主分类ID
  131. * @return array|mixed
  132. */
  133. public function getCateIds()
  134. {
  135. $cacheKey = "caches:goods:category_ids";
  136. $datas = RedisService::get($cacheKey);
  137. if($datas){
  138. return $datas;
  139. }
  140. $datas = $this->model->where(['status'=>1,'pid'=>0,'mark'=>1])->orderBy('create_time','asc')->select(['pid','cate_id'])->get();
  141. $datas = $datas? $datas->toArray() : [];
  142. if($datas){
  143. RedisService::set($cacheKey, $datas, rand(1200, 3600));
  144. }
  145. return $datas;
  146. }
  147. /**
  148. * 获取主分类ID
  149. * @return array|mixed
  150. */
  151. public function getCateSubIds()
  152. {
  153. $cacheKey = "caches:goods:category_sub_ids";
  154. $datas = RedisService::get($cacheKey);
  155. if($datas){
  156. return $datas;
  157. }
  158. $datas = $this->model->where(['status'=>1,'mark'=>1])->where('pid','>',0)->orderBy('create_time','asc')->select(['pid','cate_id'])->get();
  159. $datas = $datas? $datas->toArray() : [];
  160. if($datas){
  161. RedisService::set($cacheKey, $datas, rand(1200, 3600));
  162. }
  163. return $datas;
  164. }
  165. }