BaseModel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // |
  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. {
  29. $int = 0;
  30. $id = $this->getPk();
  31. if (!empty($post[$id])) {
  32. $int = self::where($id, $post[$id])->save($post);
  33. } else {
  34. $int = self::insert($post);
  35. }
  36. return true;
  37. }
  38. /**
  39. * 获取列表数据
  40. * @param $page
  41. * @param $where
  42. * @param $pageCount
  43. * @param $desc
  44. */
  45. public function getList($page, $where = [], $pageCount = 20, $filed = ['*', ""], $desc = '')
  46. {
  47. $that = $this;
  48. if (!empty($filed[1])) {
  49. $that = $this->alias($filed[1]);
  50. }
  51. $that = $that->field($filed[0])
  52. ->when(!empty($where), function ($query) use ($where) {
  53. foreach ($where as $k => $v) {
  54. if ($v instanceof Closure) {
  55. $v($query);
  56. } else {
  57. if (is_array($v)) {
  58. //whereLike
  59. if ($v[1] == 'whereLike') {
  60. if (!empty($v[0]))
  61. $query->whereLike($k, $v[0]);
  62. continue;
  63. }
  64. if ($v[1] == 'whereBetween') {
  65. if (!empty($v[0])) $query->whereBetween($k, $v[0]);
  66. continue;
  67. }
  68. $bool = false;
  69. eval('$bool = ' . $v[1] . '($v[0]);');
  70. if ($bool) {
  71. $query->where($k, $v[0]);
  72. }
  73. } else {
  74. $query->where($k, $v);
  75. }
  76. }
  77. }
  78. });
  79. if ($pageCount > 0) {
  80. $data = $that->order($desc)
  81. ->paginate(['list_rows' => $pageCount, 'page' => $page])
  82. ->toArray();
  83. } else {
  84. $odata = $that->order($desc)
  85. ->select();
  86. $data['total'] = count($odata);
  87. $data['data'] = $data['total'] > 0 ? $odata->toArray() : [];
  88. }
  89. // echo $this->getLastSql();
  90. return [$data['total'], $data['data']];
  91. }
  92. /**
  93. * 设置错误信息
  94. * @param string $errorMsg
  95. * @return bool
  96. */
  97. protected static function setErrorInfo($errorMsg = self::DEFAULT_ERROR_MSG, $rollback = false)
  98. {
  99. if ($rollback) self::rollbackTrans();
  100. self::$errorMsg = $errorMsg;
  101. return false;
  102. }
  103. /**
  104. * 获取错误信息
  105. * @param string $defaultMsg
  106. * @return string
  107. */
  108. public static function getErrorInfo($defaultMsg = self::DEFAULT_ERROR_MSG)
  109. {
  110. return !empty(self::$errorMsg) ? self::$errorMsg : $defaultMsg;
  111. }
  112. /**
  113. * 开启事务
  114. */
  115. public static function beginTrans()
  116. {
  117. Db::startTrans();
  118. }
  119. /**
  120. * 提交事务
  121. */
  122. public static function commitTrans()
  123. {
  124. Db::commit();
  125. }
  126. /**
  127. * 关闭事务
  128. */
  129. public static function rollbackTrans()
  130. {
  131. Db::rollback();
  132. }
  133. /**
  134. * 根据结果提交滚回事务
  135. * @param $res
  136. */
  137. public static function checkTrans($res)
  138. {
  139. if ($res) {
  140. self::commitTrans();
  141. } else {
  142. self::rollbackTrans();
  143. }
  144. }
  145. }