123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- <?php
- namespace app\dao;
- use crmeb\basic\BaseAuth;
- use crmeb\basic\BaseModel;
- use crmeb\traits\dao\CacheDaoTrait;
- use think\Model;
- abstract class BaseDao
- {
- use CacheDaoTrait;
-
- protected $alias;
-
- protected $joinAlis;
-
- abstract protected function setModel(): string;
-
- protected function setJoinModel(): string
- {
- }
-
- public function count(array $where = []): int
- {
- return $this->search($where)->count();
- }
-
- public function getCount(array $where)
- {
- return $this->getModel()->where($where)->count();
- }
-
- public function getDistinctCount(array $where, $field, $search = true)
- {
- if ($search) {
- return $this->search($where)->field('COUNT(distinct(' . $field . ')) as count')->select()->toArray()[0]['count'] ?? 0;
- } else {
- return $this->getModel()->where($where)->field('COUNT(distinct(' . $field . ')) as count')->select()->toArray()[0]['count'] ?? 0;
- }
- }
-
- protected function getModel()
- {
- return app()->make($this->setModel());
- }
-
- protected function getPk()
- {
- return $this->getModel()->getPk();
- }
-
- public function get($id, ?array $field = [], ?array $with = [])
- {
- if (is_array($id)) {
- $where = $id;
- } else {
- $where = [$this->getPk() => $id];
- }
- return $this->getModel()::where($where)->when(count($with), function ($query) use ($with) {
- $query->with($with);
- })->field($field ?? ['*'])->find();
- }
-
- public function be($map, string $field = '')
- {
- if (!is_array($map) && empty($field)) $field = $this->getPk();
- $map = !is_array($map) ? [$field => $map] : $map;
- return 0 < $this->getModel()->where($map)->count();
- }
-
- public function getOne(array $where, ?string $field = '*', array $with = [])
- {
- $field = explode(',', $field);
- return $this->get($where, $field, $with);
- }
-
- public function value(array $where, ?string $field = '')
- {
- $pk = $this->getPk();
- return $this->getModel()::where($where)->value($field ?: $pk);
- }
-
- public function getColumn(array $where, string $field, string $key = '', bool $search = false)
- {
- if ($search) {
- return $this->search($where)->column($field, $key);
- } else {
- return $this->getModel()::where($where)->column($field, $key);
- }
- }
-
- public function delete($id, ?string $key = null)
- {
- if (is_array($id)) {
- $where = $id;
- } else {
- $where = [is_null($key) ? $this->getPk() : $key => $id];
- }
- return $this->getModel()::where($where)->delete();
- }
-
- public function update($id, array $data, ?string $key = null)
- {
- if (is_array($id)) {
- $where = $id;
- } else {
- $where = [is_null($key) ? $this->getPk() : $key => $id];
- }
- return $this->getModel()::update($data, $where);
- }
-
- public function batchUpdate(array $ids, array $data, ?string $key = null)
- {
- return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
- }
-
- public function batchUpdateAppendWhere(array $ids, array $data, array $where, ?string $key = null)
- {
- return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->where($where)->update($data);
- }
-
- public function save(array $data)
- {
- return $this->getModel()::create($data);
- }
-
- public function saveAll(array $data)
- {
- return $this->getModel()::insertAll($data);
- }
-
- public function getFieldValue($value, string $filed, ?string $valueKey = '', ?array $where = [])
- {
- return $this->getModel()->getFieldValue($value, $filed, $valueKey, $where);
- }
-
- protected function withSearchSelect(array $withSearch, ?array $data = [])
- {
- [$with] = app()->make(BaseAuth::class)->________($withSearch, $this->setModel());
- return $this->getModel()->withSearch($with, $data);
- }
-
- protected function search(array $where = [])
- {
- if ($where) {
- return $this->withSearchSelect(array_keys($where), $where);
- } else {
- return $this->getModel();
- }
- }
-
- public function sum(array $where, string $field, bool $search = false)
- {
- if ($search) {
- return $this->search($where)->sum($field);
- } else {
- return $this->getModel()::where($where)->sum($field);
- }
- }
-
- public function bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2)
- {
- return $this->bc($key, $incField, $inc, $keyField, 1);
- }
-
- public function bcDec($key, string $decField, string $dec, string $keyField = null, int $acc = 2, bool $dec_return_false = true)
- {
- return $this->bc($key, $decField, $dec, $keyField, 2, $acc, $dec_return_false);
- }
-
- public function bc($key, string $incField, string $inc, string $keyField = null, int $type = 1, int $acc = 2, bool $dec_return_false = true)
- {
- if ($keyField === null) {
- $result = $this->get($key);
- } else {
- $result = $this->getOne([$keyField => $key]);
- }
- if (!$result) return false;
- if ($type === 1) {
- $new = bcadd($result[$incField], $inc, $acc);
- } else if ($type === 2) {
- if ($result[$incField] < $inc) {
- if ($dec_return_false) return false;
- $new = 0;
- } else {
- $new = bcsub($result[$incField], $inc, $acc);
- }
- }
- $result->{$incField} = $new;
- return false !== $result->save();
- }
-
- public function decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
- {
- return app()->make(BaseAuth::class)->_____($this->getModel(), $where, $num, $stock, $sales) !== false;
- }
-
- public function incStockDecSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
- {
- return app()->make(BaseAuth::class)->___($this->getModel(), $where, $num, $stock, $sales) !== false;
- }
-
- public function destroy($id, ?string $key = null)
- {
- if (is_array($id)) {
- $where = $id;
- } else {
- $where = [is_null($key) ? $this->getPk() : $key => $id];
- }
- return $this->getModel()::destroy($where);
- }
-
- public function incUpdate($where, string $field, int $number = 1)
- {
- return $this->getModel()->where(is_array($where) ? $where : [$this->getPk() => $where])->inc($field, $number)->update();
- }
-
- public function decUpdate($where, string $field, int $number = 1)
- {
- return $this->getModel()->where(is_array($where) ? $where : [$this->getPk() => $where])->dec($field, $number)->update();
- }
- }
|