FreightCategoryService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Common;
  12. use App\Models\FreightCategoryModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 货物分类管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class FreightCategoryService
  20. * @package App\Services\Common
  21. */
  22. class FreightCategoryService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new FreightCategoryModel();
  34. }
  35. /**
  36. * 静态入口
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = new static();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 列表
  47. * @param $params
  48. * @param int $pageSize
  49. * @return array
  50. */
  51. public function getDataList($params, $pageSize = 30, $level=1)
  52. {
  53. $where = ['a.mark' => 1];
  54. //var_dump($params['pid']);
  55. $list = $this->model->from('freight_category as a')
  56. ->leftJoin('freight_category as b','b.id','=','a.pid')
  57. ->where($where)
  58. ->where(function ($query) use($params){
  59. $keyword = isset($params['name'])? $params['name'] : '';
  60. if($keyword){
  61. $query->where('a.name','like',"%{$keyword}%");
  62. }
  63. })
  64. ->where(function ($query) use($params){
  65. $status = isset($params['status'])? $params['status'] : 0;
  66. if($status>0 && is_array($status)){
  67. $query->whereIn('a.status', $status);
  68. }else if($status){
  69. $query->where('a.status', $status);
  70. }
  71. $keyword = isset($params['name'])? $params['name'] : '';
  72. $pid = isset($params['pid'])? $params['pid'] : -1;
  73. if($pid>=0 && empty($keyword)){
  74. $query->where('a.pid', $pid);
  75. }else if($keyword && $pid){
  76. $query->where('a.pid', $pid);
  77. }
  78. $type = isset($params['type'])? $params['type'] : 0;
  79. if($type>0){
  80. $query->where('a.type', $type);
  81. }
  82. })
  83. ->select(['a.*','b.name as parent_name'])
  84. ->orderBy('a.sort','desc')
  85. ->orderBy('a.create_time','asc')
  86. ->orderBy('a.id','asc')
  87. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  88. $list = $list? $list->toArray() :[];
  89. if($list){
  90. $params1 = $params;
  91. foreach($list['data'] as &$item){
  92. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  93. $item['icon'] = isset($item['icon']) && $item['icon']? get_image_url($item['icon']) : '';
  94. $item['hasChildren']= false;
  95. $params1['pid'] = $item['id'];
  96. $params1['name'] = '';
  97. if($level<=2){
  98. $children = $this->getDataList($params1,9999, $level+1);
  99. $item['children'] = isset($children['list'])? $children['list']:[];
  100. if($item['children']){
  101. $item['hasChildren']= true;
  102. }
  103. }
  104. }
  105. }
  106. return [
  107. 'pageSize'=> $pageSize,
  108. 'total'=>isset($list['total'])? $list['total'] : 0,
  109. 'list'=> isset($list['data'])? $list['data'] : []
  110. ];
  111. }
  112. /**
  113. * 编辑
  114. * @return array
  115. */
  116. public function edit()
  117. {
  118. $data = request()->post();
  119. if(isset($data['icon']) && $data['icon']){
  120. $data['icon'] = get_image_path($data['icon']);
  121. }
  122. return parent::edit($data);
  123. }
  124. /**
  125. * 车型选项
  126. * @param int $num
  127. * @return array|mixed
  128. */
  129. public function getOptions($type=0, $num = 99)
  130. {
  131. $cacheKey = "caches:index:freight_category_{$type}";
  132. $datas = RedisService::get($cacheKey);
  133. if($datas){
  134. return $datas;
  135. }
  136. $where = ['status'=>1,'mark'=>1];
  137. $datas = $this->model->with(['children','specs'])
  138. ->where($where)
  139. ->where(function($query) use($type){
  140. if($type){
  141. $query->whereIn('type',[0, $type]);
  142. }
  143. })
  144. ->select(['id','name','icon','pid','type','status'])
  145. ->limit($num)
  146. ->orderBy('sort','desc')
  147. ->orderBy('id','asc')
  148. ->get();
  149. $datas = $datas? $datas->toArray() : [];
  150. if($datas){
  151. RedisService::set($cacheKey, $datas, rand(5, 10));
  152. }
  153. return $datas;
  154. }
  155. }