public async Task <CustomerProfileModel> GetCustomerProfile(string business_id, string id) { try { List <System.Threading.Tasks.Task> tasks = new List <System.Threading.Tasks.Task>(); CustomerProfileModel model = new CustomerProfileModel(); //var key = "GetCustomerProfile" + business_id + id; //var lst = await CacheBase.cacheManagerGet<CustomerProfileModel>(key); //if (lst != null) // return lst; Customer c = null; FacebookUserProfile profile = null; var date1 = DateTime.UtcNow; Thread thread = null; //tasks.Add(System.Threading.Tasks.Task.Factory.StartNew(() => //{ c = _customerService.GetById(business_id, id); if (c == null) { thread = _threadService.GetById(business_id, id); if (thread != null) { c = _customerService.GetById(business_id, thread.customer_id); } } //})); //System.Threading.Tasks.Task.WaitAll(tasks.ToArray()); if (c == null) { return(model); } if (c != null) { if (c.active_thread == null) { thread = _threadService.GetByIdFromCustomerId(business_id, c.id); if (thread != null) { c.active_thread = JsonConvert.SerializeObject(thread); System.Threading.Tasks.Task.Factory.StartNew(() => { _customerService.CreateCustomer(c, false); }); } } } var sp = c.channel_id.Split('_'); var channel_ext_id = sp[sp.Length - 1]; #region lay ten that neu da tao don hang if (string.IsNullOrWhiteSpace(c.real_name)) { c.real_name = c.name; _customerService.UpdateRealName(c.business_id, c.id, c.name); } if (c.real_name == c.name && !string.IsNullOrWhiteSpace(c.phone)) { try { System.Threading.Tasks.Task.Factory.StartNew(() => { var uri = _appSettings.Value.BaseUrls.ApiOrder + "api/order/list?logonId=" + c.phone + "&imei=&token=@bazavietnam"; var orders = Core.Helpers.WebHelper.HttpGetAsyncSting(uri).Result; if (orders.Length > 10) { var orderBs = JsonConvert.DeserializeObject(orders); var array = (JArray)orderBs; var orderItem = array[array.Count - 1]; { if (orderItem["OrderId"].ToString() != "") { var urlDetail = _appSettings.Value.BaseUrls.ApiOrder + "api/order/detail2?id=" + orderItem["OrderId"] + "&logonId=" + c.phone + "&imei=&token=@bazavietnam"; var orderDetail = Core.Helpers.WebHelper.HttpGetAsyncSting(urlDetail).Result; var obj = JsonConvert.DeserializeObject <JToken>(orderDetail); if (obj != null) { var real_name = (string)obj["Fullname"]; if (!string.IsNullOrWhiteSpace(real_name)) { _customerService.UpdateRealName(c.business_id, c.id, real_name); } } } } } }); } catch (Exception ex) { } } #endregion var customer = new CustomerModel(c); var customer_id1 = ""; var customer_id2 = customer.id; model.id = customer.id; model.customer_id = customer.id; if (string.IsNullOrWhiteSpace(customer.sex) || (!string.IsNullOrWhiteSpace(customer.avatar) && customer.avatar.IndexOf(_appSettings.Value.BaseUrls.ApiSaveImage) < 0)) { tasks.Add(System.Threading.Tasks.Task.Factory.StartNew(() => { try { var channel = _channelService.GetById(business_id, customer.channel_id); profile = _facebookConversationService.GetProfile(customer.active_thread.ext_id, channel.token); if (profile != null && !string.IsNullOrWhiteSpace(profile.sex)) { c.sex = profile.sex; if (string.IsNullOrWhiteSpace(customer.avatar) || (!string.IsNullOrWhiteSpace(customer.avatar) && customer.avatar.IndexOf(_appSettings.Value.BaseUrls.ApiSaveImage) < 0)) { c.avatar = ImagesService.UpsertImageStore(profile.avatar, _appSettings.Value).Result; } _customerService.CreateCustomer(c, false); } } catch { } })); } tasks.Add(System.Threading.Tasks.Task.Factory.StartNew(() => { model.referrals = new List <ReferralModel>(); if (!string.IsNullOrWhiteSpace(customer_id1)) { foreach (var item in _referralService.GetReferralsByCustomer(business_id, customer_id1, new Paging { Limit = 5 }).Result) { model.referrals.Add(new ReferralModel(item)); } } if (customer_id2 != customer_id1 && !string.IsNullOrWhiteSpace(customer_id2)) { foreach (var item in _referralService.GetReferralsByCustomer(business_id, customer_id2, new Paging { Limit = 5 }).Result) { model.referrals.Add(new ReferralModel(item)); } model.referrals = model.referrals.OrderByDescending(r => r.created_time).Take(5).ToList(); } })); tasks.Add(System.Threading.Tasks.Task.Factory.StartNew(() => { model.id = id; model.name = customer.name; model.blocked = customer.blocked; model.phone = !string.IsNullOrWhiteSpace(customer.phone) ? customer.phone : ""; model.avatar = customer.avatar != null && customer.avatar.IndexOf("http://") < 0 && customer.avatar.IndexOf("https://") < 0 ? _appSettings.Value.BaseUrls.Api + customer.avatar : customer.avatar; model.profile_ext_url = "/customers/openprofile/" + business_id + "/" + id; model.openlink = "/" + business_id + "/messages/openlink/?thread_id=" + customer.active_thread.id; model.last_contacted_since = Core.Helpers.CommonHelper.UnixTimestampToDateTime(customer.timestamp).ToLocalTime().ToString("dd/MM/yyyy"); model.last_visits = customer.active_thread.last_visits; model.sex = !string.IsNullOrWhiteSpace(customer.sex) ? customer.sex : ""; model.weight = customer.weight; model.height = customer.height; model.address = !string.IsNullOrWhiteSpace(customer.address) ? customer.address : ""; model.phone_list = c.phone_list; })); tasks.Add(System.Threading.Tasks.Task.Factory.StartNew(() => { model.notes = new List <NoteModel>(); if (!string.IsNullOrWhiteSpace(customer_id1)) { foreach (var item in _noteService.GetCustomerNotes(business_id, customer_id1, new Paging { Limit = 10 })) { model.notes.Add(new NoteModel(item)); } } if (customer_id2 != customer_id1 && !string.IsNullOrWhiteSpace(customer_id2)) { foreach (var item in _noteService.GetCustomerNotes(business_id, customer_id2, new Paging { Limit = 10 })) { model.notes.Add(new NoteModel(item)); } model.notes = model.notes.OrderByDescending(n => n.created_time).Take(10).ToList(); } })); tasks.Add(System.Threading.Tasks.Task.Factory.StartNew(() => { model.tickets = new List <TicketModel>(); if (!string.IsNullOrWhiteSpace(customer_id1)) { foreach (var item in _ticketService.GetCustomerTickets(business_id, customer_id1, new Paging { Limit = 10 })) { model.tickets.Add(new TicketModel(item)); } } if (customer_id2 != customer_id1 && !string.IsNullOrWhiteSpace(customer_id2)) { foreach (var item in _ticketService.GetCustomerTickets(business_id, customer_id2, new Paging { Limit = 10 })) { model.tickets.Add(new TicketModel(item)); } model.tickets = model.tickets.OrderByDescending(t => t.created_time).Take(10).ToList(); } })); tasks.Add(System.Threading.Tasks.Task.Factory.StartNew(() => { model.starred_messages = new List <MessageModel>(); if (!string.IsNullOrWhiteSpace(customer_id1)) { foreach (var item in _messageService.GetStarredMesagesByCustomer(business_id, new Paging { Limit = 10 }, customer_id1).Result) { model.starred_messages.Add(new MessageModel(item)); } } if (customer_id2 != customer_id1 && !string.IsNullOrWhiteSpace(customer_id2)) { foreach (var item in _messageService.GetStarredMesagesByCustomer(business_id, new Paging { Limit = 10 }, customer_id2).Result) { model.starred_messages.Add(new MessageModel(item)); } model.starred_messages = model.starred_messages.OrderByDescending(m => m.timestamp).Take(10).ToList(); } })); tasks.Add(System.Threading.Tasks.Task.Factory.StartNew(() => { model.last_messages = new List <MessageModel>(); if (!string.IsNullOrWhiteSpace(customer_id1)) { foreach (var item in _messageService.GetByCustomerExcludeCurrentThread(business_id, customer_id1, channel_ext_id, new Paging { Limit = 10, Next = "9999999999" }).Result) { item.sender_name = item.sender_ext_id == item.channel_ext_id ? item.sender_name : customer.name; model.last_messages.Add(new MessageModel(item)); } } if (customer_id2 != customer_id1 && !string.IsNullOrWhiteSpace(customer_id2)) { foreach (var item in _messageService.GetByCustomerExcludeCurrentThread(business_id, customer_id2, channel_ext_id, new Paging { Limit = 10, Next = "9999999999" }).Result) { item.sender_name = item.sender_ext_id == item.channel_ext_id ? item.sender_name : customer.name; model.last_messages.Add(new MessageModel(item)); } model.last_messages = model.last_messages.OrderByDescending(m => m.timestamp).Take(10).ToList(); } })); System.Threading.Tasks.Task.WaitAll(tasks.ToArray()); model.sex = string.IsNullOrWhiteSpace(model.sex) && profile != null && !string.IsNullOrWhiteSpace(profile.sex) ? profile.sex : model.sex; model.avatar = string.IsNullOrWhiteSpace(model.avatar) && profile != null && !string.IsNullOrWhiteSpace(profile.avatar) ? profile.avatar : model.avatar; // CacheBase.cacheManagerSetForProceduce(key, model, DateTime.Now.AddMinutes(10), null, null, false); return(model); } catch (Exception ex) { return(null); } }