public static List <Edge> vsechnyDcerineVazbyInternal(Lib.Data.Osoba person, int level, bool goDeep, Edge parent, ExcludeDataCol excludeICO = null, IEnumerable <int> excludeOsobaId = null, DateTime?datumOd = null, DateTime?datumDo = null, Relation.AktualnostType aktualnost = Relation.AktualnostType.Libovolny) { if (excludeOsobaId == null) { excludeOsobaId = new int[] { } } ; string sql = @"select vazbakIco, vazbakOsobaId, datumOd, datumDo, typVazby, pojmenovaniVazby, podil from OsobaVazby where osobaId = @osobaId and ( (datumOd <= @datumOd or @datumOd is null) OR (datumOd >= @datumOd and datumDo is null) ) and (datumDo >= @datumDo or datumDo is null or @datumDo is null) "; var p = new IDataParameter[] { new SqlParameter("osobaId", person.InternalId), new SqlParameter("datumOd", datumOd), new SqlParameter("datumDo", datumDo), }; var relForPerson = GetChildrenRelations(sql, Node.NodeType.Person, person.InternalId.ToString(), datumOd, datumDo, p, level, goDeep, parent, excludeICO, aktualnost); var relForConnectedPersons = new List <Edge>(); using (DbEntities db = new DbEntities()) { var navazaneOsoby = db.OsobaVazby.Where(m => m.OsobaID == person.InternalId && m.VazbakOsobaId != null).ToList(); if (navazaneOsoby.Count > 0) { foreach (var ov in navazaneOsoby) { Edge parentRelFound = relForPerson .Where(r => r.To.Type == Node.NodeType.Person && r.To.Id == ov.VazbakOsobaId.Value.ToString()) .FirstOrDefault(); if (!excludeOsobaId.Contains(ov.VazbakOsobaId.Value)) { Osoba o = Osoby.GetById.Get(ov.VazbakOsobaId.Value); excludeOsobaId = excludeOsobaId.Union(new int[] { ov.VazbakOsobaId.Value }); var rel = vsechnyDcerineVazbyInternal(o, level + 1, true, parentRelFound, excludeOsobaId: excludeOsobaId, aktualnost: aktualnost); relForConnectedPersons = Edge.Merge(relForConnectedPersons, rel); } } } } var finalRel = Edge.Merge(relForConnectedPersons, relForPerson); return(finalRel); }
public static List <Edge> vsechnyDcerineVazbyInternal(string ico, int level, bool goDeep, Edge parent, ExcludeDataCol excludeICO = null, DateTime?datumOd = null, DateTime?datumDo = null, Relation.AktualnostType aktualnost = Relation.AktualnostType.Libovolny) { string sql = @"select vazbakIco, datumOd, datumDo, typVazby, pojmenovaniVazby, podil from Firmavazby where ico=@ico and dbo.IsSomehowInInterval(@datumOd, @datumDo, datumOd, datumDo) = 1 "; var p = new IDataParameter[] { new SqlParameter("ico", ico), new SqlParameter("datumOd", datumOd), new SqlParameter("datumDo", datumDo), }; var rel = GetChildrenRelations(sql, Node.NodeType.Company, ico, datumOd, datumDo, p, level, goDeep, parent, excludeICO, aktualnost); return(rel); }
public static List <Edge> GetChildrenRelations(string sql, Node.NodeType nodeType, string nodeId, DateTime?datumOd, DateTime?datumDo, IDataParameter[] parameters, int level, bool goDeep, Edge parent, ExcludeDataCol excludeICO, decimal minPodil, Relation.AktualnostType aktualnost) { if (excludeICO == null) { excludeICO = new ExcludeDataCol(); } string cnnStr = Devmasters.Config.GetWebConfigValue("CnnString"); List <Edge> relations = new List <Edge>(); if (level == 0 && parent == null) { //add root node / edge relations.Add( new Edge() { From = null, Root = true, To = new Node() { Id = nodeId, Type = nodeType }, RelFrom = datumOd, RelTo = datumDo, Distance = 0 } ); } //get zakladni informace o subj. //find politician in the DB var db = new Devmasters.PersistLib(); var sqlCall = HlidacStatu.Lib.DirectDB.GetRawSql(System.Data.CommandType.Text, sql, parameters); //string sqlFirma = "select top 1 stav_subjektu from firma where ico = @ico"; var ds = db.ExecuteDataset(cnnStr, System.Data.CommandType.Text, sqlCall, null); if (ds.Tables[0].Rows.Count > 0) { List <AngazovanostData> rows = new List <AngazovanostData>(); foreach (DataRow dr in ds.Tables[0].Rows) { AngazovanostData angaz = null; var ico = (string)dr["VazbakIco"]; if (string.IsNullOrEmpty(ico)) { if (dr.Table.Columns.Contains("vazbakOsobaId")) { var vazbakOsobaId = (int?)PersistLib.IsNull(dr["vazbakOsobaId"], null); if (vazbakOsobaId != null) { Osoba o = Osoby.GetById.Get(vazbakOsobaId.Value); angaz = new AngazovanostData() { subjId = vazbakOsobaId.Value.ToString(), NodeType = Node.NodeType.Person, fromDate = (DateTime?)PersistLib.IsNull(dr["datumOd"], null), toDate = (DateTime?)PersistLib.IsNull(dr["datumDo"], null), kod_ang = Convert.ToInt32(dr["typVazby"]), descr = (string)PersistLib.IsNull(dr["PojmenovaniVazby"], ""), podil = (decimal?)PersistLib.IsNull(dr["podil"], null) }; } } } else { angaz = new AngazovanostData() { subjId = ico, subjname = "", NodeType = Node.NodeType.Company, fromDate = (DateTime?)PersistLib.IsNull(dr["datumOd"], null), toDate = (DateTime?)PersistLib.IsNull(dr["datumDo"], null), kod_ang = Convert.ToInt32(dr["typVazby"]), descr = (string)PersistLib.IsNull(dr["PojmenovaniVazby"], ""), podil = (decimal?)PersistLib.IsNull(dr["podil"], null) }; } rows.Add(angaz); } List <AngazovanostData> filteredRels = new List <AngazovanostData>(); //delete vazby ve stejnem obdobi if (rows.Count > 0) { //per ico foreach (var gIco in rows.Select(m => m.subjId).Distinct()) { var relsForIco = rows.Where(m => m.subjId == gIco); //find longest, or separate relation foreach (var r in relsForIco) { if (relsForIco .Any(rr => rr != r && rr.fromDate < r.fromDate && (rr.toDate > r.toDate || rr.toDate.HasValue == false) ) ) { //skip } else { filteredRels.Add(r); } } } } foreach (AngazovanostData ang in filteredRels.OrderBy(m => m.kod_ang)) { if (ang.kod_ang == 100) //souhrny (casove) vztah, zkontroluj, zda uz tam neni jiny vztah se stejnym rozsahem doby { if ( relations.Any( r => r.To.Id == ang.subjId && r.To.Type == ang.NodeType && r.RelFrom == ang.fromDate && r.RelTo == ang.toDate ) ) { continue; } } var rel = AngazovanostDataToEdge(ang, new Node() { Type = nodeType, Id = nodeId }, new Node() { Type = ang.NodeType, Id = ang.subjId }, level + 1 ); if (excludeICO.Contains(rel)) { continue;//skip to the next } if (rel.Aktualnost >= aktualnost) { relations.Add(rel); } } } if (goDeep && relations.Count > 0) { level++; List <Edge> deeperRels = new List <Edge>(); List <Edge> excludeMore = new List <Edge>(); if (parent != null) { excludeMore = relations.ToList(); } ////navazej na ten, ktery je nejdelsi //var parentRels = relations.GroupBy(x => new { x.To.Id }, (key, rels) => //{ // DateTime? fromDate = rels.Any(m => m.FromDate == null) ? (DateTime?)null : rels.Min(m => m.FromDate); // DateTime? toDate = rels.Any(m => m.ToDate == null) ? (DateTime?)null : rels.Max(m => m.ToDate); // Relation bestRelation = Relation.GetLongestRelation(rels); // return new // { // SubjIco = key.To.Id, // FromDate = fromDate, // ToDate = toDate, // BestRelation = bestRelation, // }; //}); foreach (var rel in relations.Where(m => m.Root == false)) { //old deeperRels.AddRange( vsechnyDcerineVazbyInternal(rel.To.Id, level, goDeep, rel, excludeICO.AddItem(new ExcludeData(rel)), rel.RelFrom, rel.RelTo, minPodil, aktualnost) ); } relations.AddRange(deeperRels); } if (level == 0) { //remove inactive companies from last branches //TODO } return(relations); }
public static List <Edge> GetChildrenRelations(string sql, Node.NodeType nodeType, string nodeId, DateTime?datumOd, DateTime?datumDo, IDataParameter[] parameters, int level, bool goDeep, Edge parent, ExcludeDataCol excludeICO, Relation.AktualnostType aktualnost) { if (excludeICO == null) { excludeICO = new ExcludeDataCol(); } string cnnStr = Devmasters.Core.Util.Config.GetConfigValue("CnnString"); List <Edge> relations = new List <Edge>(); if (level == 0 && parent == null) { //add root node / edge relations.Add( new Edge() { From = null, Root = true, To = new Node() { Id = nodeId, Type = nodeType }, RelFrom = datumOd, RelTo = datumDo, Distance = 0 } ); } //get zakladni informace o subj. //find politician in the DB var db = new Devmasters.Core.PersistLib(); var sqlCall = HlidacStatu.Lib.DirectDB.GetRawSql(System.Data.CommandType.Text, sql, parameters); //string sqlFirma = "select top 1 stav_subjektu from firma where ico = @ico"; var ds = db.ExecuteDataset(cnnStr, System.Data.CommandType.Text, sqlCall, null); if (ds.Tables[0].Rows.Count > 0) { List <AngazovanostData> rows = new List <AngazovanostData>(); foreach (DataRow dr in ds.Tables[0].Rows) { AngazovanostData angaz = null; var ico = (string)dr["VazbakIco"]; if (string.IsNullOrEmpty(ico)) { if (dr.Table.Columns.Contains("vazbakOsobaId")) { var vazbakOsobaId = (int?)PersistLib.IsNull(dr["vazbakOsobaId"], null); if (vazbakOsobaId != null) { Osoba o = Osoby.GetById.Get(vazbakOsobaId.Value); angaz = new AngazovanostData() { subjId = vazbakOsobaId.Value.ToString(), NodeType = Node.NodeType.Person, fromDate = (DateTime?)PersistLib.IsNull(dr["datumOd"], null), toDate = (DateTime?)PersistLib.IsNull(dr["datumDo"], null), kod_ang = Convert.ToInt32(dr["typVazby"]), descr = (string)PersistLib.IsNull(dr["PojmenovaniVazby"], ""), podil = (decimal?)PersistLib.IsNull(dr["podil"], null) }; } } } else { angaz = new AngazovanostData() { subjId = ico, subjname = "", NodeType = Node.NodeType.Company, fromDate = (DateTime?)PersistLib.IsNull(dr["datumOd"], null), toDate = (DateTime?)PersistLib.IsNull(dr["datumDo"], null), kod_ang = Convert.ToInt32(dr["typVazby"]), descr = (string)PersistLib.IsNull(dr["PojmenovaniVazby"], ""), podil = (decimal?)PersistLib.IsNull(dr["podil"], null) }; } rows.Add(angaz); } List <AngazovanostData> filteredRels = new List <AngazovanostData>(); //delete vazby ve stejnem obdobi if (rows.Count > 0) { //per ico foreach (var gIco in rows.Select(m => m.subjId).Distinct()) { var relsForIco = rows.Where(m => m.subjId == gIco); //find longest, or separate relation foreach (var r in relsForIco) { if (relsForIco .Any(rr => rr != r && rr.fromDate < r.fromDate && (rr.toDate > r.toDate || rr.toDate.HasValue == false) ) ) { //skip } else { filteredRels.Add(r); } } } } foreach (AngazovanostData ang in filteredRels.OrderBy(m => m.kod_ang)) { if (ang.kod_ang == 100) //souhrny (casove) vztah, zkontroluj, zda uz tam neni jiny vztah se stejnym rozsahem doby { if ( relations.Any( r => r.To.Id == ang.subjId && r.To.Type == ang.NodeType && r.RelFrom == ang.fromDate && r.RelTo == ang.toDate ) ) { continue; } } var rel = new Edge(); rel.From = new Node() { Type = nodeType, Id = nodeId }; rel.To = new Node() { Type = ang.NodeType, Id = ang.subjId }; rel.Distance = level + 1; rel.RelFrom = (DateTime?)PersistLib.IsNull(ang.fromDate, null); if (rel.RelFrom < minDate) { rel.RelFrom = null; } rel.RelTo = (DateTime?)PersistLib.IsNull(ang.toDate, null); if (rel.RelTo < minDate) { rel.RelTo = null; } int kod_ang = ang.kod_ang; /* * 3 - prokura * 4 - člen dozorčí rady * 24 - spolecnik * 5 - Jediný akcionář * 1 - jednatel * */ rel.Descr = ang.descr; Firma.RelationSimpleEnum relRelationship = Firma.RelationSimpleEnum.Jiny; switch (kod_ang) { case 1: relRelationship = Firma.RelationSimpleEnum.Statutarni_organ; if (string.IsNullOrEmpty(rel.Descr)) { rel.Descr = Firma.RelationSimpleEnum.Jednatel.ToNiceDisplayName(); } break; case 3: relRelationship = Firma.RelationSimpleEnum.Statutarni_organ; if (string.IsNullOrEmpty(rel.Descr)) { rel.Descr = Firma.RelationSimpleEnum.Prokura.ToNiceDisplayName(); } break; case 4: case 7: case 2: case 18: case 25: case 26: case 28: case 31: relRelationship = Firma.RelationSimpleEnum.Statutarni_organ; if (string.IsNullOrEmpty(rel.Descr)) { rel.Descr = Firma.RelationSimpleEnum.Dozorci_rada.ToNiceDisplayName(); } break; case 33: case 34: case 35: relRelationship = Firma.RelationSimpleEnum.Zakladatel; if (string.IsNullOrEmpty(rel.Descr)) { rel.Descr = Firma.RelationSimpleEnum.Dozorci_rada.ToNiceDisplayName(); } break; case 5: case 9: case 10: case 15: case 19: case 24: relRelationship = Firma.RelationSimpleEnum.Spolecnik; if (string.IsNullOrEmpty(rel.Descr)) { rel.Descr = Firma.RelationSimpleEnum.Spolecnik.ToNiceDisplayName(); } break; case 100: relRelationship = Firma.RelationSimpleEnum.Souhrnny; if (string.IsNullOrEmpty(rel.Descr)) { rel.Descr = Firma.RelationSimpleEnum.Jednatel.ToNiceDisplayName(); } break; case 23: // case 29: // case 11: // case 12: // case 13: // case 16: // case 17: // case 37: // case 40: // case 41: // case 42: // case 99: relRelationship = Firma.RelationSimpleEnum.Jiny; break; default: if (kod_ang < 0) { relRelationship = (Firma.RelationSimpleEnum)kod_ang; } else { //rel.Relationship = Relation.RelationDescriptionEnum.Jednatel; relRelationship = Firma.RelationSimpleEnum.Jiny; if (string.IsNullOrEmpty(rel.Descr)) { rel.Descr = Firma.RelationSimpleEnum.Jednatel.ToNiceDisplayName(); } } break; } if (excludeICO.Contains(rel)) { continue;//skip to the next } if (string.IsNullOrEmpty(rel.Descr)) { rel.Descr = relRelationship.ToNiceDisplayName(); } rel.UpdateAktualnost(); if (rel.Aktualnost >= aktualnost) { relations.Add(rel); } } } if (goDeep && relations.Count > 0) { level++; List <Edge> deeperRels = new List <Edge>(); List <Edge> excludeMore = new List <Edge>(); if (parent != null) { excludeMore = relations.ToList(); } ////navazej na ten, ktery je nejdelsi //var parentRels = relations.GroupBy(x => new { x.To.Id }, (key, rels) => //{ // DateTime? fromDate = rels.Any(m => m.FromDate == null) ? (DateTime?)null : rels.Min(m => m.FromDate); // DateTime? toDate = rels.Any(m => m.ToDate == null) ? (DateTime?)null : rels.Max(m => m.ToDate); // Relation bestRelation = Relation.GetLongestRelation(rels); // return new // { // SubjIco = key.To.Id, // FromDate = fromDate, // ToDate = toDate, // BestRelation = bestRelation, // }; //}); foreach (var rel in relations.Where(m => m.Root == false)) { //old deeperRels.AddRange( vsechnyDcerineVazbyInternal(rel.To.Id, level, goDeep, rel, excludeICO.AddItem(new ExcludeData(rel)), rel.RelFrom, rel.RelTo, aktualnost) ); } relations.AddRange(deeperRels); } if (level == 0) { //remove inactive companies from last branches //TODO } return(relations); }