96 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			96 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
| <?php
 | |
| namespace app\model;
 | |
| 
 | |
| use think\Paginator;
 | |
| 
 | |
| class PositionModel extends Base
 | |
| {
 | |
|     protected $name = 'position';
 | |
| 
 | |
|     public static function onAfterInsert($item)
 | |
|     {
 | |
|         $item->sort  = $item->id;
 | |
|         $item->save();
 | |
|     }
 | |
| 
 | |
|     public static function getPaginateList($categoryId, $per = 20,  $isSample = false)
 | |
|     {
 | |
|         $paginate = [
 | |
|             'list_rows' => $per,
 | |
|             'query'     => ['category_id' => $categoryId]
 | |
|         ];
 | |
|         return self::where('category_id', $categoryId)->order('sort', 'desc')->paginate($paginate, $isSample);
 | |
|     }
 | |
| 
 | |
|     public static function getByCategoryId($categoryId, $onlyVisible = false, $pre = 50)
 | |
|     {
 | |
|         $items = self::where('category_id', $categoryId)
 | |
|         ->when($onlyVisible, function($query){
 | |
|             $query->where('visible', 1);
 | |
|         })
 | |
|         ->order('sort', 'desc')
 | |
|         ->limit($pre)
 | |
|         ->select();
 | |
|         return $items->toArray();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 获取列表
 | |
|      * @param  int  $categoryId  分类ID
 | |
|      * @param  int  $per  每页数量
 | |
|      * @param  string  $keyword  关键词
 | |
|      * @param  array  $param  类型:置顶、热门、推荐 ['top','hot','recommend']
 | |
|      * @param  int  $status  状态,-1表示不限制
 | |
|      * @param  array  $orderList  排序
 | |
|      * @param  bool  $onlyChild  仅获取下级 默认true false=获取所有后代分类
 | |
|      * @return Paginator
 | |
|      */
 | |
|     public static function getList(int $categoryId, int $per = 20, string $keyword = '', array $param = [], int $status = -1, array $orderList = ['a.sort' => 'desc'], bool $onlyChild = true)
 | |
|     {
 | |
|         $whereMap  = [];
 | |
|         $pageParam = [];
 | |
|         if (is_numeric($categoryId) && $categoryId > 0) {
 | |
|             $children = Category::getChildrenByParentId($categoryId, $onlyChild);
 | |
|             if (!empty($children)) {
 | |
|                 $categoryIds = [$categoryId];
 | |
|                 foreach ($children as $child) {
 | |
|                     if ($child['model_id'] == Model::MODEL_PRODUCT) {
 | |
|                         $categoryIds[] = $child['id'];
 | |
|                     }
 | |
|                 }
 | |
|                 $whereMap[] = ['a.category_id', 'in', $categoryIds];
 | |
|             } else {
 | |
|                 $whereMap[] = ['a.category_id', '=', $categoryId];
 | |
|             }
 | |
|             $pageParam['category_id'] = $categoryId;
 | |
|         }
 | |
|         if (!empty($keyword)) {
 | |
|             $whereMap[]           = ['a.title', 'like', '%'.$keyword.'%'];
 | |
|             $pageParam['keyword'] = $keyword;
 | |
|         }
 | |
|         if (is_array($param) && count($param) > 0) {
 | |
|             $pageParam['param'] = $param;
 | |
|             foreach ($param as $vo) {
 | |
|                 if (in_array($vo, ['top', 'hot', 'recommend'], true)) {
 | |
|                     $whereMap[] = ["a.{$vo}", '=', 1];
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         $paginate = [
 | |
|             'list_rows' => $per,
 | |
|             'query'     => $pageParam
 | |
|         ];
 | |
| 
 | |
|         return self::alias('a')
 | |
|             ->leftJoin('category c', 'c.id = a.category_id')
 | |
|             ->when(count($whereMap) > 0, function ($query) use ($whereMap) {
 | |
|                 $query->where($whereMap);
 | |
|             })
 | |
|             ->when($status != -1, function ($query) use ($status) {
 | |
|                 $query->where('a.visible', $status);
 | |
|             })
 | |
|             ->order($orderList)
 | |
|             ->field('a.*, c.title as category_title')
 | |
|             ->paginate($paginate, false);
 | |
|     }
 | |
| } |