Files
2021-07-05 09:56:27 +08:00

608 lines
16 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: linfan
* Date: 2018/11/1
* Time: 15:10
*/
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class HD_Model extends CI_Model {
private $table_name;
private $mc_cache_expire = '600';
private $file_cache_expire = '86400';
public $db;
static $dbs = array();
public function __construct($table_name = '', $db_group = 'default')
{
//走自定义db
$app_db = $GLOBALS['app_db'];
if($app_db && "default" == $db_group){
$this->load->config("dbtable", true, true);
$dbtable = $this->config->item($app_db, "dbtable");
if(is_array($dbtable) && in_array($table_name, $dbtable)){
$db_group = $GLOBALS['app_db'];
}
}
$this->set_db($db_group);
$this->set_table_name($table_name);
}
public function set_db($db_group = 'default')
{
if(!isset(self::$dbs[$db_group]))
{
self::$dbs[$db_group] = $this->load->database($db_group, true);
}
$this->db = self::$dbs[$db_group];
}
/**
* 根据app应用表设置库
* @param $db_group
* @param $apptabs
*/
public function set_appdb($db_group, $apptabs){
$table_name = $this->table_name;
if(is_array($apptabs) && in_array($table_name, $apptabs)){
$this->set_db($db_group);
}
}
public function set_table_name($table_name)
{
$this->table_name = $table_name;
}
/**
* 表字段列表
* @return mixed
*/
public function fields(){
return $this->db->list_fields($this->table_name);
}
/**
* 添加单条数据
* @param $data
* @return mixed
*/
public function add($data)
{
$result = $this->db->insert($this->table_name, $data);
return $this->db->insert_id() ? $this->db->insert_id() : $result;
}
/**
* 添加多条数据
* @param $data_array
* @return mixed
*/
public function add_batch($data_array)
{
if(is_array($data_array))
{
return $this->db->insert_batch($this->table_name, $data_array);
}
return false;
}
/**
* 修改数据
* @param $data
* @param $where
* @return mixed
*/
public function update($data, $where)
{
foreach($data as $k => $v)
{
if($v == null) {
if(strstr($k, ' = ')) {
$field = explode(' = ', $k);
$this->db->set($field[0], $field[1], FALSE);
unset($data[$k]);
}
}
}
$result = $this->db->update($this->table_name, $data, $where);
return $this->db->affected_rows() ? $this->db->affected_rows() : $result;
}
/**
* 修改数据2:支持order与limit
* @param $data
* @param $where
* @param $order
* @param $limit
* @return mixed
*/
public function update2($data, $where, $order = '', $limit = 0)
{
foreach($data as $k => $v)
{
if($v == null) {
if(strstr($k, ' = ')) {
$field = explode(' = ', $k);
$this->db->set($field[0], $field[1], FALSE);
unset($data[$k]);
}
}
}
if($order){
$this->db->order_by($order);
}
if($limit) {
$this->db->limit($limit);
}
$result = $this->db->update($this->table_name, $data, $where);
return $this->db->affected_rows() ? $this->db->affected_rows() : $result;
}
/**
* 更新或插入数据
* @param $data
* @return mixed
*/
public function replace($data)
{
if($data)
{
$result = $this->db->replace($this->table_name, $data);
return $this->db->affected_rows() ? $this->db->affected_rows() : $result;
}
return false;
}
/**
* 批量插入
* @param $data
* @return CI_DB_active_record
*/
public function replace_batch($data)
{
if($data && is_array($data[0]))
{
$keys = array_keys($data[0]);
$keys = implode(',', $keys);
$values = array();
foreach($data as $item)
{
$item_values = array_values($item);
foreach($item_values as &$v)
{
if(!is_numeric($v))
{
$v = addslashes($v);
$v = "'{$v}'";
}
}
$item_values = implode(',', $item_values);
$values[] = "({$item_values})";
}
$values = implode(',', $values);
$sql = "REPLACE {$this->table_name}({$keys}) VALUES {$values}";
return $this->db->query($sql);
}
}
/**
* 获取单条数据
* @param $where
* @param string $select
* @param object $obj 自定义结果对象
* @return mixed
*/
public function get($where, $select = '', $obj = '')
{
if($select)
{
$this->db->select($select, false);
}
if($obj && file_exists($class = COMMPATH.'libraries/entity/'.ucfirst($obj).'.php'))
{
require_once $class;
if (class_exists(ucfirst($obj)))
{
return $this->db->get_where($this->table_name, $where)->custom_row_object(0, $obj);
}
}
return $this->db->get_where($this->table_name, $where)->row_array();
}
/**
* 获取多条数据集
* @param array $where
* @param string $order
* @param int $page
* @param int $page_size
* @param string $select
* @param object obj
* @return mixed
*/
public function select($where = array(), $order = '', $page = 0, $page_size = 20, $select = '', $obj = '')
{
if($select)
{
$this->db->select($select, false);
}
if($where)
{
$this->db->where($where);
}
if($order)
{
$this->db->order_by($order);
}
if($page)
{
$offset = ($page - 1) * $page_size;
$limit = $page_size;
}
else
{
$offset = null;
$limit = null;
}
$result = $this->db->get($this->table_name, $limit, $offset)->result_array();
if($obj && file_exists($class = APPPATH.'libraries/entity/'.ucfirst($obj).'.php'))
{
require_once $class;
if(class_exists($obj))
{
$result = $this->db->get($this->table_name, $limit, $offset)->custom_result_object($obj);
}
}
return $result;
}
/**
* @param $groupby
* @param array $where
* @param string $order
* @param int $page
* @param int $page_size
* @param string $select
* @param string $obj
* @return mixed
*/
public function select_groupby($groupby, $where = array(), $order = '', $page = 0, $page_size = 20, $select = '', $obj = ''){
$this->db->group_by($groupby);
return $this->select($where, $order, $page, $page_size, $select, $obj);
}
/**
* 获取最大值
* @param $field
* @param array $where
* @return mixed
*/
public function max($field, $where=array())
{
if($where)
{
$this->db->where($where);
}
if(is_array($field))
{
foreach($field as $v)
{
$this->db->select_max($v);
}
}
else
{
$this->db->select_max($field);
}
return $this->db->get($this->table_name)->row_array();
}
/**
* 删除
* @param array $where
* @return mixed
*/
public function delete($where = array())
{
$result = $this->db->delete($this->table_name, $where);
return $this->db->affected_rows() ? $this->db->affected_rows() : $result;
}
/**
* 将获取的数据集组装成map类型
* @param string $map_key
* @param string $map_value
* @param array $where
* @param string $order
* @param int $page
* @param int $page_size
* @param string $select
* @return array
*/
public function map($map_key = 'id', $map_value = '', $where = array(), $order = '', $page = 0, $page_size = 20, $select = '')
{
$map = array();
$list = $this->select($where, $order, $page, $page_size, $select);
if($list)
{
foreach($list as $item)
{
//指定列, map格式为{k:v}
if($map_value)
{
//指定列存在取具体值,格式为{k:v},指定列不存在取全部列,格式为{k:{k1:v1,k2:v2}}
$map[$item[$map_key]] = null !== $item[$map_value] ? $item[$map_value] : $item;
}
else
{//不指定列,表示一个k对应多个v,格式为{k:[{k1:v1,k2:v2},{k1:v1,k2:v2}]}
$map[$item[$map_key]][] = $item;
}
}
}
return $map;
}
/**
* 获取总条数
* @param array $where
* @param string $distinct
* @return mixed
*/
public function count($where = array(), $distinct = '')
{
if($where)
{
$this->db->where($where);
}
if($distinct)
{
$this->db->distinct();
$this->db->select($distinct, false);
}
return $this->db->count_all_results($this->table_name);
}
/**
* 获取某字段总和
* @param $field
* @param array $where
* @return mixed
*/
public function sum($field, $where = array())
{
if(is_array($field))
{
foreach($field as $v)
{
$this->select_sum($v);
}
}
else
{
$this->db->select_sum($field);
}
if($where)
{
$this->db->where($where);
}
return $this->db->get($this->table_name)->row_array();
}
/**
* 对方法的某个结果集做mc缓存【结果集为非对象数组】
* @param $func
* @param array $param
* @param string $ttl
* @return mixed
*/
public function mc_cache($func, $param = array(), $ttl = 0, $skey = '')
{
if(IF_MC_CACHE)
{
$cache = & load_cache('mc');
$cache_key = MC_CACHE_PREFIX.md5(get_called_class().'.'.$func.json_encode($param));
$ttl = is_numeric($ttl) ? $ttl : $this->mc_cache_expire;
$result = $cache->get($cache_key);
if($result === FALSE )
{
$result = call_user_func_array(array($this, $func), $param);
$cache->save($cache_key, $result, $ttl);
$this->add_cache_key(get_called_class(), $func, 'mc', $cache_key, $ttl, $skey);
}
return $result;
}
return call_user_func_array(array($this, $func), $param);
}
/**
* 删除某方法某个结果集的mc缓存【结果集为非对象数组】
* @param $func
* @param array $param
* @return mixed
*/
public function un_mc_cache($func, $param = array())
{
if(IF_MC_CACHE)
{
$cache = & load_cache('mc');
$cache_key = MC_CACHE_PREFIX.md5(get_called_class().'.'.$func.json_encode($param));
return $cache->delete($cache_key);
}
return false;
}
/**
* 对方法的某个结果集做redis缓存【结果集为非对象数组】
* @param $func
* @param array $param
* @param bool $force
* @return array|mixed
*/
public function redis_cache($func,$param = array())
{
if(IF_REDIS_CACHE)
{
$cache = & load_cache('redis');
$cache_key = REDIS_CACHE_PREFIX.md5(get_called_class().'.'.$func.json_encode($param));
if(!$result = $cache->get($cache_key))
{
$result = call_user_func_array(array($this, $func), $param);
$cache->save($cache_key, $result);
$this->add_cache_key(get_called_class(), $func, 'redis', $cache_key);
}
return $result;
}
return call_user_func_array(array($this, $func), $param);
}
/**
* 删除某方法某个结果集的redis缓存【结果集为非对象数组】
* @param $func
* @param array $param
* @return mixed
*/
public function un_redis_cache($func, $param = array())
{
if(IF_REDIS_CACHE)
{
$cache = & load_cache('redis');
$cache_key = REDIS_CACHE_PREFIX.md5(get_called_class().'.'.$func.json_encode($param));
return $cache->delete($cache_key);
}
return false;
}
/**
* 对某方法某个结果集的file缓存【结果集为非对象数组】
* @param $func
* @param array $param
* @return mixed
*/
public function file_cache($func, $param = array())
{
if(IF_FILE_CACHE)
{
$cache = & load_cache('file');
$cache_key = FILE_CACHE_PREFIX.get_called_class().'.'.$func.'_'.md5($param);
if(!$result = $cache->get($cache_key))
{
$result = call_user_func_array(array($this, $func), $param);
$cache->save($cache_key, $result, $this->file_cache_expire);
$this->add_cache_key(get_called_class(), $func, 'file', $cache_key);
}
return $result;
}
return call_user_func_array(array($this, $func), $param);
}
/**
* 删除某方法某个结果集的file缓存【结果集为非对象数组】
* @param $func
* @param array $param
* @return mixed
*/
public function un_file_cache($func, $param =array())
{
if(IF_FILE_CACHE)
{
$cache = & load_cache('file');
$cache_key = FILE_CACHE_PREFIX.get_called_class().'.'.$func.'_'.md5($param);
return $cache->delete($cache_key);
}
}
/**
* 清除cache
* @param $method
* @param $type
* @return bool
*/
public function del_cache($method, $type, $skey = '')
{
$cache = & load_cache($type);
$db = $this->load->database('default', true);
$where = $skey ? array("skey like '{$skey}'" => null) : array('class' => get_called_class(), 'method' => $method, 'cache_type' => $type);
$db->where($where);
$cache_list = $db->get('hd_cache_key')->result_array();
if($cache_list)
{
foreach($cache_list as $v)
{
$cache->delete($v['cache_key']);
}
}
$db->delete('hd_cache_key', $where);
return true;
}
private function add_cache_key($class, $method, $type, $key, $expire_time = 0, $skey = '')
{
$db = $this->load->database('default', true);
$data = array(
'class' => $class,
'method' => $method,
'cache_type' => $type,
'cache_key' => $key,
'expire_time' => time() + $expire_time,
'skey' => $skey ? $skey : ''
);
$result = $db->insert('hd_cache_key', $data);
return $this->db->insert_id() ? $this->db->insert_id() : $result;
}
}