123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- <?php
- namespace app\model;
- use think\facade\Db;
- use think\Validate;
- class Model
- {
-
-
- private static $query_obj = null;
-
- protected $rule = [];
-
- protected $message = [];
-
- protected $scene = [];
-
- protected $error;
-
- public function __construct($table = '') {
- if ($table) {
- $this->table = $table;
- }
- }
-
-
- final public function getList($condition = [], $field = true, $order = '', $alias = 'a', $join = [], $group = '', $limit = null)
- {
- 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();
- return $result;
- }
-
- final public function all(){
- return Db::name($this->table)->select()->toArray();
- }
-
-
- final public function pageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [], $group = null, $limit = null){
- 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();
- return $result;
- }
-
-
- final public function rawPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [], $group = null, $limit = null){
- 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();
- return $result;
- }
-
-
- final public function getInfo($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;
- }
-
-
- 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;
- }
-
-
- final public function getColumn($where = [], $field = '', $key = '')
- {
- return Db::name($this->table)->where($where)->column($field, $key);
- }
-
-
- final public function getValue($where = [], $field = '', $default = null, $force = false)
- {
- return Db::name($this->table)->where($where)->value($field, $default, $force)->toArray();
- }
-
-
- final public function add($data = [], $is_return_pk = true)
- {
- return Db::name($this->table)->insert($data, true, $is_return_pk);
- }
-
-
- final public function addList($data = [], $limit = null)
- {
- return Db::name($this->table)->insertAll($data, false, $limit);
- }
-
-
- final public function update($data = [], $where = [])
- {
- return Db::name($this->table)->where($where)->update($data);
- }
-
-
- final public function setFieldValue($where = [], $field = '', $value = '')
- {
- return $this->update([$field => $value], $where);
- }
-
-
- final public function setList($data_list = [], $replace = false)
- {
- return Db::name($this->table)->saveAll($data_list, $replace);
- }
-
-
- final public function delete($where = [])
- {
- return Db::name($this->table)->where($where)->delete();
- }
-
-
- final public function stat($where = [], $type = 'count', $field = 'id')
- {
- return Db::name($this->table)->where($where)->$type($field);
- }
-
- final public function query($sql = '')
- {
-
- return Db::query($sql);
- }
-
-
- final public function getCount($where = [], $field = '*')
- {
- return Db::name($this->table)->where($where)->count($field);
- }
-
-
- final public function getSum($where = [], $field = '')
- {
- return Db::name($this->table)->where($where)->sum($field);
- }
-
-
- final public function execute($sql = '')
- {
- return Db::execute($sql);
- }
-
-
- final function getFirstData($condition, $field='*',$order="" )
- {
- $data = Db::name($this->table)->where($condition)->order($order)->field($field)->find();
- return $data;
- }
-
-
- 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(){
-
- return Db::rollback();
- }
-
-
- final public function getError(){
- return $this->error;
- }
-
-
- final public function setInc($where = [], $field, $num = 1)
- {
- return Db::name($this->table)->where($where)->inc($field, $num)->update();
- }
-
- final public function setDec($where = [], $field, $num = 1)
- {
- return Db::name($this->table)->where($where)->dec($field, $num)->update();
- }
-
- final public function getMax($where = [], $field){
- return Db::name($this->table)->where($where)->max($field);
- }
- }
|