using System.Linq.Expressions; using YLErp.Configuration; using YLErp.Models; namespace YLErp.Modules.ClientModule { /// /// 客户数据提供接口实现 /// public static class ClientDataQueryService { /// /// 获取客户主要信息 /// public static IEnumerable GetClientsFromDB(Expression> predicate) { using (var db = DbContextFactory.GetClientDbContext(null)) { var query = db.client.AsNoTracking().AsQueryable(); if (predicate != null) { query = query.Where(predicate); } return query.Select(ClientMainInfo.FromClient).ToArray(); } } /// /// 检查客户是否存在 /// /// 客户ID /// 如果未找到是否抛出异常 public static bool CheckClientExists(int clientId, bool throwIfNotFound = false) { if (clientId > 0 && DataCacheProvider.GetClientDataSource().GetData(clientId) != null) { return true; } if (throwIfNotFound) { throw new ServiceException("客户信息不存在,id:" + clientId); } return false; } /// /// 获取客户主要信息 /// /// 客户ID /// 如果未找到是否抛出异常 public static ClientMainInfo GetClient(int clientId, bool throwIfNotFound = false) { if (clientId < 1) { if (throwIfNotFound) { throw new ServiceException("客户信息不存在,id:" + clientId); } return null; } var data = DataCacheProvider.GetClientDataSource().GetData(clientId); if (data != null) { return ClientMainInfo.FromClient(data); } if (throwIfNotFound) { throw new ServiceException("客户信息不存在,id:" + clientId); } else { return null; } } /// /// 获取客户主要信息 /// public static ClientMainInfo GetClient(string clientName, bool throwIfNotFound = false) { if (string.IsNullOrEmpty(clientName)) { if (throwIfNotFound) { throw new ServiceException("查询参数错误,客户名称为空"); } return null; } var data = DataCacheProvider.GetClientDataSource().AsQueryable() .FirstOrDefault(n => clientName.Equals(n.Name, StringComparison.OrdinalIgnoreCase)); if (data != null) { return ClientMainInfo.FromClient(data); } if (throwIfNotFound) { throw new ServiceException("客户信息不存在,clientName:" + clientName); } else { return null; } } /// /// 获取客户主要信息 /// public static ClientMainInfo GetClientByNumber(string clientNumber, bool throwIfNotFound = false) { if (string.IsNullOrEmpty(clientNumber)) { if (throwIfNotFound) { throw new ServiceException("查询参数错误,客户名称为空"); } return null; } var data = DataCacheProvider.GetClientDataSource().AsQueryable() .FirstOrDefault(n => clientNumber.Equals(n.Number, StringComparison.OrdinalIgnoreCase)); if (data != null) { return ClientMainInfo.FromClient(data); } if (throwIfNotFound) { throw new ServiceException("客户信息不存在,客户编号:" + clientNumber); } else { return null; } } /// /// 获取客户主要信息 /// public static ClientMainInfo GetClientByGuid(string clientGuid, bool throwIfNotFound = false) { if (string.IsNullOrEmpty(clientGuid)) { if (throwIfNotFound) { throw new ServiceException("查询参数不能为空"); } return null; } using (var db = DbContextFactory.GetClientDbContext(null)) { var data = db.client.Where(c => c.Guid == clientGuid).Select(ClientMainInfo.FromClient).FirstOrDefault(); if (data == null && throwIfNotFound) { throw new ServiceException("客户信息不存在,GUID:" + clientGuid); } return data; } } public static List GetChlidClientIdsByParentId(int parentClientId) { using (var db = DbContextFactory.GetClientDbContext(null)) { List clientMainInfos = new List(); var data = db.client.Where(a => a.ParentId == parentClientId); if (data.Any()) { foreach (var c in data) { clientMainInfos.Add(c.id); } } return clientMainInfos; } } /// /// 获取客户联系人 /// public static ClientDuty GetClientDuty(int clientId, bool byContactType = false) { using (var db = DbContextFactory.GetClientDbContext(null)) { if (byContactType) { return db.clientduty.AsNoTracking() .OrderBy(x => x.ContactTypeId).ThenBy(x => x.id) .FirstOrDefault(x => x.ApprovalOrder < 1 && x.ClientId == clientId && (x.ContactTypeId.Contains("1") || x.ContactTypeId.Contains("2"))); } return db.clientduty.AsNoTracking().FirstOrDefault(x => x.ApprovalOrder < 1 && x.ClientId == clientId); } } /// /// 获取客户联系人 /// public static List GetClientDuties(int clientId) { using (var db = DbContextFactory.GetClientDbContext(null)) { return db.clientduty.AsNoTracking().Where(x => x.ApprovalOrder < 1 && x.ClientId == clientId).ToList(); } } /// /// 获取客户银行卡 /// public static ClientBankCard GetBankCard(int clientId, string cardNumber = null) { using (var db = DbContextFactory.GetClientDbContext(null)) { var predicate = PredicateBuilder.Create(x => x.ApprovalOrder < 1 && x.ClientId == clientId && x.ValidState != ConsGlobal.InValid); if (!string.IsNullOrEmpty(cardNumber)) { predicate = predicate.And(n => n.Card == cardNumber); } return db.bankcard.AsNoTracking().FirstOrDefault(predicate); } } /// /// 根据客户Id获取该客户所有需要收邮件的邮件地址 /// /// /// 多个邮件地址,用分号隔开 public static IEnumerable GetClientEmails(int clientId, bool includeClientMail = false, List receiver = null,bool isEodValue = false) { if (clientId < 1) { return Enumerable.Empty(); } using (var db = DbContextFactory.GetClientDbContext(null)) { var clientContactMails = new List(); var clientContacts = db.clientduty.Where(x => x.ApprovalOrder < 1 && x.ClientId == clientId && (x.DeadLine == null || x.DeadLine > DateTime.Now) && x.IsReceiveEmail == 1 && x.Email != null) .ToList(); if (receiver != null && receiver.Count > 0) { foreach (var item in clientContacts) { var ids = item.ContactTypeId.Split(','); if (ids.Intersect(receiver).Count() != 0) { clientContactMails.Add(item.Email); } } } else { if (isEodValue) { foreach (var item in clientContacts) { var ids = item.ContactTypeId.Split(','); //1 是估值接收人 if (ids.Contains("1")) { clientContactMails.Add(item.Email); } } } else { clientContactMails = clientContacts.Select(o => o.Email).ToList(); } } if (includeClientMail) { var client = db.client.Where(x => x.id == clientId).Select(n => new { n.Email }).FirstOrDefault(); if (client == null) { return null; } if (!string.IsNullOrWhiteSpace(client.Email)) { clientContactMails.Add(client.Email); } } return clientContactMails.Where(n => !string.IsNullOrWhiteSpace(n)) .SelectMany(x => x.Trim().Split(new[] { ',', ';', '/' }, StringSplitOptions.RemoveEmptyEntries)) .ToHashSet(StringComparer.OrdinalIgnoreCase); } } /// /// 获取所有客户id /// public static IEnumerable GetAllClientIds(bool onlyOpened = true) { using (var db = DbContextFactory.GetClientDbContext(null)) { var query = db.client.AsNoTracking().AsQueryable(); if (onlyOpened) { query = query.Where(n => n.ProcessStatus == "已开户"); } return query.Select(n => n.id).ToArray(); } } /// /// 判断是否 平台方客户(平台方配置的客户本身) /// public static bool IsBaseClient(int clientId) { using (var db = DbContextFactory.GetClientDbContext(null)) { var baseName = PS.Config.CompanyFullName; return db.client.Any(t => t.id == clientId && baseName.Equals(t.Name)); } } } }