122 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			122 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
<?php
 | 
						|
namespace app\model;
 | 
						|
 | 
						|
use think\Paginator;
 | 
						|
 | 
						|
class CasesModel extends Base
 | 
						|
{
 | 
						|
    protected $name = 'cases';
 | 
						|
 | 
						|
    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', 'asc')
 | 
						|
        ->limit($pre)
 | 
						|
        ->select();
 | 
						|
        return $items->toArray();
 | 
						|
    }
 | 
						|
 | 
						|
    public static function getByParentId($parentId, $onlyVisible = false, $pre = 50): array
 | 
						|
    {
 | 
						|
        $categoryIds = Category::where('parent_id', $parentId)->column('id');
 | 
						|
        return self::whereIn('category_id', $categoryIds)
 | 
						|
            ->when($onlyVisible, function($query){
 | 
						|
                $query->where('visible', 1);
 | 
						|
            })
 | 
						|
            ->order('sort', 'asc')
 | 
						|
            ->limit($pre)
 | 
						|
            ->select()->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, array $queryParam = [])
 | 
						|
    {
 | 
						|
        $whereMap  = [];
 | 
						|
        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];
 | 
						|
            }
 | 
						|
        }
 | 
						|
        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'     => $queryParam
 | 
						|
        ];
 | 
						|
 | 
						|
        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);
 | 
						|
    }
 | 
						|
 | 
						|
    public static function findListByWhere(array $where, int $page = 1, int $size = 10, callable $call = null, array $sortList = ['sort' => 'desc'])
 | 
						|
    {
 | 
						|
        $q = new static();
 | 
						|
        $q = $q->where($where);
 | 
						|
        if ($call) {
 | 
						|
            $q = $call($q);
 | 
						|
        }
 | 
						|
        if ($size > 0) {
 | 
						|
            $q = $q->page($page, $size);
 | 
						|
        }
 | 
						|
        if (count($sortList)) {
 | 
						|
            $q = $q->order($sortList);
 | 
						|
        }
 | 
						|
        return $q->select();
 | 
						|
    }
 | 
						|
} |