<?php

use App\Models\Advert;
use App\Models\Category;
use App\Models\Article;
use App\Models\Resume;
use Encore\Admin\Config\ConfigModel;

function getConfigList($name, $take = 8)
{
    return ConfigModel::where('name', 'like', "{$name}%")->get();
}

function getConfig($name, $return = '')
{
    $info = ConfigModel::where('name', $name)->first();
    if ($info) {
        if ($return) {
            return $info->{$return};
        }

        return $info;
    }

    return new ConfigModel;

}

function getOneCategory($categoryId, $return = '')
{
    $category = Category::find($categoryId);
    if ($category) {
        if ($return) {
            return $category->{$return};
        }

        return $category;
    }

    return new Category;
}

/**
 * Notes: 获取文章分类详情
 *
 * @Author: 玄尘
 * @Date  : 2020/9/10 13:21
 * @param          $categoryId
 * @param  string  $result
 * @return \App\Models\Article
 */
function getOneArticleBYCate($categoryId, $result = '')
{
    $info = Article::latest('sort')
        ->whereHas('categories', function ($q) use ($categoryId) {
            $q->where('id', $categoryId);
        })
        ->latest('sort')
        ->latest('created_at')
        ->first();

    if ($info) {
        if ($result) {
            return $info->{$result};
        }

        return $info;
    } else {
        return '';
    }

}

/**
 * Notes: 获取分类下的文章
 *
 * @Author: 玄尘
 * @Date: 2021/12/1 13:27
 * @param $categoryId
 * @param  int  $take
 * @param  string  $mark
 * @param  false  $hasCover
 * @param  string  $order
 * @return mixed
 */
function getArticlesBYCate($categoryId, int $take = 8, string $mark = 'one', $hasCover = false, string $order = 'desc')
{
    if ($mark == 'one') {
        $articles = Article::where('status', 1)
            ->whereHas('categories', function ($q) use ($categoryId) {
                $q->where('id', $categoryId);
            })
            ->latest('sort')
            ->latest('created_at')
            ->when($hasCover, function ($q) {
                $q->whereNotNull('cover');
            })
            ->take($take)
            ->get();
    } else {
        $cate = Category::find($categoryId);
        $ids  = $cate->getAllChildrenId();

        $articles = Article::where('status', 1)
            ->whereHas('categories', function ($q) use ($ids) {
                $q->whereIn('id', $ids);
            })
            ->when($hasCover, function ($q) {
                $q->whereNotNull('cover');
            })
            ->latest('sort')
            ->latest('created_at')
            ->take($take)
            ->get();
    }

    return $articles;
}

//获取子分类
function getCateChild($categoryId)
{
    return Category::where('status', 1)
        ->where('parent_id', $categoryId)
        ->orderBy('order', 'asc')
        ->get();
}

//获取顶级分类
function getTopCate($categoryId)
{
    $parent = Category::find($categoryId);

    while ($parent->parent_id != 0) {
        $parent = $parent->parent;
    }

    return $parent;
}

//获取一个广告
function getOneAdvertByCate($categoryId, $result = '')
{
    $info = Advert::where('category_id', $categoryId)
        ->latest('sort')
        ->latest()
        ->first();
    if ($info) {
        if ($result) {
            return $info->{$result};
        }

        return $info;
    } else {
        return '';
    }
}

/**
 * Notes: 获取广告
 *
 * @Author: 玄尘
 * @Date: 2021/12/1 13:27
 * @param $categoryId
 * @param  int  $take
 * @return mixed
 */
function getAdvertsByCate($categoryId, int $take = 8)
{
    return Advert::where('category_id', $categoryId)
        ->latest('sort')
        ->latest()
        ->take($take)
        ->get();
}

/**
 * Notes: 根据分类id 获取文章
 *
 * @Author: 玄尘
 * @Date  : 2021/5/21 9:45
 * @param  int  $take
 * @param  false  $hasCover
 * @return mixed
 */
function getArticlesByCateIds(int $take = 8, $hasCover = false)
{
    //分院新闻  媒体报道 科研动态 交流合作 党建文化 专题学习
    $ids = [7, 17, 8, 5, 12, 30];

    return Article::query()->where('status', 1)
        ->whereHas('categories', function ($q) use ($ids) {
            $q->whereIn('id', $ids);
        })
        ->latest('sort')
        ->latest('created_at')
        ->when($hasCover, function ($q) {
            $q->whereNotNull('cover')->orWhere('cover', '<>', '');
        })
        ->take($take)
        ->get();

}

/**
 * Notes: 获取推荐文章
 *
 * @Author: 玄尘
 * @Date  : 2021/4/16 13:31
 * @param $postion
 * @return mixed
 */
function getArticlesByPosition($postion)
{
    return Article::ofPosition($postion)
        ->latest('sort')
        ->latest('created_at')
        ->take(5)
        ->get();
}

/**
 * Notes: 根据分类获取简历
 *
 * @Author: 玄尘
 * @Date: 2021/12/1 13:28
 * @param $category_id
 * @param  int  $take
 */
function getResumesByCate($category_id, $take = 8)
{
    return Resume::query()
        ->where('status', 1)
        ->where('category_id', $category_id)
        ->take($take)
        ->get();
}

