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; } }