<?php

namespace app\common\model;

use think\Model;


class Article extends Model
{

    

    

    // 表名
    protected $name = 'article';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

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

    // 追加属性
    protected $append = [
        'hidedata_text',
        'is_hotdata_text',
        'is_recdata_text',
        'status_text'
    ];
    

    
    public function getHidedataList()
    {
        return ['0' => __('Hidedata 0'), '1' => __('Hidedata 1')];
    }

    public function getIsHotdataList()
    {
        return ['0' => __('Is_hotdata 0'), '1' => __('Is_hotdata 1')];
    }

    public function getIsRecdataList()
    {
        return ['0' => __('Is_recdata 0'), '1' => __('Is_recdata 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getHidedataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['hidedata']) ? $data['hidedata'] : '');
        $list = $this->getHidedataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsHotdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_hotdata']) ? $data['is_hotdata'] : '');
        $list = $this->getIsHotdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsRecdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_recdata']) ? $data['is_recdata'] : '');
        $list = $this->getIsRecdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public static function lst($where)
    {
        $model = new self;
        $xwhere = null;
        $xwhere['hidedata'] = 0;
        $xwhere['status'] = 1;
        $order = "sort desc,id desc";
        if(isset($where['cid']) && $where['cid']>0)  $xwhere['cid'] = $where['cid'];
        if(isset($where['is_hot']) && $where['is_hot']==1)  $xwhere['is_hotdata'] = $where['is_hot'];
        if(isset($where['is_rec']) && $where['is_rec']==1)   $xwhere['is_recdata'] = $where['is_rec'];
        if(isset($where['ifyid']) && $where['ifyid']>0)
        {
            $ids = get_all_categories(ArticleCategory::lst(['cid'=>$where['cid']]),$where['ifyid']);
            $xwhere['article_category_id'] =['in',$ids];
        }
        if(isset($where['key']) && $where['key']!='') $xwhere['title|synopsis']  = ['like',"%{$where['key']}%"];
        if(isset($where['order']) && $where['order']!='') $order = $where['order'];
        $list = $model->where($xwhere)->order($order)->page($where['page'],$where['limit'])->select();

        $count =  $model->where($xwhere)->count();
        return compact('count','list');
    }



}
