private IActionResult ImportTalent(Stream input) { Companies = null; var errors = new ErrorManager(); var account = this.CurrentAccount(); var models = new List <ModelWrapper <Talent> >(); EachRow(input, (row, EachColumn) => { var model = new Talent { Account = account, Source = DataSource.Excel, ProfileLabel = new TalentProfileLabel() }; model.Experiences.Add(new TalentExperience() { CurrentJob = true }); EachColumn((column, value) => { switch (column) { case 1: { if (string.IsNullOrEmpty(value)) { errors.Error($"Empty function: [row: {row}][column: {column}]", column); } else { value.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).ToList().ForEach(x => { var node = FindFunction(x); if (node == null) { errors.Error($"Cannot find function: [row: {row}][column: {column}][value: {x}]", column); } else { model.Functions.Add(node); } }); } } break; case 2: { if (string.IsNullOrEmpty(value)) { errors.Error($"Empty industry: [row: {row}][column: {column}]", column); } else { value.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).ToList().ForEach(x => { var node = FindIndustry(x); if (node == null) { errors.Error($"Cannot find industry: [row: {row}][column: {column}][value: {x}]", column); } else { model.Industries.Add(node); } }); } } break; case 3: { if (string.IsNullOrEmpty(value)) { errors.Error($"Empty company: [row: {row}][column: {column}]", column); } else { if (Companies == null) { Companies = this.GetMonCollection <Company>().MongoCollection .Find(x => x.Account.Id.Equals(this.CurrentAccount().Id)) .ToList(); } var company = Companies .Where(x => x.Name.Trim().Equals(value.Trim(), StringComparison.CurrentCultureIgnoreCase)) .FirstOrDefault(); if (company == null) { errors.Error($"Cannot find company: [row: {row}][column: {column}][value: {value}]", column); } else { model.Experiences.First().Company = company; } } } break; case 4: { if (string.IsNullOrEmpty(value)) { errors.Error($"Empty title: [row: {row}][column: {column}]", column); } else { model.Experiences.First().Title = value; } } break; case 5: { model.Experiences.First().Responsibility = value?.Trim(); } break; case 6: { } break; case 7: { model.EnglishName = value?.Trim(); } break; case 8: { model.ChineseName = value?.Trim(); } break; case 9: { if (!string.IsNullOrEmpty(value)) { var year = 0; if (!int.TryParse(value.Trim(), out year)) { errors.Error($"Invalid DOB: [row: {row}][column: {column}][value: {value}]", column); } else { model.BirthYear = year; } } } break; case 10: { if (!string.IsNullOrEmpty(value)) { if (value.Trim().Equals("F", StringComparison.CurrentCultureIgnoreCase) || value.Trim().Equals("Female", StringComparison.CurrentCultureIgnoreCase)) { model.Gender = Gender.Female; } else if (value.Trim().Equals("M", StringComparison.CurrentCultureIgnoreCase) || value.Trim().Equals("Male", StringComparison.CurrentCultureIgnoreCase)) { model.Gender = Gender.Male; } else { errors.Error($"Invalid Gender: [row: {row}][column: {column}][value: {value}]", column); } } } break; case 11: { if (!string.IsNullOrEmpty(value)) { value.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).ToList().ForEach(x => { var node = FindLocation(x.Trim()); if (node == null) { errors.Error($"Cannot find location: [row: {row}][column: {column}][value: {x}]", column); } else { model.CurrentLocations.Add(node); } }); } } break; case 12: { if (!string.IsNullOrEmpty(value)) { value.Split(new char[] { '|', ',', '&', ';' }).Where(x => !string.IsNullOrWhiteSpace(x)).ToList().ForEach(x => { var node = FindLocation(x.Trim()); if (node == null) { errors.Error($"Cannot find mobility: [row: {row}][column: {column}][value: {x}]", column); } else { model.MobilityLocations.Add(node); } }); } } break; case 13: { if (!string.IsNullOrEmpty(value)) { var num = 0; if (!int.TryParse(value.Trim().Replace(",", ""), out num)) { errors.Error($"Annual total package numer parse failed: [row: {row}][column: {column}][value: {value}]", column); } else { model.Experiences.First().AnnualPackage = GetAnnualPackage(num); } } } break; case 14: { model.Mobile = value?.Trim(); } break; case 15: { model.Phone = value?.Trim(); } break; case 16: { model.Email = value?.Trim(); } break; case 17: { model.Wechat = value?.Trim(); } break; case 18: { model.QQ = value?.Trim(); } break; case 19: { if (!string.IsNullOrEmpty(value)) { value.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).ToList().ForEach(x => { var node = FindFunction(x); if (node == null) { errors.Error($"Cannot find function: [row: {row}][column: {column}][value: {x}]", column); } else { model.ProfileLabel.CrossFunctions.Add(node); } }); } } break; case 20: { if (!string.IsNullOrEmpty(value)) { value.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).ToList().ForEach(x => { var node = FindCategory(x); if (node == null) { errors.Error($"Cannot find category: [row: {row}][column: {column}][value: {x}]", column); } else { model.ProfileLabel.CrossCategories.Add(node); } }); } } break; case 21: { model.ProfileLabel.BrandExp = value?.Trim(); } break; case 22: { if (!string.IsNullOrEmpty(value)) { value.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).ToList().ForEach(x => { var node = FindChannel(x); if (node == null) { errors.Error($"Cannot find channel: [row: {row}][column: {column}][value: {x}]", column); } else { model.ProfileLabel.CrossChannels.Add(node); } }); } } break; case 23: { model.ProfileLabel.KeyAccountExp = value?.Trim(); } break; case 24: { model.Linkedin = value?.Trim(); } break; case 25: { model.Notes = value?.Trim(); } break; default: break; } }); models.Add(new ModelWrapper <Talent>(model, row)); }); //models.GroupBy(x => x.Model.EnglishName).ToList().ForEach(group => { // if (group.Count() > 1 && !string.IsNullOrEmpty(group.Key)) { // errors.Error($"Repetitive items by english name \"{group.Key}\": [rows: { string.Join(", ", group.Select(x => x.Row)) }]", 101); // } //}); //models.GroupBy(x => x.Model.ChineseName).ToList().ForEach(group => { // if (group.Count() > 1 && !string.IsNullOrEmpty(group.Key)) { // errors.Error($"Repetitive items by chinese name \"{group.Key}\": [rows: { string.Join(", ", group.Select(x => x.Row)) }]", 102); // } //}); models.GroupBy(x => x.Model.Mobile).ToList().ForEach(group => { if (group.Count() > 1 && !string.IsNullOrEmpty(group.Key)) { errors.Error($"Repetitive items by mobile phone \"{group.Key}\": [rows: { string.Join(", ", group.Select(x => x.Row)) }]", 103); } }); try { errors.ThrowIfError(); var collection = this.GetMonCollection <Talent>(); var results = models.Select(x => x.Model).Where(model => !collection.Exist(x => x.Mobile.Equals(model.Mobile))); var count = results.Count(); if (count > 0) { collection.InsertMany(results); } return(Content($"Great!!! there are no problems found. [total: {models.Count}][insert: {count}]")); } catch (Exception ex) { return(Content(ex.Message)); } }
private IActionResult ImportCompany(Stream input) { var errors = new ErrorManager(); var account = this.CurrentAccount(); var models = new List <ModelWrapper <Company> >(); EachRow(input, (row, EachColumn) => { var model = new Company { Account = account, Source = DataSource.Excel }; EachColumn((column, value) => { switch (column) { case 1: { if (string.IsNullOrEmpty(value)) { errors.Error($"Empty name: [row: {row}][column: {column}]"); } else { model.Name = value; } } break; case 2: { if (string.IsNullOrEmpty(value)) { errors.Error($"Empty industry: [row: {row}][column: {column}]"); } else { value.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).ToList().ForEach(x => { var node = FindIndustry(x); if (node == null) { errors.Error($"Cannot find industry: [row: {row}][column: {column}][value: {x}]"); } else { model.Industries.Add(node); } }); } } break; case 3: { if (!string.IsNullOrEmpty(value)) { value.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).ToList().ForEach(x => { var node = FindIndustry(x); if (node == null) { errors.Error($"Cannot find industry: [row: {row}][column: {column}][value: {x}]"); } else { model.Industries.Add(node); } }); } } break; case 4: { if (string.IsNullOrEmpty(value)) { errors.Error($"Empty type: [row: {row}][column: {column}]"); } else { model.Type = GetEnumValue <CompanyType>(value); if (model.Type == null) { errors.Error($"Invalid company type: [row: {row}][column: {column}]"); } } } break; case 5: { if (string.IsNullOrEmpty(value)) { errors.Error($"Empty status: [row: {row}][column: {column}]"); } else { model.Status = GetEnumValue <CompanyStatus>(value); if (model.Status == null) { errors.Error($"Invalid company status: [row: {row}][column: {column}]"); } } } break; default: break; } }); models.Add(new ModelWrapper <Company>(model, row)); }); models.GroupBy(x => x.Model.Name).ToList().ForEach(group => { if (group.Count() > 1) { errors.Error($"Repeations items: [rows: { string.Join(", ", group.Select(x => x.Row)) }]"); } }); try { errors.ThrowIfError(); var companies = this.GetMonCollection <Company>(); var newModels = models.Select(x => x.Model).Where(model => !companies.Exist(x => x.Name.Equals(model.Name))); if (newModels.Count() > 0) { companies.InsertMany(newModels); } return(Content($"Great!!! there are no problems found. [total: {models.Count}][insert: {newModels.Count()}]")); } catch (Exception ex) { return(Content(ex.Message)); } }