<?php
/**
 * JYmusic音乐管理系统 - [幻灯片模型]
 *
 * @version   2.0
 * @author    战神~~巴蒂 [jyuucn@163.com]
 * @license   license: http://jyuu.cn
 * @copyright JYmusic 2014 - 2017
 */
namespace addons\Slider\model;

use think\Model;

/**
 * 幻灯片模型
 */
class Slider extends Model
{
    protected $autoWriteTimestamp = true;

    public $fields = [
        [
            'name'  => 'title',
            'title' => '幻灯片标题',
            'type'  => 'text',
        ],
        [
            'name'  => 'type',
            'title' => '幻灯片类型',
            'type' => 'radio',
            'options' => [
                '1' => '首页幻灯片',
                '2' => '资讯幻灯片',
            ],
            'value' => '1'
        ],
        [
            'name'  => 'link_title',
            'title' => '链接标题',
            'type'  => 'text',
        ],
        [
            'name'  => 'link',
            'title' => '链接地址',
            'type'  => 'long_text',
            'remark' => ''
        ],
        [
            'name'  => 'cover_url',
            'title' => '幻灯片图片',
            'type'  => 'image',
            'remark' => '可填写外链地址'
        ],
        [
            'name'  => 'level',
            'title' => '优先级',
            'type'  => 'num',
            'remark' => '优先级越高排序越往前'
        ],
        [
            'name'  => 'description',
            'title' => '描述',
            'type'  => 'textarea',
            'remark' => ''
        ],
    ];

    public function getStatusTextAttr($value, $data)
    {
        $status = [-1 => '删除', 0 => '禁用', 1 => '正常', 2 => '待审核'];
        return $status[$data['status']];
    }

    /*展示数据*/
    public function getList($limit = 10, $type = 1)
    {
        $data = $this->where('status = 1')
            ->where('status', 1)
            ->where('type', $type)
            ->field('create_time,update_time,status', true)
            ->cache('slider_lists_' . $type, 86400, 'limit_cache')
            ->limit($limit)
            ->order('level desc,id desc')
            ->select();
        return $data;
    }
}
