<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/21
 * Time: 8:59
 */

namespace App\Modules\Promotion\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class Headline  标题
 * @package App\Modules\Promotion\Models
 */
class Headline extends Model
{
	protected $connection = 'api_mysql';
	
    protected $table = 'headlines';

    protected $fillable = [
        'id',
        'title',
        'category',
        'serial_number',
        'remark',
        'type',
        'quality',
        'created_at',
        'updated_at'
    ];

    /**
     * category:类型 (1: 男频,2:女频)
     * @return mixed  获取类型下的所有的标题
     */
    static function getAllHeadlines($category)
    {
        if (1 == $category) {
            $category = '男频';
        } else {
            $category = '女频';
        }
        return $result = self::select('id', 'title')->where('type', 1)->where('category', $category)->get();
    }

    /**
     * @return mixed  获取所有的标题
     */
    static function getHeadlines()
    {
        return $result = self::select('id', 'title')->where('type', 2)->get();
    }

    /**
     * category:类型 (1: 男频,2:女频)
     * count: 获取的条数(默认为1条)
     * @return mixed  随机获取标题库中的若干条标题
     */
    static function getRandomHeadline($category, $count = 1)
    {
        if (1 == $category) {
            $category = '男频';
        } else {
            $category = '女频';
        }
        return $result = self::select('id', 'title')->where('category', $category)->where('type', 2)->where('remark', 2)->inRandomOrder()->take($count)->get();
    }
    
    static function getFullHeadline()
    {
    	return $result = self::select('id', 'title')->where('type', 2)->where('remark', 2)->get();
    }


    /**
     * 添加标题
     */
    static function addHeadline($data)
    {
        return self::create($data);
    }

    /**
     * 添加标题
     */
    static function delHeadlines($ids)
    {
        return self::whereIn('id', $ids)->delete();
    }

    /**
     * 获取标题库中的serial_number 的最大值
     * @return mixed
     */
    static function getMaxSerialNumber()
    {
        return self::max('serial_number');
    }


    static function getHeadlineByParams($param)
    {
        $search_object = self::orderBy('id', 'desc');
        if (isset($param['id']) && $param['id']) {
            $search_object->where('id', $param['id']);
        }

        if (isset($param['remark']) && $param['remark']) {
            $search_object->where('remark', $param['remark']);
        }

        if (isset($param['title']) && $param['title']) {
            $search_object->where('title', 'like', '%' . $param['title'] . '%');
        }

        if (isset($param['category']) && $param['category']) {
            //category:类型 (1: 男频,2:女频)
            $category = $param['category'];
            if (1 == $category) {
                $category = '男频';
            } elseif (2 == $category) {
                $category = '女频';
            }
            $search_object->where('category', $category);
        }

        if (isset($param['type']) && $param['type']) {
            $search_object->where('type', $param['type']);
        }

        if (isset($param['quality']) && $param['quality']) {
            $search_object->where('quality', $param['quality']);
        }
        return $search_object->paginate();
    }
}