320 lines
11 KiB
C#
320 lines
11 KiB
C#
using System.Linq.Expressions;
|
|
using YLErp.Configuration;
|
|
using YLErp.Models;
|
|
|
|
namespace YLErp.Modules.ClientModule
|
|
{
|
|
/// <summary>
|
|
/// 客户数据提供接口实现
|
|
/// </summary>
|
|
public static class ClientDataQueryService
|
|
{
|
|
/// <summary>
|
|
/// 获取客户主要信息
|
|
/// </summary>
|
|
public static IEnumerable<ClientMainInfo> GetClientsFromDB(Expression<Func<Client, bool>> 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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查客户是否存在
|
|
/// </summary>
|
|
/// <param name="clientId">客户ID</param>
|
|
/// <param name="throwIfNotFound">如果未找到是否抛出异常</param>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户主要信息
|
|
/// </summary>
|
|
/// <param name="clientId">客户ID</param>
|
|
/// <param name="throwIfNotFound">如果未找到是否抛出异常</param>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户主要信息
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户主要信息
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户主要信息
|
|
/// </summary>
|
|
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<int> GetChlidClientIdsByParentId(int parentClientId)
|
|
{
|
|
using (var db = DbContextFactory.GetClientDbContext(null))
|
|
{
|
|
List<int> clientMainInfos = new List<int>();
|
|
var data = db.client.Where(a => a.ParentId == parentClientId);
|
|
if (data.Any())
|
|
{
|
|
foreach (var c in data)
|
|
{
|
|
clientMainInfos.Add(c.id);
|
|
}
|
|
}
|
|
return clientMainInfos;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户联系人
|
|
/// </summary>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户联系人
|
|
/// </summary>
|
|
public static List<ClientDuty> GetClientDuties(int clientId)
|
|
{
|
|
using (var db = DbContextFactory.GetClientDbContext(null))
|
|
{
|
|
return db.clientduty.AsNoTracking().Where(x => x.ApprovalOrder < 1 && x.ClientId == clientId).ToList();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户银行卡
|
|
/// </summary>
|
|
public static ClientBankCard GetBankCard(int clientId, string cardNumber = null)
|
|
{
|
|
using (var db = DbContextFactory.GetClientDbContext(null))
|
|
{
|
|
var predicate = PredicateBuilder.Create<ClientBankCard>(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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据客户Id获取该客户所有需要收邮件的邮件地址
|
|
/// </summary>
|
|
/// <param name="clientId"></param>
|
|
/// <returns>多个邮件地址,用分号隔开</returns>
|
|
public static IEnumerable<string> GetClientEmails(int clientId, bool includeClientMail = false, List<string> receiver = null)
|
|
{
|
|
if (clientId < 1)
|
|
{
|
|
return Enumerable.Empty<string>();
|
|
}
|
|
|
|
using (var db = DbContextFactory.GetClientDbContext(null))
|
|
{
|
|
var clientContactMails = new List<string>();
|
|
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
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有客户id
|
|
/// </summary>
|
|
public static IEnumerable<int> 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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断是否 平台方客户(平台方配置的客户本身)
|
|
/// </summary>
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
}
|