public ActionResult Create(string parentId, string name, bool enable) { JObject jo = new JObject(); if (string.IsNullOrWhiteSpace(parentId) || string.IsNullOrWhiteSpace(name)) { jo.Add("Msg", "請輸入必要資料"); return Content(JsonConvert.SerializeObject(jo), "application/json"); } bool isRoot = parentId.Equals("root", StringComparison.OrdinalIgnoreCase); Guid parentNodeID; bool parseResult = Guid.TryParse(parentId, out parentNodeID); if (!isRoot && !parseResult) { jo.Add("Msg", "上層節點 ID 資料錯誤"); return Content(JsonConvert.SerializeObject(jo), "application/json"); } bool checkRoot = isRoot || this._service.IsExists(parentNodeID); if (!isRoot && !checkRoot) { jo.Add("Msg", "找不到指定的上層資料"); return Content(JsonConvert.SerializeObject(jo), "application/json"); } if (string.IsNullOrWhiteSpace(name)) { jo.Add("Msg", "需輸入節點名稱"); return Content(JsonConvert.SerializeObject(jo), "application/json"); } try { TreeNode node = new TreeNode(); node.ID = Guid.NewGuid(); if (!isRoot) { node.ParentID = parentNodeID; } node.Name = name; node.IsEnable = enable; this._service.Create(node); jo.Add("Msg", "Success"); } catch (Exception ex) { jo.Add("Msg", ex.Message); } return Content(JsonConvert.SerializeObject(jo), "application/json"); }
/// <summary> /// Creates the specified instance. /// </summary> /// <param name="instance">The instance.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public IResult Create(TreeNode instance) { if (instance == null) { throw new InvalidOperationException("請輸入 TreeNode 資料"); } IResult result = new Result(false); try { //決定排序 if (instance.IsRoot) { instance.Sort = 0; } else { TreeNode parentNode = this.GetSingle(instance.ParentID.Value); instance.Sort = !parentNode.HasChildren ? 1 : parentNode.Children.Count() + 1; } instance.CreateDate = DateTime.Now; instance.UpdateDate = DateTime.Now; this._repository.Add(instance); result.Success = true; } catch (Exception ex) { result.Exception = ex; } return result; }
/// <summary> /// Gets the child nodes. /// </summary> /// <param name="nodes">The nodes.</param> /// <param name="parentNode">The parent node.</param> /// <param name="nodeList">The node list.</param> private void GetChildNodes(IEnumerable<TreeNode> nodes, TreeNode parentNode, ref List<TreeNode> nodeList) { if (!nodes.Any()) return; if (!parentNode.HasChildren) return; if (!nodes.Contains(parentNode)) return; var children = nodes.Where(x => x.ParentID == parentNode.ID) .OrderBy(x => x.Sort) .ThenBy(x => x.CreateDate); foreach (var item in children) { if (!nodeList.Contains(item)) { nodeList.Add(item); } this.GetChildNodes(nodes, item, ref nodeList); } }
/// <summary> /// Gets the child array. /// </summary> /// <param name="parentNode">The parent node.</param> /// <param name="nodes">The nodes.</param> /// <returns></returns> private JArray GetChildArray(TreeNode parentNode, IEnumerable<TreeNode> nodes) { JArray childArray = new JArray(); foreach (var node in nodes.Where(x => x.ParentID == parentNode.ID).OrderBy(x => x.Sort)) { JObject subObject = new JObject { {"id", node.ID.ToString()}, {"text", node.Name} }; if (nodes.Any(y => y.ParentID == node.ID)) { subObject.Add("children", this.GetChildArray(node, nodes)); } childArray.Add(subObject); } return childArray; }
/// <summary> /// Gets the node json object. /// </summary> /// <param name="node">The node.</param> /// <returns></returns> private JObject GetNodeJsonObject(TreeNode node) { var jsonObject = new JObject { {"ID", node.ID.ToString()}, {"Name", node.Name}, //節點名稱 {"Delete", !node.HasChildren}, //刪除(當有子節點存在時,該節點是不能被刪除的) {"IsEnable", node.IsEnable }, //是否啟用 {"CreateDate", node.CreateDate.ToString()}, //建立日期 {"UpdateDate", node.UpdateDate.ToString()} //更新日期 }; //節點上下移動 if (node.ParentID.HasValue) { var sameParentNodes = node.ParentNode.Children.ToList(); var firstOrDefault = sameParentNodes.FirstOrDefault(); var lastOrDefault = sameParentNodes.LastOrDefault(); jsonObject.Add("MoveUp", firstOrDefault != null && !firstOrDefault.ID.Equals(node.ID)); jsonObject.Add("MoveDown", lastOrDefault != null && !lastOrDefault.ID.Equals(node.ID)); } return jsonObject; }
/// <summary> /// Gets the child array. /// </summary> /// <param name="nodes">The nodes.</param> /// <param name="parentNode">The parent node.</param> /// <returns></returns> private JArray GetChildArray(IEnumerable<TreeNode> nodes, TreeNode parentNode) { JArray childArray = new JArray(); foreach (var node in nodes.Where(x => x.ParentID == parentNode.ID).OrderBy(x => x.Sort)) { JObject subObject = this.GetNodeJsonObject(node); if (nodes.Where(y => y.ParentID == node.ID).Any()) { subObject.Add("children", this.GetChildArray(nodes, node)); } childArray.Add(subObject); } return childArray; }
/// <summary> /// Updates the specified node. /// </summary> /// <param name="instance">The node.</param> /// <returns></returns> /// <exception cref="System.InvalidOperationException">請輸入 TreeNode 資料</exception> /// <exception cref="System.NotImplementedException"></exception> public IResult Update(TreeNode instance, Guid originalParentID) { if (instance == null) { throw new InvalidOperationException("請輸入 TreeNode 資料"); } IResult result = new Result(false); var before = this.GetSingle(instance.ID); int beforeSort = instance.Sort; try { //判斷修改前後的 parentID 是否相同,如果不同則要重新調整 Sort 值 if (!instance.IsRoot && !instance.ParentID.Equals(originalParentID)) { TreeNode parentNode = this.GetSingle(instance.ParentID.Value); if (!parentNode.HasChildren) { instance.Sort = 1; } else { instance.Sort = parentNode.Children.Count() + 1; } //重新調整原來 parent node 的 ChildrenNode 排序 var beforeParentChildren = this.GetSingle(originalParentID).Children; foreach (var item in beforeParentChildren.Where(x => x.Sort > beforeSort)) { var currentNode = this.GetSingle(item.ID); currentNode.Sort -= 1; currentNode.UpdateDate = DateTime.Now; this.Update(currentNode); } } instance.UpdateDate = DateTime.Now; this._repository.Update(instance); result.Success = true; } catch (Exception ex) { result.Exception = ex; } return result; }
/// <summary> /// Updates the specified instance. /// </summary> /// <param name="instance">The instance.</param> /// <returns></returns> /// <exception cref="System.InvalidOperationException">請輸入 TreeNode 資料</exception> public IResult Update(TreeNode instance) { if (instance == null) { throw new InvalidOperationException("請輸入 TreeNode 資料"); } IResult result = new Result(false); try { instance.UpdateDate = DateTime.Now; this._repository.Update(instance); result.Success = true; } catch (Exception ex) { result.Exception = ex; } return result; }
/// <summary> /// Deletes the specified instance. /// </summary> /// <param name="instance">The instance.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public IResult Delete(TreeNode instance) { if (null == instance) { throw new ArgumentNullException("需輸入TreeNode instance"); } IResult result = new Result(false); try { var sameParentNodes = instance.ParentNode.Children; // 重新調整同層節點的排序 foreach (var node in sameParentNodes.Where(x => x.Sort > instance.Sort)) { var modifyNode = this.GetSingle(node.ID); modifyNode.Sort -= 1; modifyNode.UpdateDate = DateTime.Now; this.Update(modifyNode); } this._repository.Delete(instance); result.Success = true; } catch (Exception ex) { result.Exception = ex; } return result; }