public ResultDto InsertProjectCategory(string userId, string name, string description, string projectIds) { ResultDto result = new ResultDto(); if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(userId)) { return(result); } var projectIdList = GetObjIdListFromStr(projectIds); //项目列表 //新建项目组 var builder = Builders <Dnl_ProjectCategory> .Filter; var col = MongoDBHelper.Instance.GetDnl_ProjectCategory(); var filter = builder.Eq(x => x.Name, name); filter &= builder.Eq(x => x.UsrId, new ObjectId(userId)); filter &= builder.Eq(x => x.IsDel, false); var dto = col.Find(filter).FirstOrDefault(); if (dto != null) { result.Message = "项目组‘" + name + "’已存在!"; return(result); } Dnl_ProjectCategory prj = new Dnl_ProjectCategory { UsrId = new ObjectId(userId), Name = name, CreatedAt = DateTime.Now.AddHours(8), IsDel = false, ProjectCount = projectIdList.Count, Description = description }; col.InsertOne(prj); if (projectIdList.Count > 0) { //获取新创建的项目组Id var builderId = Builders <Dnl_ProjectCategory> .Filter; var filterId = builderId.Eq(x => x.Name, name); filterId &= builderId.Eq(x => x.UsrId, new ObjectId(userId)); filterId &= builderId.Eq(x => x.IsDel, false); var proCategoryId = col.Find(filterId).Project(x => x._id).FirstOrDefault(); var builderProGroup = Builders <Dnl_ProjectGroup> .Filter; var colProGroup = MongoDBHelper.Instance.GetDnl_ProjectGroup(); var builderPro = Builders <IW2S_Project> .Filter; var colPro = MongoDBHelper.Instance.GetIW2S_Projects(); //创建项目信息 foreach (var proId in projectIdList) { var filterPro = builderPro.Eq(x => x._id, proId); var query = colPro.Find(filterPro).Project(x => new{ Name = x.Name, Description = x.Description }).FirstOrDefault(); var proGroup = new Dnl_ProjectGroup { Name = query.Name, Description = query.Description, UsrId = new ObjectId(userId), ProjectCategoryId = proCategoryId, ProjectId = proId, }; colProGroup.InsertOne(proGroup); } } result.IsSuccess = true; return(result); }
public ResultDto UpdateProjectCategory(string categoryId, string name, string description, string projectIds) { ResultDto result = new ResultDto(); var newProList = GetObjIdListFromStr(projectIds); //新的项目列表 //获取Group内原有项目,并删除 var builderGroup = Builders <Dnl_ProjectGroup> .Filter; var filterGroup = builderGroup.Eq(x => x.ProjectCategoryId, new ObjectId(categoryId)); var colGroup = MongoDBHelper.Instance.GetDnl_ProjectGroup(); colGroup.DeleteMany(filterGroup); //添加新项目 if (newProList.Count > 0) { //获取新创建的项目组Id var builderId = Builders <Dnl_ProjectCategory> .Filter; var filterId = builderId.Eq(x => x.Name, name); filterId &= builderId.Eq(x => x.IsDel, false); var builderProGroup = Builders <Dnl_ProjectGroup> .Filter; var colProGroup = MongoDBHelper.Instance.GetDnl_ProjectGroup(); var builderPro = Builders <IW2S_Project> .Filter; var colPro = MongoDBHelper.Instance.GetIW2S_Projects(); //创建项目信息 var proCreate = new List <Dnl_ProjectGroup>(); foreach (var proId in newProList) { var filterPro = builderPro.Eq(x => x._id, proId); var query = colPro.Find(filterPro).Project(x => new { Name = x.Name, Description = x.Description, UsrId = x.UsrId }).FirstOrDefault(); var proGroup = new Dnl_ProjectGroup { Name = query.Name, Description = query.Description, UsrId = query.UsrId, ProjectCategoryId = new ObjectId(categoryId), ProjectId = proId }; proCreate.Add(proGroup); } colProGroup.InsertMany(proCreate); } //更新项目组字段 var builder = Builders <Dnl_ProjectCategory> .Filter; var filter = builder.Eq(x => x._id, new ObjectId(categoryId)); if (string.IsNullOrEmpty(name)) { result.Message = "项目组名不能为空"; return(result); } if (string.IsNullOrEmpty(description)) { result.Message = "项目组描述不能为空"; return(result); } var updateCategory = new UpdateDocument { { "$set", new QueryDocument { { "Name", name }, { "Description", description }, { "ProjectCount", newProList.Count } } } }; MongoDBHelper.Instance.GetDnl_ProjectCategory().UpdateOne(filter, updateCategory); result.IsSuccess = true; return(result); }