BaseDao.php 7.6 KB

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