Dao.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\extra\basic;
  3. abstract class Dao
  4. {
  5. /**
  6. * @var Model 模型单例实例
  7. */
  8. protected $modelInstance = null;
  9. /**
  10. * 获取当前模型
  11. * @return string
  12. */
  13. abstract protected function setModel(): string;
  14. /**
  15. * 获取模型(单例,避免重复实例化)
  16. * @return Model
  17. */
  18. protected function getModel()
  19. {
  20. if ($this->modelInstance === null) {
  21. $modelClass = $this->setModel();
  22. $this->modelInstance = new $modelClass();
  23. }
  24. return $this->modelInstance;
  25. }
  26. /**
  27. * 根据条件获取一条数据
  28. * @param array $where
  29. * @param string|null $field
  30. * @param array $with
  31. * @return array|Model|null
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function getOne(array $where, ?string $field = '*')
  37. {
  38. return $this->getModel()->where($where)->field($field)->findOrEmpty();
  39. }
  40. /**
  41. * @param array $where
  42. * @param string $field
  43. */
  44. public function selectWhere(array $where, string $field = '*')
  45. {
  46. return $this->getModel()->where($where)->field($field)->select();
  47. }
  48. /**
  49. * 读取数据条数
  50. * @param array $where
  51. * @return int
  52. */
  53. public function count(array $where = []): int
  54. {
  55. return $this->getModel()->where($where)->count();
  56. }
  57. /**
  58. * 更新数据
  59. * @param int|string|array $id
  60. * @param array $data
  61. * @param string|null $key
  62. * @return mixed
  63. */
  64. public function update($id, array $data, ?string $key = null)
  65. {
  66. if (is_array($id)) {
  67. $where = $id;
  68. } else {
  69. $where = [is_null($key) ? $this->getModel()->getPk() : $key => $id];
  70. }
  71. $data['updated_at'] = date('Y-m-d H:i:s');
  72. return $this->getModel()->where($where)->update($data);
  73. }
  74. /**
  75. * 插入数据
  76. * @param array $data
  77. * @return mixed
  78. */
  79. public function save(array $data)
  80. {
  81. $data['created_at'] = date('Y-m-d H:i:s');
  82. return $this->getModel()->create($data);
  83. }
  84. /**
  85. * 插入数据
  86. * @param array $data
  87. * @return mixed
  88. */
  89. public function saveAll(array $data)
  90. {
  91. $data['created_at'] = date('Y-m-d H:i:s');
  92. return $this->getModel()->insertAll($data);
  93. }
  94. /**
  95. * 求和
  96. * @param array $where
  97. * @param string $field
  98. * @param bool $search
  99. * @return float
  100. */
  101. public function sum(array $where, string $field)
  102. {
  103. return $this->getModel()->where($where)->sum($field);
  104. }
  105. /**
  106. * 某个字段累加某个数值
  107. * @param string $field
  108. * @param int $num
  109. */
  110. public function incField(array $where, string $field, $num = 1)
  111. {
  112. return $this->getModel()->where($where)->inc($field, $num)->update();
  113. }
  114. /**
  115. * 某个字段累加某个数值
  116. * @param string $field
  117. * @param int $num
  118. */
  119. public function decField(array $where, string $field, $num = 1)
  120. {
  121. return $this->getModel()->where($where)->dec($field, $num)->update();
  122. }
  123. }