public void UpdateOrgInfo(int oldParentId, SvrModels.SvrOrgInfo svrModel) { this.dbModel.OrgName = svrModel.OrgName; this.dbModel.OrgAttribute = (int)svrModel.OrgAttribute; this.dbModel.IsOut = svrModel.IsOut; this.dbModel.Remarks = svrModel.Remarks; this.dbModel.UpdateTime = helper.SvrUser.CurrentTime; this.dbModel.UpdateUserId = helper.SvrUser.UserId; var oldIdList = this.dbModel.OrgIdList; if (svrModel.ParentId == svrModel.OrgId) { throw new UIValidateException("所属机构不能设置和当前机构一样"); } if (oldParentId != svrModel.ParentId)//已经移动了节点 { //相同一个节点下。父节点不能向子节点移动 var qryIsSubNode = this.helper.DbContext.T_QWF_Org.ToList().Where(w => w.OrgId == svrModel.ParentId && w.OrgIdList.Contains("," + svrModel.OrgId + ",")); if (qryIsSubNode.Count() > 0) { throw new UIValidateException("上级节点不能向下级节点移动"); } //原来的父节点 var oldParentNode = this.helper.DbContext.T_QWF_Org.Where(w => w.OrgId == oldParentId && w.IsDelete == 0).FirstOrDefault(); if (oldParentNode == null && oldParentId != 0) { throw new UIValidateException(string.Format("old orgid ={0} 不存在", oldParentId)); } var newParentNode = this.helper.DbContext.T_QWF_Org.Where(w => w.OrgId == svrModel.ParentId && w.IsDelete == 0).FirstOrDefault(); if (newParentNode == null && svrModel.ParentId != 0) { throw new UIValidateException(string.Format("new orgid ={0} 不存在", svrModel.ParentId)); } this.dbModel.ParentId = svrModel.ParentId; //层级信息 if (svrModel.ParentId == 0) { this.dbModel.OrgIdList = "," + this.dbModel.OrgId + ","; this.dbModel.LayerId = 1; } else { this.dbModel.OrgIdList = newParentNode.OrgIdList + this.dbModel.OrgId + ","; this.dbModel.LayerId = newParentNode.LayerId + 1; } this.dbModel.SortId = 0; //更新 变更后当前节点子节点的 idlist var subList = this.helper.DbContext.T_QWF_Org.Where(w => w.OrgIdList != null && w.OrgIdList.Contains("," + this.dbModel.OrgId + ",") && w.OrgId != dbModel.OrgId).ToList(); foreach (var sub in subList) { sub.OrgIdList = sub.OrgIdList.Replace(oldIdList, dbModel.OrgIdList); sub.LayerId = sub.OrgIdList.StringHelper().SplitToArray(",", StringSplitOptions.RemoveEmptyEntries).Length; } //这里改变了层级关系,防止后面计算错,先更新到db 中 this.helper.DbContext.SaveChanges(); this.UpdateSubNodeFlag(svrModel.OrgId); this.UpdateSubNodeFlag(svrModel.ParentId); this.UpdateSubNodeFlag(oldParentId); } this.helper.DbContext.SaveChanges(); //更新路径 this.UpdateOrgPath(svrModel.OrgId); this.UpdateOrgPath(svrModel.ParentId); this.UpdateOrgPath(oldParentId); }
public Org CreateOrgInfo(SvrModels.SvrOrgInfo info) { var dbModel = new DbAccess.T_QWF_Org(); dbModel.OrgCode = info.OrgCode; dbModel.OrgName = info.OrgName; dbModel.OrgAttribute = (int)info.OrgAttribute; dbModel.IsOut = info.IsOut; dbModel.ParentId = info.ParentId; dbModel.CreateTime = SvrUser.CurrentTime; dbModel.CreateUserId = SvrUser.UserId; dbModel.Remarks = info.Remarks; dbModel.IsSubNode = 1; //关键参数 dbModel.ParentId = info.ParentId; string orgIdList = string.Empty; if (info.ParentId == 0) { //顶级节点 dbModel.LayerId = 1; dbModel.SortId = 0; orgIdList = ","; } else { //找到父节点信息 var dbParentNode = DbContext.T_QWF_Org.Where(w => w.OrgId == info.ParentId && w.IsDelete == 0).FirstOrDefault(); if (dbParentNode == null) { throw new UIValidateException("上级节点信息获取失败"); } //设置当前节点信息 dbModel.LayerId = dbParentNode.LayerId + 1;//层级+1 orgIdList = dbParentNode.OrgIdList; var qrySort = DbContext.T_QWF_Org.Where(w => w.ParentId == info.ParentId && w.IsDelete == 0); if (qrySort.Count() == 0) { dbModel.SortId = 1; } else { //获取父接点下的子节点最大的SortId + 1 dbModel.SortId = qrySort.Max(m => m.SortId) + 1; } //更新父节点的IsSubNode dbParentNode.IsSubNode = 1; } //这里要获取到自增ID,只能这样做了 DbContext.T_QWF_Org.Add(dbModel); DbContext.SaveChanges(); //这里再修改OrgIdList; dbModel.OrgIdList = orgIdList + dbModel.OrgId + ","; var org = new Org(dbModel, this); org.UpdateOrgPath(dbModel.OrgId); return(org); }