public static bool Delete(IDal dal, int id,out CDept hisDept) { int i; hisDept = Get(dal, id); if (hisDept == null) return false; dal.BeginTran(); dal.Execute("UPDATE tUser SET DeptCode=null WHERE DeptCode=@DeptCode", out i, dal.CreateParameter("@DeptCode",hisDept.DeptCode)); dal.Execute("DELETE FROM tDept WHERE Id=@Id",out i, dal.CreateParameter("@Id",id)); dal.CommitTran(); return true; }
/// <summary> /// 新建部门 /// </summary> /// <param name="dal"></param> /// <param name="dept"></param> /// <param name="editUser"></param> /// <returns></returns> public static bool Create(IDal dal, CDept dept,string editUser) { int i; dal.Execute("INSERT INTO tDept( DeptCode ,DeptName ,ParentCode ,BuildUser ,EditUser) VALUES ( @DeptCode ,@DeptName ,@ParentCode ,@BuildUser ,@EditUser )",out i, dal.CreateParameter("@DeptCode",dept.DeptCode.Trim()), dal.CreateParameter("@DeptName",dept.DeptName.Trim()), dal.CreateParameter("@ParentCode",dept.ParentCode.Trim()), dal.CreateParameter("@BuildUser", editUser), dal.CreateParameter("@EditUser", editUser)); if (i == 0) return false; var dt = dal.Select("SELECT Id FROM dbo.tDept WHERE DeptCode=@DeptCode",out i, dal.CreateParameter("@DeptCode",dept.DeptCode)); if (i == 0) return false; dept.Id = Convert.ToInt16(dt.Rows[0]["Id"]); return true; }
/// <summary> /// 修改 /// </summary> /// <param name="dal"></param> /// <param name="dept"></param> /// <param name="editUser"></param> /// <returns></returns> public static bool Update(IDal dal, CDept dept, string editUser) { int i; dal.Execute("UPDATE tDept SET DeptName=@DeptName ,EditDate=GETDATE(),EditUser=@EditUser WHERE Id=@Id", out i, dal.CreateParameter("@DeptName",dept.DeptName.Trim()), dal.CreateParameter("@EditUser", editUser), dal.CreateParameter("@Id",dept.Id)); return i == 1; }
/// <summary> /// 获取指定节点的子节点 /// </summary> /// <returns></returns> private static CDept[] GetChilds(string parentCode,DataTable dt,CDept[] people) { if (parentCode == null) return null; return (from DataRow row in dt.Rows where Convert.ToString(row["ParentCode"]) == parentCode select new CDept { Id = Convert.ToInt16(row["Id"]), DeptCode =Convert.ToString(row["DeptCode"]), DeptName =Convert.ToString(row["DeptName"]), ParentCode =Convert.ToString(row["ParentCode"]), Childs = GetChilds(Convert.ToString(row["DeptCode"]), dt,people), People = people.Any(p => p.Id == Convert.ToInt16(row["Id"])) ? people.First(p => p.Id == Convert.ToInt16(row["Id"])).People : 0 } ).ToArray(); }
// PUT api/deptapi/5 public CDept Put(int id, CDept value) { var user = (CSign)HttpContext.Current.Session[ConfigurationManager.AppSettings["AuthSaveKey"]]; if (user == null) { throw new HttpResponseException(new SiginFailureMessage()); } using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { bool ok; try { dal.Open(); ok = DeptBll.Update(dal, value,string.Format("{0}-{1}", user.UserCode, user.UserName)); } catch(Exception ex) { LogBll.Write(dal,new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("{0}#{1}", "Dept.Put", ex.Message), LogType = LogType.系统异常 }); throw new HttpResponseException(new SystemExceptionMessage()); } if (!ok) { LogBll.Write(dal, new CLog { LogContent = string.Format("修改部门{0}-{1}", value.DeptCode, value.DeptName), LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName) }); throw new HttpResponseException(new DataNotFoundMessage()); } LogBll.Write(dal, new CLog { LogContent = string.Format("修改部门{0}-{1}", value.DeptCode, value.DeptName), LogType = LogType.操作成功, LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName) }); dal.Close(); return value; } }