BaseModel.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace crmeb\basic;
  8. use think\facade\Db;
  9. use think\Model;
  10. class BaseModel extends Model
  11. {
  12. private static $errorMsg;
  13. private static $transaction = 0;
  14. private static $DbInstance = [];
  15. const DEFAULT_ERROR_MSG = '操作失败,请稍候再试!';
  16. /**
  17. * 设置错误信息
  18. * @param string $errorMsg
  19. * @return bool
  20. */
  21. protected static function setErrorInfo($errorMsg = self::DEFAULT_ERROR_MSG, $rollback = false)
  22. {
  23. if ($rollback) self::rollbackTrans();
  24. self::$errorMsg = $errorMsg;
  25. return false;
  26. }
  27. /**
  28. * 获取错误信息
  29. * @param string $defaultMsg
  30. * @return string
  31. */
  32. public static function getErrorInfo($defaultMsg = self::DEFAULT_ERROR_MSG)
  33. {
  34. return !empty(self::$errorMsg) ? self::$errorMsg : $defaultMsg;
  35. }
  36. /**
  37. * 开启事务
  38. */
  39. public static function beginTrans()
  40. {
  41. Db::startTrans();
  42. }
  43. /**
  44. * 提交事务
  45. */
  46. public static function commitTrans()
  47. {
  48. Db::commit();
  49. }
  50. /**
  51. * 关闭事务
  52. */
  53. public static function rollbackTrans()
  54. {
  55. Db::rollback();
  56. }
  57. /**
  58. * 根据结果提交滚回事务
  59. * @param $res
  60. */
  61. public static function checkTrans($res)
  62. {
  63. if ($res) {
  64. self::commitTrans();
  65. } else {
  66. self::rollbackTrans();
  67. }
  68. }
  69. /**
  70. * 获取ID
  71. * @param string $key 键名
  72. * @return mixed
  73. */
  74. public static function getkeytoid(string $key)
  75. {
  76. $rs = Db::query(sprintf("call keytoid('%s')",$key));
  77. return $rs[0][0]['cus_no'];
  78. }
  79. }