BaseDao.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace app\common\dao;
  3. use app\common\model\BaseModel;
  4. use think\Collection;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\DbException;
  7. use think\db\exception\ModelNotFoundException;
  8. use think\Model;
  9. /**
  10. * Class BaseDao
  11. * @package app\common\dao
  12. * @author zfy
  13. * @day 2020-03-30
  14. */
  15. abstract class BaseDao
  16. {
  17. /**
  18. * @return BaseModel
  19. * @author zfy
  20. * @day 2020-03-30
  21. */
  22. abstract protected function getModel(): string;
  23. /**
  24. * @return string
  25. * @author zfy
  26. * @day 2020-03-30
  27. */
  28. public function getPk()
  29. {
  30. return ($this->getModel())::tablePk();
  31. }
  32. /**
  33. * @param int $id
  34. * @return bool
  35. * @author zfy
  36. * @day 2020-03-27
  37. */
  38. public function exists(int $id)
  39. {
  40. return $this->fieldExists($this->getPk(), $id);
  41. }
  42. /**
  43. * @param $field
  44. * @param $value
  45. * @param int|null $except
  46. * @return bool
  47. * @author zfy
  48. * @day 2020-03-30
  49. */
  50. public function fieldExists($field, $value, ?int $except = null): bool
  51. {
  52. $query = ($this->getModel())::getDB()->where($field, $value);
  53. if (!is_null($except)) $query->where($this->getPk(), '<>', $except);
  54. return $query->count() > 0;
  55. }
  56. /**
  57. * @param array $data
  58. * @return self|Model
  59. * @author zfy
  60. * @day 2020-03-27
  61. */
  62. public function create(array $data)
  63. {
  64. return ($this->getModel())::create($data);
  65. }
  66. /**
  67. * @param int $id
  68. * @param array $data
  69. * @return int
  70. * @throws DbException
  71. * @author zfy
  72. * @day 2020-03-27
  73. */
  74. public function update(int $id, array $data)
  75. {
  76. return ($this->getModel())::getDB()->where($this->getPk(), $id)->update($data);
  77. }
  78. /**
  79. * @param array $ids
  80. * @param array $data
  81. * @return int
  82. * @throws DbException
  83. * @author zfy
  84. * @day 2020/6/8
  85. */
  86. public function updates(array $ids, array $data)
  87. {
  88. return ($this->getModel())::getDB()->whereIn($this->getPk(), $ids)->update($data);
  89. }
  90. /**
  91. * @param int $id
  92. * @return int
  93. * @throws DbException
  94. * @author zfy
  95. * @day 2020-03-27
  96. */
  97. public function delete(int $id)
  98. {
  99. return ($this->getModel())::getDB()->where($this->getPk(), $id)->delete();
  100. }
  101. /**
  102. * @param int $id
  103. * @return array|Model|null
  104. * @throws DataNotFoundException
  105. * @throws DbException
  106. * @throws ModelNotFoundException
  107. * @author zfy
  108. * @day 2020-03-27
  109. */
  110. public function get($id)
  111. {
  112. return ($this->getModel())::getInstance()->find($id);
  113. }
  114. /**
  115. * @param array $where
  116. * @param string $field
  117. * @return array|Model|null
  118. * @throws DataNotFoundException
  119. * @throws DbException
  120. * @throws ModelNotFoundException
  121. * @author zfy
  122. * @day 2020/6/1
  123. */
  124. public function getWhere(array $where, string $field = '*', array $with = [])
  125. {
  126. return ($this->getModel())::getInstance()->where($where)->when($with, function ($query) use ($with) {
  127. $query->with($with);
  128. })->field($field)->find();
  129. }
  130. /**
  131. * @param array $where
  132. * @param string $field
  133. * @return Collection
  134. * @throws DataNotFoundException
  135. * @throws DbException
  136. * @throws ModelNotFoundException
  137. * @author zfy
  138. * @day 2020/6/1
  139. */
  140. public function selectWhere(array $where, string $field = '*')
  141. {
  142. return ($this->getModel())::getInstance()->where($where)->field($field)->select();
  143. }
  144. /**
  145. * @param int $id
  146. * @param array $with
  147. * @return array|Model|null
  148. * @throws DataNotFoundException
  149. * @throws DbException
  150. * @throws ModelNotFoundException
  151. * @author zfy
  152. * @day 2020-03-27
  153. */
  154. public function getWith(int $id, $with = [])
  155. {
  156. return ($this->getModel())::getInstance()->with($with)->find($id);
  157. }
  158. /**
  159. * @param array $data
  160. * @return int
  161. * @author zfy
  162. * @day 2020/6/8
  163. */
  164. public function insertAll(array $data)
  165. {
  166. return ($this->getModel())::getDB()->insertAll($data);
  167. }
  168. /**
  169. * TODO 通过条件判断是否存在
  170. * @param array $where
  171. * @author Qinii
  172. * @day 2020-06-13
  173. */
  174. public function getWhereCount(array $where)
  175. {
  176. return ($this->getModel()::getDB())->where($where)->count();
  177. }
  178. public function existsWhere($where)
  179. {
  180. return ($this->getModel())::getDB()->where($where)->count() > 0;
  181. }
  182. /**
  183. * TODO 查询,如果不存在就创建
  184. * @Author:Qinii
  185. * @Date: 2020/9/8
  186. * @param array $where
  187. * @return array|Model|null
  188. */
  189. public function findOrCreate(array $where)
  190. {
  191. $res = ($this->getModel()::getDB())->where($where)->find();
  192. if(!$res)$res = $this->getModel()::create($where);
  193. return $res;
  194. }
  195. /**
  196. * TODO 搜索
  197. * @param $where
  198. * @return BaseModel
  199. * @author Qinii
  200. * @day 2020-10-16
  201. */
  202. public function getSearch(array $where)
  203. {
  204. foreach ($where as $key => $item) {
  205. if ($item !== '') {
  206. $keyArray[] = $key;
  207. $whereArr[$key] = $item;
  208. }
  209. }
  210. if(empty($keyArray)){
  211. return ($this->getModel())::getDB();
  212. }else{
  213. return ($this->getModel())::withSearch($keyArray, $whereArr);
  214. }
  215. }
  216. /**
  217. * TODO 自增
  218. * @param array $id
  219. * @param string $field
  220. * @param int $num
  221. * @return mixed
  222. * @author Qinii
  223. * @day 1/11/21
  224. */
  225. public function incField(int $id, string $field , $num = 1)
  226. {
  227. return ($this->getModel()::getDB())->where($this->getPk(),$id)->inc($field,$num)->update();
  228. }
  229. /**
  230. * TODO 自减
  231. * @param array $id
  232. * @param string $field
  233. * @param int $num
  234. * @return mixed
  235. * @author Qinii
  236. * @day 1/11/21
  237. */
  238. public function decField(int $id, string $field , $num = 1)
  239. {
  240. return ($this->getModel()::getDB())->where($this->getPk(),$id)->dec($field,$num)->update();
  241. }
  242. }