| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 | 
							- <?php
 
- // +----------------------------------------------------------------------
 
- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
 
- // +----------------------------------------------------------------------
 
- // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
 
- // +----------------------------------------------------------------------
 
- // | Author: pl125 <xskjs888@163.com>
 
- // +----------------------------------------------------------------------
 
- namespace huo\model\common;
 
- use huolib\constant\CommonConst;
 
- use think\Loader;
 
- use think\Model;
 
- class CommonModel extends Model {
 
-     //  关联模型过滤
 
-     protected $relationFilter = [];
 
-     public static function className() {
 
-         return get_called_class();
 
-     }
 
-     /**
 
-      * @param array $params
 
-      *
 
-      * @return array|false|\PDOStatement|string|\think\Collection|Model
 
-      * @throws \think\db\exception\DataNotFoundException
 
-      * @throws \think\db\exception\ModelNotFoundException
 
-      * @throws \think\exception\DbException
 
-      */
 
-     public function getDatas($params = []) {
 
-         if (empty($params)) {
 
-             return $this->select();
 
-         }
 
-         $this->setCondition($params);
 
-         if (!empty($params['id'])) {
 
-             $datas = $this->find();
 
-         } else {
 
-             $datas = $this->select();
 
-         }
 
-         if (!empty($params['relation'])) {
 
-             $allowedRelations = $this->allowedRelations($params['relation']);
 
-             if (!empty($allowedRelations)) {
 
-                 if (!empty($params['id'])) {
 
-                     if (!empty($datas)) {
 
-                         $datas->append($allowedRelations);
 
-                     }
 
-                 } else {
 
-                     if (count($datas) > 0) {
 
-                         $datas->load($allowedRelations);
 
-                         $datas->append($allowedRelations);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         return $datas;
 
-     }
 
-     /**
 
-      * @access public
 
-      *
 
-      * @param array $params 过滤参数
 
-      *
 
-      * @return $this
 
-      */
 
-     public function setCondition($params) {
 
-         if (empty($params)) {
 
-             return $this;
 
-         }
 
-         if (!empty($params['relation'])) {
 
-             $allowedRelations = $this->allowedRelations($params['relation']);
 
-             if (!empty($allowedRelations)) {
 
-                 if (!empty($params['id']) && count($allowedRelations) == 1) {
 
-                     $this->paramsFilter($params);
 
-                 } else {
 
-                     $this->paramsFilter($params);//->with($allowedRelations);
 
-                 }
 
-             }
 
-         } else {
 
-             $this->paramsFilter($params);
 
-         }
 
-         return $this;
 
-     }
 
-     public function orderFilter($order = []) {
 
-         if (empty($order)) {
 
-             return '';
 
-         }
 
-         $orderWhere = [];
 
-         $_order = $this->strToArr($order);
 
-         foreach ($_order as $key => $value) {
 
-             $upDwn = substr($value, 0, 1);
 
-             $orderType = $upDwn == '-' ? 'desc' : 'asc';
 
-             $orderField = substr($value, 1);
 
-             $orderWhere[$orderField] = $orderType;
 
-         }
 
-         return $orderWhere;
 
-     }
 
-     /**
 
-      * @access public
 
-      *
 
-      * @param array $params 过滤参数
 
-      * @param model $model  关联模型
 
-      *
 
-      * @return model|array  $this|链式查询条件数组
 
-      */
 
-     public function paramsFilter($params, $model = null) {
 
-         if (!empty($model)) {
 
-             $_this = $model;
 
-         } else {
 
-             $_this = $this;
 
-         }
 
-         if (isset($_this->visible)) {
 
-             $whiteParams = $_this->visible;
 
-         }
 
-         // 设置field字段过滤
 
-         if (!empty($params['field'])) {
 
-             $filterParams = $this->strToArr($params['field']);
 
-             if (!empty($whiteParams)) {
 
-                 $mixedField = array_intersect($filterParams, $whiteParams);
 
-             } else {
 
-                 $mixedField = $filterParams;
 
-             }
 
-             if (!empty($mixedField)) {
 
-                 $_this->field($mixedField);
 
-             }
 
-         }
 
-         // 设置id,ids
 
-         if (!empty($params['ids'])) {
 
-             $ids = $this->strToArr($params['ids']);
 
-             foreach ($ids as $key => $value) {
 
-                 $ids[$key] = intval($value);
 
-             }
 
-         }
 
-         if (!empty($params['id'])) {
 
-             $id = intval($params['id']);
 
-             if (!empty($id)) {
 
-                 return $_this->where('id', $id);
 
-             }
 
-         } elseif (!empty($ids)) {
 
-             $_this->where('id', 'in', $ids);
 
-         }
 
-         if (!empty($params['where'])) {
 
-             if (empty($model)) {
 
-                 $_this->where($params['where']);
 
-             }
 
-         }
 
-         // 设置分页
 
-         if (!empty($params['page'])) {
 
-             $pageArr = $this->strToArr($params['page']);
 
-             $page = [];
 
-             foreach ($pageArr as $value) {
 
-                 $page[] = intval($value);
 
-             }
 
-             if (count($page) == 1) {
 
-                 $_this->page($page[0]);
 
-             } elseif (count($page) == 2) {
 
-                 $_this->page($page[0], $page[1]);
 
-             }
 
-         } elseif (!empty($params['limit'])) { // 设置limit查询
 
-             $limitArr = $this->strToArr($params['limit']);
 
-             $limit = [];
 
-             foreach ($limitArr as $value) {
 
-                 $limit[] = intval($value);
 
-             }
 
-             if (count($limit) == 1) {
 
-                 $_this->limit($limit[0]);
 
-             } elseif (count($limit) == 2) {
 
-                 $_this->limit($limit[0], $limit[1]);
 
-             }
 
-         } else {
 
-             $_this->limit(10);
 
-         }
 
-         //设置排序
 
-         if (!empty($params['order'])) {
 
-             $order = $this->strToArr($params['order']);
 
-             foreach ($order as $key => $value) {
 
-                 $upDwn = substr($value, 0, 1);
 
-                 $orderType = $upDwn == '-' ? 'desc' : 'asc';
 
-                 $orderField = substr($value, 1);
 
-                 if (!empty($whiteParams)) {
 
-                     if (in_array($orderField, $whiteParams)) {
 
-                         $orderWhere[$orderField] = $orderType;
 
-                     }
 
-                 } else {
 
-                     $orderWhere[$orderField] = $orderType;
 
-                 }
 
-             }
 
-             if (!empty($orderWhere)) {
 
-                 $_this->order($orderWhere);
 
-             }
 
-         }
 
-         return $_this;
 
-     }
 
-     /**
 
-      * 设置链式查询
 
-      *
 
-      * @access public
 
-      *
 
-      * @param array $params 链式查询条件
 
-      * @param model $model  模型
 
-      *
 
-      * @return $this
 
-      */
 
-     public function setParamsQuery($params, $model = null) {
 
-         if (!empty($model)) {
 
-             $_this = $model;
 
-         } else {
 
-             $_this = $this;
 
-         }
 
-         $_this->alias('articles');
 
-         if (!empty($params['field'])) {
 
-             $_this->field($params['field']);
 
-         }
 
-         if (!empty($params['ids'])) {
 
-             $_this->where('articles.id', $params['ids'][1], $params['ids'][2]);
 
-         }
 
-         if (!empty($params['limit'])) {
 
-             $_this->limit($params['limit']);
 
-         }
 
-         if (!empty($params['page'])) {
 
-             $_this->page($params['page']);
 
-         }
 
-         if (!empty($params['order'])) {
 
-             $_this->order($params['order']);
 
-         }
 
-         return $_this;
 
-     }
 
-     public function allowedRelations($relations) {
 
-         if (is_string($relations)) {
 
-             $relations = explode(',', $relations);
 
-         }
 
-         if (!is_array($relations)) {
 
-             return false;
 
-         }
 
-         return array_intersect($this->relationFilter, $relations);
 
-     }
 
-     /**
 
-      * 是否允许关联
 
-      *
 
-      * @access public
 
-      *
 
-      * @param string $relationName 模型关联方法名
 
-      *
 
-      * @return boolean
 
-      */
 
-     public function isWhite($relationName) {
 
-         if (!is_string($relationName)) {
 
-             return false;
 
-         }
 
-         $name = Loader::parseName($relationName, 1, false);
 
-         if (in_array($name, $this->relationFilter)) {
 
-             return true;
 
-         } else {
 
-             return false;
 
-         }
 
-     }
 
-     /**
 
-      * 懒人函数
 
-      *
 
-      * @access public
 
-      *
 
-      * @param string $string 字符串
 
-      *
 
-      * @return array
 
-      */
 
-     public function strToArr($string) {
 
-         return is_string($string) ? explode(',', $string) : $string;
 
-     }
 
-     /**
 
-      * 懒人函数
 
-      *
 
-      * @access public
 
-      *
 
-      * @param array $arr 数组
 
-      *
 
-      * @return string
 
-      */
 
-     public function arrToStr($arr) {
 
-         return is_array($arr) ? implode(',', $arr) : $arr;
 
-     }
 
-     /**
 
-      * @param array      $data
 
-      * @param string|int $key
 
-      * @param string     $default
 
-      *
 
-      * @return string
 
-      */
 
-     protected function getVal($data, $key, $default = '') {
 
-         if (empty($key) || empty($data) || !isset($data[$key])) {
 
-             return $default;
 
-         }
 
-         return $data[$key];
 
-     }
 
-     /**
 
-      * 计算在哪张表
 
-      *
 
-      * @return \Think\Model
 
-      */
 
-     public function computeTable() {
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param      $data
 
-      * @param bool $replace
 
-      * @param bool $get_last_insert_id
 
-      *
 
-      * @return bool|int|string
 
-      */
 
-     public function insertLog($data, $replace = false, $get_last_insert_id = true) {
 
-         if ($_id = $this->computeTable()->insert($data, $replace, $get_last_insert_id)) {
 
-             return $_id;
 
-         } else {
 
-             return false;
 
-         }
 
-     }
 
-     /**
 
-      * 获取单条记录
 
-      *
 
-      * @param int $id
 
-      *
 
-      * @return array|false
 
-      */
 
-     public function getInfoById($id) {
 
-         if (isset($this->pk)) {
 
-             $_map[$this->pk] = $id;
 
-         } else {
 
-             $_map['id'] = $id;
 
-         }
 
-         $_info = $this->where($_map)->find();
 
-         if (false === $_info) {
 
-             return false;
 
-         }
 
-         if (is_object($_info)) {
 
-             return $_info->toArray();
 
-         } else {
 
-             return $_info;
 
-         }
 
-     }
 
-     /**当前模型名称(数据库表名,不带前缀)
 
-      *
 
-      * @return string
 
-      */
 
-     public function getName() {
 
-         return $this->name;
 
-     }
 
-     /**
 
-      * 校验字段
 
-      *
 
-      * @return bool
 
-      */
 
-     protected function checkField() {
 
-         return true;
 
-     }
 
-     /**
 
-      * 添加数据
 
-      *
 
-      * @param array $data 需要添加的数据
 
-      *
 
-      * @return false|int  添加失败返回 false 添加成功 返回添加的ID
 
-      */
 
-     public function parentAddData($data) {
 
-         if (empty($data)) {
 
-             return false;
 
-         }
 
-         $_data = $data;
 
-         $_model = new static();
 
-         $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
 
-         if (false !== $_rs) {
 
-             return $_model->getLastInsID();
 
-         }
 
-         return false;
 
-     }
 
-     /**
 
-      * 通过ID获取信息
 
-      *
 
-      * @param int $id 主键ID
 
-      *
 
-      * @return array|false
 
-      */
 
-     public function parentGetInfoById($id) {
 
-         $_map[$this->pk] = $id;
 
-         $_info = $this->useGlobalScope(false)->where($_map)->find();
 
-         if (false === $_info) {
 
-             return false;
 
-         }
 
-         if (is_object($_info)) {
 
-             return $_info->toArray();
 
-         } else {
 
-             return $_info;
 
-         }
 
-     }
 
-     /**
 
-      * 更新数据
 
-      *
 
-      * @param array     $data 数据
 
-      * @param int|array $id   主集ID
 
-      *
 
-      * @return bool
 
-      */
 
-     public function parentUpdateData($data, $id) {
 
-         $this->checkField();
 
-         if (is_array($id)) {
 
-             $_map[$this->pk] = ['in', $id];
 
-         } else {
 
-             $_map[$this->pk] = $id;
 
-         }
 
-         $_data = $data;
 
-         $_model = new static();
 
-         $_rs = $_model->allowField(true)->isUpdate(true)->save($_data, $_map);
 
-         if (false === $_rs) {
 
-             return false;
 
-         } else {
 
-             return true;
 
-         }
 
-     }
 
-     /**
 
-      * 删除数据
 
-      *
 
-      * @param array|int $ids         ID合集
 
-      * @param bool      $is_complete 是否完成删除
 
-      *
 
-      * @return bool|int
 
-      */
 
-     public function parentDeleteData($ids, $is_complete = false) {
 
-         $this->checkField();
 
-         if (true == is_array($ids)) {
 
-             $_ids = $ids;
 
-         } else {
 
-             $_ids = [$ids];
 
-         }
 
-         $_map[$this->pk] = ['in', $_ids];
 
-         if (true == $is_complete) {
 
-             /* 彻底删除 */
 
-             return $this->useGlobalScope(false)->where($_map)->delete();
 
-         } else {
 
-             $_data['is_delete'] = CommonConst::CONST_DELETED;
 
-             $_data['delete_time'] = time();
 
-             foreach ($_ids as $_id) {
 
-                 $_rs = self::parentUpdateData($_data, $_id);
 
-                 if (false === $_rs) {
 
-                     return false;
 
-                 }
 
-             }
 
-             return true;
 
-         }
 
-     }
 
-     /**
 
-      * 通过条件获取数量
 
-      *
 
-      * @param array $where 条件
 
-      *
 
-      * @return int
 
-      */
 
-     public function parentGetCnt($where = []) {
 
-         $_map = $where;
 
-         $_cnt = $this->useGlobalScope(false)->where($_map)->count();
 
-         if (empty($_cnt)) {
 
-             return 0;
 
-         }
 
-         return $_cnt;
 
-     }
 
-     /**
 
-      * 通过条件获取汇总
 
-      *
 
-      * @param string $field 字段
 
-      * @param array  $where 条件
 
-      *
 
-      * @return double
 
-      */
 
-     public function parentGetSum($field, $where = []) {
 
-         $_map = $where;
 
-         $_sum = $this->useGlobalScope(false)->where($_map)->sum($field);
 
-         if (false == $_sum) {
 
-             return false;
 
-         }
 
-         return $_sum;
 
-     }
 
-     /**
 
-      * 添加数据
 
-      *
 
-      * @param array $data 需要添加的数据
 
-      *
 
-      * @return false|int  添加失败返回 false 添加成功 返回添加的ID
 
-      */
 
-     public function addData($data) {
 
-         if (empty($data)) {
 
-             return false;
 
-         }
 
-         $_data = $data;
 
-         $_model = new static();
 
-         $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
 
-         if (false !== $_rs) {
 
-             return $_model->getLastInsID();
 
-         }
 
-         return false;
 
-     }
 
-     /**
 
-      * 更新数据
 
-      *
 
-      * @param array     $data 数据
 
-      * @param int|array $id   主集ID
 
-      *
 
-      * @return bool
 
-      */
 
-     public function updateData($data, $id) {
 
-         if (is_array($id)) {
 
-             $_map[$this->pk] = ['in', $id];
 
-         } else {
 
-             $_map[$this->pk] = $id;
 
-         }
 
-         $_data = $data;
 
-         $_model = new static();
 
-         $_rs = $_model->allowField(true)->isUpdate(true)->save($_data, $_map);
 
-         if (false === $_rs) {
 
-             return false;
 
-         } else {
 
-             return true;
 
-         }
 
-     }
 
-     /**
 
-      * 删除数据
 
-      *
 
-      * @param array|int $ids         ID合集
 
-      * @param bool      $is_complete 是否完成删除
 
-      *
 
-      * @return bool|int
 
-      */
 
-     public function deleteData($ids, $is_complete = false) {
 
-         if (true == is_array($ids)) {
 
-             $_ids = $ids;
 
-         } else {
 
-             $_ids = [$ids];
 
-         }
 
-         $_map[$this->pk] = ['in', $_ids];
 
-         if (true == $is_complete) {
 
-             /* 彻底删除 */
 
-             return $this->useGlobalScope(false)->where($_map)->delete();
 
-         } else {
 
-             $_data['is_delete'] = CommonConst::CONST_DELETED;
 
-             $_data['delete_time'] = time();
 
-             foreach ($_ids as $_id) {
 
-                 $_rs = self::updateData($_data, $_id);
 
-                 if (false === $_rs) {
 
-                     return false;
 
-                 }
 
-             }
 
-             return true;
 
-         }
 
-     }
 
- }
 
 
  |