123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\common\model;
- use think\Model;
- use think\db\Query;
- /**
- * 模型基类
- * Class BaseModel
- * @package app\common\model
- */
- class BaseModel extends Model
- {
- // 当前访问的商城ID
- public static $storeId;
- // 定义表名
- protected $name;
- // 模型别名
- protected $alias = '';
- // 定义全局的查询范围
- protected $globalScope = ['store_id'];
- // 是否允许全局查询store_id
- protected $isGlobalScopeStoreId = false;
- // 错误信息
- protected $error = '';
- public static $oss_domain;
- /**
- * 模型基类初始化
- */
- public static function init()
- {
- parent::init();
- // 绑定store_id
- self::getStoreId();
- }
- /**
- * 查找单条记录
- * @param $data
- * @param array $with
- * @return array|static|null
- */
- public static function get($data, $with = [])
- {
- try {
- $query = (new static)->with($with);
- return is_array($data) ? $query->where($data)->find() : $query->find((int)$data);
- } catch (\Exception $e) {
- return false;
- }
- }
- /**
- * 定义全局的查询范围
- * @param Query $query
- * @return bool
- */
- public function scopeStore_id(Query $query)
- {
- if (!$this->isGlobalScopeStoreId) return false;
- $storeId = self::getStoreId();
- $storeId > 0 && $query->where($query->getTable() . '.store_id', $storeId);
- return true;
- }
- /**
- * 获取当前操作的商城ID
- * @return int|null
- */
- private static function getStoreId()
- {
- if (empty(self::$storeId) && in_array(app_name(), ['store', 'api'])) {
- self::$storeId = getStoreId();
- }
- return self::$storeId;
- }
- /**
- * 获取当前调用来源的应用名称
- * 例如:admin, api, store, task
- * @return string|bool
- */
- protected static function getCalledModule()
- {
- if (preg_match('/app\\\(\w+)/', get_called_class(), $class)) {
- return $class[1];
- }
- return 'common';
- }
- /**
- * 设置默认的检索数据
- * @param array $query
- * @param array $default
- * @return array
- */
- protected function setQueryDefaultValue(array $query, array $default = [])
- {
- $data = array_merge($default, $query);
- foreach ($query as $field => $value) {
- // 不存在默认值跳出循环
- if (!isset($default[$field])) continue;
- // 如果传参为空, 设置默认值
- if (empty($value) && $value !== '0') {
- $data[$field] = $default[$field];
- }
- }
- return $data;
- }
- /**
- * 设置基础查询条件(用于简化基础alias和field)
- * @test 2019-4-25
- * @param string $alias
- * @param array $join
- * @return BaseModel
- */
- public function setBaseQuery($alias = '', $join = [])
- {
- // 设置别名
- $aliasValue = $alias ?: $this->alias;
- $query = $this->alias($aliasValue)->field("{$aliasValue}.*");
- // join条件
- if (!empty($join)) : foreach ($join as $item):
- $query->join($item[0], "{$item[0]}.{$item[1]}={$aliasValue}."
- . (isset($item[2]) ? $item[2] : $item[1]));
- endforeach; endif;
- return $query;
- }
- /**
- * 更新数据
- * 重写\think\Model::update方法, 目的可以返回更新的状态bool
- * @access public
- * @param array $data 数据数组
- * @param array $where 更新条件
- * @param array $allowField 允许字段
- * @return bool
- */
- public static function updateBase(array $data, array $where = [], array $allowField = []): bool
- {
- $model = new static;
- if (!empty($allowField)) {
- $model->allowField($allowField);
- }
- if (!empty($where)) {
- $model->setUpdateWhere($where);
- }
- return $model->exists(true)->save($data);
- }
- /**
- * 批量更新数据(支持带where条件)
- * @param iterable $dataSet [0 => ['data'=>[], 'where'=>[]]]
- * @return array|false
- */
- public function updateAll(iterable $dataSet)
- {
- if (empty($dataSet)) {
- return false;
- }
- return $this->transaction(function () use ($dataSet) {
- $result = [];
- foreach ($dataSet as $key => $item) {
- $result[$key] = self::updateBase($item['data'], $item['where']);
- }
- return $result;
- });
- }
- /**
- * 批量新增数据
- * @param iterable $dataSet [0 => ['id'=>10001, 'name'=>'wang']]
- * @return array|false
- */
- public function addAll(iterable $dataSet)
- {
- if (empty($dataSet)) {
- return false;
- }
- return $this->transaction(function () use ($dataSet) {
- $result = [];
- foreach ($dataSet as $key => $item) {
- $result[$key] = self::create($item, $this->field);
- }
- return $result;
- });
- }
- /**
- * 删除记录
- * @param array $where
- * 方式1: ['goods_id' => $goodsId]
- * 方式2: [
- * ['store_user_id', '=', $storeUserId],
- * ['role_id', 'in', $deleteRoleIds]
- * ]
- * @return int
- */
- public static function deleteAll(array $where)
- {
- return (new static)->where($where)->delete();
- }
- /**
- * 返回错误信息
- * @return string
- */
- public function getError()
- {
- return empty($this->error) ? false : $this->error;
- }
- /**
- * 字段值增长
- * @param array|int|bool $where
- * @param string $field
- * @param float $step
- * @return mixed
- */
- protected function setInc($where, string $field, float $step = 1)
- {
- if (is_numeric($where)) {
- $where = [$this->getPk() => (int)$where];
- }
- return $this->where($where)->inc($field, $step)->update();
- }
- /**
- * 字段值消减
- * @param array|int|bool $where
- * @param string $field
- * @param float $step
- * @return mixed
- */
- protected function setDec($where, string $field, float $step = 1)
- {
- if (is_numeric($where)) {
- $where = [$this->getPk() => (int)$where];
- }
- return $this->where($where)->dec($field, $step)->update();
- }
- /**
- * 实例化新查询对象
- * @return \think\db\BaseQuery
- */
- protected function getNewQuery()
- {
- return $this->db();
- }
- /**
- * 新增hidden属性
- * @param array $hidden
- * @return $this
- */
- protected function addHidden(array $hidden)
- {
- $this->hidden = array_merge($this->hidden, $hidden);
- return $this;
- }
- /**
- * 生成字段列表(字段加上$alias别名)
- * @param string $alias 别名
- * @param array $withoutFields 排除的字段
- * @return array
- */
- protected function getAliasFields(string $alias, $withoutFields = [])
- {
- $fields = array_diff($this->getTableFields(), $withoutFields);
- foreach ($fields as &$field) {
- $field = "$alias.$field";
- }
- return $fields;
- }
- /**
- * 多字段增减
- * @param array|int|bool $where
- * @param array $incData //自增字段
- * @param array $decData //自减字段
- * @param array $data //正常update字段
- * @return mixed
- */
- protected function setIncDec($where,array $incData,array $decData,array $data=[])
- {
- if (is_numeric($where)) {
- $where = [$this->getPk() => (int)$where];
- }
- $query = $this->where($where);
- foreach ($incData as $field=>$step){
- $query->inc($field, (float)$step);
- }
- foreach ($decData as $field=>$step){
- $query->dec($field, (float)$step);
- }
- return $query->update($data);
- }
- }
|