private static int CreateLeadInAmo(Lead1C lead1C, IAmoRepo <Lead> leadRepo, int acc_id, int contact_id, int course_id, int company_id, RecentlyUpdatedEntityFilter filter) { Lead lead = new() { name = "Новая сделка", price = lead1C.price, responsible_user_id = UserList.GetAmoUser(lead1C.responsible_user), custom_fields_values = new(), _embedded = new() { tags = new() { new() { name = "1C" } } } }; if (lead.responsible_user_id is null) { lead.responsible_user_id = UserList.GetAmoUser(lead1C.author); } AddUIDToEntity(lead1C, acc_id, lead); PopulateCFs(lead1C, acc_id, lead); PopulateConnectedEntities(contact_id, company_id, lead); try { var result = leadRepo.AddNewComplex(lead).ToList(); result.ForEach(x => filter.AddEntity(x)); if (result.Any()) { EntityLink link = new() { to_entity_id = course_id, to_entity_type = "catalog_elements", metadata = new() { quantity = 1, catalog_id = GetCatalogId(acc_id) } }; leadRepo.LinkEntity(result.First(), link); return(result.First()); } else { throw new Exception("Amo returned no lead Ids."); } } catch (Exception e) { throw new Exception($"Unable to update lead {lead1C.lead_id_1C} in amo: {e.Message}"); } }
public Task Send() { if (_token.IsCancellationRequested) { _processQueue.Remove($"corp2ret-{_leadNumber}"); return(Task.FromCanceled(_token)); } try { #region Getting source entities Lead sourceLead = _sourceLeadRepo.GetById(_leadNumber); if (sourceLead._embedded is null || sourceLead._embedded.contacts is null || !sourceLead._embedded.contacts.Any()) { return(Task.CompletedTask); } var sourceContacts = _sourceContRepo.BulkGetById(sourceLead._embedded.contacts.Select(x => (int)x.id)); #endregion Lead lead = new() { name = sourceLead.name, responsible_user_id = GetResponsibleUserId((int)sourceLead.responsible_user_id), _embedded = new() }; List <Note> calls = new(); List <Note> notes = new(); foreach (var c in sourceContacts) { #region Prepare contacts string phone = c.GetCFStringValue(33575); string email = c.GetCFStringValue(33577); phone = phone.Trim().Replace("+", "").Replace("-", "").Replace(" ", "").Replace("(", "").Replace(")", ""); phone = phone.StartsWith("89") ? $"7{phone[1..]}" : phone; email = email.Trim().Replace(" ", ""); if (phone == "" && email == "") { continue; } #endregion var contactNotes = _sourceContRepo.GetEntityNotes((int)c.id); notes.AddRange(contactNotes.Where(x => x.note_type == "common")); calls.AddRange(contactNotes.Where(x => x.note_type == "call_in" || x.note_type == "call_out")); #region Checking for contacts List <Contact> similarContacts = new(); Contact contact = new() { name = c.name, responsible_user_id = lead.responsible_user_id, }; try { if (phone != "") { similarContacts.AddRange(_contRepo.GetByCriteria($"query={phone}")); } if (email != "") { similarContacts.AddRange(_contRepo.GetByCriteria($"query={email}")); } } catch (Exception e) { _log.Add($"Не удалось осуществить поиск похожих контактов: {e}"); } if (similarContacts.Any()) { contact.id = similarContacts.First().id; contact.responsible_user_id = similarContacts.First().responsible_user_id; lead.responsible_user_id = similarContacts.First().responsible_user_id; _log.Add($"Найден похожий контакт: {similarContacts.First().id}."); } else { contact.custom_fields_values = new(); if (email != "") { contact.AddNewCF(264913, email); } if (phone != "") { contact.AddNewCF(264911, phone); } } lead._embedded.contacts = new() { contact }; break; #endregion } #region Setting pipeline and status if any lead.pipeline_id = 3198184; lead.status_id = 32532880; #endregion #region Getting comments var leadNotes = _sourceLeadRepo.GetEntityNotes(_leadNumber); notes.AddRange(leadNotes.Where(x => x.note_type == "common")); calls.AddRange(leadNotes.Where(x => x.note_type == "call_in" || x.note_type == "call_out")); StringBuilder sb = new(); if (sourceLead.HasCF(748383)) //Тип обращения { sb.Append($"{sourceLead.GetCFStringValue(748383)}\r\n"); } foreach (var n in notes) { sb.Append($"{n.parameters.text}\r\n"); } string comment = sb.ToString(); #endregion #region Tags List <Tag> tags = new() { TagList.GetRetTagByName("Сделка из корп. отдела") }; if (sourceLead._embedded is not null && sourceLead._embedded.tags is not null && sourceLead._embedded.tags.Any()) { foreach (var t in sourceLead._embedded.tags) { tags.Add(new() { name = t.name }); } } lead._embedded.tags = new(tags); #endregion #region Custom fields lead.AddNewCF(724771, sourceLead.id); //поле corp_id if (sourceLead.HasCF(748383)) //Тип обращения { lead.AddNewCF(639075, sourceLead.GetCFValue(748383)); } if (sourceLead.HasCF(758213)) //Сайт { lead.AddNewCF(639081, sourceLead.GetCFValue(758213)); } if (sourceLead.HasCF(758215)) //Посадочная страница { lead.AddNewCF(639083, sourceLead.GetCFValue(758215)); } if (sourceLead.HasCF(748385)) //Маркер { lead.AddNewCF(639085, sourceLead.GetCFValue(748385)); } if (sourceLead.HasCF(758217)) //roistat { lead.AddNewCF(639073, sourceLead.GetCFValue(758217)); } #endregion var created = _leadRepo.AddNewComplex(lead); _log.Add($"Создана новая сделка {created.First()}"); #region Adding notes if (created.Any() && comment != "") { _leadRepo.AddNotes(created.First(), comment); } if (created.Any() && calls.Any()) { foreach (var n in calls.Select(x => new Note() { entity_id = created.First(), note_type = x.note_type, parameters = x.parameters })) { _leadRepo.AddNotes(n); } } #endregion _processQueue.Remove($"corp2ret-{_leadNumber}"); return(Task.CompletedTask); } catch (Exception e) { _processQueue.Remove($"corp2ret-{_leadNumber}"); _log.Add($"Не получилось перенести сделку {_leadNumber} из корп. в розницу: {e.Message}."); return(Task.FromException(e)); } }