| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824 |
- <?php
- /**
- * Niushop商城系统 - 团队十年电商经验汇集巨献!
- * =========================================================
- * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
- * ----------------------------------------------
- * 官方网址: https://www.niushop.com
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
- * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
- * =========================================================
- */
- namespace app\model;
- use think\facade\Db;
- use think\Validate;
- use think\facade\Cache;
- /**
- * 模型基类
- */
- class Model
- {
- // 查询对象
- private static $query_obj = null;
- protected $table = '';
- //验证规则
- protected $rule = [];
- //验证信息
- protected $message = [];
- //验证场景
- protected $scene = [];
- //错误信息
- protected $error;
- protected $is_cache = 1;
- public function __construct($table = '')
- {
- if ($table) {
- $this->table = $table;
- }
- $this->is_cache = $this->isCache();
- }
- public function isCache()
- {
- return 1;
- }
- /**
- * 获取列表数据
- * @param array $condition
- * @param string $field
- * @param string $order
- * @param number $page
- * @param array $join
- * @param string $group
- * @param string $limit
- * @param string $data
- * @return mixed
- */
- final public function getList($condition = [], $field = true, $order = '', $alias = 'a', $join = [], $group = '', $limit = null)
- {
- if ($this->is_cache && empty($join)) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- self::$query_obj = Db::name($this->table)->where($condition)->order($order);
- if (!empty($join)) {
- self::$query_obj->alias($alias);
- self::$query_obj = $this->parseJoin(self::$query_obj, $join);
- }
- if (!empty($group)) {
- self::$query_obj = self::$query_obj->group($group);
- }
- if (!empty($limit)) {
- self::$query_obj = self::$query_obj->limit($limit);
- }
- $result = self::$query_obj->field($field)->select()->toArray();
- self::$query_obj->removeOption();
- if ($this->is_cache && empty($join)) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
- }
- return $result;
- }
- final public function all()
- {
- if ($this->is_cache) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_';
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- $result = Db::name($this->table)->select()->toArray();
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
- }
- return $result;
- }
- /**
- * 获取分页列表数据
- * @param unknown $where
- * @param string $field
- * @param string $order
- * @param number $page
- * @param string $list_rows
- * @param string $alias
- * @param unknown $join
- * @param string $group
- * @param string $limit
- */
- final public function pageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [], $group = null, $limit = null)
- {
- //关联查询多表无法控制不缓存,单独业务处理
- if ($this->is_cache && empty($join)) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- self::$query_obj = Db::name($this->table)->alias($alias)->where($condition)->order($order);
- $count_obj = Db::name($this->table)->alias($alias)->where($condition)->order($order);
- if (!empty($join)) {
- $db_obj = self::$query_obj;
- self::$query_obj = $this->parseJoin($db_obj, $join);
- $count_obj = $this->parseJoin($count_obj, $join);
- }
- if (!empty($group)) {
- self::$query_obj = self::$query_obj->group($group);
- $count_obj = $count_obj->group($group);
- }
- if (!empty($limit)) {
- self::$query_obj = self::$query_obj->limit($limit);
- }
- $count = $count_obj->count();
- if ($list_rows == 0) {
- //查询全部
- $result_data = self::$query_obj->field($field)->limit($count)->page($page)->select()->toArray();
- $result[ 'page_count' ] = 1;
- } else {
- $result_data = self::$query_obj->field($field)->limit($list_rows)->page($page)->select()->toArray();
- $result[ 'page_count' ] = ceil($count / $list_rows);
- }
-
- $result[ 'count' ] = $count;
- $result[ 'list' ] = $result_data;
- self::$query_obj->removeOption();
- if ($this->is_cache && empty($join)) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
- }
- return $result;
- }
- /**
- * 获取分页列表数据
- * @param unknown $where
- * @param string $field
- * @param string $order
- * @param number $page
- * @param string $list_rows
- * @param string $alias
- * @param unknown $join
- * @param string $group
- * @param string $limit
- */
- final public function rawPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [], $group = null, $limit = null)
- {
- //关联查询多表无法控制不缓存,单独业务处理
- if ($this->is_cache && empty($join)) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- if (is_array($order)) {
- self::$query_obj = Db::name($this->table)->alias($alias)->where($condition)->order($order);
- $count_obj = Db::name($this->table)->alias($alias)->where($condition)->order($order);
- } else {
- self::$query_obj = Db::name($this->table)->alias($alias)->where($condition)->orderRaw($order);
- $count_obj = Db::name($this->table)->alias($alias)->where($condition)->orderRaw($order);
- }
- if (!empty($join)) {
- $db_obj = self::$query_obj;
- self::$query_obj = $this->parseJoin($db_obj, $join);
- $count_obj = $this->parseJoin($count_obj, $join);
- }
- if (!empty($group)) {
- self::$query_obj = self::$query_obj->group($group);
- $count_obj = $count_obj->group($group);
- }
- if (!empty($limit)) {
- self::$query_obj = self::$query_obj->limit($limit);
- }
- $count = $count_obj->count();
- if ($list_rows == 0) {
- //查询全部
- $result_data = self::$query_obj->field($field)->limit($count)->page($page)->select()->toArray();
- $result[ 'page_count' ] = 1;
- } else {
- $result_data = self::$query_obj->field($field)->limit($list_rows)->page($page)->select()->toArray();
- $result[ 'page_count' ] = ceil($count / $list_rows);
- }
- $result[ 'count' ] = $count;
- $result[ 'list' ] = $result_data;
- self::$query_obj->removeOption();
- if ($this->is_cache && empty($join)) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
- }
- return $result;
- }
- /**
- * 获取单条数据
- * @param array $where
- * @param string $field
- * @param string $join
- * @param string $data
- * @return mixed
- */
- final public function getInfo($where = [], $field = true, $alias = 'a', $join = null, $data = null)
- {
- //关联查询多表无法控制不缓存,单独业务处理
- if ($this->is_cache && empty($join)) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- if (empty($join)) {
- $result = Db::name($this->table)->where($where)->field($field)->find($data);
- } else {
- $db_obj = Db::name($this->table)->alias($alias);
- $db_obj = $this->parseJoin($db_obj, $join);
- $result = $db_obj->where($where)->field($field)->find($data);
- }
- if ($this->is_cache && empty($join)) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
- }
- return $result;
- }
- /**
- * join分析
- * @access protected
- * @param array $join
- * @param array $options 查询条件
- * @return string
- */
- protected function parseJoin($db_obj, $join)
- {
- foreach ($join as $item) {
- list($table, $on, $type) = $item;
- $type = strtolower($type);
- switch ( $type ) {
- case "left":
- $db_obj = $db_obj->leftJoin($table, $on);
- break;
- case "inner":
- $db_obj = $db_obj->join($table, $on);
- break;
- case "right":
- $db_obj = $db_obj->rightjoin($table, $on);
- break;
- case "full":
- $db_obj = $db_obj->fulljoin($table, $on);
- break;
- default:
- break;
- }
- }
- return $db_obj;
- }
- /**
- * /**
- * 获取某个列的数组
- * @param array $where 条件
- * @param string $field 字段名 多个字段用逗号分隔
- * @param string $key 索引
- * @return array
- */
- final public function getColumn($where = [], $field = '', $key = '')
- {
- if ($this->is_cache) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- $result = Db::name($this->table)->where($where)->column($field, $key);
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
- }
- return $result;
- }
- /**
- * 得到某个字段的值
- * @access public
- * @param array $where 条件
- * @param string $field 字段名
- * @param mixed $default 默认值
- * @param bool $force 强制转为数字类型
- * @return mixed
- */
- final public function getValue($where = [], $field = '', $default = null, $force = false)
- {
- if ($this->is_cache) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- $result = Db::name($this->table)->where($where)->value($field, $default, $force);
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
- }
- return $result;
- }
- /**
- * 新增数据
- * @param array $data 数据
- * @param boolean $is_return_pk 返回自增主键
- */
- final public function add($data = [], $is_return_pk = true)
- {
- $res = Db::name($this->table)->insert($data, true, $is_return_pk);
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->clear();
- }
- return $res;
- }
- /**
- * 新增多条数据
- * @param array $data 数据
- * @param int $limit 限制插入行数
- */
- final public function addList($data = [], $limit = null)
- {
- $res = Db::name($this->table)->insertAll($data, false, $limit);
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->clear();
- }
- return $res;
- }
- /**
- * 更新数据
- * @param array $where 条件
- * @param array $data 数据
- */
- final public function update($data = [], $where = [])
- {
- $res = Db::name($this->table)->where($where)->update($data);
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->clear();
- }
- return $res;
- }
- /**
- * 设置某个字段值
- * @param array $where 条件
- * @param string $field 字段
- * @param string $value 值
- */
- final public function setFieldValue($where = [], $field = '', $value = '')
- {
- $res = $this->update([ $field => $value ], $where);
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->clear();
- }
- return $res;
- }
- /**
- * 设置数据列表
- * @param array $data_list 数据
- * @param boolean $replace 是否自动识别更新和写入
- */
- final public function setList($data_list = [], $replace = false)
- {
- $res = Db::name($this->table)->saveAll($data_list, $replace);
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->clear();
- }
- return $res;
- }
- /**
- * 删除数据
- * @param array $where 条件
- */
- final public function delete($where = [])
- {
- $res = Db::name($this->table)->where($where)->delete();
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->clear();
- }
- return $res;
- }
- /**
- * 统计数据
- * @param array $where 条件
- * @param string $type 查询类型 count:统计数量|max:获取最大值|min:获取最小值|avg:获取平均值|sum:获取总和
- */
- final public function stat($where = [], $type = 'count', $field = 'id')
- {
- if ($this->is_cache) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- $result = Db::name($this->table)->where($where)->$type($field);
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
- }
- return $result;
- }
- /**
- * SQL查询
- * @param string $sql
- * @return mixed
- */
- final public function query($sql = '')
- {
- return Db::query($sql);
- }
- /**
- * 返回总数
- * @param unknown $where
- */
- final public function getCount($where = [], $field = '*', $alias = 'a', $join = null, $group = null)
- {
- if ($this->is_cache && empty($join)) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- if (empty($join)) {
- if (empty($group)) {
- $result = Db::name($this->table)->where($where)->count($field);
- } else {
- $result = Db::name($this->table)->group($group)->where($where)->count($field);
- }
- } else {
- $db_obj = Db::name($this->table)->alias($alias);
- $db_obj = $this->parseJoin($db_obj, $join);
- if (empty($group)) {
- $result = $db_obj->where($where)->count($field);
- } else {
- $result = $db_obj->group($group)->where($where)->count($field);
- }
- }
- if ($this->is_cache && empty($join)) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
- }
- return $result;
- }
- /**
- * 返回总数
- * @param unknown $where
- */
- final public function getSum($where = [], $field = '', $alias = 'a', $join = null)
- {
- if ($this->is_cache && empty($join)) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- if (empty($join)) {
- $result = Db::name($this->table)->where($where)->sum($field);
- } else {
- $db_obj = Db::name($this->table)->alias($alias);
- $db_obj = $this->parseJoin($db_obj, $join);
- $result = $db_obj->where($where)->sum($field);
- }
- if ($this->is_cache && empty($join)) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
- }
- return $result;
- }
- /**
- * SQL执行,注意只能处理查询问题,如果执行修改需要手动删除缓存
- */
- final public function execute($sql = '')
- {
- return Db::execute($sql);
- }
- /**
- * 查询第一条数据
- * @param array $condition
- */
- final function getFirstData($condition, $field = '*', $order = "")
- {
- if ($this->is_cache) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- $data = Db::name($this->table)->where($condition)->order($order)->field($field)->find();
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $data);
- }
- return $data;
- }
- /**
- * 查询第一条数据
- * @param array $condition
- */
- final function getFirstDataView($condition, $field = '*', $order = "", $alias = 'a', $join = [], $group = null)
- {
- if ($this->is_cache && empty($join)) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- self::$query_obj = Db::name($this->table)->alias($alias)->where($condition)->order($order)->field($field);
- if (!empty($join)) {
- $db_obj = self::$query_obj;
- self::$query_obj = $this->parseJoin($db_obj, $join);
- }
- if (!empty($group)) {
- self::$query_obj = self::$query_obj->group($group);
- }
- $data = self::$query_obj->find();
- if ($this->is_cache && empty($join)) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $data);
- }
- return $data;
- }
- /**
- * 验证
- * @param array $data
- * @param string $scene_name
- * @return array[$code, $error]
- */
- public function fieldValidate($data, $scene_name = '')
- {
- $validate = new Validate($this->rule, $this->message);
- if (empty($scene_name)) {
- $validate_result = $validate->batch(false)->check($data);
- } else {
- $validate->scene($this->scene);
- $validate_result = $validate->scene($scene_name)->batch(false)->check($data);
- }
- return $validate_result ? [ true, '' ] : [ false, $validate->getError() ];
- }
- /**
- * 事物开启
- */
- final public function startTrans()
- {
- return Db::startTrans();
- }
- /**
- * 事物提交
- */
- final public function commit()
- {
- return Db::commit();
- }
- /**
- * 事物回滚
- */
- final public function rollback()
- {
- Cache::clear();
- return Db::rollback();
- }
- /**
- * 获取错误信息
- */
- final public function getError()
- {
- return $this->error;
- }
- /**
- * 自增数据
- * @param array $where
- * @param $field
- * @param int $num
- * @return int
- * @throws \think\db\exception\DbException
- */
- final public function setInc($where, $field, $num = 1)
- {
- $res = Db::name($this->table)->where($where)->inc($field, $num)->update();
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->clear();
- }
- return $res;
- }
- /**
- * 自减数据
- * @param $where
- * @param $field
- * @param int $num
- * @return int
- * @throws \think\db\exception\DbException
- */
- final public function setDec($where, $field, $num = 1)
- {
- $res = Db::name($this->table)->where($where)->dec($field, $num)->update();
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->clear();
- }
- return $res;
- }
- /**
- * 获取最大值
- * @param array $where
- * @param $field
- * @return mixed
- */
- final public function getMax($where, $field)
- {
- if ($this->is_cache) {
- $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
- $cache = Cache::get($cache_name);
- if (!empty($cache)) {
- return $cache;
- }
- }
- $data = Db::name($this->table)->where($where)->max($field);
- if ($this->is_cache) {
- Cache::tag("cache_table" . $this->table)->set($cache_name, $data);
- }
- return $data;
- }
- /**
- * 获取分页列表数据 只是单纯的实现部分功能 其他使用还是用pageList吧
- * @param unknown $where
- * @param string $field
- * @param string $order
- * @param number $page
- * @param string $list_rows
- * @param string $alias
- * @param unknown $join
- * @param string $group
- * @param string $limit
- */
- final public function Lists($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [], $group = null, $limit = null,$gwhere='')
- {
- // self::$query_obj = Db::name($this->table)->alias($alias)->where($condition);
- // $count_obj = Db::name($this->table)->alias($alias)->where($condition);
- if(empty($gwhere)){
- self::$query_obj = Db::name($this->table)->alias($alias)->where($condition) ;
- $count_obj = Db::name($this->table)->alias($alias)->where($condition) ;
- }else{
- self::$query_obj = Db::name($this->table)->alias($alias)->where($condition)->where($gwhere) ;
- $count_obj = Db::name($this->table)->alias($alias)->where($condition)->where($gwhere) ;
- }
- if (!empty($join)) {
- $db_obj = self::$query_obj;
- self::$query_obj = $this->parseJoin($db_obj, $join);
- $count_obj = $this->parseJoin($count_obj, $join);
- }
- if (!empty($group)) {
- self::$query_obj = self::$query_obj->group($group);
- $count_obj = $count_obj->group($group);
- }
- if (!empty($limit)) {
- self::$query_obj = self::$query_obj->limit($limit);
- }
- $count = $count_obj->count();
- if ($list_rows == 0) {
- //查询全部
- $result_data = self::$query_obj->field($field)->order($order)->limit($count)->page($page)->select()->toArray();
- $result[ 'page_count' ] = 1;
- } else {
- $result_data = self::$query_obj->field($field)->order($order)->limit($list_rows)->page($page)->select()->toArray();
- $result[ 'page_count' ] = ceil($count / $list_rows);
- }
-
- $result[ 'count' ] = $count;
- $result[ 'list' ] = $result_data;
- self::$query_obj->removeOption();
- return $result;
- }
- /**
- * 不读取缓存--获取单条数据
- * @param array $where
- * @param string $field
- * @param string $join
- * @param string $data
- * @return mixed
- */
- final public function getInfoTo($where = [], $field = true, $alias = 'a', $join = null, $data = null)
- {
- if (empty($join)) {
- $result = Db::name($this->table)->where($where)->field($field)->find($data);
- } else {
- $db_obj = Db::name($this->table)->alias($alias);
- $db_obj = $this->parseJoin($db_obj, $join);
- $result = $db_obj->where($where)->field($field)->find($data);
- }
- return $result;
- }
- public function setIsCache($is_cache = 1){
- $this->is_cache = $is_cache;
- return $this;
- }
- }
|