public virtual void SaveImportHouse(ImportedHouse house) { MongoServer houseCaijiMongoDbServer = GetMongoClient(connectionName); MongoDatabase houseDb = houseCaijiMongoDbServer.GetDatabase(databaseName); string dbCollection = "ImportHouse"; MongoCollection <BsonDocument> houseCollection = houseDb.GetCollection(dbCollection); houseCollection.Save(house); }
public virtual void AddImportHouse(IEnumerable <ImportedHouse> houseList, int siteId, int userId, string id_Prefix, string siteUserName) { MongoServer houseCaijiMongoDbServer = GetMongoClient(connectionName); MongoDatabase houseDb = houseCaijiMongoDbServer.GetDatabase(databaseName); string dbCollection = "ImportHouse"; MongoCollection <BsonDocument> houseCollection = houseDb.GetCollection(dbCollection); var query = Query.EQ("RefUser.UserID", userId); var update = Update.Pull("RefUser", Query.EQ("UserID", userId)); houseCollection.Update(query, update, UpdateFlags.Multi); foreach (ImportedHouse item in houseList) { string houseId = item.HouseID; item.MoveStatus = 0; item.HouseID = id_Prefix + "_" + item.HouseID; item.SiteUserName = siteUserName; item.SiteId = siteId; if (item.houseInfo != null) { item.BuildType = 1; } else if (item.villaInfo != null) { item.BuildType = 2; } else if (item.shopInfo != null) { item.BuildType = 3; } else if (item.officeInfo != null) { item.BuildType = 4; } else if (item.factoryInfo != null) { item.BuildType = 5; } ImportedHouse oldHouse = houseCollection.FindOneAs <ImportedHouse>(Query.EQ("_id", item.HouseID)); if (oldHouse != null) { int moveHouseId = 0; int moveStatus = 0; if (oldHouse.RefUser == null) { item.RefUser = new List <ImportedHouseRefUser>(); } else { item.RefUser = oldHouse.RefUser; } ImportedHouseRefUser thisRefUser = item.RefUser.Where(u => u.UserID == userId).FirstOrDefault(); if (thisRefUser != null) { moveHouseId = thisRefUser.MoveHouseId; moveStatus = thisRefUser.MoveStatus; } else if (oldHouse.UserID == userId) { moveHouseId = oldHouse.MoveHouseId; moveStatus = oldHouse.MoveStatus; } //oldHouse.MoveStatus = 0; // oldHouse.UserID = 0; // oldHouse.MoveHouseId = 0; // oldHouse.RefUser = null; //string oldStr = ZJB.Core.Utilities.StringUtility.ToMd5String(jss.Serialize(oldHouse)); // string newStr = ZJB.Core.Utilities.StringUtility.ToMd5String(jss.Serialize(item)); //if (oldStr != newStr) //{ if (thisRefUser != null) { thisRefUser.MoveStatus = moveStatus; thisRefUser.MoveHouseId = moveHouseId; } else { thisRefUser = new ImportedHouseRefUser() { UserID = userId, MoveStatus = moveStatus, MoveHouseId = moveHouseId }; item.RefUser.Add(thisRefUser); } houseCollection.Save(item); // } } else { List <ImportedHouseRefUser> refUserList = new List <ImportedHouseRefUser>(); refUserList.Add(new ImportedHouseRefUser() { MoveHouseId = 0, UserID = userId, MoveStatus = 0 }); item.RefUser = refUserList; houseCollection.Save(item); } } }