BaseDao.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-03-24
  7. *
  8. *
  9. */
  10. namespace app\common\dao;
  11. use app\common\model\BaseModel;
  12. use think\Collection;
  13. use think\db\exception\DataNotFoundException;
  14. use think\db\exception\DbException;
  15. use think\db\exception\ModelNotFoundException;
  16. use think\facade\Db;
  17. use think\Model;
  18. /**
  19. * Class BaseDao
  20. * @package app\common\dao
  21. * @author xaboy
  22. * @day 2020-03-30
  23. */
  24. abstract class BaseDao
  25. {
  26. /**
  27. * @return BaseModel
  28. * @author xaboy
  29. * @day 2020-03-30
  30. */
  31. abstract protected function getModel(): string;
  32. /**
  33. * @return string
  34. * @author xaboy
  35. * @day 2020-03-30
  36. */
  37. public function getPk()
  38. {
  39. return ($this->getModel())::tablePk();
  40. }
  41. /**
  42. * @param int $id
  43. * @return bool
  44. * @author xaboy
  45. * @day 2020-03-27
  46. */
  47. public function exists(int $id)
  48. {
  49. return $this->fieldExists($this->getPk(), $id);
  50. }
  51. /**
  52. * @param $field
  53. * @param $value
  54. * @param int|null $except
  55. * @return bool
  56. * @author xaboy
  57. * @day 2020-03-30
  58. */
  59. public function fieldExists($field, $value, ?int $except = null): bool
  60. {
  61. $query = ($this->getModel())::getDB()->where($field, $value);
  62. if (!is_null($except)) $query->where($this->getPk(), '<>', $except);
  63. return $query->count() > 0;
  64. }
  65. /**
  66. * @param array $data
  67. * @return self|Model
  68. * @author xaboy
  69. * @day 2020-03-27
  70. */
  71. public function create(array $data)
  72. {
  73. return ($this->getModel())::create($data);
  74. }
  75. /**
  76. * @param int $id
  77. * @param array $data
  78. * @return int
  79. * @throws DbException
  80. * @author xaboy
  81. * @day 2020-03-27
  82. */
  83. public function update(int $id, array $data)
  84. {
  85. return ($this->getModel())::getDB()->where($this->getPk(), $id)->update($data);
  86. }
  87. /**
  88. * @param array $ids
  89. * @param array $data
  90. * @return int
  91. * @throws DbException
  92. * @author xaboy
  93. * @day 2020/6/8
  94. */
  95. public function updates(array $ids, array $data)
  96. {
  97. return ($this->getModel())::getDB()->whereIn($this->getPk(), $ids)->update($data);
  98. }
  99. /**
  100. * @param int $id
  101. * @return int
  102. * @throws DbException
  103. * @author xaboy
  104. * @day 2020-03-27
  105. */
  106. public function delete(int $id)
  107. {
  108. return ($this->getModel())::getDB()->where($this->getPk(), $id)->delete();
  109. }
  110. /**
  111. * @param int $id
  112. * @return array|Model|null
  113. * @throws DataNotFoundException
  114. * @throws DbException
  115. * @throws ModelNotFoundException
  116. * @author xaboy
  117. * @day 2020-03-27
  118. */
  119. public function get($id)
  120. {
  121. return ($this->getModel())::getInstance()->find($id);
  122. }
  123. /**
  124. * @param array $where
  125. * @param string $field
  126. * @return array|Model|null
  127. * @throws DataNotFoundException
  128. * @throws DbException
  129. * @throws ModelNotFoundException
  130. * @author xaboy
  131. * @day 2020/6/1
  132. */
  133. public function getWhere(array $where, string $field = '*', array $with = [])
  134. {
  135. return ($this->getModel())::getInstance()->where($where)->when($with, function ($query) use ($with) {
  136. $query->with($with);
  137. })->field($field)->find();
  138. }
  139. /**
  140. * @param array $where
  141. * @param string $field
  142. * @return Collection
  143. * @throws DataNotFoundException
  144. * @throws DbException
  145. * @throws ModelNotFoundException
  146. * @author xaboy
  147. * @day 2020/6/1
  148. */
  149. public function selectWhere(array $where, string $field = '*')
  150. {
  151. return ($this->getModel())::getInstance()->where($where)->field($field)->select();
  152. }
  153. /**
  154. * @param int $id
  155. * @param array $with
  156. * @return array|Model|null
  157. * @throws DataNotFoundException
  158. * @throws DbException
  159. * @throws ModelNotFoundException
  160. * @author xaboy
  161. * @day 2020-03-27
  162. */
  163. public function getWith(int $id, $with = [])
  164. {
  165. return ($this->getModel())::getInstance()->with($with)->find($id);
  166. }
  167. /**
  168. * @param array $data
  169. * @return int
  170. * @author xaboy
  171. * @day 2020/6/8
  172. */
  173. public function insertAll(array $data)
  174. {
  175. return ($this->getModel())::getDB()->insertAll($data);
  176. }
  177. /**
  178. * TODO 通过条件判断是否存在
  179. * @param array $where
  180. * @author Qinii
  181. * @day 2020-06-13
  182. */
  183. public function getWhereCount(array $where)
  184. {
  185. return ($this->getModel()::getDB())->where($where)->count();
  186. }
  187. public function existsWhere($where)
  188. {
  189. return ($this->getModel())::getDB()->where($where)->count() > 0;
  190. }
  191. }