<?php
namespace app\model;
use think\Model;
class Base extends Model
{
protected $deleteTime;
/**
* @title:默认删除
* @param array $ids
* @return bool
*/
public function defaultDelete($ids = [])
{
if($this->deleteTime){
$force = false;
}else{
$force = true;
}
$this::destroy($ids,$force);
return true;
}
/**
* @title:默认保存
* @param array $data 参数数组
* @param array $where 主键值查询约束,不为空时删除范围内的其他数据
* @param string $pk_check 参数数组中需要保留的数据主体字段,为空时默认为主键
* @return bool
* @throws \Exception
*/
public function defaultSave($data,$where=[],$pk_check='')
{
if(isset($data[$this->pk])){
$data = [$data];
}
if(empty($where)){
$this->model->replace()->saveAll($data);
return true;
}
$pk = $this->model->getPk();
$use = [];
if(!empty($pk_check)){
$all = $this->model->where($where)->column($pk,$pk_check);
foreach ($data as &$v){
if(!empty($all[$v[$pk_check]])){
$pk_value = $all[$v[$pk_check]];
$v[$pk] = $pk_value;
$use[] = $pk_value;
}}
unset($v);
}else{
$use = array_column($data,$pk);
}
$this->model->where($where)->where($pk,'not in',$use)->delete();
$this->model->replace()->saveAll($data);
return true;
}
/**
* @title:默认查看
* @param array $where
* @param int $pageNo
* @param int $pageSize
* @return array
* @throws \Exception
*/
public function defaultList($where = [], $pageNo = 1, $pageSize = 0)
{
$list = $this->where($where)->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]);
$total = $list->total();
$items = json_decode(json_encode($list->items()),true);
return ['total'=>$total,'items'=>$items];
}
/**
* @title:默认选择
* @param array $where
* @param string $field
* @return array
* @throws \Exception
*/
public function defaultSelect($where = [], $field = '*')
{
$list = $this->where($where)->field($field)->select();
return $list->isEmpty()?[]:$list->toArray();
}
/**
* @title:默认列
* @param array $where
* @param string $column
* @param string $index
* @return array
*/
public function defaultColumn($where = [], $column = '*', $index = '')
{
if(empty($index)){
return $this->where($where)->column($column);
}else{
return $this->where($where)->column($column, $index);
}
}
/**
* @title:单一数据查询
* @param array $where
* @param string $field
* @return array|mixed
* @throws \Exception
*/
public function defaultFind($where = [], $field = '*')
{
if($field!='*' && stripos($field,',') == false){
return $this->where($where)->value($field);
}else{
$info = $this->where($where)->field($field)->find();
return empty($info)?[]:$info->toArray();
}
}
/**
* @title:过滤短时间内的重复任务
* @param $data
* @return bool
*/
public function filterRepeatTask($data)
{
if(!app('commonInfo')){
return true;
}
$token = app('commonInfo')->token;
$str = json_encode($data);
$has = cache('taskRecord_'.$token);
if($str != $has){
cache('taskRecord',$str,2);
return true;
}
exit(json(['code'=>1,'msg'=>'保存成功']));
}
}
tp6的简易查询等,嗯,更美观了一点,两行变一行了。。。
于 2022-11-09 11:44:45 首次发布