BaseDao.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao;
  12. use app\common\model\BaseModel;
  13. use think\Collection;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  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. /**
  192. * TODO 查询,如果不存在就创建
  193. * @Author:Qinii
  194. * @Date: 2020/9/8
  195. * @param array $where
  196. * @return array|Model|null
  197. */
  198. public function findOrCreate(array $where)
  199. {
  200. $res = ($this->getModel()::getDB())->where($where)->find();
  201. if(!$res)$res = $this->getModel()::create($where);
  202. return $res;
  203. }
  204. /**
  205. * TODO 搜索
  206. * @param $where
  207. * @return BaseModel
  208. * @author Qinii
  209. * @day 2020-10-16
  210. */
  211. public function getSearch(array $where)
  212. {
  213. foreach ($where as $key => $item) {
  214. if ($item !== '') {
  215. $keyArray[] = $key;
  216. $whereArr[$key] = $item;
  217. }
  218. }
  219. if(empty($keyArray)){
  220. return ($this->getModel())::getDB();
  221. }else{
  222. return ($this->getModel())::withSearch($keyArray, $whereArr);
  223. }
  224. }
  225. /**
  226. * TODO 自增
  227. * @param array $id
  228. * @param string $field
  229. * @param int $num
  230. * @return mixed
  231. * @author Qinii
  232. * @day 1/11/21
  233. */
  234. public function incField(int $id, string $field , $num = 1)
  235. {
  236. return ($this->getModel()::getDB())->where($this->getPk(),$id)->inc($field,$num)->update();
  237. }
  238. /**
  239. * TODO 自减
  240. * @param array $id
  241. * @param string $field
  242. * @param int $num
  243. * @return mixed
  244. * @author Qinii
  245. * @day 1/11/21
  246. */
  247. public function decField(int $id, string $field , $num = 1)
  248. {
  249. return ($this->getModel()::getDB())->where($this->getPk(),$id)->dec($field,$num)->update();
  250. }
  251. }