2019-11-01 16:56:35 +08:00

115 lines
4.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------
namespace think\admin\extend;
use think\db\Query;
/**
* 数据处理扩展
* Class DataExtend
* @package think\admin\extend
*/
class DataExtend
{
/**
* 数据增量保存
* @param Query|string $dbQuery 数据查询对象
* @param array $data 需要保存或更新的数据
* @param string $key 条件主键限制
* @param array $where 其它的where条件
* @return boolean|integer
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function save($dbQuery, $data, $key = 'id', $where = [])
{
$app = app();
$db = is_string($dbQuery) ? $app->db->name($dbQuery) : $dbQuery;
list($table, $value) = [$db->getTable(), isset($data[$key]) ? $data[$key] : null];
$map = isset($where[$key]) ? [] : (is_string($value) ? [[$key, 'in', explode(',', $value)]] : [$key => $value]);
if (is_array($info = $app->db->table($table)->master()->where($where)->where($map)->find()) && !empty($info)) {
if ($app->db->table($table)->strict(false)->where($where)->where($map)->update($data) !== false) {
return isset($info[$key]) ? $info[$key] : true;
} else {
return false;
}
} else {
return $app->db->table($table)->strict(false)->insertGetId($data);
}
}
/**
* 一维数据数组生成数据树
* @param array $list 数据列表
* @param string $id 父ID Key
* @param string $pid ID Key
* @param string $son 定义子数据Key
* @return array
*/
public static function arr2tree($list, $id = 'id', $pid = 'pid', $son = 'sub')
{
list($tree, $map) = [[], []];
foreach ($list as $item) $map[$item[$id]] = $item;
foreach ($list as $item) if (isset($item[$pid]) && isset($map[$item[$pid]])) {
$map[$item[$pid]][$son][] = &$map[$item[$id]];
} else $tree[] = &$map[$item[$id]];
unset($map);
return $tree;
}
/**
* 一维数据数组生成数据树
* @param array $list 数据列表
* @param string $id ID Key
* @param string $pid 父ID Key
* @param string $path
* @param string $ppath
* @return array
*/
public static function arr2table(array $list, $id = 'id', $pid = 'pid', $path = 'path', $ppath = '')
{
$tree = [];
foreach (self::arr2tree($list, $id, $pid) as $attr) {
$attr[$path] = "{$ppath}-{$attr[$id]}";
$attr['sub'] = isset($attr['sub']) ? $attr['sub'] : [];
$attr['spt'] = substr_count($ppath, '-');
$attr['spl'] = str_repeat(" ├ ", $attr['spt']);
$sub = $attr['sub'];
unset($attr['sub']);
$tree[] = $attr;
if (!empty($sub)) $tree = array_merge($tree, self::arr2table($sub, $id, $pid, $path, $attr[$path]));
}
return $tree;
}
/**
* 获取数据树子ID
* @param array $list 数据列表
* @param int $id 起始ID
* @param string $key 子Key
* @param string $pkey 父Key
* @return array
*/
public static function getArrSubIds($list, $id = 0, $key = 'id', $pkey = 'pid')
{
$ids = [intval($id)];
foreach ($list as $vo) if (intval($vo[$pkey]) > 0 && intval($vo[$pkey]) === intval($id)) {
$ids = array_merge($ids, self::getArrSubIds($list, intval($vo[$key]), $key, $pkey));
}
return $ids;
}
}