public ThreadFeed GetThreads(string business_id, [FromQuery] ThreadFilter filter) { if (filter.status == "unread") { filter.status = ""; filter.flag = "unread"; } if (filter.status == "nonreply") { filter.status = ""; filter.flag = "nonreply"; } var data = _threadService.GetThreads(business_id, filter.channel_id == "all" ? "" : filter.channel_id, filter.agent_id == "all" ? "" : filter.agent_id, filter.status, filter.flag, filter.search ?? "", new Paging { Limit = filter.quantity, Next = (filter.first == 0 ? 9999999999 : filter.first).ToString() }).Result; return(new ThreadFeed { Data = data == null ? new List <ThreadModel>() : data.Select(t => new ThreadModel(t)) }); }
public async Task <CustomerFeed> GetCustomers(string business_id, [FromQuery] ThreadFilter filter) { IEnumerable <Customer> data = null; filter.channel_id = filter.channel_id == "all" ? "" : filter.channel_id; filter.agent_id = filter.agent_id == "all" ? "" : filter.agent_id; filter.search = filter.search ?? ""; filter.first = filter.first == 0 ? 999999999999999 : filter.first; // var now = DateTime.Now.ToString("dd-MM-yyyy"); if (!string.IsNullOrWhiteSpace(filter.from_date) && !string.IsNullOrWhiteSpace(filter.to_date)) { var time = Convert.ToInt64(filter.to_date.Substring(0, 10)); if (filter.first >= time) { filter.first = time; } } switch (filter.status) { case "unread": filter.status = ""; filter.flag = "unread"; break; case "nonreply": filter.status = ""; filter.flag = "nonreply"; break; case "open": filter.status = ""; filter.flag = "open"; break; default: break; } if (string.IsNullOrWhiteSpace(filter.search)) { if (string.IsNullOrWhiteSpace(filter.status)) { switch (filter.flag) { case "unread": if (string.IsNullOrWhiteSpace(filter.channel_id)) { if (string.IsNullOrWhiteSpace(filter.agent_id)) { data = await _customerService.GetUnreadCustomers(business_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } else { data = await _customerService.GetUnreadCustomersByAgent(business_id, filter.agent_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } } else { if (string.IsNullOrWhiteSpace(filter.agent_id)) { data = await _customerService.GetUnreadCustomersByChannel(business_id, filter.channel_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } else { data = await _customerService.GetUnreadCustomersByChannelAndAgent(business_id, filter.channel_id, filter.agent_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } } break; case "nonreply": if (string.IsNullOrWhiteSpace(filter.channel_id)) { if (string.IsNullOrWhiteSpace(filter.agent_id)) { data = await _customerService.GetNonReplyCustomers(business_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } else { data = await _customerService.GetNonReplyCustomersByAgent(business_id, filter.agent_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } } else { if (string.IsNullOrWhiteSpace(filter.agent_id)) { data = await _customerService.GetNonReplyCustomersByChannel(business_id, filter.channel_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } else { data = await _customerService.GetNonReplyCustomersByChannelAndAgent(business_id, filter.channel_id, filter.agent_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } } break; case "open": if (string.IsNullOrWhiteSpace(filter.channel_id)) { if (string.IsNullOrWhiteSpace(filter.agent_id)) { data = await _customerService.GetOpenCustomers(business_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } else { data = await _customerService.GetOpenCustomersByAgent(business_id, filter.agent_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } } else { if (string.IsNullOrWhiteSpace(filter.agent_id)) { data = await _customerService.GetOpenCustomersByChannel(business_id, filter.channel_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } else { data = await _customerService.GetOpenCustomersByChannelAndAgent(business_id, filter.channel_id, filter.agent_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } } break; default: if (string.IsNullOrWhiteSpace(filter.channel_id)) { if (string.IsNullOrWhiteSpace(filter.agent_id)) { data = await _customerService.GetAllCustomers(business_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } else { data = await _customerService.GetAllCustomersByAgent(business_id, filter.agent_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } } else { if (string.IsNullOrWhiteSpace(filter.agent_id)) { data = await _customerService.GetAllCustomersByChannel(business_id, filter.channel_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } else { data = await _customerService.GetAllCustomersByChannelAndAgent(business_id, filter.channel_id, filter.agent_id, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }); } } break; } } } if (data == null) { data = _customerService.SearchCustomers(business_id, filter.channel_id, filter.agent_id, filter.status, filter.flag, filter.search, new Paging { Limit = filter.quantity, Next = (filter.first).ToString() }).Result; if (data != null) { foreach (var v in data) { if (v.active_thread == null) { var thread = _threadService.GetByIdFromCustomerId(business_id, v.id); if (thread == null) { var page = new Paging(); page.Limit = 1; var mess = await _messageService.GetByCustomer(v.business_id, v.id, page); if (mess != null && mess.Count > 0) { thread = _threadService.GetById(mess[0].business_id, mess[0].thread_id); } } if (thread != null) { v.active_thread = JsonConvert.SerializeObject(thread); _customerService.CreateCustomer(v, false); } } } } } return(new CustomerFeed { Data = data == null ? new List <CustomerModel>() : data.Where(t => !string.IsNullOrWhiteSpace(t.active_thread)).Select(t => new CustomerModel(t)) }); }