using YLErp.BLL; namespace YLErp.Modules.ClientModule { public class ClientHierarchyService : ClientBaseService { public ClientHierarchyService(OptUserInfo userInfo) : base(userInfo) { } public List clients = new List(); public List clientsdata = new List(); public List Treeleveldata(int clientid) { var systemDate = valuedateBLL.SystemDate; List client = DataCacheProvider.GetClientDataSource().AsQueryable().ToList(); var nowclient = client.Where(c => c.id == clientid).FirstOrDefault(); if (nowclient == null) { throw new ServiceException("当前客户已不存在"); } clients.Add(client.Where(c => c.id == clientid).FirstOrDefault()); FoundChildNode(client, client.Where(c => c.id == clientid).FirstOrDefault()); var ParentData = clients.Where(c => c.ParentId == 0).FirstOrDefault(); clientsdata.Add(ParentData); List res = FoundParentNode(client, ParentData); var query = from cli in res join cd in yldb.credit.Where(t => t.ProcessStatus == "已审批" && (!t.CreditDeadLine.HasValue || t.CreditDeadLine >= systemDate.ValueDate) && (!t.CreditStartDate.HasValue || t.CreditStartDate <= systemDate.ValueDate)).ToList() on cli.id equals cd.ClientId into cl from clie in cl.DefaultIfEmpty() select new Clientlayered { Credit = clie == null ? 0 : clie.Credit ?? 0, StockEqvNotional = clie == null ? 0 : clie.StockEqvNotional ?? 0, id = cli.id, Name = cli.Name, ParentId = cli.ParentId, ProcessStatus = cli.ProcessStatus }; return query.ToList(); } public void FoundChildNode(List all, Client item) { Client childItems = all.Where(c => c.id == item.ParentId).FirstOrDefault(); if (childItems != null) { clients.Add(childItems); FoundChildNode(all, childItems); } } public List FoundParentNode(List all, Client clientlist) { List childItems = all.Where(c => c.ParentId == clientlist.id).ToList(); foreach (var item in childItems) { clientsdata.Add(item); FoundParentNode(all, item); } return clientsdata; } } public class Clientlayered { /// /// 客户Id /// public int id { get; set; } /// /// 客户名称 /// public string Name { get; set; } /// /// 父级Id /// public int ParentId { get; set; } /// ///授信额度 /// public double Credit { get; set; } /// /// 名义本金规模 /// public double StockEqvNotional { get; set; } /// /// 用户状态 /// public string ProcessStatus { get; set; } } }