BaseModel.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\basic;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-08-29 16:20
  12. // +----------------------------------------------------------------------
  13. use Closure;
  14. use think\facade\Db;
  15. use think\Model;
  16. class BaseModel extends Model
  17. {
  18. private static $errorMsg;
  19. private static $transaction = 0;
  20. private static $DbInstance = [];
  21. const DEFAULT_ERROR_MSG = '操作失败,请稍候再试!';
  22. /**
  23. * 保存 | 添加 数据
  24. * @param $post
  25. * @return bool
  26. */
  27. public function saveModel($post) {
  28. $int = 0;
  29. $id = $this->getPk();
  30. if(!empty($post[$id])) {
  31. $int = self::where($id,$post[$id])->save($post);
  32. } else {
  33. $int = self::insert($post);
  34. }
  35. return true;
  36. }
  37. /**
  38. * 获取列表数据
  39. * @param $page
  40. * @param $where
  41. * @param $pageCount
  42. * @param $desc
  43. */
  44. public function getList($page,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
  45. $data = $this
  46. ->when(!empty($where),function ($query) use($where){
  47. foreach ($where as $k=>$v) {
  48. if($v instanceof Closure) {
  49. $v($query);
  50. } else {
  51. if(is_array($v)) {
  52. //whereLike
  53. if($v[1] == 'whereLike') {
  54. if(!empty($v[0]))
  55. $query->whereLike($k,$v[0]);
  56. continue;
  57. }
  58. if($v[1] == 'whereBetween') {
  59. if(!empty($v[0])) $query->whereBetween($k,$v[0]);
  60. continue;
  61. }
  62. $bool = false;
  63. eval('$bool = '.$v[1].'($v[0]);');
  64. if($bool) {
  65. $query->where($k,$v[0]);
  66. }
  67. } else {
  68. $query->where($k,$v);
  69. }
  70. }
  71. }
  72. })
  73. ->order($desc)
  74. ->paginate(['list_rows'=>$pageCount,'page'=>$page])
  75. ->toArray();
  76. // echo $this->getLastSql();
  77. return [$data['total'],$data['data']];
  78. }
  79. /**
  80. * 设置错误信息
  81. * @param string $errorMsg
  82. * @return bool
  83. */
  84. protected static function setErrorInfo($errorMsg = self::DEFAULT_ERROR_MSG, $rollback = false)
  85. {
  86. if ($rollback) self::rollbackTrans();
  87. self::$errorMsg = $errorMsg;
  88. return false;
  89. }
  90. /**
  91. * 获取错误信息
  92. * @param string $defaultMsg
  93. * @return string
  94. */
  95. public static function getErrorInfo($defaultMsg = self::DEFAULT_ERROR_MSG)
  96. {
  97. return !empty(self::$errorMsg) ? self::$errorMsg : $defaultMsg;
  98. }
  99. /**
  100. * 开启事务
  101. */
  102. public static function beginTrans()
  103. {
  104. Db::startTrans();
  105. }
  106. /**
  107. * 提交事务
  108. */
  109. public static function commitTrans()
  110. {
  111. Db::commit();
  112. }
  113. /**
  114. * 关闭事务
  115. */
  116. public static function rollbackTrans()
  117. {
  118. Db::rollback();
  119. }
  120. /**
  121. * 根据结果提交滚回事务
  122. * @param $res
  123. */
  124. public static function checkTrans($res)
  125. {
  126. if ($res) {
  127. self::commitTrans();
  128. } else {
  129. self::rollbackTrans();
  130. }
  131. }
  132. }