// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\CarCategoryModel; use App\Services\BaseService; use App\Services\RedisService; /** * 车型管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class CarCategoryService * @package App\Services\Common */ class CarCategoryService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * CarCategoryService constructor. */ public function __construct() { $this->model = new CarCategoryModel(); } /** * 静态入口 * @return MemberService|static|null */ public static function make() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } /** * 列表 * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['a.mark' => 1]; $list = $this->model->from('car_category as a') ->where($where) ->where(function ($query) use($params){ $keyword = isset($params['keyword'])? $params['keyword'] : ''; if($keyword){ $query->where('a.name','like',"%{$keyword}%"); } }) ->where(function ($query) use($params){ $status = isset($params['status'])? $params['status'] : 0; if($status>0 && is_array($status)){ $query->whereIn('a.status', $status); }else if($status){ $query->where('a.status', $status); } $type = isset($params['type'])? $params['type'] : 0; if($type>0){ $query->where('a.type', $type); } }) ->select(['a.*']) ->orderBy('a.create_time','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ foreach($list['data'] as &$item){ $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : ''; $item['icon'] = isset($item['icon']) && $item['icon']? get_image_url($item['icon']) : ''; } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 编辑 * @return array */ public function edit() { $data = request()->post(); if(isset($data['icon']) && $data['icon']){ $data['icon'] = get_image_path($data['icon']); } $data['prices'] = $data['prices']? str_replace(['|','-',':'],['|','-',':'], $data['prices']) : ''; return parent::edit($data); } /** * 车型选项 * @param int $num * @param int $type * @param array $field * @return array */ public function getOptions($num = 99,$type=2, $field=[]) { $field = $field? $field : ['id','name','type']; $datas = $this->model->where(['status'=>1,'mark'=>1]) ->where(function($query) use($type){ if($type==2){ $query->whereIn('type',[2,3]); } }) ->select($field) ->limit($num) ->orderBy('sort','desc') ->orderBy('id','desc') ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ foreach ($datas as &$item){ if(isset($item['prices'])){ $priceType = isset($item['price_type'])? $item['price_type'] : 0; if($priceType == 2){ $item['prices'] = $item['prices']? getPriceData($item['prices']) : ''; }else{ $item['prices'] = floatval($item['prices']); } } } unset($item); } return $datas; } }