| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\GoodsCategoryModel;
- use App\Models\GoodsModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 商品分类管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class GoodsCategoryService
- * @package App\Services\Api
- */
- class GoodsCategoryService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * GoodsCategoryService constructor.
- */
- public function __construct()
- {
- $this->model = new GoodsCategoryModel();
- }
- /**
- * 静态入口
- * @return 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, $field = '')
- {
- $cacheKey = "caches:goodsCategorys:{$pageSize}_".md5(json_encode($params));
- $datas = RedisService::get($cacheKey);
- // var_dump($datas);
- if($datas){
- $datas['cache'] = true;
- return $datas;
- }
- $where = ['a.mark' => 1];
- $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';
- $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
- $order = 'id desc';
- if($sortType == 1){
- $order = 'lev_a.is_recommend asc,lev_a.sort desc, lev_a.id asc';
- }
- $list = $this->model->with(['subList'])->from('goods_category as a')
- ->where($where)
- ->where(function ($query) use ($params) {
- $status = isset($params['status']) ? $params['status'] : 0;
- $status = $status > 0 ? $status : 1;
- if ($status > 0) {
- $query->where('a.status', $status);
- }
- $pid = isset($params['pid']) ? $params['pid'] : -1;
- if ($pid >= 0) {
- $query->where('a.pid', $pid);
- }
- if (isset($params['is_recommend']) && $params['is_recommend']) {
- $query->where('a.is_recommend', $params['is_recommend']);
- }
- // 商户
- $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
- if ($merchId > 0) {
- $query->where('a.merch_id', $merchId);
- }
- // 商户
- $ids = isset($params['ids']) ? $params['ids'] : [];
- if (count($ids) > 0) {
- $query->whereIn('a.id', $ids);
- }
- })
- ->where(function ($query) use ($params) {
- $keyword = isset($params['kw']) ? $params['kw'] : '';
- if ($keyword) {
- $query->where('a.name', 'like', "%{$keyword}%");
- }
- })
- ->selectRaw($field)
- ->orderByRaw($order)
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- foreach ($list['data'] as &$item) {
- $item['icon'] = isset($item['icon']) && $item['icon'] ? get_image_url($item['icon']) : '';
- $item['sub_list'] = isset($item['sub_list']) && $item['sub_list']? $item['sub_list'] : [];
- if($item['sub_list']){
- foreach ($item['sub_list'] as &$v){
- $v['icon'] = isset($v['icon']) && $v['icon'] ? get_image_url($v['icon']) : get_image_url('/images/member/logo.png');
- }
- unset($v);
- }
- }
- }
- $total = isset($list['total']) ? $list['total'] : 0;
- $datas = [
- 'pageSize' => $pageSize,
- 'total' => $total,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- if($total>0){
- RedisService::set($cacheKey, $datas, rand(5,10));
- }
- return $datas;
- }
- /**
- * 获取主分类ID
- * @return array|mixed
- */
- public function getCateIds()
- {
- $cacheKey = "caches:goods:category_ids";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $datas = $this->model->where(['status'=>1,'pid'=>0,'mark'=>1])->orderBy('create_time','asc')->select(['pid','cate_id'])->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- RedisService::set($cacheKey, $datas, rand(1200, 3600));
- }
- return $datas;
- }
- /**
- * 获取主分类ID
- * @return array|mixed
- */
- public function getCateSubIds()
- {
- $cacheKey = "caches:goods:category_sub_ids";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $datas = $this->model->where(['status'=>1,'mark'=>1])->where('pid','>',0)->orderBy('create_time','asc')->select(['pid','cate_id'])->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- RedisService::set($cacheKey, $datas, rand(1200, 3600));
- }
- return $datas;
- }
- }
|