/// <summary> /// 编辑 /// </summary> /// <param name="p"></param> /// <returns></returns> public Result Edit(DeptEditParam p) { // 判断机构名称是否重复 var query1 = Restrictions.Eq("Name", p.Name); ICriterion query2 = null; if (p.PID == 0) // 顶级机构 { query2 = Restrictions.Eq("PDept", null); } else // 不是顶级机构 { query2 = Restrictions.Eq("PDept", new Dept { ID = p.PID }); } var query3 = Restrictions.Not(Restrictions.Eq("ID", p.ID)); var query = Restrictions.And(query1, query2); query = Restrictions.And(query, query3); var dept1 = dal.Get(query); if (dept1 != null) { return(new Result(300, "该机构已经存在!")); } // 保存机构 var dept = dal.Get(p.ID); if (dept == null) { return(new Result(300, "数据不存在!")); } dept.Comment = p.Comment; dept.Name = p.Name; dept.Sort = p.Sort; dept.Status = p.Status; if (p.TypeID != null) { dept.Type = new DicItem { ID = p.TypeID.Value }; } var result = dal.Edit(dept); if (result) { return(new Result(200, "编辑成功!")); } return(new Result(300, "编辑失败!")); }
public JsonResult Edit(DeptEditParam p) { var result = bll.Edit(p); return(Json(result)); }
public JsonResult Add([FromBody] DeptEditParam p) { var result = bll.Add(p); return(Json(result)); }
/// <summary> /// 添加 /// </summary> /// <param name="p"></param> /// <returns></returns> public Result Add(DeptEditParam p) { // 判断机构名称是否重复 var query1 = Restrictions.Eq("Name", p.Name); ICriterion query2 = null; if (p.PID == 0) // 顶级机构 { query2 = Restrictions.Eq("PDept", null); } else // 不是顶级机构 { query2 = Restrictions.Eq("PDept", new Dept { ID = p.PID }); } var query = Restrictions.And(query1, query2); var count = dal.Count(query); if (count > 0) { return(new Result(300, "该机构已经存在!")); } // 查找父节点的Code var PCode = ""; if (p.PID > 0) // 不是顶级机构 { var pdept = dal.Get(p.PID); if (pdept != null) { PCode = pdept.Code; } } // 为当前结点生成Code var Code = ""; if (p.PID == 0) // 顶级机构 { var query3 = Restrictions.Eq("PDept", null); var dept1 = dal.List(query3, "Code", Sort.Desc).FirstOrDefault(); if (dept1 == null) // 第一个 { Code = "001"; } else // 不是第一个 { Code = StringHelper.ZeroFill((int.Parse(dept1.Code.TrimStart('0')) + 1).ToString(), 3); } } else // 不是顶级机构 { var query3 = Restrictions.Eq("PDept", null); var dept1 = dal.List(query3, "Code", Sort.Desc).FirstOrDefault(); if (dept1 == null) // 第一个 { Code = "001"; } else // 不是第一个 { Code = PCode + StringHelper.ZeroFill((int.Parse(dept1.Code.Substring(PCode.Length, 3).TrimStart('0')) + 1).ToString(), 3); } } // 添加 var dept = new Dept { AddTime = DateTime.Now, AddUser = AdminHelper.Admin, Code = Code, Comment = p.Comment, ID = 0, Name = p.Name, PDept = p.PID == 0 ? null : new Dept { ID = p.PID }, Sort = p.Sort, Status = p.Status }; if (p.TypeID != null) { dept.Type = new DicItem { ID = p.TypeID.Value }; } var result = dal.Add(dept); if (result) { return(new Result(200, "添加成功!")); } else { return(new Result(300, "添加失败!")); } }