DiverseYouthNightSchool/application/common/model/BaseModel.php

162 lines
5.6 KiB
PHP

<?php
namespace app\common\model;
use think\Model;
/**
* 基础扩展模型
*/
class BaseModel extends Model
{
use \traits\ModelTrait;
// use \traits\ErrorTrait;
public $timeKey = 'createtime';
public static $staticTimeKey = 'createtime';
/**得到基础条件
* @param $status
* @param null $model
* @param string $alisa
*/
public static function getBaseWhere($whereData = [], $model = null, $alisa = '',$with = false)
{
if (!$model) {
$model = new static;
if ($alisa&&!$with) $model = $model->alias($alisa);
}
if ($alisa) $alisa = $alisa . '.';
//$model = $model->where($alisa . 'status', '1');
$tableFields = (new static)->getTableFields();
foreach ($tableFields as $fields)
{
if (isset($whereData[$fields]) && $whereData[$fields]){
if(is_array($whereData[$fields])){
$model = $model->where("{$alisa}{$fields}", $whereData[$fields][0], $whereData[$fields][1]);
}else{
$model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);
}
}
}
return $model;
}
/**
* 基础列表
*/
public function getBaseList($whereData = [], $page = 0, $limit = 0, $sort = '',$field =[],$where=[],$toArray = true)
{
$alisa = $this->getWithAlisaName();
if($field){
//如果是一维数组
if(is_array($field)&&count($field) == count($field,1)) $field = ['base'=>$field];
//如果是字符串
if(is_string($field)) $field = ['base'=>explode(',',$field)];
}
$this->withTable = array_keys($field);
$base_i = array_search("base",$this->withTable);
if($base_i!==false)unset($this->withTable[$base_i]);
if(!$this->withTable)$alisa = '';
$alisa_name = '';
if($alisa)$alisa_name = "{$alisa}.";
if(!$sort)$sort = "{$alisa_name}id asc";
$self = static::getBaseWhere($whereData, null, $alisa,true);
if($this->withTable)$self = $self->with($this->withTable);
if($page&&$limit)$self = $self->orderRaw($sort)->where($where)->page($page, $limit);
$list = $self->select();
foreach ($list as $row) {
if(isset($field['base'])&&$field['base']!=['*']){
$row->visible($field['base']);
}else{
$getTableFields = $this->getTableFields();
if(!empty($this->hidden) && is_array($this->hidden)){
$getTableFields = array_diff($getTableFields,$this->hidden);
}
$row->visible($getTableFields);
}
foreach ($this->withTable as $withName) {
if(isset($field[$withName])&&$field[$withName]!=['*']){
$row->visible([$withName]);
$row->getRelation($withName)->visible($field[$withName]);
}elseif(isset($field[$withName])&&$field[$withName]==['*']){
$row->visible([$withName]);
}
}
}
if($toArray)$list = collection($list)->toArray();
$countSelf = static::getBaseWhere($whereData, null, $alisa,true);
if($this->withTable)$countSelf = $countSelf->with($this->withTable);
$count = $countSelf->where($where)->count();
return compact('list', 'count','page','limit');
}
/**
* 时间段搜索器
* @param Model $query
* @param $value
*/
public function scopeTime($query, $value)
{
$timeKey = $this->timeKey;
if(static::$staticTimeKey)$timeKey =static::$staticTimeKey;
if(is_array($value)){
$timeKey = $value[0];
$value = $value[1];
}
switch ($value) {
case 'today':
case 'week':
case 'month':
case 'year':
case 'yesterday':
case 'last year':
case 'last week':
case 'last month':
$query->whereTime($timeKey, $value);
break;
case 'quarter':
list($startTime, $endTime) = static::getMonth();
$query->whereTime($timeKey, 'between', [$startTime, $endTime]);
break;
case 'lately7':
$query->whereTime($timeKey, 'between', [strtotime("-7 day"), time()]);
break;
case 'lately30':
$query->whereTime($timeKey, 'between', [strtotime("-30 day"), time()]);
break;
default:
if (strstr($value, '---') !== false||strstr($value, '-') !== false) {
if(strstr($value, '---') !== false){
[$startTime, $endTime] = explode('---', $value);
}elseif (strstr($value, '-') !== false){
[$startTime, $endTime] = explode('-', $value);
}
$startTime = trim($startTime);
$endTime = trim($endTime);
if ($startTime && $endTime) {
$query->whereTime($timeKey, 'between', [strtotime($startTime), $startTime == $endTime ? strtotime($endTime) + 86400 : strtotime($endTime)]);
} else if (!$startTime && $endTime) {
$query->whereTime($timeKey, '<', strtotime($endTime) + 86400);
} else if ($startTime && !$endTime) {
$query->whereTime($timeKey, '>=', strtotime($startTime));
}
}
break;
}
}
}