<?php

namespace app\admin\model\books;

use think\Model;


class Books extends Model
{





    // 表名
    protected $name = 'books';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'book_type_text',
        'is_new_recommend_text',
        'createtime',
        'updatetime',
        'is_awards_text'
    ];



    public function getBookTypeList()
    {
        return ['0' => __('纸质书'), '1' => __('电子书')];
    }

    public function getIsNewRecommendList()
    {
        return ['0' => __('是'), '1' => __('否')];
    }

    public function getIsAwardsList()
    {
        return ['0' => __('是'), '1' => __('否')];
    }

    public function getIsSeriesList()
    {
        return ['0' => __('是'), '1' => __('否')];
    }


    public function getBookTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['book_type']) ? $data['book_type'] : '');
        $list = $this->getBookTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsNewRecommendTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_new_recommend']) ? $data['is_new_recommend'] : '');
        $list = $this->getIsNewRecommendList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsAwardsTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_awards']) ? $data['is_awards'] : '');
        $list = $this->getIsAwardsList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function file(){
        return $this->hasMany(BooksFile::class,'books_id','id');
    }

    //课件
    public function coursewareFile(){
        return $this->hasMany(BooksFile::class,'books_id','id')->where('is_deleted',1)->where('type','0')->order('sort desc,id desc');
    }

    //资源
    public function resourceFile(){
        return $this->hasMany(BooksFile::class,'books_id','id')->where('is_deleted',1)->where('type','1')->order('sort desc,id desc');
    }

    //资料包
    public function packageFile(){
        return $this->hasMany(BooksFile::class,'books_id','id')->where('is_deleted',1)->where('type','2')->order('sort desc,id desc');
    }

    //样张
    public function specimenFile(){
        return $this->hasMany(BooksFile::class,'books_id','id')->where('is_deleted',1)->where('is_specimen','0')->order('sort desc,id desc');
    }


}
