Dao.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 = '*', ?string $order = '')
  37. {
  38. return $this->getModel()->where($where)->field($field)->when($order,function($query)use($order){
  39. $query->order($order);
  40. })->find();
  41. }
  42. /**
  43. * @param array $where
  44. * @param string $field
  45. */
  46. public function selectWhere(array $where, string $field = '*',string $order = '')
  47. {
  48. return $this->getModel()->where($where)->field($field)->when($order,function($query)use($order){
  49. $query->order($order);
  50. })->select();
  51. }
  52. /**
  53. * 读取数据条数
  54. * @param array $where
  55. * @return int
  56. */
  57. public function count(array $where = []): int
  58. {
  59. return $this->getModel()->where($where)->count();
  60. }
  61. /**
  62. * 更新数据
  63. * @param int|string|array $id
  64. * @param array $data
  65. * @param string|null $key
  66. * @return mixed
  67. */
  68. public function update($id, array $data, ?string $key = null)
  69. {
  70. if (is_array($id)) {
  71. $where = $id;
  72. } else {
  73. $where = [is_null($key) ? $this->getModel()->getPk() : $key => $id];
  74. }
  75. $data['updated_at'] = date('Y-m-d H:i:s');
  76. return $this->getModel()->where($where)->update($data);
  77. }
  78. /**
  79. * 插入数据
  80. * @param array $data
  81. * @return mixed
  82. */
  83. public function save(array $data)
  84. {
  85. $data['created_at'] = date('Y-m-d H:i:s');
  86. return $this->getModel()->create($data);
  87. }
  88. /**
  89. * 插入数据
  90. * @param array $data
  91. * @return mixed
  92. */
  93. public function saveAll(array $data)
  94. {
  95. $data['created_at'] = date('Y-m-d H:i:s');
  96. return $this->getModel()->insertAll($data);
  97. }
  98. /**
  99. * 求和
  100. * @param array $where
  101. * @param string $field
  102. * @param bool $search
  103. * @return float
  104. */
  105. public function sum(array $where, string $field)
  106. {
  107. return $this->getModel()->where($where)->sum($field);
  108. }
  109. /**
  110. * 某个字段累加某个数值
  111. * @param string $field
  112. * @param int $num
  113. */
  114. public function incField(array $where, string $field, $num = 1)
  115. {
  116. return $this->getModel()->where($where)->inc($field, $num)->update();
  117. }
  118. /**
  119. * 某个字段累加某个数值
  120. * @param string $field
  121. * @param int $num
  122. */
  123. public function decField(array $where, string $field, $num = 1)
  124. {
  125. return $this->getModel()->where($where)->dec($field, $num)->update();
  126. }
  127. /**
  128. * 获取单个字段值
  129. * @param array $where
  130. * @param string|null $field
  131. * @return mixed
  132. */
  133. public function value(array $where, ?string $field = '')
  134. {
  135. $pk = $this->getModel()->getPk();
  136. return $this->getModel()::where($where)->value($field ?: $pk);
  137. }
  138. /**
  139. * 获取某个字段数组
  140. * @param array $where
  141. * @param string $field
  142. * @param string $key
  143. * @return array
  144. */
  145. public function getColumn(array $where, string $field, string $key = '')
  146. {
  147. return $this->getModel()::where($where)->column($field, $key);
  148. }
  149. /**
  150. * 删除
  151. * @param int|string|array $id
  152. * @return bool
  153. */
  154. public function delete($id, ?string $key = null)
  155. {
  156. if (is_array($id)) {
  157. $where = $id;
  158. } else {
  159. $where = [is_null($key) ? $this->getModel()->getPk() : $key => $id];
  160. }
  161. return $this->getModel()::where($where)->delete();
  162. }
  163. }