<?php

namespace app\extra\service\blue;

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

class ExpertPlatformService extends Service
{
    /**
     * 列表
     * @param array $param
     */
    public function getList(array $param = [])
    {
        $this->mode = new BlueExpertPlatform();
        return $this->searchVal($param,$this->searchFilter($param))->paginate([
            "list_rows" => $param['pageSize'],
            "page"      => $param['page']
        ]);
    }


    /**
     * 达人之家
     * @param array $param
     * @return mixed
     */
    public function getListStar(array $param = [],string $field = "*")
    {
        $this->mode = new BlueExpertPlatform();
        return $this->searchVal($param,$this->searchFilter($param))->with(['job' => function($query){
            $query->where("status","in",[0,1,2])->field("id,openid");
        },"user" => function($query){
            $query->field("openid,group_id")->with(['level' => function($subQue) {
                $subQue->field("id,name,level");
            }]);
        }])->limit(10)->field($field)->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']-1)];
        !empty($param['truename']) && $filter[] = ["truename", 'like', "%{$param['truename']}%"];
        !empty($param['nick']) && $filter[] = ["nickname", 'like', "%{$param['nick']}%"];
        !empty($param['source']) && $filter[] = ["source", '=', $param['source']];
        return $filter;
    }

}