lumen 底层model

最近项目有些紧,对博客懈怠了,临时补充项目中用到的两篇,下面代码是项目中用到的 底层model ,lumen版本6.0X,贴代码:里面有多个数据库链接、分页等等很方便

<?php
/**
 * base model
 *
 * User: Faithlee
 * Date: 2020/5/18
 * Time: 下午3:16
 */

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Facades\DB;

class BaseModel extends Model
{
    protected $perPage = 10;

    /**
     * 分页查询方法
     *
     * @param mixed|null $where 查询判断条件
     * @param string $select 查询字段
     * @param array $orders 查询排序
     * @param int $page 查询页码
     * @param int $perPage 自定义分页大小
     * @return mixed
     */
    public function getCommonList($where = null, $select = '*', array $orders = [], int $page, int $perPage = 0)
    {
        $table = $this->getBaseTable()->select($select);
        if ($where) {
            if (!isset($where['bindingParams'])) {
                $table->where($where);
            } else {
                $table->whereRaw($where['where'], $where['bindingParams']);
            }
        }
        if ($orders) {
            foreach ($orders as $key => $value) {
                $table->orderBy($key, $value);
            }
        }
        $pageSize = $perPage ? $perPage : $this->perPage;
        return $table->paginate($pageSize, ['*'], 'page', $page)->toArray();
    }

    public function getCommonFirst($where = [], $select = '*')
    {
        $table = $this->getBaseTable();
        if (is_array($select)) {
            $table->select($select);
        } else {
            $table->selectRaw($select);
        }
        if (!isset($where['bindingParams'])) {
            $table->where($where);
        } else {
            $table->whereRaw($where['where'], $where['bindingParams']);
        }
        return $table->first();

    }

    /**
     * 插入数据
     *
     * @param $data
     * @return mixed
     */
    public function baseInsert($data)
    {
        return $this->getBaseTable()->insertGetId($data);
    }

    /**
     * 更新数据
     *
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function baseUpdate($where = [], array $data)
    {
        $table = $this->getBaseTable();
        if ($where) {
            $table->where($where);
        }

        return $table->update($data);
    }

    protected function getBaseTable()
    {
        if ($this->connection) {
            return DB::connection($this->connection)->table($this->table);
        }

        return DB::table($this->table);
    }


    public function dealPaginate(QueryBuilder $query, $page)
    {
        $data = $query->paginate($this->perPage, ['*'], 'page', $page)->toArray();

        $newData['list'] = $data['data'];
        $newData['current_page'] = $data['current_page'];
        $newData['per_page'] = $data['per_page'];
        $newData['total'] = $data['total'];
        $newData['total_page'] = ceil($data['total'] / $data['per_page']);

        return $newData;
    }


    /**
     * 查询符合条件的总条数
     * @param array $where
     * @return array
     */
    public function getCommonNum(array $where = [], array $wheres = [])
    {
        $table = $this->getBaseTable();
        if ($where) {
            if (isset($where['where'])) {
                $table->whereRaw($where['where'], $where['bindingParams']);
            } else {
                $table->where($where);
            }
        }

        if ($wheres) {
            foreach ($wheres as $key => $item) {
                $table->whereIn($key, $item);
            }
        }
        $totalNum = $table->count();
        return $totalNum;
    }

    /**
     * 求和
     * @param array $where
     * @return array
     */
    public function getSum(array $where = [],$field = '', array $wheres = [])
    {
        $table = $this->getBaseTable();
        if ($where) {
            if (isset($where['where'])) {
                $table->whereRaw($where['where'], $where['bindingParams']);
            } else {
                $table->where($where);
            }
        }

        if ($wheres) {
            foreach ($wheres as $key => $item) {
                $table->whereIn($key, $item);
            }
        }
        return $table->pluck($field)->sum();
    }

    /**
     * 根据条件查询
     * @param array $where 查询条件
     * @param string $select 查询字段
     * @param array $orders 排序规则 ['id' => 'asc', 'create_time' => 'desc']
     * @param array $likes 模糊查询 ['project_cate_name' => '2323']
     * @return mixed
     */
    public function getBaseInfo($where = [], $select = '*', array $orders = [], array $likes = [])
    {
        $table = $this->getBaseTable();
        $table->select($select);
        if (!empty($where)) {
            if (!isset($where['bindingParams'])) {
                $table->where($where);
            } else {
                $table->whereRaw($where['where'], $where['bindingParams']);
            }
        }

        if (!empty($likes)) {
            foreach ($likes as $lkey => $item) {
                $table->where($lkey, 'like', '%' . $item . '%');
            }
        }

        if (!empty($orders)) {
            foreach ($orders as $key => $value) {
                $table->orderBy($key, $value);
            }
        }

        $data = $table->get()->toArray();
        return $data;
    }

    /**
     * 获取单条记录
     * @param array $where
     * @param string $select
     * @return Model|\Illuminate\Database\Query\Builder|object|null
     */
    public function getInfoRow(array $where = [], $select = '*', array $orders = [])
    {
        $table = $this->getBaseTable();
        $table->select($select);

        if (!empty($where)) {
            if(isset($where['where'])){
                $table->whereRaw($where['where'], $where['bindingParams']);
            }else {
                $table->where($where);
            }
        }
        if ($orders) {
            foreach ($orders as $key => $value) {
                $table->orderBy($key, $value);
            }
        }
        return $table->first();
    }

    /**
     * 查询某个字段的最大值
     * @param array $where
     * @param string $column
     * @return bool|mixed
     */
    public function getMaxValue(array $where = [], string $column = '')
    {
        if (empty($column)) {
            return false;
        }

        $table = $this->getBaseTable();

        if (!empty($where)) {
            $table->where($where);
        }

        return $table->max($column);
    }

    /**
     * whereIn 批量修改
     * @param array $where
     * @param array $update
     * @return int
     */
    public function baseUpdateBatch(array $where = [], $update = [])
    {
        $table = $this->getBaseTable();

        if ($where) {
            foreach ($where as $key => $item) {
                $table->whereIn($key, $item);
            }
        }

        return $table->update($update);
    }

    /**
     * 基本删除
     * @param array $where
     */
    public function baseDelete(array $where = [])
    {
        $table = $this->getBaseTable();
        return $table->where($where)->delete();
    }

    /**
     * 批量插入数据
     *
     * @param $data
     * @return mixed
     */
    public function patInsert($data)
    {
        return $this->getBaseTable()->insert($data);
    }

    /**
     * 改变数据库连接
     * 同一model中需要操作两个数据库中的表
     * @param string $dbName 数据库名称
     * @param string $table 表明
     * @return bool|QueryBuilder
     */
    protected function changeDbTable(string $dbName = '', string $table = '')
    {
        try {
            return DB::connection($dbName ? $dbName : $this->changeConnection)->table($table ? $table : $this->changeTable);
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 批量获取信息
     * @param array $where
     * @param string $select
     * @param array $orders
     * @return array
     */
    public function getInfoBatch(array $wheres, array $where, $select = '*', array $orders = [], $page = 0, $pageSize = 0)
    {
        $table = $this->getBaseTable()->select($select);

        if ($wheres) {
            foreach ($wheres as $key => $item) {
                $table->whereIn($key, $item);
            }
        }

        if ($where) {
            $table->where($where);
        }

        if ($orders) {
            foreach ($orders as $key => $value) {
                $table->orderBy($key, $value);
            }
        }

        if ($page) {
            $pageSize = $pageSize ? $pageSize : $this->perPage;
            return $table->paginate($pageSize, ['*'], 'page', $page)->toArray();
        }

        return $table->get()->toArray();
    }


}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值