GoodsCategoryService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. $where = ['a.mark' => 1];
  57. $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';
  58. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
  59. $order = 'id desc';
  60. if($sortType == 1){
  61. $order = 'lev_a.sort desc, lev_a.id asc';
  62. }
  63. $list = $this->model->from('goods_category as a')
  64. ->where($where)
  65. ->where(function ($query) use ($params) {
  66. $status = isset($params['status']) ? $params['status'] : 0;
  67. $status = $status > 0 ? $status : 1;
  68. if ($status > 0) {
  69. $query->where('a.status', $status);
  70. }
  71. $pid = isset($params['pid']) ? $params['pid'] : -1;
  72. if ($pid >= 0) {
  73. $query->where('a.pid', $pid);
  74. }
  75. // 商户
  76. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  77. if ($merchId > 0) {
  78. $query->where('a.merch_id', $merchId);
  79. }
  80. })
  81. ->where(function ($query) use ($params) {
  82. $keyword = isset($params['kw']) ? $params['kw'] : '';
  83. if ($keyword) {
  84. $query->where('a.name', 'like', "%{$keyword}%");
  85. }
  86. })
  87. ->selectRaw($field)
  88. ->orderByRaw($order)
  89. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  90. $list = $list ? $list->toArray() : [];
  91. if ($list) {
  92. foreach ($list['data'] as &$item) {
  93. $item['icon'] = isset($item['icon']) && $item['icon'] ? get_image_url($item['icon']) : '';
  94. }
  95. }
  96. return [
  97. 'pageSize' => $pageSize,
  98. 'total' => isset($list['total']) ? $list['total'] : 0,
  99. 'list' => isset($list['data']) ? $list['data'] : []
  100. ];
  101. }
  102. /**
  103. * 获取主分类ID
  104. * @return array|mixed
  105. */
  106. public function getCateIds()
  107. {
  108. $cacheKey = "caches:goods:category_ids";
  109. $datas = RedisService::get($cacheKey);
  110. if($datas){
  111. return $datas;
  112. }
  113. $datas = $this->model->where(['status'=>1,'pid'=>0,'mark'=>1])->orderBy('create_time','asc')->select(['pid','cate_id'])->get();
  114. $datas = $datas? $datas->toArray() : [];
  115. if($datas){
  116. RedisService::set($cacheKey, $datas, rand(1200, 3600));
  117. }
  118. return $datas;
  119. }
  120. }