<?php

namespace App\Repositories;

use App\Models\Article;
use Prettus\Repository\Eloquent\BaseRepository;

/**
 * Class ArticleRepositoryEloquent.
 *
 * @package namespace App\Repositories;
 */
class ArticleRepository extends BaseRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Article::class;
    }

    /**
     * @param $where array
     * @return null|\Illuminate\Database\Eloquent\Model
     */
    public function getArticles($where, $page)
    {
        /*$res = $this->model->when(get_subsite_id()>0, function ($query) {
                $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});
        })->where($where)->orderByRaw('list_order asc,id desc');*/
        $res = $this->model->whereHas('subsites', function ($query) {
            $query->where('subsite_id', get_subsite_id());
        })->where($where)->orderByRaw('list_order desc,created_at desc');
        if ($page) {
            return $res->paginate($page);
        } else {
            return $res->get();
        }
    }

    /*
     *根据分类查询
     */
    public function getArticle($where,$page)
    {
        return $this->model->whereHas('show_category',function ($query) use ($where){
            $query->where($where);
        })->paginate($page);
    }

    /*
     *根据分类查询
     */
    public function getArticlesByType($type_id,$limit)
    {
        $where = array('is_display'=>'1','type_id'=>$type_id);
        return  $this->model->selectRaw('id,small_img')->whereHas('subsites', function ($query) {
            $query->where('subsite_id', get_subsite_id());
        })->where($where)->where('small_img','<>','')->orderByRaw('list_order desc,created_at desc')->limit($limit)->get();
    }

    /**
     * @param $where array
     * @return null|\Illuminate\Database\Eloquent\Model
     */
    public function getAllArticles($where, $orWhere, $page)
    {
        /*$res = $this->model->when(get_subsite_id()>0, function ($query) {
                        $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});
                    })->where(function ($query) use ($where, $orWhere) {
                        $query->where($where)->orwhere($orWhere);
                })->orderByRaw('list_order asc,id desc');*/

        $res = $this->model->whereHas('subsites', function ($query) {
            $query->where('subsite_id', get_subsite_id());
        })->where(function ($query) use ($where, $orWhere) {
            $query->where($where)->orwhere($orWhere);
        })->orderByRaw('list_order desc,created_at desc');
        if ($page) {
            return $res->paginate($page);
        } else {
            return $res->get();
        }
    }

    public function getPropertyArticles($property_id, $limit)
    {
        $where = array('is_display'=>'1','property_id'=>$property_id);
        /*return  $this->model->when(get_subsite_id()>0, function ($query) {
            $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});
        })->where($where)->orderByRaw('list_order asc,id desc')->limit($limit)->get();*/
        return  $this->model->whereHas('subsites', function ($query) {
            $query->where('subsite_id', get_subsite_id());
        })->where($where)->orderByRaw('list_order desc,created_at desc')->limit($limit)->get();
    }
    public function firstWhere($where)
    {
        return $this->model->where($where)->first();
    }
    public function incrementData($where, $num, $filed)
    {
        return $this->model->where($where)->increment($filed, $num);
    }

    public function filterArticles($where = array(), $whereIn = array(), $order = array())
    {
        /*$rst = $this->model->where($where);*/
        /*$rst = $this->model->when(get_subsite_id()>0, function ($query) {
                    $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});
                })->where($where);*/
        $rst = $this->model->whereHas('subsites', function ($query) {
            $query->where('subsite_id', get_subsite_id());
        })->where($where);
        if ($whereIn) {
            foreach ($whereIn as $k => $v) {
                $rst->whereIn($k, $v);
            }
        }
        if ($order) {
            if (is_array($order)) {
                foreach ($order as $k => $v) {
                    $rst->orderBy($k, $v);
                }
            } else {
                $rst->orderbyRaw($order);
            }
        }
        return $rst->get();
    }


}