tp6的简易查询等,嗯,更美观了一点,两行变一行了。。。

<?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'=>'保存成功']));
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值