internal static string GetLayer(TableResolver resolver, DbTreeDefinition tree, string parentId) { TkDbContext context = resolver.Context; IFieldInfo layerField = resolver.GetFieldInfo(tree.LayerField); IParamBuilder fixBuilder = resolver.CreateFixCondition(); string execRootId = tree.ExecuteRootId; if (execRootId == parentId) { string subStringSql = context.ContextConfig.GetFunction("SubString", layerField.FieldName, 1, 3); string sql = string.Format(ObjectUtil.SysCulture, "SELECT MAX({0}) FROM {1}", subStringSql, resolver.TableName); string value = (fixBuilder == null ? DbUtil.ExecuteScalar(sql, context) : DbUtil.ExecuteScalar(sql, context, fixBuilder)).ToString(); if (string.IsNullOrEmpty(value)) { return("000"); } else { return(AddLayer(value)); } } else { try { string sql = string.Format(ObjectUtil.SysCulture, "SELECT {0} FROM {1}", layerField.FieldName, resolver.TableName); IParamBuilder builder = ParamBuilder.CreateParamBuilder(fixBuilder, SqlParamBuilder.CreateEqualSql(context, resolver.GetFieldInfo(tree.IdField), parentId)); string topLayer = DbUtil.ExecuteScalar(sql, context, builder).ToString(); string subStringSql = context.ContextConfig.GetFunction("SubString", layerField.FieldName, topLayer.Length + 1, 3); builder = CreateLayerParamBuilder(context, layerField, topLayer); builder = SqlParamBuilder.CreateParamBuilder(fixBuilder, builder); sql = string.Format(ObjectUtil.SysCulture, "SELECT MAX({0}) FROM {1}", subStringSql, resolver.TableName); string value = DbUtil.ExecuteScalar(sql, context, builder).ToString().Trim(); if (string.IsNullOrEmpty(value)) { DataRow parentRow = resolver.SelectRowWithParam(tree.IdField, parentId); parentRow[tree.LeafField] = 0; resolver.SetCommands(AdapterCommand.Update); return(topLayer + "000"); } else { return(topLayer + AddLayer(value.Substring(value.Length - 3))); } } catch { return(string.Empty); } } }
internal static void SortTree(TableResolver resolver, DbTreeDefinition tree, string id, TreeNodeMoveDirection direct) { TkDebug.AssertArgumentNull(resolver, "resolver", null); TkDebug.AssertArgumentNull(tree, "tree", null); TkDebug.AssertArgumentNullOrEmpty(id, "id", null); IParamBuilder fixBuilder = resolver.CreateFixCondition(); IParamBuilder builder = ParamBuilder.CreateParamBuilder(fixBuilder, SqlParamBuilder.CreateEqualSql(resolver.Context, resolver.GetFieldInfo(tree.IdField), id)); IFieldInfo layerField = resolver.GetFieldInfo(tree.LayerField); string sql = string.Format(ObjectUtil.SysCulture, "SELECT {0} FROM {1}", layerField.FieldName, resolver.TableName); string layer = DbUtil.ExecuteScalar(sql, resolver.Context, builder).ToString(); string parentLayer = layer.Substring(0, layer.Length - 3); resolver.SetCommands(AdapterCommand.Update); builder = SqlParamBuilder.CreateSingleSql(resolver.Context, layerField, "LIKE", parentLayer + "___"); resolver.Select(builder, "ORDER BY " + layerField.FieldName); if (resolver.HostTable == null || resolver.HostTable.Rows.Count == 0) { return; } int rowNum = GetRowNum(resolver.HostTable, tree.IdField, id); if (rowNum == -1) { return; } //根据移动方向,执行不同操作 switch (direct) { case TreeNodeMoveDirection.Up: if (rowNum == 0) //已经最前,不能向上移动 { return; } SwapLayer(resolver, tree, rowNum, rowNum - 1); break; case TreeNodeMoveDirection.Down: if (rowNum == resolver.HostTable.Rows.Count - 1) //已经最后,不能向下移动 { return; } SwapLayer(resolver, tree, rowNum, rowNum + 1); break; } }
internal static void MoveTree(TableResolver resolver, DbTreeDefinition tree, string sourceId, string destId) { TkDebug.AssertArgumentNull(resolver, "resolver", null); TkDebug.AssertArgumentNull(tree, "tree", null); TkDebug.AssertArgumentNullOrEmpty(sourceId, "sourceId", null); TkDebug.AssertArgumentNullOrEmpty(destId, "destId", null); IFieldInfo layerField = resolver.GetFieldInfo(tree.LayerField); string sql = string.Format(ObjectUtil.SysCulture, "SELECT {0} FROM {1}", layerField.FieldName, resolver.TableName); IParamBuilder builder = ParamBuilder.CreateParamBuilder(resolver.CreateFixCondition(), SqlParamBuilder.CreateEqualSql(resolver.Context, resolver.GetFieldInfo(tree.IdField), destId)); string destLayer = DbUtil.ExecuteScalar(sql, resolver.Context, builder).ToString(); DataRow srcRow = resolver.SelectRowWithParam(tree.IdField, sourceId); if (destId == srcRow[tree.ParentIdField].ToString()) { //throw new InvalidMoveException(); } string oldLayer = srcRow[tree.LayerField].ToString(); if (destLayer.StartsWith(oldLayer, StringComparison.Ordinal)) { //throw new InvalidMoveException(); } resolver.SetCommands(AdapterCommand.Update); string newLayer = GetLayer(resolver, tree, destId); srcRow[tree.LayerField] = newLayer; DataTable table = resolver.HostTable; int currentCount = table.Rows.Count; builder = CreateLayerParamBuilder(resolver.Context, layerField, oldLayer); resolver.Select(builder); for (int i = currentCount; i < table.Rows.Count; ++i) { DataRow row = table.Rows[i]; row[tree.LayerField] = row[tree.LayerField].ToString().Replace( oldLayer, newLayer); } SetParentLeaf(resolver, tree, srcRow[tree.ParentIdField].ToString()); srcRow[tree.ParentIdField] = destId; }
internal static void SetParentLeaf(TableResolver resolver, DbTreeDefinition fields, string parentId) { //DataTable table = resolver.HostTable; string sql = string.Format(ObjectUtil.SysCulture, "SELECT COUNT(*) FROM {0}", resolver.TableName); IParamBuilder builder = SqlParamBuilder.CreateEqualSql( resolver.Context, resolver.GetFieldInfo(fields.ParentIdField), parentId); builder = SqlParamBuilder.CreateParamBuilder(resolver.CreateFixCondition(), builder); int count = DbUtil.ExecuteScalar(sql, resolver.Context, builder).Value <int>(); if (count == 1) { DataRow row = resolver.TrySelectRowWithParam(fields.IdField, parentId); if (row != null) { row[fields.LeafField] = 1; } } }