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

namespace App\Modules\Promotion\Models;

use Illuminate\Database\Eloquent\Model;


class DocumentCovers extends Model
{
    protected $table = 'document_covers';
    protected $fillable = ['id', 'link', 'type', 'sequence'];

    /**
     * @return mixed  获取所有的文案封面
     */
    static function getAllDocumentCovers($distribution_channel_id)
    {
        $type = (123 == $distribution_channel_id) ? 4 : 1;
        return $documentCoversResult = self::select('id', 'link')->where('type', $type)->orderBy('updated_at', 'desc')->get();
    }

    /**
     * @return mixed  获取所有的文案封面
     */
    static function getAllMessageDocumentCovers($distribution_channel_id)
    {
        $link1 = 'https://cdn-novel.iycdm.com/distribution/message_document_cover/68.jpg';
        $link2 = 'https://cdn-novel.iycdm.com/distribution/message_document_cover/9.9.jpg';
        $link3 = 'https://cdn-novel.iycdm.com/distribution/message_document_cover/201811091716.jpg';
        $array = self::select('id', 'link')->where('type', 1)->orderBy('updated_at', 'desc')->get()->toArray();
        array_splice($array, 0, 0, [['id' => 10001, 'link' => $link1]]);
        array_splice($array, 0, 0, [['id' => 10002, 'link' => $link2]]);
        array_splice($array, 0, 0, [['id' => 10003, 'link' => $link3]]);
        $array = json_decode(json_encode($array));
        return $array;
    }

    /**
     *
     * @return mixed  随机获取模板消息的文案封面
     * $type： 类型(2: 带黄的， 3： 不带黄的)
     * $count：条数(默认为 1 条)
     */
    static function getMessageRandomDocumentCovers($type, $count = 1)
    {
        return self::select('id', 'link')->where('type', $type)->inRandomOrder()->take($count)->orderBy('updated_at', 'desc')->get()->toArray();
    }
    
    static function getFullMessageRandomDocumentCovers($type=3)
    {
    	return self::select('id', 'link')->where('type', $type)->orderBy('updated_at', 'desc')->get();
    }

    static function search($params = [])
    {
        $obj = self::orderBy('id', 'desc');
        if (isset($params['type']) && $params['type']) $obj->where('type', $params['type']);
        return $obj->paginate(50);
    }

    static function delCover($ids = [])
    {
        return $ids && self::whereIn('id', $ids)->delete();
    }
}
