public void UpdateMakes(IExcelDataReader reader) { reader.Read(); while (reader.Read()) { int makeID = 0; int.TryParse(reader[0].ToString(), out makeID); if (makeID <= 0) { continue; } AutoMake make = AutoMakeService.GetByID(makeID); if (make == null) { make = new AutoMake() { ID = makeID, Name = reader[1].ToString(), Name_ru = reader[2].ToString() }; AutoMakeService.Create(make); } else { make.Name = reader[1].ToString(); make.Name_ru = reader[2].ToString(); AutoMakeService.Edit(make); } } }
public void UpdateModels(IExcelDataReader reader) { reader.Read(); while (reader.Read()) { int modelID = 0; int.TryParse(reader[0].ToString(), out modelID); int makeID = 0; int.TryParse(reader[1].ToString(), out makeID); AutoMake make = AutoMakeService.GetByID(makeID); if (modelID <= 0 || make == null) { continue; } AutoModel model = AutoModelService.GetByID(modelID); if (model == null) { model = new AutoModel() { ID = modelID, MakeID = makeID, Name = reader[2].ToString(), Name_ru = reader[3].ToString() }; AutoModelService.Create(model); } else { model.MakeID = makeID; model.Name = reader[2].ToString(); model.Name_ru = reader[3].ToString(); AutoModelService.Edit(model); } } }
private string BuildPostData(Auto auto, List <Tuple <string, string, string> > additionalFields = null) { string elasticSearchDateFormat = $"{XCarsConfiguration.ElasticSearchDateFormat}"; string fullInfo = ""; string postData = @"{ ""AutoID"": " + auto.ID + @" ,""UserID"": " + auto.UserID + @" ,""DateCreated"": """ + auto.DateCreated.ToString(elasticSearchDateFormat) + @""" ,""DateUpdated"": """ + auto.DateUpdated.ToString(elasticSearchDateFormat) + @""""; if (auto.DateExpires != null) { postData += @",""DateExpires"": """ + auto.DateExpires.Value.ToString(elasticSearchDateFormat) + @""""; } if (auto.DateAppearance != null) { postData += @",""DateAppearance"": """ + auto.DateAppearance.Value.ToString(elasticSearchDateFormat) + @""""; } //string photoUrl = XCarsConfiguration.AutoNoPhotoUrl; //AutoPhoto photo = auto.AutoPhotoes.FirstOrDefault(p => p.IsMain); //if (photo != null) // photoUrl = photo.Url; //postData += @",""AutoPhotoUrl"": """ + photoUrl + @""""; int mainPhotoID = 0; AutoPhoto photo = auto.AutoPhotoes.FirstOrDefault(p => p.IsMain); if (photo != null) { mainPhotoID = photo.ID; } postData += @",""MainPhotoID"": " + mainPhotoID + @""; string name = AutoStatusService.GetByID(auto.StatusID).Name; postData += @" ,""Status"": { ""ID"": " + auto.StatusID + @", ""Name"": """ + name + @""" }"; City city = CityService.GetByID(auto.RegionID); if (city != null) { name = city.Region.Name; fullInfo += name + " "; postData += @",""AutoRegion"": { ""ID"": " + auto.RegionID + @", ""Name"": """ + name + @""" }"; name = city.Name; fullInfo += name + " "; postData += @",""AutoCity"": { ""ID"": " + auto.RegionID + @", ""Name"": """ + name + @""" }"; } name = AutoTransportTypeService.GetByID(auto.TransportTypeID).Name; fullInfo += name + " "; postData += @" ,""AutoTransportType"": { ""ID"": " + auto.TransportTypeID + @", ""Name"": """ + name + @""" }"; name = AutoBodyTypeService.GetByID(auto.BodyTypeID).Name; fullInfo += name + " "; postData += @" ,""AutoBodyType"": { ""ID"": " + auto.BodyTypeID + @", ""Name"": """ + name + @""" }"; name = AutoMakeService.GetByID(auto.MakeID).Name; fullInfo += name + " "; postData += @" ,""AutoMake"": { ""ID"": " + auto.MakeID + @", ""Name"": """ + name + @""" }"; name = AutoModelService.GetByID(auto.ModelID).Name; fullInfo += name + " "; postData += @" ,""AutoModel"": { ""ID"": " + auto.ModelID + @", ""Name"": """ + name + @""" }"; name = AutoTSRegistrationService.GetByID(auto.TSRegistrationID).NameFull; fullInfo += name + " "; postData += @" ,""AutoTSRegistration"": { ""ID"": " + auto.TSRegistrationID + @", ""Name"": """ + name + @""" }"; postData += @",""YearOfIssue"": " + auto.YearOfIssue + @""; if (!string.IsNullOrWhiteSpace(auto.Modification)) { postData += @",""Modification"": """ + auto.Modification + @""""; fullInfo += auto.Modification + " "; } postData += @",""Probeg"": " + auto.Probeg + @""; //if (auto.PriceUSD != null) postData += @",""PriceUSD"": " + ((auto.PriceUSD != null) ? auto.PriceUSD : 0) + @""; //if (auto.PriceUAH != null) postData += @",""PriceUAH"": " + ((auto.PriceUAH != null) ? auto.PriceUAH : 0) + @""; postData += @",""PriceUSDSearch"": " + auto.PriceUSDSearch + @""; postData += @",""PriceUAHSearch"": " + auto.PriceUAHSearch + @""; string boolValue = "false"; if (auto.IsExchangeAvailable) { boolValue = "true"; } postData += @",""IsExchangeAvailable"": " + boolValue; boolValue = "false"; if (auto.IsTorgAvailable) { boolValue = "true"; } postData += @",""IsTorgAvailable"": " + boolValue; if (auto.TransmissionTypeID != null) { name = AutoTransmissionTypeService.GetByID((int)auto.TransmissionTypeID).Name; fullInfo += name + " "; postData += @" ,""AutoTransmissionType"": { ""ID"": " + auto.TransmissionTypeID + @", ""Name"": """ + name + @""" }"; } if (auto.DriveTypeID != null) { name = AutoDriveTypeService.GetByID((int)auto.DriveTypeID).Name; fullInfo += name + " "; postData += @" ,""AutoDriveType"": { ""ID"": " + auto.DriveTypeID + @", ""Name"": """ + name + @""" }"; } if (auto.NumberOfDoors != null) { postData += @",""NumberOfDoors"": " + auto.NumberOfDoors + @""; } if (auto.NumberOfSeats != null) { postData += @",""NumberOfSeats"": " + auto.NumberOfSeats + @""; } if (auto.ColorID != null) { name = AutoColorService.GetByID((int)auto.ColorID).Name; fullInfo += name + " "; postData += @" ,""AutoColor"": { ""ID"": " + auto.ColorID + @", ""Name"": """ + name + @""" }"; } boolValue = "false"; if (auto.IsColorMetallic) { boolValue = "true"; } postData += @",""IsColorMetallic"": " + boolValue; if (auto.FuelTypeID != null) { name = AutoFuelTypeService.GetByID((int)auto.FuelTypeID).Name; fullInfo += name + " "; postData += @" ,""AutoFuelType"": { ""ID"": " + auto.FuelTypeID + @", ""Name"": """ + name + @""" }"; } if (auto.FuelConsumptionCity != null) { string tmpDoubleVar = auto.FuelConsumptionCity.ToString(); tmpDoubleVar = tmpDoubleVar.Replace(',', '.'); postData += @",""FuelConsumptionCity"": " + tmpDoubleVar + @""; } if (auto.FuelConsumptionHighway != null) { string tmpDoubleVar = auto.FuelConsumptionHighway.ToString(); tmpDoubleVar = tmpDoubleVar.Replace(',', '.'); postData += @",""FuelConsumptionHighway"": " + tmpDoubleVar + @""; } if (auto.FuelConsumptionMixed != null) { string tmpDoubleVar = auto.FuelConsumptionMixed.ToString(); tmpDoubleVar = tmpDoubleVar.Replace(',', '.'); postData += @",""FuelConsumptionMixed"": " + tmpDoubleVar + @""; } if (auto.EngineCapacity != null) { string tmpDoubleVar = auto.EngineCapacity.ToString(); tmpDoubleVar = tmpDoubleVar.Replace(',', '.'); postData += @",""EngineCapacity"": " + tmpDoubleVar + @""; } if (auto.Power != null) { postData += @",""Power"": " + auto.Power + @""; } if (!string.IsNullOrWhiteSpace(auto.Description)) { fullInfo += auto.Description + " "; postData += @",""Description"": """ + auto.Description + @""""; } postData += @",""Views"": " + auto.Views + @""; if (auto.AutoSecurities != null && auto.AutoSecurities.Count > 0) { postData += @", ""AutoSecurities"": ["; int i = 0; foreach (var item in auto.AutoSecurities) { if (i > 0) { postData += ","; } //postData += @"{""ID"": " + item.ID + @", ""Name"": """ + item.Name + @"""}"; postData += item.ID; i++; } postData += @"]"; } if (auto.AutoComforts != null && auto.AutoComforts.Count > 0) { postData += @", ""AutoComforts"": ["; int i = 0; foreach (var item in auto.AutoComforts) { if (i > 0) { postData += ","; } //postData += @"{""ID"": " + item.ID + @", ""Name"": """ + item.Name + @"""}"; postData += item.ID; i++; } postData += @"]"; } if (auto.AutoMultimedias != null && auto.AutoMultimedias.Count > 0) { postData += @", ""AutoMultimedias"": ["; int i = 0; foreach (var item in auto.AutoMultimedias) { if (i > 0) { postData += ","; } //postData += @"{""ID"": " + item.ID + @", ""Name"": """ + item.Name + @"""}"; postData += item.ID; i++; } postData += @"]"; } if (auto.AutoStates != null && auto.AutoStates.Count > 0) { postData += @", ""AutoStates"": ["; int i = 0; foreach (var item in auto.AutoStates) { if (i > 0) { postData += ","; } //postData += @"{""ID"": " + item.ID + @", ""Name"": """ + item.Name + @"""}"; postData += item.ID; i++; } postData += @"]"; } if (auto.AutoMiscs != null && auto.AutoMiscs.Count > 0) { postData += @", ""AutoMiscs"": ["; int i = 0; foreach (var item in auto.AutoMiscs) { if (i > 0) { postData += ","; } //postData += @"{""ID"": " + item.ID + @", ""Name"": """ + item.Name + @"""}"; postData += item.ID; i++; } postData += @"]"; } postData += @",""Top"": " + auto.Top; postData += @",""FullInfo"": """ + fullInfo + @""""; if (additionalFields != null) { string quot = ""; foreach (var item in additionalFields) { quot = @""""; if (item.Item3 == "int" || item.Item3 == "bool") { quot = @""; } postData += @",""" + item.Item1 + @""" : " + quot + @"" + item.Item2 + quot; } } postData += @" }"; return(postData); }
public void UpdateBothMakeAndModel(IExcelDataReader reader) { //clear all unused makes and models AutoModelService.DeleteUnused(); //List<AutoModel> unusedModels = AutoModelService.GetAll().Where(model => model.Autoes.FirstOrDefault() == null).ToList(); //for (int i = 0; i < unusedModels.Count; i++) // AutoModelService.Delete(unusedModels[i]); AutoMakeService.DeleteUnused(); //List<AutoMake> unusedMakes = AutoMakeService.GetAll().Where(make => make.Autoes.FirstOrDefault() == null && make.AutoModels.Count == 0).ToList(); //for (int i = 0; i < unusedMakes.Count; i++) // AutoMakeService.Delete(unusedMakes[i]); //insert new makes and models reader.Read(); List <AutoMake> newMakes = new List <AutoMake>(); List <AutoModel> newModels = new List <AutoModel>(); int row = 0; try { while (reader.Read()) { row++; if (reader[0] == null) { break; } int modelID = 0; int.TryParse(reader[0].ToString(), out modelID); int makeID = 0; int.TryParse(reader[1].ToString(), out makeID); if (modelID <= 0 || makeID <= 0) { return; } //AutoMake make = AutoMakeService.GetByID(makeID); //if (make == null) //{ AutoMake make = new AutoMake() { ID = makeID, Name = reader[5].ToString(), Name_ru = reader[6].ToString() }; if (newMakes.FirstOrDefault(item => item.ID == makeID) == null) { newMakes.Add(make); } //AutoMakeService.Create(make); //} //AutoModel model = AutoModelService.GetByID(modelID); //if (model == null) //{ AutoModel model = new AutoModel() { ID = modelID, MakeID = makeID, Name = reader[3].ToString(), Name_ru = reader[4].ToString() }; if (newModels.FirstOrDefault(item => item.ID == modelID) == null) { newModels.Add(model); } // //AutoModelService.Create(model); //} } AutoMakeService.CreateMany(newMakes); AutoModelService.CreateMany(newModels); } catch (Exception ex) { } }