123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- declare (strict_types=1);
- namespace library\basic;
- // +----------------------------------------------------------------------
- // | [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018-2020 rights reserved.
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // | Date: 2020-08-29 16:20
- // +----------------------------------------------------------------------
- use Closure;
- use think\facade\Db;
- use think\Model;
- class BaseModel extends Model
- {
- private static $errorMsg;
- private static $transaction = 0;
- private static $DbInstance = [];
- const DEFAULT_ERROR_MSG = '操作失败,请稍候再试!';
- /**
- * 保存 | 添加 数据
- * @param $post
- * @return bool
- */
- public function saveModel($post)
- {
- $int = 0;
- $id = $this->getPk();
- if (!empty($post[$id])) {
- $int = self::where($id, $post[$id])->save($post);
- } else {
- $int = self::insert($post);
- }
- return true;
- }
- /**
- * 获取列表数据
- * @param $page
- * @param $where
- * @param $pageCount
- * @param $desc
- */
- public function getList($page, $where = [], $pageCount = 20, $filed = ['*', ""], $desc = '')
- {
- $that = $this;
- if (!empty($filed[1])) {
- $that = $this->alias($filed[1]);
- }
- $that = $that->field($filed[0])
- ->when(!empty($where), function ($query) use ($where) {
- foreach ($where as $k => $v) {
- if ($v instanceof Closure) {
- $v($query);
- } else {
- if (is_array($v)) {
- //whereLike
- if ($v[1] == 'whereLike') {
- if (!empty($v[0]))
- $query->whereLike($k, $v[0]);
- continue;
- }
- if ($v[1] == 'whereBetween') {
- if (!empty($v[0])) $query->whereBetween($k, $v[0]);
- continue;
- }
- $bool = false;
- eval('$bool = ' . $v[1] . '($v[0]);');
- if ($bool) {
- $query->where($k, $v[0]);
- }
- } else {
- $query->where($k, $v);
- }
- }
- }
- });
- if ($pageCount > 0) {
- $data = $that->order($desc)
- ->paginate(['list_rows' => $pageCount, 'page' => $page])
- ->toArray();
- } else {
- $odata = $that->order($desc)
- ->select();
- $data['total'] = count($odata);
- $data['data'] = $data['total'] > 0 ? $odata->toArray() : [];
- }
- // echo $this->getLastSql();
- return [$data['total'], $data['data']];
- }
- /**
- * 设置错误信息
- * @param string $errorMsg
- * @return bool
- */
- protected static function setErrorInfo($errorMsg = self::DEFAULT_ERROR_MSG, $rollback = false)
- {
- if ($rollback) self::rollbackTrans();
- self::$errorMsg = $errorMsg;
- return false;
- }
- /**
- * 获取错误信息
- * @param string $defaultMsg
- * @return string
- */
- public static function getErrorInfo($defaultMsg = self::DEFAULT_ERROR_MSG)
- {
- return !empty(self::$errorMsg) ? self::$errorMsg : $defaultMsg;
- }
- /**
- * 开启事务
- */
- public static function beginTrans()
- {
- Db::startTrans();
- }
- /**
- * 提交事务
- */
- public static function commitTrans()
- {
- Db::commit();
- }
- /**
- * 关闭事务
- */
- public static function rollbackTrans()
- {
- Db::rollback();
- }
- /**
- * 根据结果提交滚回事务
- * @param $res
- */
- public static function checkTrans($res)
- {
- if ($res) {
- self::commitTrans();
- } else {
- self::rollbackTrans();
- }
- }
- }
|