123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- <?php
- namespace app\common\dao;
- use app\common\model\BaseModel;
- use think\Collection;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Model;
- abstract class BaseDao
- {
-
- abstract protected function getModel(): string;
-
- public function getPk()
- {
- return ($this->getModel())::tablePk();
- }
-
- public function exists(int $id)
- {
- return $this->fieldExists($this->getPk(), $id);
- }
-
- public function fieldExists($field, $value, ?int $except = null): bool
- {
- $query = ($this->getModel())::getDB()->where($field, $value);
- if (!is_null($except)) $query->where($this->getPk(), '<>', $except);
- return $query->count() > 0;
- }
-
- public function create(array $data)
- {
- return ($this->getModel())::create($data);
- }
-
- public function update(int $id, array $data)
- {
- return ($this->getModel())::getDB()->where($this->getPk(), $id)->update($data);
- }
-
- public function updates(array $ids, array $data)
- {
- return ($this->getModel())::getDB()->whereIn($this->getPk(), $ids)->update($data);
- }
-
- public function delete(int $id)
- {
- return ($this->getModel())::getDB()->where($this->getPk(), $id)->delete();
- }
-
- public function get($id)
- {
- return ($this->getModel())::getInstance()->find($id);
- }
-
- public function getWhere(array $where, string $field = '*', array $with = [])
- {
- return ($this->getModel())::getInstance()->where($where)->when($with, function ($query) use ($with) {
- $query->with($with);
- })->field($field)->find();
- }
-
- public function selectWhere(array $where, string $field = '*')
- {
- return ($this->getModel())::getInstance()->where($where)->field($field)->select();
- }
-
- public function getWith(int $id, $with = [])
- {
- return ($this->getModel())::getInstance()->with($with)->find($id);
- }
-
- public function insertAll(array $data)
- {
- return ($this->getModel())::getDB()->insertAll($data);
- }
-
- public function getWhereCount(array $where)
- {
- return ($this->getModel()::getDB())->where($where)->count();
- }
- public function existsWhere($where)
- {
- return ($this->getModel())::getDB()->where($where)->count() > 0;
- }
-
- public function findOrCreate(array $where)
- {
- $res = ($this->getModel()::getDB())->where($where)->find();
- if(!$res)$res = $this->getModel()::create($where);
- return $res;
- }
-
- public function getSearch(array $where)
- {
- foreach ($where as $key => $item) {
- if ($item !== '') {
- $keyArray[] = $key;
- $whereArr[$key] = $item;
- }
- }
- if(empty($keyArray)){
- return ($this->getModel())::getDB();
- }else{
- return ($this->getModel())::withSearch($keyArray, $whereArr);
- }
- }
-
- public function incField(int $id, string $field , $num = 1)
- {
- return ($this->getModel()::getDB())->where($this->getPk(),$id)->inc($field,$num)->update();
- }
-
- public function decField(int $id, string $field , $num = 1)
- {
- return ($this->getModel()::getDB())->where($this->getPk(),$id)->dec($field,$num)->update();
- }
- }
|