<?php<web88@qq.com>
namespace app\cms\model;
class Document extends BaseCmsModel {
protected $readonly = ['id'];
protected $auto = ['update_time'];
protected $insert = ['status'];
protected $type = [
'id' => 'integer',
'cover_id' => 'integer',
'deadline' => 'integer',
'view' => 'integer',
'bookmark' => 'integer',
];
public function setUpdateTimeAttr($value){
if ($value && is_string($value)){
return strtotime($value);
}elseif($value && is_numeric($value)){
return $value;
}else{
return time();
}
}
public function setIstopAttr($value) {
if ($value == 'on'){
return 1;
}else{
return 0;
}
}
public function getStatusAttr($value){
return $this->getStatusType($value);
}
public function setStatusAttr($value){
return is_numeric($value) ? $value:$this->getStatusType($value);
}
public function setPositionAttr($value) {
$pos = 0;
if($value && is_array($value)){
foreach ($value as $key=>$v){
$pos += $v;
}
}
return $pos;
}
public function getPositionAttr($value) {
return $this->getPosition($value,true);
}
public function categoryinfo()
{
return $this->hasOne('\\app\\admin\\model\\AdminCategory','id','category_id');
}
public function category()
{
return $this->hasOne('\\app\\admin\\model\\AdminCategory','id','category_id')->bind([
'category_name'=>'title'
]);
}
public function article()
{
return $this->hasOne('DocumentContent','document_id','id')->bind([
'content'=>'content'
,'template'=>'template'
]);
}
public function oss()
{
return $this->hasOne('\\app\\oss\\model\\Oss','id','cover_id')->bind([
'cover_path'=>'url'
]);
}
public function user()
{
return $this->hasOne('\\app\\user\\model\\User','id','uid')->bind(['username'=>'username']);
}
public function getStatusType($status=null){
$type = config('?config.status_type') ? config('config.status_type'):[-1=>'删除',0=>'禁用',1=>'正常',2=>'待审核'];
if (is_numeric($status)){
return isset($type[$status]) ? $type[$status]:'';
}elseif (is_string($status)){
$type = array_flip($type); return isset($type[$status]) ? $type[$status]:'';
}
return $type;
}
public function getPosition($value=null,$formatPosition=false,$reArray=false){
$data = config('config.document_position');
if ($formatPosition){
$pos = [];
foreach ($data as $k=>$v){
$res = $value & $k;
if($res !== 0){
$pos[$k]=$v;
}
}
return $pos;
}
if ($value && is_numeric($value)){
return isset($data[$value]) ? $data[$value]:'';
}elseif ($value && is_array($value)){
$arr = [];
foreach ($value as $v){
$arr[] =isset($data[$v]) ? $data[$v]:'';
}
return $reArray ? $arr:implode(',',$arr);
}elseif (!is_array($value) && $value == null){
return $data;
}else{
return '';
}
}
public function position($pos, $category = null, $limit = 5,$order='istop desc,update_time desc', $field = true){
$map[] = ['status','=',1];
$where ='';
if(!is_null($category)){
if(is_numeric($category)){
$map[] = ['category_id','=',$category];
} else {
$map[] = ['category_id','in',str2arr($category)];
}
}
if($pos && is_numeric($pos)){
$where = "position & {$pos} = {$pos}";
}
return $this->with('oss')->field($field)->where($map)->where($where)->order($order)->limit($limit)->cache(false)->select();
}
}