<?php
/**
 * @desc: 文章模型
 * @Author: Dr.Xing <ezfwork@foxmail.com>
 * @Since: 2018/2/2 16:54
 */

namespace app\common\model;


use think\Model;

class Archives extends Model
{

    // 追加属性
    protected $append = [
        'views',
        'updatetime_text',
    ];

    protected function base(&$query){
        if(\think\Session::has("user_site_id")) {
            $query->where(['status'=>'normal', 'isDelete'=>0, 'site_id' =>\think\Session::get("user_site_id")]);
            /*strict
            $query->where('site_id', 'in', function($query){
                $domain = request()->domain();
                $query->table(config("database.prefix") . 'sites')->where('domain', $domain)->field('id');
            });
            */
        }else{
            $query->where(['status'=>'normal', 'isDelete'=>0]);
        }
        //$query->field('site_id,authorid', true);
        //$query->alias('a')->join('counter as c', 'c.item_id=a.id and c.item_type=`archive`');
        //$query->alias('a')->join('counter c', 'c.item_id=a.id')->where('c.item_type','archive')->field('c.views, c.good, c.bad, c.comments');
    }

    public function getViewsAttr($value,$data)
    {
        if(!isset($data['id'])) return 0;
        $result = Counter::get(['item_id'=>$data['id'], 'item_type'=>'archive']);
        return $result?$result['views']:0;
    }

    public function getUpdatetimeTextAttr($value,$data)
    {
        if(!isset($data['updatetime'])) return '';
        return datetime($data['updatetime'], 'Y-m-d');
    }
}