// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\FreightCategoryModel; use App\Services\BaseService; use App\Services\RedisService; /** * 货物分类管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class FreightCategoryService * @package App\Services\Common */ class FreightCategoryService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new FreightCategoryModel(); } /** * 静态入口 */ 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 = 30, $level=1) { $where = ['a.mark' => 1]; //var_dump($params['pid']); $list = $this->model->from('freight_category as a') ->leftJoin('freight_category as b','b.id','=','a.pid') ->where($where) ->where(function ($query) use($params){ $keyword = isset($params['name'])? $params['name'] : ''; 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); } $keyword = isset($params['name'])? $params['name'] : ''; $pid = isset($params['pid'])? $params['pid'] : -1; if($pid>=0 && empty($keyword)){ $query->where('a.pid', $pid); }else if($keyword && $pid){ $query->where('a.pid', $pid); } $type = isset($params['type'])? $params['type'] : 0; if($type>0){ $query->where('a.type', $type); } }) ->select(['a.*','b.name as parent_name']) ->orderBy('a.sort','desc') ->orderBy('a.create_time','asc') ->orderBy('a.id','asc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ $params1 = $params; 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']) : ''; $item['hasChildren']= false; $params1['pid'] = $item['id']; $params1['name'] = ''; if($level<=2){ $children = $this->getDataList($params1,9999, $level+1); $item['children'] = isset($children['list'])? $children['list']:[]; if($item['children']){ $item['hasChildren']= true; } } } } 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']); } return parent::edit($data); } /** * 车型选项 * @param int $num * @return array|mixed */ public function getOptions($type=0, $num = 99) { $cacheKey = "caches:index:freight_category_{$type}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $where = ['status'=>1,'mark'=>1]; $datas = $this->model->with(['children','specs']) ->where($where) ->where(function($query) use($type){ if($type){ $query->whereIn('type',[0, $type]); } }) ->select(['id','name','icon','pid','type','status']) ->limit($num) ->orderBy('sort','desc') ->orderBy('id','asc') ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, rand(5, 10)); } return $datas; } }