using BaseOUDAL;
using System.Data;
using System.Drawing;
using YLErp.Commons;
using YLErp.Model;
using YLErp.Model.Enum;
using YLErp.Modules.ClientModule.Models;
using YLErp.Modules.SystemModule;
using static YLErp.Commons.ExcelHelper;
namespace YLErp.Modules.ClientModule
{
public class ClientDataService : ClientBaseService
{
public ClientDataService(OptUserInfo userInfo) : base(userInfo)
{
}
///
/// 根据客户名称获取客户ID
///
public int[] GetClientIdByName(string clientName)
{
if (string.IsNullOrWhiteSpace(clientName))
{
return Array.Empty();
}
clientName = clientName.Trim();
return DbContext.client.Where(n => n.Name == clientName).Select(n => n.id).ToArray();
}
///
/// 根据客户Id判断客户是否存在
///
///
///
public bool ExsistById(int id)
{
return DbContext.client.AsNoTracking().Any(p => p.id == id);
}
///
/// 根据客户编号获取客户ID
///
public int[] GetClientIdByNumber(string clientNumber)
{
if (string.IsNullOrWhiteSpace(clientNumber))
{
return Array.Empty();
}
clientNumber = clientNumber.Trim();
return DbContext.client.Where(n => n.Number == clientNumber).Select(n => n.id).ToArray();
}
public int GetClientIdByclientNumber(string clientNumber)
{
var clientIds = GetClientIdByNumber(clientNumber);
if (clientIds.Length > 0)
{
return clientIds[0];
}
return 0;
}
///
/// 根据客户ID获取客户编号
///
public Client GetClientById(int clientId)
{
return DbContext.client.Where(n => n.id == clientId).FirstOrDefault();
}
public List GetClientIds()
{
var clients = DbContext.client.AsQueryable().ToList();
return GetClientsByCustomerManager(clients, true).Select(c => c.id).ToList();
}
///
/// 有客户经理角色,则找到客户经理下面的客户
/// 没有客户经理角色,如果需要返回全部,则返回全部客户,否则返回空客户
///
///
///
///
public List GetClientsByCustomerManager(List clients, bool IsAll)
{
if ((UserBLL.GetAccountPostEnum(OptUser.UserId) & (int)AccountPostEnum.客户经理) > 0)
{
var systemUserIds = UserBLL.GetChildrenSystemUsers(OptUser.UserId, false).Select(a => a.Id).ToList();
return clients.Where(a => systemUserIds.Any(b => a.CustomerManagerIdsInt.Contains(b))).ToList();
}
if (IsAll)
{
return clients;
}
else
{
return new List();
}
}
public List GetClientsByCustomerManager(List clients, bool IsAll)
{
if ((UserBLL.GetAccountPostEnum(OptUser.UserId) & (int)AccountPostEnum.客户经理) > 0)
{
var systemUserIds = UserBLL.GetChildrenSystemUsers(OptUser.UserId, false).Select(a => a.Id).ToList();
return clients.Where(a => systemUserIds.Any(b => a.CustomerManagerIdsInt.Contains(b))).Select(p => p.Id).Distinct().ToList();
}
if (IsAll)
{
return clients.Select(p => p.Id).Distinct().ToList();
}
else
{
return new List();
}
}
#region 浙期-导出客户信息的3张表合并到1个excel
///
/// 浙期-导出3个客户表数据合并处理
///
///
///
public byte[] exprotClientAllExcel(List clientList, List colList)
{
//获取配置的客户基础字段
//var colList = ClientEditConfigService.GetClientList();
//yldb.configcolumn.FirstOrDefault(o=>o.userid == )
var dc = new List();
#region 基本信息
var styleBase = new ExcelStyle()
{
BackgroundColor = Color.FromArgb(255, 255, 255),
ForegroundColor = Color.FromArgb(0, 0, 0),
BorderColor = Color.FromArgb(0, 0, 0)
};
var basicFieldCount = 0;
var basicFieldList = new List();
var colNameList = new List();
foreach (var col in colList)
{
//已隐藏的,label为空的,过滤掉
if (col.hidden || string.IsNullOrEmpty(col.label))
{
continue;
}
if (col.name == "ClientName")
{
col.name = "Name";
col.label = "客户名称";
}
else if (col.name == "Name")
{
col.name = "Name";
col.label = "客户名称";
}
basicFieldList.Add(new DataColumnModel(col.label, col.name, typeof(string)));
colNameList.Add(col.name);
}
basicFieldList.Add(new DataColumnModel("客户状态", "ProcessStatus", typeof(string)));
basicFieldList.Add(new DataColumnModel("期权费授信", "IsTradeCredit", typeof(string)));
colNameList.Add("ProcessStatus"); colNameList.Add("IsTradeCredit");
dc.AddRange(basicFieldList.Select(x =>
{
x.Style = styleBase;
return x;
}));
basicFieldCount = basicFieldList.Count();
#endregion
#region 人员信息
var styleContact = new ExcelStyle()
{
BackgroundColor = Color.FromArgb(221, 235, 247),
ForegroundColor = Color.FromArgb(0, 0, 0),
BorderColor = Color.FromArgb(0, 0, 0)
};
var contactFieldConut = 0;
var contactFieldList = new DataColumnModel[]
{
new DataColumnModel("客户名称", "ClientName_Contact",typeof(string)),
new DataColumnModel("姓名", "ContactName_Contact", typeof(string)),
new DataColumnModel("职责类型", "ContactType",typeof(string)),
new DataColumnModel("证件类型", "IdCardType" ,typeof(string)),
new DataColumnModel("证件号码", "IdCardNo", typeof(string)),
new DataColumnModel("电子邮箱", "Email_Contact", typeof(string)),
new DataColumnModel("是否接收相关邮件", "IsReceiveEmail", typeof(string)),
new DataColumnModel("联系电话", "PhoneNumber", typeof(string)),
new DataColumnModel("传真", "Fax", typeof(string)),
new DataColumnModel("联系地址", "Address_Contact", typeof(string))
};
dc.AddRange(contactFieldList.Select(x =>
{
x.Style = styleContact;
return x;
}));
contactFieldConut = contactFieldList.Count();
#endregion
#region 银行信息
var styleBank = new ExcelStyle()
{
BackgroundColor = Color.FromArgb(244, 176, 132),
ForegroundColor = Color.FromArgb(0, 0, 0),
BorderColor = Color.FromArgb(0, 0, 0)
};
var bankFieldCount = 0;
var bankFieldList = new DataColumnModel[]
{
new DataColumnModel("客户名称", "ClientName_Bank",typeof(string)),
new DataColumnModel("开户行", "Bank",typeof(string)),
new DataColumnModel("账号", "Card",typeof(string)),
new DataColumnModel("户名", "AccountName",typeof(string)),
new DataColumnModel("大额行号", "Payment",typeof(string)),
new DataColumnModel("是否有效", "ValidState_Bank",typeof(string)),
new DataColumnModel("客户备注", "Comments",typeof(string))
};
dc.AddRange(bankFieldList.Select(x =>
{
x.Style = styleBank;
return x;
}));
bankFieldCount = bankFieldList.Count();
#endregion
var dataGroupModelList = new List
{
new DataGroupModel{
Caption="客户基本信息",
Length=basicFieldCount,
Style = new ExcelStyle(){ BackgroundColor =Color.FromArgb(255, 255, 255),ForegroundColor = Color.Black,BorderColor = Color.Black}
},
new DataGroupModel{
Caption="客户人员信息",
Length=contactFieldConut,
Style = new ExcelStyle(){ BackgroundColor =Color.FromArgb(221, 235, 247),ForegroundColor = Color.Black,BorderColor = Color.Black}
},
new DataGroupModel{
Caption="银行账号信息",
Length=bankFieldCount,
Style = new ExcelStyle(){ BackgroundColor =Color.FromArgb(244, 176, 132),ForegroundColor = Color.Black,BorderColor = Color.Black}
}
};
//获取数据
var ret = getExprotClientData(clientList, colNameList);
new ExcelHelper().ListToExcel>(dc.ToArray(), ret, "客户合并信息", true, out var buffer, true, dataGroupModelList.ToArray());
return buffer;
}
///
/// 浙期 - 处理需要导出的客户数据
///
///
///
private List> getExprotClientData(List clientList, List colNameList)
{
var clientIds = clientList.Select(o => o.id);
var clientdutyList = DbContext.clientduty.AsNoTracking().AsQueryable().Where(o => o.ApprovalOrder < 1 && clientIds.Contains(o.ClientId ?? 0)).OrderBy(o => o.ClientId).ToList();
var bankcardList = DbContext.bankcard.AsNoTracking().AsQueryable().Where(o => o.ApprovalOrder < 1 && clientIds.Contains(o.ClientId ?? 0)).OrderBy(o => o.ClientName).ToList();
var meteDics = DbContext.ClientMeta.AsNoTracking().AsQueryable().Where(o => clientIds.Contains(o.ClientId)).OrderBy(o => o.ClientId).ToList();
var ret = new List>();
var propertity = typeof(ClientLinq).GetProperties();
foreach (var cItem in clientList)
{
var curClientDuty = clientdutyList.Where(o => o.ClientId == cItem.id).ToList();
var bankCard = bankcardList.Where(o => o.ClientId == cItem.id).ToList();
//当前客户需循环的次数
var index = curClientDuty.Count > bankCard.Count ? curClientDuty.Count : bankCard.Count;
if (index == 0)
{
index = 1;
}
for (int i = 0; i < index; i++)
{
Dictionary dic = new Dictionary();
foreach (var colName in colNameList)
{
//第一条时,客户基本信息赋值
if (i == 0)
{
var p = propertity.Where(O => O.Name == colName).FirstOrDefault();
if (p == null)
{
if (colName.Contains("meta"))
{
var cMeteDic = meteDics.Where(o => o.ClientId == cItem.id).ToList();
var meteName = colName.Replace("meta_", "");
dic[colName] = cMeteDic.FirstOrDefault(o => o.MetaKey == meteName)?.MetaValue;
}
else if (colName.Contains("Str") || colName.Contains("str") || colName.Contains("STR"))
{
var colName2 = colName.Replace("Str", "").Replace("str", "").Replace("STR", "");
var p2 = propertity.Where(O => O.Name == colName2).FirstOrDefault();
dic[colName] = procPropertyType(p2, cItem);
}
else
{
dic[colName] = "";
}
}
else
{
dic[colName] = procPropertyType(p, cItem);
}
}
//客户基本信息赋值为空
else
{
dic[colName] = "";
}
}
//人员信息
if (i < curClientDuty.Count() && curClientDuty[i] != null)
{
dic["ClientName_Contact"] = cItem.Name;
dic["ContactName_Contact"] = curClientDuty[i].ContactName;
var dutyTypes = curClientDuty[i].ContactTypeId.Split(',').ToList();
var result = DbContext.contactype.Where(o => dutyTypes.Contains(o.id.ToString())).Select(o => o.ContactType).ToList();
dic["ContactType"] = string.Join(",", result);
if (curClientDuty[i].IdCardType != null)
{
dic["IdCardType"] = Enum.GetName(typeof(IdCardTypeEnum), curClientDuty[i].IdCardType);
}
else
{
dic["IdCardType"] = "";
}
dic["IdCardNo"] = curClientDuty[i].IdCardNo;
dic["Email_Contact"] = curClientDuty[i].Email;
dic["IsReceiveEmail"] = curClientDuty[i].IsReceiveEmail == 1 ? "是" : "否";
dic["PhoneNumber"] = curClientDuty[i].PhoneNumber;
dic["Fax"] = curClientDuty[i].Fax;
dic["Address_Contact"] = curClientDuty[i].Address;
}
else
{
dic["ClientName_Contact"] = "";
dic["ContactName_Contact"] = "";
dic["ContactType"] = "";
dic["IdCardType"] = "";
dic["IdCardNo"] = "";
dic["Email_Contact"] = "";
dic["IsReceiveEmail"] = "";
dic["PhoneNumber"] = "";
dic["Fax"] = "";
dic["Address_Contact"] = "";
}
//银行信息
if (i < bankCard.Count() && bankCard[i] != null)
{
dic["ClientName_Bank"] = cItem.Name;
dic["Bank"] = bankCard[i].Bank;
dic["Card"] = bankCard[i].Card;
dic["AccountName"] = cItem.Name;
dic["Payment"] = bankCard[i].Payment;
dic["ValidState_Bank"] = bankCard[i].ValidState == "InValid" ? "无效" : "有效";
dic["Comments"] = bankCard[i].Comments;
}
else
{
dic["ClientName_Bank"] = "";
dic["Bank"] = "";
dic["Card"] = "";
dic["AccountName"] = "";
dic["Payment"] = "";
dic["ValidState_Bank"] = "";
dic["Comments"] = "";
}
ret.Add(dic);
}
}
return ret;
}
///
/// 客户数据源处理
///
///
///
///
private string procPropertyType(System.Reflection.PropertyInfo p, ClientLinq client)
{
var resultVal = "";
var pType = p.PropertyType;
if (pType == typeof(string))
{
resultVal = p.GetValue(client)?.ToString() ?? "";
}
else if (pType.IsEnum)
{
resultVal = Enum.Parse(pType, p.GetValue(client)?.ToString() ?? "").ToString();
if (p.Name == "TradingInstType")
{
switch (resultVal.ToLower())
{
case "none": resultVal = "全部"; break;
case "equity": resultVal = "权益类"; break;
case "commodity": resultVal = "商品类"; break;
}
}
}
else if (pType == typeof(bool) || pType == typeof(bool?))
{
if (p.GetValue(client) == null)
{
if (p.Name == "AgreementBookType")
{
resultVal = "单向";
}
else
{
resultVal = "";
}
}
else
{
if (p.Name == "AgreementBookType")
{
resultVal = p.GetValue(client)?.ToString() == "true" ? "单向" : "双向";
}
else
{
resultVal = p.GetValue(client)?.ToString() == "true" ? "是" : "否";
}
}
}
else if (pType == typeof(double) || pType == typeof(double?))
{
resultVal = p.GetValue(client)?.ToString();
}
else if (pType == typeof(int) || pType == typeof(int?))
{
if (p.GetValue(client) == null)
{
if (p.Name == "MarginOptionType")
{
resultVal = "单向追保";
}
else
{
resultVal = "";
}
}
else
{
if (p.Name == "MarginOptionType")
{
switch (p.GetValue(client)?.ToString())
{
case "0": resultVal = "单向追保"; break;
case "1": resultVal = "双向追保"; break;
case "2": resultVal = "对手方单向追保"; break;
case "3": resultVal = "其他"; break;
}
}
else
{
resultVal = p.GetValue(client)?.ToString() == "1" ? "是" : "否";
}
}
}
else if (pType == typeof(DateTime) || pType == typeof(DateTime?))
{
resultVal = p.GetValue(client)?.ToString();
}
return resultVal;
}
#endregion
}
}