using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using YLErp.DBModels.Enums;
using YLErp.Helpers;
using YLErp.Model.Enum;
using YLErp.Office;
namespace YLErp.Modules.ClientModule
{
///
/// 客户信息查询服务
///
public class ClientInfoQueryService : ClientBaseService
{
public ClientInfoQueryService(OptUserInfo userInfo) : base(userInfo)
{
}
public ClientInfoQueryResult GetClientInfo(ClientInfoQueryModel queryModel)
{
Client client = null;
var clientQuery = DbContext.client.AsNoTracking();
if (queryModel.ClientId > 0)
{
client = clientQuery.FirstOrDefault(n => n.id == queryModel.ClientId);
}
else if (!string.IsNullOrWhiteSpace(queryModel.ClientNumber))
{
client = clientQuery.FirstOrDefault(n => n.Number == queryModel.ClientNumber);
}
else if (!string.IsNullOrWhiteSpace(queryModel.ClientName))
{
client = clientQuery.FirstOrDefault(n => n.Name == queryModel.ClientName);
}
if (client == null)
{
return null;
}
client.MetaDic = new ClientMetaService(this).GetMetaDic(client.id);
queryModel.ClientId = client.id;
var result = new ClientInfoQueryResult
{
ClientInfo = client,
Duties = GetClientDuties(queryModel),
BankCards = GetClientBankCards(queryModel)
};
return result;
}
///
/// 客户银行卡列表
///
public IEnumerable GetClientBankCards(ClientInfoQueryModel queryModel)
{
IEnumerable bankcards = null;
if (queryModel.ClientId > 0)
{
bankcards = DbContext.bankcard.AsNoTracking().Where(n => n.ApprovalOrder < 1 && n.ClientId == queryModel.ClientId).ToArray();
}
else
{
Expression> predicate;
if (!string.IsNullOrEmpty(queryModel.ClientNumber))
{
predicate = PredicateBuilder.Create(t => t.Number == queryModel.ClientNumber);
}
else if (!string.IsNullOrEmpty(queryModel.ClientName))
{
predicate = PredicateBuilder.Create(t => t.Name == queryModel.ClientName);
}
else
{
return Array.Empty();
}
var query = from bankcard in DbContext.bankcard.Where(x=> x.ApprovalOrder < 1).AsNoTracking()
join client in DbContext.client.Where(predicate) on bankcard.ClientId equals client.id
select bankcard;
bankcards = query.ToArray();
}
return bankcards.Select(n =>
{
return new ClientBankCardResult
{
Bank = n.Bank,
Card = n.Card,
Name = n.ClientName,
Comments = n.Comments,
Payment = n.Payment,
Usage = n.Use,
ValidState = ConsGlobal.IsValid(n.ValidState) ? "有效" : "无效"
};
}).ToArray();
}
///
/// 客户联系人列表
///
public IEnumerable GetClientDuties(ClientInfoQueryModel queryModel)
{
var contactypeDic = DbContext.contactype.ToDictionary(n => n.id, n => n.ContactType);
IEnumerable duties = null;
if (queryModel.ClientId > 0)
{
duties = DbContext.clientduty.AsNoTracking().Where(n => n.ApprovalOrder < 1 && n.ClientId == queryModel.ClientId).ToArray();
}
else
{
Expression> predicate;
if (!string.IsNullOrEmpty(queryModel.ClientNumber))
{
predicate = PredicateBuilder.Create(t => t.Number == queryModel.ClientNumber);
}
else if (!string.IsNullOrEmpty(queryModel.ClientName))
{
predicate = PredicateBuilder.Create(t => t.Name == queryModel.ClientName);
}
else
{
return Array.Empty();
}
var query = from duty in DbContext.clientduty.Where(n => n.ApprovalOrder < 1).AsNoTracking()
join client in DbContext.client.Where(predicate) on duty.ClientId equals client.id
select duty;
duties = query.ToArray();
}
return duties.Select(n =>
{
var contactTypeIds = n.ContactTypeIdsInt;
var contactTypes = new List(contactTypeIds.Count);
foreach (var m in contactTypeIds)
{
if (contactypeDic.TryGetValue(m, out var type))
{
contactTypes.Add(type);
}
}
return new ClientDutyResult
{
ContactName = n.ContactName,
ContactTypes = contactTypes,
IdCardType = n.IdCardType.HasValue ? ((IdCardTypeEnum)n.IdCardType).ToString() : string.Empty,
IdCardNo = n.IdCardNo,
PhoneNumber = n.PhoneNumber,
Fax = n.Fax,
Email = n.Email,
Address = n.Address,
DeadLine = n.DeadLine?.ToString("yyyy-MM-dd"),
IsReceiveEmail = n.IsReceiveEmail == 1
};
}).ToArray();
}
///
/// 获取客户文件批量导出zip文件
///
public byte[] GetZipFileOfClientFiles(ClientInfoQueryModel queryModel)
{
var predicate = PredicateBuilder.True();
if (queryModel.ClientId > 0)
{
predicate = PredicateBuilder.Create(n => n.id == queryModel.ClientId);
}
else if (!string.IsNullOrWhiteSpace(queryModel.ClientNumber))
{
predicate = PredicateBuilder.Create(n => n.Number == queryModel.ClientNumber);
}
else if (!string.IsNullOrWhiteSpace(queryModel.ClientName))
{
predicate = PredicateBuilder.Create(n => n.Name == queryModel.ClientName);
}
var query = from c in DbContext.client.Where(predicate)
join cf in DbContext.client_file on c.id equals cf.ClientId
where cf.IsValid && cf.ApprovalOrder < 1
select new ClientFileExportInfo
{
ClientNumber = c.Number,
id = cf.id,
ParentId = cf.ParentClientFileId,
BookTime = cf.BookTime,
FileDesc = cf.FileDesc,
FileName = cf.FileName,
FilePath = cf.FilePath,
FileTypeName = cf.FileTypeName,
MainProtocolNumber = cf.MainProtocolNumber,
ProtocolNumber = cf.ProtocolNumber,
ReportorRole = cf.ReportorRole,
SignDate = cf.SignDate,
SignType = cf.SignType,
OptDate = cf.OptDate
};
var fileList = query.ToArray();
var zipEntryList = new List(fileList.Length + 1);
foreach (var file in fileList)
{
if (file.SignDate.HasValue && file.SignDate.Value == DateTime.MinValue)
{
file.SignDate = null;
}
if (file.BookTime.HasValue && file.BookTime.Value == DateTime.MinValue)
{
file.BookTime = null;
}
if (file.BookTime == null)
{
file.BookTime = file.OptDate;
}
if (string.IsNullOrEmpty(file.FilePath))
{
file.ErrorMsg = "文件不存在";
}
else
{
var physicalPath = OtcAppContext.MapPath(file.FilePath);
if (File.Exists(physicalPath))
{
zipEntryList.Add(new MyZipEntry
{
Name = file.FileName,
Buffer = File.ReadAllBytes(physicalPath)
});
}
else
{
file.ErrorMsg = "文件不存在";
}
}
}
zipEntryList.Add(new MyZipEntry
{
Name = "info.txt",
Buffer = Encoding.UTF8.GetBytes(JsonBeautify.Serialize(fileList))
});
return ZipFileHelper.ZipFiles(zipEntryList);
}
///
///
///
public static Dictionary ConvertToViewData(Client client)
{
var config = SystemModule.ClientEditConfigService.GetConfig();
var type = typeof(Client);
var dic = new Dictionary();
foreach (var section in config.sections)
{
if (section.fields == null || !section.fields.Any())
{
continue;
}
var flatList = SystemModule.ClientEditConfigService.GetFlatList(section.fields, out _);
foreach (var f in flatList)
{
if (f == null || !f.visible)
{
continue;
}
string strVal = null;
if (f.name.StartsWith("meta_") || f.name.StartsWith("meta."))
{
client.MetaDic.TryGetValue(f.name, out strVal);
}
else
{
var prop = type.GetMember(f.name, BindingFlags.Instance | BindingFlags.Public).FirstOrDefault();
if (prop is PropertyInfo pi)
{
strVal = pi.GetValue(client)?.ToString();
}
}
if (string.IsNullOrWhiteSpace(strVal))
{
strVal = string.Empty;
}
else if (f.name.Equals(nameof(client.TradingInstType)))
{
strVal = TradingInstTypeUtil.GetDesc(client.TradingInstType);
}
else if (config.selects.TryGetValue(f.name, out var options))
{
if (f.name.Equals(nameof(client.ParentId)) && client.ParentId < 1)
{
strVal = string.Empty;
}
else if (f.type == "select-m" || f.type == "select-sm")
{
var arr = strVal.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s =>
{
var opt = options.FirstOrDefault(n => n.Value == s);
return opt == null ? s : opt.Text;
}).ToArray();
strVal = string.Join(",", arr);
}
else
{
var opt = options.FirstOrDefault(n => n.Value == strVal);
if (opt != null)
{
strVal = opt.Text;
}
}
}
dic[f.label] = strVal;
}
}
return dic;
}
}
class ClientFileExportInfo
{
public string ClientNumber { get; set; }
public int id { get; set; }
///
/// 文件名
///
public string FileName { get; set; }
///
/// 文件说明
///
public string FileDesc { get; set; }
///
/// 双录时间
///
public DateTime? BookTime { get; set; }
///
/// 文件类型名
///
public string FileTypeName { get; set; }
///
/// 协议编号
///
public string ProtocolNumber { get; set; }
///
/// 主协议编号
///
public string MainProtocolNumber { get; set; }
///
/// 母协议编号记录Id
///
public int ParentId { get; set; }
///
/// 签署时间
///
public DateTime? SignDate { get; set; }
///
/// 签署类型
///
public string SignType { get; set; }
///
/// 填报方角色
///
public string ReportorRole { get; set; }
///
///
///
public string FilePath { get; set; }
///
///
///
internal DateTime? OptDate { get; set; }
///
/// 错误信息
///
public string ErrorMsg { get; set; }
}
}