<?php

namespace app\extra\service\blue;

use app\extra\basic\Service;
use app\model\blue\BlueCategory;

class CategoryService extends Service
{


    /**
     * 列表
     * @param array $param
     */
    public function getList(array $param = [])
    {
        $this->mode = new BlueCategory();
        return $this->searchVal($param,$this->searchFilter($param))->select()->toArray();
    }

    public function getPortalData(array $param = []): array
    {
        $this->mode = new BlueCategory();
        return $this->searchVal($param,$this->searchFilter($param))->field("id,name,icon,small_icon")->with(['article' => function($query){
            $query->whereJsonContains("attribute",['top'])->field('id,title,category,view,zan,author,source,comment,cover')->limit(10);
        }])->whereJsonContains("attribute",['recommend'])->select()->toArray();
    }


    /**
     *
     * @param array $param
     * @return array
     */
    public function searchFilter(array $param = []): array
    {
        $filter = [];
        !empty($param['type']) && $filter[] = ["type", '=', $param['type']];
        !empty($param['status']) && $filter[] = ["status", '=', $param['status']];
        !empty($param['name']) && $filter[] = ["name", 'like', "%{$param['name']}%"];
        return $filter;
    }

}