using BaseOUDAL; using NPOI.Util; using Org.BouncyCastle.Ocsp; using Qdp.Pricing.Library.Base.Utilities; using System.Data; using System.Globalization; using YieldChain.Helpers; using YLErp.Commons; using YLErp.Configuration; using YLErp.Helpers; using YLErp.Model.Enum; namespace YLErp.Modules.ClientModule { /// /// 客户人员信息服务 /// public class ClientDutyService : ClientBaseService { public ClientDutyService(OptUserInfo userInfo) : base(userInfo) { } public ClientDutyService(ClientBaseService baseService) : base(baseService) { } /// /// 查询ClientDuty /// public SearchListResult SearchList(ClientDutyReq req) { var query = DbContext.clientduty.AsNoTracking().AsQueryable().Where(d => d.ApprovalOrder < 1); if (!string.IsNullOrEmpty(req.ContactName)) { query = query.Where(d => d.ContactName.Contains(req.ContactName)); } if (!string.IsNullOrEmpty(req.ContactTypeId)) { query = query.Where(d => d.ContactTypeId.Contains(req.ContactTypeId)); } if (!string.IsNullOrEmpty(req.IdCardNo)) { query = query.Where(d => d.IdCardNo.Contains(req.IdCardNo)); } if (req.PhoneNumber != null) { query = query.Where(d => d.PhoneNumber == req.PhoneNumber); } if (!string.IsNullOrEmpty(req.Email)) { query = query.Where(d => d.Email.Contains(req.Email)); } if (req.IsReceiveEmail != null) { query = query.Where(d => d.IsReceiveEmail == req.IsReceiveEmail); } if (string.IsNullOrEmpty(req.sidx)) { req.sidx = "id"; req.sord = "asc"; } return query.ToSearchList(req); } /// /// 保存客户人员信息 /// public ClientDuty SaveClientDuty(ClientDuty req, HashSet updatedPropertyNameSet = null, bool isDirectSubApp = false) { ClientDuty dbmodel; Client client; var changeStr = string.Empty; var contactypelist = DbContext.contactype.ToList(); //新增 bool isAdd = req.id < 1; if (isAdd) { client = req.ClientId > 0 ? DbContext.client.Find(req.ClientId) : null; if (client == null) { throw new ServiceException("没有找到客户数据"); } foreach (var contactType in req.ContactTypeIdsInt) { //针对法人,授权协议签署人才做唯一性判断,有效期结束后可新增 if ((contactType == contactypelist.FirstOrDefault(x => x.ContactType == "法人")?.id || (contactType == contactypelist.FirstOrDefault(x => x.ContactType == "授权协议签署人")?.id && PS.Config.Company == Configuration.CompanyEnum.国泰君安)) && DbContext.clientduty.Any(x => x.ApprovalOrder < 1 && x.ClientId == req.ClientId && x.ContactTypeId.Contains(contactType.ToString()) && (x.DeadLine >= DateTime.Now || !x.DeadLine.HasValue)) && PS.Config.Company != Configuration.CompanyEnum.国联) { throw new ServiceException("法人在有效期内必须唯一,授权协议签署人必须唯一"); } } DbContext.clientduty.Add(dbmodel = new ClientDuty()); } else { foreach (var contactType in req.ContactTypeIdsInt) { //针对法人,授权协议签署人才做唯一性判断,有效期结束后可新增 if ((contactType == contactypelist.FirstOrDefault(x => x.ContactType == "法人")?.id || contactType == contactypelist.FirstOrDefault(x => x.ContactType == "授权协议签署人")?.id) && DbContext.clientduty.Any(x => x.ApprovalOrder < 1 && x.ClientId == req.ClientId && x.ContactTypeId.Contains(contactType.ToString()) && x.id != req.id && (x.DeadLine >= DateTime.Now || !x.DeadLine.HasValue)) && PS.Config.Company != Configuration.CompanyEnum.国联) { throw new ServiceException("法人在有效期内必须唯一,授权协议签署人必须唯一"); } } dbmodel = DbContext.clientduty.Find(req.id); client = DbContext.client.Find(dbmodel.ClientId); if (PS.Config.Is东莞) { var typeids = DataConvert.ConvertCommaValuesToInt32Array(dbmodel.ContactTypeId); var reqtypeids = DataConvert.ConvertCommaValuesToInt32Array(req.ContactTypeId); using (var clientDB = DbContextFactory.GetClientDbContext(null)) { // var listDirector = dic.Where(l=>l.Value== "控股股东" || l.Value == "董事会成员" || l.Value == "普通股东").Select(n => n.Key.ToString()); if (req.IdentityOfTheBoardOfDirectors != null) { //string strContactType = ""; //string strContactTypeIds = ""; var strContactTypeList = new List(); var strContactTypeIdsList = new List(); var contype = clientDB.contactype; var types = contype.Where(n => typeids.Contains(n.id)).Select(n => new { n.id, n.ContactType }).ToList(); strContactTypeList = types.Select(l => l.ContactType).ToList(); strContactTypeIdsList = types.Select(l => l.id).ToList(); if (req.IdentityOfTheBoardOfDirectors == (int)IdentityOfTheBoardOfDirectorsEnum.非董事会成员) { var hasDirectors = types.Where(n => n.ContactType.Contains("董事会成员")).FirstOrDefault(); if (hasDirectors != null) { strContactTypeList.Remove(hasDirectors.ContactType); strContactTypeIdsList.Remove(hasDirectors.id); } } else { var hasDirectors = types.Where(n => n.ContactType.Contains("董事会成员")).FirstOrDefault(); if (hasDirectors == null) { var contypeHasDirector = contype.Where(l => l.ContactType.Contains("董事会成员")).FirstOrDefault(); strContactTypeList.Add(contypeHasDirector.ContactType); strContactTypeIdsList.Add(contypeHasDirector.id); } } if (req.IsShareholder == false) { var hasShareholder = types.Where(n => n.ContactType.Contains("普通股东")).FirstOrDefault(); if (hasShareholder != null) { strContactTypeList.Remove(hasShareholder.ContactType); strContactTypeIdsList.Remove(hasShareholder.id); } req.ShareholdingByShareholdersType = null; req.ShareholdingByShareholdersRatio = null; } else { var hasShareholder = types.Where(n => n.ContactType.Contains("普通股东")).FirstOrDefault(); if (hasShareholder == null) { var contypeHasShareholder = contype.Where(l => l.ContactType.Contains("普通股东")).FirstOrDefault(); strContactTypeList.Add(contypeHasShareholder.ContactType); strContactTypeIdsList.Add(contypeHasShareholder.id); } } req.ContactType = String.Join(",", strContactTypeList); req.ContactTypeId = String.Join(",", strContactTypeIdsList); dbmodel.ContactTypeId = req.ContactTypeId; } var reqtypesHasShareholder = clientDB.contactype.Where(n => reqtypeids.Contains(n.id) && (n.ContactType == "普通股东" || n.ContactType == "控股股东")); if (reqtypesHasShareholder != null && reqtypesHasShareholder.Any()) { req.IsShareholder = true; } else { req.IsShareholder = false; } } } if (client == null) { throw new ServiceException("没有找到客户数据"); } var changeList = DataChangeHelper.GetDataChanges(dbmodel, req); changeList = ClientDataChangeLogHandle.Handle(typeof(ClientDuty), changeList); changeStr = changeList.ToJson(); } var clientCount = yldb.approvalprocess.Where(x => x.processType == "ClientProcess").Count(); if (dbmodel.ApprovalOrder == 0) { if (clientCount > 0 && client.ProcessStatus == "已开户" && PS.Config.Company == Configuration.CompanyEnum.国联) { if (!isAdd) { var update = DbContext.clientduty.Where(x => x.SourceId == req.id)?.FirstOrDefault(); if (update != null) { ObjectHelper.MapValues(update, req.Copy(), "id"); } else { update = req.Copy(); update.id = 0; DbContext.clientduty.Add(update); } update.SourceId = req.id; update.ApprovalOrder = (int)ApprovalOrderEnum.修改; } else { ObjectHelper.MapValues(dbmodel, req); dbmodel.ApprovalOrder = (int)ApprovalOrderEnum.新增; } if (isDirectSubApp) { var processlist = yldb.approvalprocess.Where(x => x.processType == "ClientProcess"); if (processlist.Count() > 0) { var groupId = UserBLL.GetApprovalProcessGroup(UserId); bool approvalBranch = processlist.Any(x => x.approvalGroupId != 0);//审批流程有分支情况 client.ProcessOrderBranch = 0; client.ApprovalOrderId = ProcessTradeLog.审批中; client.ApprovalStatus = "审批中"; client.ApprovalOptDate = DateTime.Now; if (approvalBranch)//审批流程有分支情况 { //该审批组属于流程的审批组或该审批组不属于流程审批组 var tradeProessBranchQuery = processlist.FirstOrDefault(x => (x.approvalGroupId == groupId && x.approvalCondition == 1) || (x.approvalGroupId != groupId && x.approvalCondition == 2)); if (tradeProessBranchQuery == null)//都没有情况 { throw new ServiceException("当前存在分支开户审批流程,检测到申请人不满足任意一个分支进入条件,无法提交审批,请更换申请人或调整审批组"); } client.ApprovalOrderId = tradeProessBranchQuery.order > 1 ? 1 : tradeProessBranchQuery.order + 1;//第一个节点是分支需要跳过审批组那一级 client.ProcessOrderBranch = tradeProessBranchQuery.node ?? 1;//审批组分支 } var range = DbContext.client_file_audit.Where(x => x.ClientId == req.ClientId).ToList(); DbContext.client_file_audit.RemoveRange(range); var clientAuditlog = new ClientAuditLog { ClientId = client.id, OptType = "提交信息变更审批", DataType = "00", OptId = UserId, OptName = UserName, OptDate = DateTime.Now }; ///日志记录 DbContext.ClientAuditLog.Add(clientAuditlog); DbContext.SaveChanges(); client.ApprovalLogId = clientAuditlog.id; } } DbContext.SaveChanges(); if (!isAdd) { ObjectHelper.MapValues(dbmodel, req, "ApprovalOrder", "id"); } return dbmodel; } else { ObjectHelper.MapValues(dbmodel, req); if (req.id == 0) { DbContext.ClientAuditLog.Add(new ClientAuditLog { ClientId = req.ClientId ?? 0, OptType = "新增客户人员", Changes = string.Empty, DataType = "00", OptId = UserId, OptName = UserName, OptDate = DateTime.Now }); } else { DbContext.ClientAuditLog.Add(new ClientAuditLog { ClientId = req.ClientId ?? 0, OptType = "编辑客户人员", Changes = changeStr, DataType = "C02", OptId = UserId, OptName = UserName, OptDate = DateTime.Now }); } } } DbContext.SaveChanges(); return dbmodel; } /// /// 导出客户人员信息到Excel /// public byte[] ExportClientDutyToExcel(IEnumerable clientIds) { var ret = DbContext.clientduty.AsNoTracking().AsQueryable().Where(o => o.ApprovalOrder < 1 && clientIds.Contains(o.ClientId ?? 0)).OrderBy(o => o.ClientId).ToList(); List clientList = null; if (ret != null && ret.Count > 0) { clientList = DbContext.client.AsNoTracking().Where(o => clientIds.Contains(o.id)).ToList(); } clientList ??= new List(); var contactypeList = DbContext.contactype.AsNoTracking().ToList(); var dc = new List { new ExcelHelper.DataColumnModel("客户名称", "ClientId", (cv, obj) => { var id = (int?)cv ?? 0; //return DbContext.client.Where(o => o.id == id).FirstOrDefault().Name; return clientList.FirstOrDefault(d=>d.id==id)?.Name; }), new ExcelHelper.DataColumnModel("姓名", "ContactName", typeof(string)), new ExcelHelper.DataColumnModel("职责类型", "ContactTypeId", (cv, obj) => { string types = cv; var dutyTypes = types.Split(',').ToList(); var result = contactypeList.Where(o => dutyTypes.Contains(o.id.ToString())).Select(o => o.ContactType).ToList(); return string.Join(",", result); }), new ExcelHelper.DataColumnModel("电子邮箱", "Email", typeof(string)), new ExcelHelper.DataColumnModel("联系电话", "PhoneNumber", typeof(string)), new ExcelHelper.DataColumnModel("授权有效期", "AuthorizeEffectiveEndDateStr", (cv, obj) => { if (string.IsNullOrEmpty(cv)) { return string.Empty; } string value="长期有效"; if (cv!="30001231") { DateTime time= DateTime.ParseExact(cv, "yyyyMMdd", CultureInfo.InvariantCulture); value=time.ToString("yyyy-MM-dd"); } return value; }), }; new ExcelHelper().ListToExcel(dc.ToArray(), ret, "客户人员信息", true, out var buffer); return buffer; } /// /// 删除客户人员信息 /// public ClientDuty RemoveClientDuty(int id, bool isDirectSubApp = false) { var r = DbContext.clientduty.FirstOrDefault(s => s.id == id); return RemoveClientDuty(r, isDirectSubApp); } /// /// 删除客户人员信息 /// public ClientDuty RemoveClientDuty(ClientDuty clientDuty, bool isDirectSubApp = false) { if (clientDuty == null) { throw new ServiceException("删除失败,找不到人员信息"); } var client = DbContext.client.Find(clientDuty.ClientId); if (client == null) { throw new ServiceException("删除失败,找不到客户信息"); } var clientCount = yldb.approvalprocess.Where(x => x.processType == "ClientProcess").Count(); if (clientCount > 0 && client.ProcessStatus == "已开户" && PS.Config.Company == Configuration.CompanyEnum.国联 && clientDuty.ApprovalOrder == 0) { clientDuty.ApprovalOrder = (int)ApprovalOrderEnum.删除; if (isDirectSubApp) { var processlist = yldb.approvalprocess.Where(x => x.processType == "ClientProcess"); if (processlist.Count() > 0) { var groupId = UserBLL.GetApprovalProcessGroup(UserId); bool approvalBranch = processlist.Any(x => x.approvalGroupId != 0);//审批流程有分支情况 client.ProcessOrderBranch = 0; client.ApprovalOrderId = ProcessTradeLog.审批中; client.ApprovalStatus = "审批中"; client.ApprovalOptDate = DateTime.Now; if (approvalBranch)//审批流程有分支情况 { //该审批组属于流程的审批组或该审批组不属于流程审批组 var tradeProessBranchQuery = processlist.FirstOrDefault(x => (x.approvalGroupId == groupId && x.approvalCondition == 1) || (x.approvalGroupId != groupId && x.approvalCondition == 2)); if (tradeProessBranchQuery == null)//都没有情况 { throw new ServiceException("当前存在分支开户审批流程,检测到申请人不满足任意一个分支进入条件,无法提交审批,请更换申请人或调整审批组"); } client.ApprovalOrderId = tradeProessBranchQuery.order > 1 ? 1 : tradeProessBranchQuery.order + 1;//第一个节点是分支需要跳过审批组那一级 client.ProcessOrderBranch = tradeProessBranchQuery.node ?? 1;//审批组分支 } var range = DbContext.client_file_audit.Where(x => x.ClientId == client.id).ToList(); DbContext.client_file_audit.RemoveRange(range); var clientAuditlog = new ClientAuditLog { ClientId = client.id, OptType = "提交信息变更审批", DataType = "00", OptId = UserId, OptName = UserName, OptDate = DateTime.Now }; ///日志记录 DbContext.ClientAuditLog.Add(clientAuditlog); DbContext.SaveChanges(); client.ApprovalLogId = clientAuditlog.id; } } } else { DbContext.clientduty.Remove(clientDuty); if (DbContext.clientduty.Where(x => x.SourceId == clientDuty.id).Any()) { DbContext.clientduty.RemoveRange(DbContext.clientduty.Where(x => x.SourceId == clientDuty.id).ToList()); } if (clientDuty.ApprovalOrder == 0) { DbContext.ClientAuditLog.Add(new ClientAuditLog { ClientId = clientDuty.ClientId ?? 0, OptType = "删除人员信息", Changes = string.Empty, DataType = "00", OptId = UserId, OptName = UserName, OptDate = DateTime.Now }); } } DbContext.SaveChanges(); return clientDuty; } } /// /// /// public class ClientDutyReq : BaseSearchReq { public int? ClientId { get; set; } /// /// 联系人姓名 /// public string ContactName { get; set; } /// /// 联系人类型id /// public string ContactTypeId { get; set; } public List ContactTypeIdsInt { get { if (string.IsNullOrEmpty(ContactTypeId)) { return new List(); } return ContactTypeId.Split(',').Select(v => Convert.ToInt32(v)).ToList(); } } /// /// 证件号 /// public string IdCardNo { get; set; } /// /// 证件类型 /// public int? IdCardType { get; set; } /// /// 电话号码 /// public string PhoneNumber { get; set; } /// /// 传真 /// public string Fax { get; set; } /// /// Email /// public string Email { get; set; } /// /// 有效日期 /// public DateTime? DeadLine { get; set; } /// /// 是否接收相关邮件 /// public int? IsReceiveEmail { get; set; } /// /// 联系地址 /// public string Address { get; set; } } }