304 行
7.0 KiB
PHP
可执行文件
304 行
7.0 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace Common\Model;
|
|
|
|
use Think\Model;
|
|
|
|
//[1.7.90]
|
|
|
|
/**
|
|
* 公共模型
|
|
* Class BaseModel
|
|
* @package Common\Model
|
|
*/
|
|
class BaseModel extends Model
|
|
{
|
|
|
|
/**
|
|
* 添加
|
|
* @param $data
|
|
* @return mixed
|
|
*/
|
|
public function addOrder($data)
|
|
{
|
|
$res = $this->add($data);
|
|
return $res;
|
|
}
|
|
|
|
public function createOrder($data)
|
|
{
|
|
$res = $this->create($data);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 更新
|
|
* @param $map
|
|
* @param $save
|
|
* @return mixed
|
|
*/
|
|
public function updateOrder($map, $save)
|
|
{
|
|
$res = $this->where($map)->save($save);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 获取一条
|
|
* @param array $map
|
|
* @param string $field
|
|
* @return mixed
|
|
*/
|
|
public function getFind($map = array('id' => array('gt', 0)), $field = '*')
|
|
{
|
|
$res = $this->field($field)->where($map)->find();
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 获取一条
|
|
* @param $map
|
|
* @param string $field
|
|
* @return mixed
|
|
*/
|
|
public function getFindTrue($map, $field = '*')
|
|
{
|
|
$res = $this->field($field, true)->where($map)->find();
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 获取一条
|
|
* @param $map
|
|
* @param string $field
|
|
* @param string $order
|
|
* @return mixed
|
|
*/
|
|
public function getOrderFind($map, $field = '*', $order = 'id asc')
|
|
{
|
|
$res = $this->field($field)->where($map)->order($order)->find();
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 查询多条
|
|
* @param $map 查询条件
|
|
* @param string $field 获取的字段类型
|
|
* @param string $order 排序规则
|
|
* @param string $page 分页(英文逗号拼接的两个数字)
|
|
* @return mixed
|
|
*/
|
|
public function getSelect($map, $field = '*', $order = 'id asc', $page = '')
|
|
{
|
|
if ($page) {
|
|
$res = $this->field($field)->where($map)->order($order)->page($page)->select();
|
|
} else {
|
|
$res = $this->field($field)->where($map)->order($order)->select();
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取数量
|
|
* @param $map
|
|
* @return mixed
|
|
*/
|
|
public function getCount($map)
|
|
{
|
|
$res = $this->where($map)->count();
|
|
return (int)$res;
|
|
}
|
|
|
|
/**
|
|
* 获取某字段的sum值
|
|
* @param $map
|
|
* @param $key
|
|
* @return mixed
|
|
*/
|
|
public function getSum($map, $key)
|
|
{
|
|
$res = $this->where($map)->sum($key);
|
|
return $res ? $res : 0;
|
|
}
|
|
|
|
/**
|
|
* 获取某字段
|
|
* @param $map
|
|
* @param string $fields
|
|
* @param $bol //true为获取多条
|
|
* @return mixed
|
|
*/
|
|
public function getFindField($map, $fields = 'id', $bol = false)
|
|
{
|
|
$res = $this->where($map)->getField($fields, $bol);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 获取某字段
|
|
* @param $map
|
|
* @param string $fields
|
|
* @param string $order
|
|
* @param $bol //true为获取多条
|
|
* @return mixed
|
|
*/
|
|
public function getOrderFindField($map, $fields = 'id', $order = 'id asc', $bol = false)
|
|
{
|
|
$res = $this->where($map)->order($order)->getField($fields, $bol);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 某个字段向上增加
|
|
* @param $map 条件
|
|
* @param $key 字段名
|
|
* @param $val 增加的值
|
|
* @return mixed
|
|
*/
|
|
public function toSetInc($map, $key, $val)
|
|
{
|
|
$res = $this->where($map)->setInc($key, $val);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 某个字段向下加减少
|
|
* @param $map 条件
|
|
* @param $key 字段名
|
|
* @param $val 减少的值
|
|
* @return mixed
|
|
*/
|
|
public function toSetDec($map, $key, $val)
|
|
{
|
|
$res = $this->where($map)->setDec($key, $val);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 删除记录
|
|
* @param $map
|
|
* @return mixed
|
|
*/
|
|
public function deleteOrder($map)
|
|
{
|
|
$res = $this->where($map)->delete();
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* group分组查询
|
|
* @param $map
|
|
* @param $group
|
|
* @param string $fields
|
|
* @param bool $bol
|
|
* @return mixed
|
|
*/
|
|
public function getGroupFindField($map, $group, $fields = 'id', $bol = false)
|
|
{
|
|
$res = $this->where($map)->group($group)->getField($fields, $bol);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* group分组并且根据次数限制查询
|
|
* @param $map
|
|
* @param $group
|
|
* @param $having
|
|
* @param string $fields
|
|
* @param bool $bol
|
|
* @return mixed
|
|
*/
|
|
public function getGroupHavingFindField($map, $group, $having, $fields = 'id', $bol = false)
|
|
{
|
|
$res = $this->where($map)->group($group)->having('count(id)>' . $having)->getField($fields, $bol);
|
|
return $res;
|
|
}
|
|
|
|
|
|
/**
|
|
* group分组查询,分页
|
|
* @param $map
|
|
* @param $group
|
|
* @param string $fields
|
|
* @param bool $bol
|
|
* @param string $page
|
|
* @param string $limit
|
|
* @return mixed
|
|
*/
|
|
public function getGroupFindFieldLimit($map, $group, $fields = 'id', $bol = false, $page = '', $limit = '')
|
|
{
|
|
$res = $this->where($map)->group($group)->page($page, $limit)->getField($fields, $bol);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 统计分组数量
|
|
* @param $map
|
|
* @param $group
|
|
* @return mixed
|
|
*/
|
|
public function getGroupCount($map, $group)
|
|
{
|
|
$res = $this->where($map)->count('distinct(' . $group . ')');
|
|
return (int)$res;
|
|
}
|
|
|
|
|
|
/**
|
|
* 查询根据某个字段排序的列表
|
|
* @param $map
|
|
* @param string $group_field
|
|
* @param string $field
|
|
* @param string $page
|
|
* @param string $limit
|
|
* @return mixed
|
|
*/
|
|
public function getGroupSelect($map, $group_field, $field = '*', $page = '', $limit = '')
|
|
{
|
|
$res = $this->field($field)->where($map)->group($group_field)->page($page, $limit)->select();
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 查询根据某个字段最大的key
|
|
* @param $map
|
|
* @param $key
|
|
* @return mixed
|
|
*/
|
|
public function getMax($map, $key)
|
|
{
|
|
$res = $this->where($map)->max($key);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* number_format的别名函数
|
|
* @param int|string $number 要格式化的数字
|
|
* @param int $decimals 规定多少个小数
|
|
* @param string $decimalpoint 规定用作小数点的字符串
|
|
* @param string $separator 规定用作千位分隔符的字符串
|
|
* @return string $return 格式化后的数字
|
|
*/
|
|
function nf($number = 0, $decimals = 2, $decimalpoint = '.', $separator = '')
|
|
{
|
|
if ($decimals < 0) {
|
|
$decimals = 2;
|
|
}
|
|
return number_format($number, $decimals, $decimalpoint, $separator);
|
|
}
|
|
|
|
/**
|
|
* 去掉小数点后面的0和.
|
|
* @param int|string $number 要格式化的数字
|
|
* @param int $decimals
|
|
* @return string
|
|
*/
|
|
function rnf($number, $decimals = 2)
|
|
{
|
|
if ($decimals <= 0) {
|
|
$decimals = 2;
|
|
}
|
|
return rtrim(rtrim($this->nf($number, $decimals), '0'), '.');
|
|
}
|
|
} |