CarCategoryService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\CarCategoryModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 车型管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class CarCategoryService
  20. * @package App\Services\Common
  21. */
  22. class CarCategoryService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * CarCategoryService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new CarCategoryModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return MemberService|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. * 列表
  49. * @param $params
  50. * @param int $pageSize
  51. * @return array
  52. */
  53. public function getDataList($params, $pageSize = 15)
  54. {
  55. $where = ['a.mark' => 1];
  56. $list = $this->model->from('car_category as a')
  57. ->where($where)
  58. ->where(function ($query) use($params){
  59. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  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. $type = isset($params['type'])? $params['type'] : 0;
  72. if($type>0){
  73. $query->where('a.type', $type);
  74. }
  75. })
  76. ->select(['a.*'])
  77. ->orderBy('a.create_time','desc')
  78. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  79. $list = $list? $list->toArray() :[];
  80. if($list){
  81. foreach($list['data'] as &$item){
  82. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  83. $item['icon'] = isset($item['icon']) && $item['icon']? get_image_url($item['icon']) : '';
  84. }
  85. }
  86. return [
  87. 'pageSize'=> $pageSize,
  88. 'total'=>isset($list['total'])? $list['total'] : 0,
  89. 'list'=> isset($list['data'])? $list['data'] : []
  90. ];
  91. }
  92. /**
  93. * 编辑
  94. * @return array
  95. */
  96. public function edit()
  97. {
  98. $data = request()->post();
  99. if(isset($data['icon']) && $data['icon']){
  100. $data['icon'] = get_image_path($data['icon']);
  101. }
  102. $data['prices'] = $data['prices']? str_replace(['|','-',':'],['|','-',':'], $data['prices']) : '';
  103. return parent::edit($data);
  104. }
  105. /**
  106. * 车型选项
  107. * @param int $num
  108. * @param int $type
  109. * @param array $field
  110. * @return array
  111. */
  112. public function getOptions($num = 99,$type=2, $field=[])
  113. {
  114. $field = $field? $field : ['id','name','type'];
  115. $datas = $this->model->where(['status'=>1,'mark'=>1])
  116. ->where(function($query) use($type){
  117. if($type==2){
  118. $query->whereIn('type',[2,3]);
  119. }
  120. })
  121. ->select($field)
  122. ->limit($num)
  123. ->orderBy('sort','desc')
  124. ->orderBy('id','desc')
  125. ->get();
  126. $datas = $datas? $datas->toArray() : [];
  127. if($datas){
  128. foreach ($datas as &$item){
  129. if(isset($item['prices'])){
  130. $priceType = isset($item['price_type'])? $item['price_type'] : 0;
  131. if($priceType == 2){
  132. $item['prices'] = $item['prices']? getPriceData($item['prices']) : '';
  133. }else{
  134. $item['prices'] = floatval($item['prices']);
  135. }
  136. }
  137. }
  138. unset($item);
  139. }
  140. return $datas;
  141. }
  142. }