From 9727cd7b3cb5dc978810388e196c7a625db37b32 Mon Sep 17 00:00:00 2001 From: Guilherme Branco Stracini Date: Sat, 22 Jul 2023 18:25:53 +0100 Subject: [PATCH] Add CQL to files (#265) --- BancosBrasileiros.MergeTool/Dto/Bank.cs | 845 ++++++------ BancosBrasileiros.MergeTool/Dto/Banks.cs | 27 +- .../Helpers/Constants.cs | 103 +- BancosBrasileiros.MergeTool/Helpers/Logger.cs | 32 +- .../Helpers/Patterns.cs | 137 +- BancosBrasileiros.MergeTool/Helpers/Reader.cs | 973 +++++++------- BancosBrasileiros.MergeTool/Helpers/Seeder.cs | 1140 +++++++++-------- BancosBrasileiros.MergeTool/Helpers/Source.cs | 95 +- BancosBrasileiros.MergeTool/Helpers/Writer.cs | 228 ++-- BancosBrasileiros.MergeTool/Program.cs | 32 +- BancosBrasileiros.MergeTool/Worker.cs | 246 ++-- BancosBrasileiros.sln.DotSettings | 1 + 12 files changed, 1968 insertions(+), 1891 deletions(-) diff --git a/BancosBrasileiros.MergeTool/Dto/Bank.cs b/BancosBrasileiros.MergeTool/Dto/Bank.cs index 14dc33de..6a2f4125 100644 --- a/BancosBrasileiros.MergeTool/Dto/Bank.cs +++ b/BancosBrasileiros.MergeTool/Dto/Bank.cs @@ -12,484 +12,479 @@ // // *********************************************************************** -namespace BancosBrasileiros.MergeTool.Dto +namespace BancosBrasileiros.MergeTool.Dto; + +using System; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Xml.Serialization; +using CrispyWaffle.Extensions; +using CrispyWaffle.Validations; +using Newtonsoft.Json; + +/// +/// Class Bank. +/// Implements the +/// +/// +[XmlRoot("Bank")] +[Serializable] +public class Bank : IEquatable { - using CrispyWaffle.Extensions; - using Newtonsoft.Json; - using System; - using System.Linq; - using System.Text; - using System.Xml.Serialization; - using CrispyWaffle.Validations; + /// + /// Gets or sets the COMPE. + /// + /// The COMPE. + [JsonIgnore] + [XmlIgnore] + public int Compe { get; set; } /// - /// Class Bank. - /// Implements the + /// Gets the COMPE string. /// - /// - [XmlRoot("Bank")] - [Serializable] - public class Bank : IEquatable + /// The COMPE string. + [JsonProperty("COMPE")] + [XmlElement("COMPE")] + public string CompeString { - /// - /// Gets or sets the COMPE. - /// - /// The COMPE. - [JsonIgnore] - [XmlIgnore] - public int Compe { get; set; } - - /// - /// Gets the COMPE string. - /// - /// The COMPE string. - [JsonProperty("COMPE")] - [XmlElement("COMPE")] - public string CompeString + get => Compe.ToString("000"); + set { - get => Compe.ToString("000"); - set - { - if (int.TryParse(value, out var parsed)) - Compe = parsed; - } + if (int.TryParse(value, out var parsed)) + Compe = parsed; } + } + + /// + /// Gets or sets the ispb. + /// + /// The ispb. + [JsonIgnore] + [XmlIgnore] + public int Ispb { get; set; } - /// - /// Gets or sets the ispb. - /// - /// The ispb. - [JsonIgnore] - [XmlIgnore] - public int Ispb { get; set; } - - /// - /// Gets or sets the ispb string. - /// - /// The ispb string. - [JsonProperty("ISPB")] - [XmlElement("ISPB")] - public string IspbString + /// + /// Gets or sets the ispb string. + /// + /// The ispb string. + [JsonProperty("ISPB")] + [XmlElement("ISPB")] + public string IspbString + { + get => Ispb.ToString("00000000"); + set { - get => Ispb.ToString("00000000"); - set - { - if (int.TryParse(value, out var parsed)) - Ispb = parsed; - } + if (int.TryParse(value, out var parsed)) + Ispb = parsed; } + } - /// - /// The document - /// - private string _document; - - /// - /// Gets or sets the document. - /// - /// The document. - [JsonProperty("Document")] - [XmlElement("Document")] - public string Document + /// + /// The document + /// + private string _document; + + /// + /// Gets or sets the document. + /// + /// The document. + [JsonProperty("Document")] + [XmlElement("Document")] + public string Document + { + get => _document; + set { - get => _document; - set + if (string.IsNullOrWhiteSpace(value)) + return; + var document = new string(value.Where(char.IsDigit).ToArray()); + if (document.Length == 8) { - if (string.IsNullOrWhiteSpace(value)) - return; - var document = new string(value.Where(char.IsDigit).ToArray()); - if (document.Length == 8) + if (document.Equals("00000000")) { - if (document.Equals("00000000")) - { - document = "00000000000191"; - } - else - { - document += "0001"; - document += $"{document}00".CalculateBrazilianCorporateDocument(); - } + document = "00000000000191"; + } + else + { + document += "0001"; + document += $"{document}00".CalculateBrazilianCorporateDocument(); } - - _document = document.FormatBrazilianDocument(); } + + _document = document.FormatBrazilianDocument(); } + } - /// - /// Gets or sets the long name. - /// - /// The long name. - [JsonProperty("LongName")] - [XmlElement("LongName")] - public string LongName { get; set; } - - /// - /// Gets or sets the short name. - /// - /// The short name. - [JsonProperty("ShortName")] - [XmlElement("ShortName")] - public string ShortName { get; set; } - - /// - /// Gets or sets the network. - /// - /// The network. - [JsonProperty("Network")] - [XmlElement("Network")] - public string Network { get; set; } - - /// - /// Gets or sets the type. - /// - /// The type. - [JsonProperty("Type")] - [XmlElement("Type")] - public string Type { get; set; } - - /// - /// Gets or sets the type of the pix. - /// - /// The type of the pix. - [JsonProperty("PixType")] - [XmlElement("PixType")] - public string PixType { get; set; } - - /// - /// Gets or sets the charge. - /// - /// The charge. - [JsonProperty("Charge")] - [XmlElement("Charge")] - public bool? Charge - { - get => - string.IsNullOrWhiteSpace(ChargeStr) - ? null - : ChargeStr.Equals("sim", StringComparison.InvariantCultureIgnoreCase); - set - { - if (value == null) - return; + /// + /// Gets or sets the long name. + /// + /// The long name. + [JsonProperty("LongName")] + [XmlElement("LongName")] + public string LongName { get; set; } - ChargeStr = value.Value ? "sim" : "não"; - } - } + /// + /// Gets or sets the short name. + /// + /// The short name. + [JsonProperty("ShortName")] + [XmlElement("ShortName")] + public string ShortName { get; set; } + + /// + /// Gets or sets the network. + /// + /// The network. + [JsonProperty("Network")] + [XmlElement("Network")] + public string Network { get; set; } + + /// + /// Gets or sets the type. + /// + /// The type. + [JsonProperty("Type")] + [XmlElement("Type")] + public string Type { get; set; } - /// - /// Gets or sets the charge string. - /// - /// The charge string. - [JsonIgnore] - [XmlIgnore] - public string ChargeStr { get; set; } - - /// - /// Gets or sets the credit document. - /// - /// The credit document. - [JsonProperty("CreditDocument")] - [XmlElement("CreditDocument")] - public bool? CreditDocument + /// + /// Gets or sets the type of the pix. + /// + /// The type of the pix. + [JsonProperty("PixType")] + [XmlElement("PixType")] + public string PixType { get; set; } + + /// + /// Gets or sets the charge. + /// + /// The charge. + [JsonProperty("Charge")] + [XmlElement("Charge")] + public bool? Charge + { + get => + string.IsNullOrWhiteSpace(ChargeStr) + ? null + : ChargeStr.Equals("sim", StringComparison.InvariantCultureIgnoreCase); + set { - get => - string.IsNullOrWhiteSpace(CreditDocumentStr) - ? null - : CreditDocumentStr.Equals("sim", StringComparison.InvariantCultureIgnoreCase); - set - { - if (value == null) - return; + if (value == null) + return; - CreditDocumentStr = value.Value ? "sim" : "não"; - } + ChargeStr = value.Value ? "sim" : "não"; } + } + + /// + /// Gets or sets the charge string. + /// + /// The charge string. + [JsonIgnore] + [XmlIgnore] + public string ChargeStr { get; set; } - /// - /// Gets or sets the credit document string. - /// - /// The credit document string. - [JsonIgnore] - [XmlIgnore] - public string CreditDocumentStr { get; set; } - - /// - /// Gets or sets the salary portability. - /// - /// The salary portability. - [JsonProperty("SalaryPortability")] - [XmlElement("SalaryPortability")] - public string SalaryPortability { get; set; } - - /// - /// Gets or sets the products. - /// - /// The products. - [JsonProperty("Products")] - [XmlArray("Products")] - [XmlArrayItem("Product")] - public string[] Products { get; set; } - - /// - /// The URL - /// - private string _url; - - /// - /// Gets or sets the URL. - /// - /// The URL. - [JsonProperty("Url")] - [XmlElement("Url")] - public string Url + /// + /// Gets or sets the credit document. + /// + /// The credit document. + [JsonProperty("CreditDocument")] + [XmlElement("CreditDocument")] + public bool? CreditDocument + { + get => + string.IsNullOrWhiteSpace(CreditDocumentStr) + ? null + : CreditDocumentStr.Equals("sim", StringComparison.InvariantCultureIgnoreCase); + set { - get => _url; - set - { - if (string.IsNullOrWhiteSpace(value) || value.Equals("NA")) - return; - _url = $"https://{value.ToLower().Replace("https://", "")}"; - } + if (value == null) + return; + + CreditDocumentStr = value.Value ? "sim" : "não"; } + } + + /// + /// Gets or sets the credit document string. + /// + /// The credit document string. + [JsonIgnore] + [XmlIgnore] + public string CreditDocumentStr { get; set; } + + /// + /// Gets or sets a value indicating whether [legal cheque]. + /// + /// true if [legal cheque]; otherwise, false. + [JsonProperty("LegalCheque")] + [XmlElement("LegalCheque")] + public bool LegalCheque { get; set; } + + /// + /// Gets or sets the salary portability. + /// + /// The salary portability. + [JsonProperty("SalaryPortability")] + [XmlElement("SalaryPortability")] + public string SalaryPortability { get; set; } + + /// + /// Gets or sets the products. + /// + /// The products. + [JsonProperty("Products")] + [XmlArray("Products")] + [XmlArrayItem("Product")] + public string[] Products { get; set; } - /// - /// Gets or sets the date operation started. - /// - /// The date operation started. - [JsonProperty("DateOperationStarted")] - [XmlElement("DateOperationStarted")] - public string DateOperationStarted { get; set; } - - /// - /// Gets or sets the date pix started. - /// - /// The date pix started. - [JsonProperty("DatePixStarted")] - [XmlElement("DatePixStarted")] - public string DatePixStarted { get; set; } - - /// - /// Gets or sets the date registered. - /// - /// The date registered. - [JsonProperty("DateRegistered")] - [XmlElement("DateRegistered")] - public DateTimeOffset? DateRegistered { get; set; } - - /// - /// Gets or sets the date updated. - /// - /// The date updated. - [JsonProperty("DateUpdated")] - [XmlElement("DateUpdated")] - public DateTimeOffset? DateUpdated { get; set; } - - #region Equality members - - /// - /// Indicates whether the current object is equal to another object of the same type. - /// - /// An object to compare with this object. - /// if the current object is equal to the parameter; otherwise, . - public bool Equals(Bank other) + /// + /// The URL + /// + private string _url; + + /// + /// Gets or sets the URL. + /// + /// The URL. + [JsonProperty("Url")] + [XmlElement("Url")] + public string Url + { + get => _url; + set { - if (other is null) - return false; - - if (ReferenceEquals(this, other)) - return true; - - return string.Equals( - _document, - other._document, - StringComparison.InvariantCultureIgnoreCase - ) - && string.Equals(_url, other._url, StringComparison.InvariantCultureIgnoreCase) - && string.Equals( - ChargeStr, - other.ChargeStr, - StringComparison.InvariantCultureIgnoreCase - ) - && Compe == other.Compe - && string.Equals( - CreditDocumentStr, - other.CreditDocumentStr, - StringComparison.InvariantCultureIgnoreCase - ) - && string.Equals( - DateOperationStarted, - other.DateOperationStarted, - StringComparison.InvariantCultureIgnoreCase - ) - && string.Equals( - DatePixStarted, - other.DatePixStarted, - StringComparison.InvariantCultureIgnoreCase - ) - && Nullable.Equals(DateRegistered, other.DateRegistered) - && Nullable.Equals(DateUpdated, other.DateUpdated) - && Ispb == other.Ispb - && string.Equals( - LongName, - other.LongName, - StringComparison.InvariantCultureIgnoreCase - ) - && string.Equals( - Network, - other.Network, - StringComparison.InvariantCultureIgnoreCase - ) - && string.Equals( - PixType, - other.PixType, - StringComparison.InvariantCultureIgnoreCase - ) - && - //Equals(Products, other.Products) && - string.Equals( - SalaryPortability, - other.SalaryPortability, - StringComparison.InvariantCultureIgnoreCase - ) - && string.Equals( - ShortName, - other.ShortName, - StringComparison.InvariantCultureIgnoreCase - ) - && string.Equals(Type, other.Type, StringComparison.InvariantCultureIgnoreCase); + if (string.IsNullOrWhiteSpace(value) || value.Equals("NA")) + return; + _url = $"https://{value.ToLower().Replace("https://", "")}"; } + } - /// - /// Determines whether the specified object is equal to the current object. - /// - /// The object to compare with the current object. - /// if the specified object is equal to the current object; otherwise, . - public override bool Equals(object obj) - { - if (obj is null) - return false; + /// + /// Gets or sets the date operation started. + /// + /// The date operation started. + [JsonProperty("DateOperationStarted")] + [XmlElement("DateOperationStarted")] + public string DateOperationStarted { get; set; } + + /// + /// Gets or sets the date pix started. + /// + /// The date pix started. + [JsonProperty("DatePixStarted")] + [XmlElement("DatePixStarted")] + public string DatePixStarted { get; set; } - if (ReferenceEquals(this, obj)) - return true; + /// + /// Gets or sets the date registered. + /// + /// The date registered. + [JsonProperty("DateRegistered")] + [XmlElement("DateRegistered")] + public DateTimeOffset? DateRegistered { get; set; } - return obj.GetType() == GetType() && Equals((Bank)obj); - } + /// + /// Gets or sets the date updated. + /// + /// The date updated. + [JsonProperty("DateUpdated")] + [XmlElement("DateUpdated")] + public DateTimeOffset? DateUpdated { get; set; } + + #region Equality members + + /// + /// Indicates whether the current object is equal to another object of the same type. + /// + /// An object to compare with this object. + /// if the current object is equal to the parameter; otherwise, . + public bool Equals(Bank other) + { + if (other is null) + return false; + + if (ReferenceEquals(this, other)) + return true; + + return string.Equals( + _document, + other._document, + StringComparison.InvariantCultureIgnoreCase + ) + && string.Equals(_url, other._url, StringComparison.InvariantCultureIgnoreCase) + && string.Equals( + ChargeStr, + other.ChargeStr, + StringComparison.InvariantCultureIgnoreCase + ) + && Compe == other.Compe + && string.Equals( + CreditDocumentStr, + other.CreditDocumentStr, + StringComparison.InvariantCultureIgnoreCase + ) + && string.Equals( + DateOperationStarted, + other.DateOperationStarted, + StringComparison.InvariantCultureIgnoreCase + ) + && string.Equals( + DatePixStarted, + other.DatePixStarted, + StringComparison.InvariantCultureIgnoreCase + ) + && Nullable.Equals(DateRegistered, other.DateRegistered) + && Nullable.Equals(DateUpdated, other.DateUpdated) + && Ispb == other.Ispb + && string.Equals(LongName, other.LongName, StringComparison.InvariantCultureIgnoreCase) + && string.Equals(Network, other.Network, StringComparison.InvariantCultureIgnoreCase) + && string.Equals(PixType, other.PixType, StringComparison.InvariantCultureIgnoreCase) + && LegalCheque == other.LegalCheque + //&& Equals(Products, other.Products) + && string.Equals( + SalaryPortability, + other.SalaryPortability, + StringComparison.InvariantCultureIgnoreCase + ) + && string.Equals( + ShortName, + other.ShortName, + StringComparison.InvariantCultureIgnoreCase + ) + && string.Equals(Type, other.Type, StringComparison.InvariantCultureIgnoreCase); + } + + /// + /// Determines whether the specified object is equal to the current object. + /// + /// The object to compare with the current object. + /// if the specified object is equal to the current object; otherwise, . + public override bool Equals(object obj) + { + if (obj is null) + return false; + + if (ReferenceEquals(this, obj)) + return true; + + return obj.GetType() == GetType() && Equals((Bank)obj); + } - /// - /// Serves as the default hash function. - /// - /// A hash code for the current object. + /// + /// Serves as the default hash function. + /// + /// A hash code for the current object. #pragma warning disable S2328 - public override int GetHashCode() + [SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")] + public override int GetHashCode() #pragma warning restore S2328 + { + unchecked { - unchecked - { - var hashCode = new HashCode(); - hashCode.Add(_document ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); - hashCode.Add(_url ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); - hashCode.Add(ChargeStr ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); - hashCode.Add(Compe); - hashCode.Add( - CreditDocumentStr ?? string.Empty, - StringComparer.InvariantCultureIgnoreCase - ); - hashCode.Add( - DateOperationStarted ?? string.Empty, - StringComparer.InvariantCultureIgnoreCase - ); - hashCode.Add( - DatePixStarted ?? string.Empty, - StringComparer.InvariantCultureIgnoreCase - ); - hashCode.Add(DateRegistered); - hashCode.Add(DateUpdated); - hashCode.Add(Ispb); - hashCode.Add(LongName ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); - hashCode.Add(Network ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); - hashCode.Add(PixType ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); - //hashCode.Add(Products); - hashCode.Add( - SalaryPortability ?? string.Empty, - StringComparer.InvariantCultureIgnoreCase - ); - hashCode.Add(ShortName ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); - hashCode.Add(Type ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); - - return hashCode.ToHashCode(); - } + var hashCode = new HashCode(); + hashCode.Add(_document ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); + hashCode.Add(_url ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); + hashCode.Add(ChargeStr ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); + hashCode.Add(Compe); + hashCode.Add( + CreditDocumentStr ?? string.Empty, + StringComparer.InvariantCultureIgnoreCase + ); + hashCode.Add( + DateOperationStarted ?? string.Empty, + StringComparer.InvariantCultureIgnoreCase + ); + hashCode.Add(DatePixStarted ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); + hashCode.Add(DateRegistered); + hashCode.Add(DateUpdated); + hashCode.Add(Ispb); + hashCode.Add(LongName ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); + hashCode.Add(Network ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); + hashCode.Add(PixType ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); + //hashCode.Add(Products); + hashCode.Add(LegalCheque); + hashCode.Add( + SalaryPortability ?? string.Empty, + StringComparer.InvariantCultureIgnoreCase + ); + hashCode.Add(ShortName ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); + hashCode.Add(Type ?? string.Empty, StringComparer.InvariantCultureIgnoreCase); + + return hashCode.ToHashCode(); } + } - /// - /// Returns a string that represents the current object. - /// - /// A string that represents the current object. - public override string ToString() - { - var strBuilder = new StringBuilder(); + /// + /// Returns a string that represents the current object. + /// + /// A string that represents the current object. + public override string ToString() + { + var strBuilder = new StringBuilder(); - if (Ispb > 0 || Compe.Equals(1)) - strBuilder.Append($"ISPB: {IspbString} | "); + if (Ispb > 0 || Compe.Equals(1)) + strBuilder.Append($"ISPB: {IspbString} | "); - if (!string.IsNullOrWhiteSpace(Document)) - strBuilder.Append($"Document: {Document} | "); + if (!string.IsNullOrWhiteSpace(Document)) + strBuilder.Append($"Document: {Document} | "); - if (!string.IsNullOrWhiteSpace(LongName)) - strBuilder.Append($"Long name: {LongName} | "); + if (!string.IsNullOrWhiteSpace(LongName)) + strBuilder.Append($"Long name: {LongName} | "); - if (!string.IsNullOrWhiteSpace(ShortName)) - strBuilder.Append($"Short name: {ShortName} | "); + if (!string.IsNullOrWhiteSpace(ShortName)) + strBuilder.Append($"Short name: {ShortName} | "); - if (!string.IsNullOrWhiteSpace(Network)) - strBuilder.Append($"Network: {Network} | "); + if (!string.IsNullOrWhiteSpace(Network)) + strBuilder.Append($"Network: {Network} | "); - if (!string.IsNullOrWhiteSpace(Type)) - strBuilder.Append($"Type: {Type} | "); + if (!string.IsNullOrWhiteSpace(Type)) + strBuilder.Append($"Type: {Type} | "); - if (!string.IsNullOrWhiteSpace(PixType)) - strBuilder.Append($"PIX type: {PixType} | "); + if (!string.IsNullOrWhiteSpace(PixType)) + strBuilder.Append($"PIX type: {PixType} | "); - if (Charge.HasValue) - strBuilder.Append($"Charge: {Charge} | "); + if (Charge.HasValue) + strBuilder.Append($"Charge: {Charge} | "); - if (CreditDocument.HasValue) - strBuilder.Append($"Credit document: {CreditDocument} | "); + if (CreditDocument.HasValue) + strBuilder.Append($"Credit document: {CreditDocument} | "); - if (!string.IsNullOrWhiteSpace(SalaryPortability)) - strBuilder.Append($"Salary portability: {SalaryPortability} | "); + strBuilder.Append($"Legal cheque: {LegalCheque} | "); - if (Products != null) - strBuilder.Append($"Products: {string.Join(",", Products)} | "); + if (!string.IsNullOrWhiteSpace(SalaryPortability)) + strBuilder.Append($"Salary portability: {SalaryPortability} | "); - if (!string.IsNullOrWhiteSpace(DateOperationStarted)) - strBuilder.Append($"Date operation started: {DateOperationStarted} | "); + if (Products != null) + strBuilder.Append($"Products: {string.Join(",", Products)} | "); - if (!string.IsNullOrWhiteSpace(DatePixStarted)) - strBuilder.Append($"Date PIX started: {DatePixStarted} | "); + if (!string.IsNullOrWhiteSpace(DateOperationStarted)) + strBuilder.Append($"Date operation started: {DateOperationStarted} | "); - strBuilder.Append( - $"Date registered: {DateRegistered:O} | Date updated: {DateUpdated:O}" - ); + if (!string.IsNullOrWhiteSpace(DatePixStarted)) + strBuilder.Append($"Date PIX started: {DatePixStarted} | "); - return strBuilder.ToString(); - } + strBuilder.Append($"Date registered: {DateRegistered:O} | Date updated: {DateUpdated:O}"); - /// - /// Returns a value that indicates whether the values of two objects are equal. - /// - /// The first value to compare. - /// The second value to compare. - /// true if the and parameters have the same value; otherwise, false. - public static bool operator ==(Bank left, Bank right) => Equals(left, right); - - /// - /// Returns a value that indicates whether two objects have different values. - /// - /// The first value to compare. - /// The second value to compare. - /// true if and are not equal; otherwise, false. - public static bool operator !=(Bank left, Bank right) => !Equals(left, right); - - #endregion + return strBuilder.ToString(); } + + /// + /// Returns a value that indicates whether the values of two objects are equal. + /// + /// The first value to compare. + /// The second value to compare. + /// true if the and parameters have the same value; otherwise, false. + public static bool operator ==(Bank left, Bank right) => Equals(left, right); + + /// + /// Returns a value that indicates whether two objects have different values. + /// + /// The first value to compare. + /// The second value to compare. + /// true if and are not equal; otherwise, false. + public static bool operator !=(Bank left, Bank right) => !Equals(left, right); + + #endregion } diff --git a/BancosBrasileiros.MergeTool/Dto/Banks.cs b/BancosBrasileiros.MergeTool/Dto/Banks.cs index 282f1f2e..96fe5ab7 100644 --- a/BancosBrasileiros.MergeTool/Dto/Banks.cs +++ b/BancosBrasileiros.MergeTool/Dto/Banks.cs @@ -12,21 +12,20 @@ // // *********************************************************************** -namespace BancosBrasileiros.MergeTool.Dto -{ - using System.Xml.Serialization; +namespace BancosBrasileiros.MergeTool.Dto; + +using System.Xml.Serialization; +/// +/// Class Banks. +/// +[XmlRoot("banks")] +public class Banks +{ /// - /// Class Banks. + /// Gets or sets the bank. /// - [XmlRoot("banks")] - public class Banks - { - /// - /// Gets or sets the bank. - /// - /// The bank. - [XmlElement("bank")] - public Bank[] Bank { get; set; } - } + /// The bank. + [XmlElement("bank")] + public Bank[] Bank { get; set; } } diff --git a/BancosBrasileiros.MergeTool/Helpers/Constants.cs b/BancosBrasileiros.MergeTool/Helpers/Constants.cs index 363adea8..add03f70 100644 --- a/BancosBrasileiros.MergeTool/Helpers/Constants.cs +++ b/BancosBrasileiros.MergeTool/Helpers/Constants.cs @@ -11,63 +11,70 @@ // // // *********************************************************************** -namespace BancosBrasileiros.MergeTool.Helpers + +#pragma warning disable S1075 +namespace BancosBrasileiros.MergeTool.Helpers; + +/// +/// Class Constants. +/// +internal static class Constants { /// - /// Class Constants. + /// The change log URL /// - internal static class Constants - { - /// - /// The change log URL - /// - public const string ChangeLogUrl = - "https://raw.githubusercontent.com/guibranco/BancosBrasileiros/main/CHANGELOG.md"; + public const string ChangeLogUrl = + "https://raw.githubusercontent.com/guibranco/BancosBrasileiros/main/CHANGELOG.md"; - /// - /// The base URL - /// - public const string BaseUrl = - "https://raw.githubusercontent.com/guibranco/BancosBrasileiros/main/data/bancos.json"; + /// + /// The base URL + /// + public const string BaseUrl = + "https://raw.githubusercontent.com/guibranco/BancosBrasileiros/main/data/bancos.json"; - /// - /// The string URL - /// - public const string StrUrl = - "http://www.bcb.gov.br/pom/spb/estatistica/port/ParticipantesSTRport.csv"; + /// + /// The string URL + /// + public const string StrUrl = + "http://www.bcb.gov.br/pom/spb/estatistica/port/ParticipantesSTRport.csv"; - /// - /// The spi URL - /// - public const string SpiUrl = - "https://www.bcb.gov.br/content/estabilidadefinanceira/spi/participantes-spi-{0:yyyyMMdd}.csv"; + /// + /// The spi URL + /// + public const string SpiUrl = + "https://www.bcb.gov.br/content/estabilidadefinanceira/spi/participantes-spi-{0:yyyyMMdd}.csv"; - /// - /// The SLC URL - /// - public const string SlcUrl = - "https://www2.nuclea.com.br/Monitoramento/Participantes_Homologados.pdf"; + /// + /// The SLC URL + /// + public const string SlcUrl = + "https://www2.nuclea.com.br/Monitoramento/Participantes_Homologados.pdf"; - /// - /// The siloc URL - /// - public const string SilocUrl = "https://www2.nuclea.com.br/Monitoramento/SILOC.pdf"; + /// + /// The siloc URL + /// + public const string SilocUrl = "https://www2.nuclea.com.br/Monitoramento/SILOC.pdf"; - /// - /// The sitraf URL - /// - public const string SitrafUrl = - "https://www2.nuclea.com.br/Monitoramento/Rela%C3%A7%C3%A3o%20de%20Clientes%20SITRAF.pdf"; + /// + /// The sitraf URL + /// + public const string SitrafUrl = + "https://www2.nuclea.com.br/Monitoramento/Rela%C3%A7%C3%A3o%20de%20Clientes%20SITRAF.pdf"; + + /// + /// The CTC URL + /// + public const string CtcUrl = "https://www2.nuclea.com.br/SAP/CTC.pdf"; - /// - /// The CTC URL - /// - public const string CtcUrl = "https://www2.nuclea.com.br/SAP/CTC.pdf"; + /// + /// The PCPS URL + /// + public const string PcpsUrl = + "https://www2.nuclea.com.br/SAP/Rela%C3%A7%C3%A3o%20de%20Participantes%20PCPS.pdf"; - /// - /// The PCPS URL - /// - public const string PcpsUrl = - "https://www2.nuclea.com.br/SAP/Rela%C3%A7%C3%A3o%20de%20Participantes%20PCPS.pdf"; - } + /// + /// The CQL URL + /// + public const string CqlUrl = + "https://www2.nuclea.com.br/SAP/Rela%C3%A7%C3%A3o%20de%20Participantes%20CQL.pdf"; } diff --git a/BancosBrasileiros.MergeTool/Helpers/Logger.cs b/BancosBrasileiros.MergeTool/Helpers/Logger.cs index ae2ebe81..84f78d66 100644 --- a/BancosBrasileiros.MergeTool/Helpers/Logger.cs +++ b/BancosBrasileiros.MergeTool/Helpers/Logger.cs @@ -11,25 +11,25 @@ // // // *********************************************************************** -namespace BancosBrasileiros.MergeTool.Helpers -{ - using System; +namespace BancosBrasileiros.MergeTool.Helpers; + +using System; + +/// +/// Class Helpers. +/// +internal static class Logger +{ /// - /// Class Helpers. + /// Logs the specified message. /// - internal static class Logger + /// The message. + /// The color. + public static void Log(string message, ConsoleColor color) { - /// - /// Logs the specified message. - /// - /// The message. - /// The color. - public static void Log(string message, ConsoleColor color) - { - Console.ForegroundColor = color; - Console.WriteLine(message); - Console.ForegroundColor = ConsoleColor.White; - } + Console.ForegroundColor = color; + Console.WriteLine(message); + Console.ForegroundColor = ConsoleColor.White; } } diff --git a/BancosBrasileiros.MergeTool/Helpers/Patterns.cs b/BancosBrasileiros.MergeTool/Helpers/Patterns.cs index d3532958..1c961070 100644 --- a/BancosBrasileiros.MergeTool/Helpers/Patterns.cs +++ b/BancosBrasileiros.MergeTool/Helpers/Patterns.cs @@ -11,76 +11,91 @@ // // // *********************************************************************** + +namespace BancosBrasileiros.MergeTool.Helpers; + +using System; using System.Text.RegularExpressions; -namespace BancosBrasileiros.MergeTool.Helpers +/// +/// Class Patterns. +/// +internal static class Patterns { /// - /// Class Patterns. + /// The comma separated values pattern /// - internal static class Patterns - { - /// - /// The comma separated values pattern - /// - public static readonly Regex CsvPattern = - new( - ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))", - RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled - ); + public static readonly Regex CsvPattern = + new( + ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))", + RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled, + TimeSpan.FromSeconds(5) + ); - /// - /// The semicolon separated values pattern - /// - public static readonly Regex SsvPattern = - new( - ";(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))", - RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled - ); + /// + /// The semicolon separated values pattern + /// + public static readonly Regex SsvPattern = + new( + ";(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))", + RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled, + TimeSpan.FromSeconds(5) + ); - /// - /// The SLC pattern - /// - public static readonly Regex SlcPattern = - new( - @"^(?\d{1,3})\s(?\d{1,2}\.\d{3}\.\d{3}(?:.|\/)\d{4}([-|·|\.|\s]{1,2})\d{2})\s(?.+?)(?:[\s|X]){1,7}$", - RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled - ); + /// + /// The SLC pattern + /// + public static readonly Regex SlcPattern = + new( + @"^(?\d{1,3})\s(?\d{1,2}\.\d{3}\.\d{3}(?:.|\/)\d{4}([-|·|\.|\s]{1,2})\d{2})\s(?.+?)(?:[\s|X]){1,7}$", + RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled, + TimeSpan.FromSeconds(5) + ); - /// - /// The SILOC pattern - /// - public static readonly Regex SilocPattern = - new( - @"^(?\d{1,3})\s(?\d{3})\s(?\d{8})\s(?sim|não)\s(?sim|não)\s(?.+?)$", - RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled - ); + /// + /// The SILOC pattern + /// + public static readonly Regex SilocPattern = + new( + @"^(?\d{1,3})\s(?\d{3})\s(?\d{8})\s(?sim|não)\s(?sim|não)\s(?.+?)$", + RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled, + TimeSpan.FromSeconds(5) + ); - /// - /// The sitraf pattern - /// - public static readonly Regex SitrafPattern = - new( - @"^(?\d{1,3})\s(?\d{3})\s(?\d{8})\s(?.+?)$", - RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled - ); + /// + /// The sitraf pattern + /// + public static readonly Regex SitrafPattern = + new( + @"^(?\d{1,3})\s(?\d{3})\s(?\d{8})\s(?.+?)$", + RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled, + TimeSpan.FromSeconds(5) + ); - /// - /// The ctc pattern - /// - public static readonly Regex CtcPattern = - new( - @"^(?\d{1,3})\s(?.+?)\s(?\d{1,2}\.\d{3}\.\d{3}(?:.|\/)\d{4}([-|·|\.|\s]{1,2})\d{2})\s+(?\d{8})\s(?.+?)$", - RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled - ); + /// + /// The ctc pattern + /// + public static readonly Regex CtcPattern = + new( + @"^(?\d{1,3})\s(?.+?)\s(?\d{1,2}\.\d{3}\.\d{3}(?:.|\/)\d{4}([-|·|\.|\s]{1,2})\d{2})\s+(?\d{8})\s(?.+?)$", + RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled, + TimeSpan.FromSeconds(5) + ); + + /// + /// The PCPS pattern + /// + public static readonly Regex PcpsPattern = + new( + @"^(?\d{1,3})\s(?.+?)\s(?\d{1,2}\.\d{3}\.\d{3}(?:.|\/)\d{4}([-|·|\.|\s]{1,3})\d{2})\s+(?\d{7,8})\s(?.+?)$", + RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled, + TimeSpan.FromSeconds(5) + ); - /// - /// The PCPS pattern - /// - public static readonly Regex PcpsPattern = - new( - @"^(?\d{1,3})\s(?.+?)\s(?\d{1,2}\.\d{3}\.\d{3}(?:.|\/)\d{4}([-|·|\.|\s]{1,3})\d{2})\s+(?\d{7,8})\s(?.+?)$", - RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled - ); - } + public static readonly Regex CqlPattern = + new( + @"^(?\d{1,3})\s(?.+?)\s(?\d{7,8})\s(?.+?)$", + RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled, + TimeSpan.FromSeconds(5) + ); } diff --git a/BancosBrasileiros.MergeTool/Helpers/Reader.cs b/BancosBrasileiros.MergeTool/Helpers/Reader.cs index f5155767..3f207be5 100644 --- a/BancosBrasileiros.MergeTool/Helpers/Reader.cs +++ b/BancosBrasileiros.MergeTool/Helpers/Reader.cs @@ -12,591 +12,622 @@ // // *********************************************************************** -namespace BancosBrasileiros.MergeTool.Helpers +namespace BancosBrasileiros.MergeTool.Helpers; + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using CrispyWaffle.Serialization; +using Dto; +using iTextSharp.text.pdf; +using iTextSharp.text.pdf.parser; + +/// +/// Class Reader. +/// +internal class Reader { - using System.Net.Http; - using BancosBrasileiros.MergeTool.Dto; - using iTextSharp.text.pdf; - using iTextSharp.text.pdf.parser; - using System; - using System.Collections.Generic; - using System.Globalization; - using System.Linq; - using System.Net; - using System.Text; - using CrispyWaffle.Serialization; + /// + /// The counting SLC + /// + private int _countingSlc; /// - /// Class Reader. + /// The counting sitraf /// - internal class Reader - { - /// - /// The counting SLC - /// - private int _countingSlc; - - /// - /// The counting sitraf - /// - private int _countingSitraf; - - /// - /// The counting CTC - /// - private int _countingCtc; - - /// - /// The counting PCPS - /// - private int _countingPcps; - - /// - /// Downloads the string. - /// - /// The URL. - /// System.String. - private static string DownloadString(string url) - { - var client = new HttpClient(); - using var response = client.GetAsync(url).Result; - using var content = response.Content; - return content.ReadAsStringAsync().Result; - } + private int _countingSitraf; - private static List DownloadAndParsePdf( - string url, - string system, - Func> callback - ) - { - var result = new List(); - PdfReader reader; + /// + /// The counting CTC + /// + private int _countingCtc; - try - { - Logger.Log($"Downloading {system}", ConsoleColor.Green); - reader = new(url); - } - catch (Exception e) - { - Logger.Log($"Error downloading {system}: {e.Message}", ConsoleColor.DarkRed); - return result; - } + /// + /// The counting PCPS + /// + private int _countingPcps; - for (var currentPage = 1; currentPage <= reader.NumberOfPages; currentPage++) - { - var currentText = PdfTextExtractor.GetTextFromPage( - reader, - currentPage, - new SimpleTextExtractionStrategy() - ); - currentText = Encoding.UTF8.GetString( - Encoding.Convert( - Encoding.Default, - Encoding.UTF8, - Encoding.Default.GetBytes(currentText) - ) - ); - result.AddRange(callback(currentText)); - } + /// + /// The counting CQL + /// + private int _countingCql; - return result; - } + /// + /// Downloads the string. + /// + /// The URL. + /// System.String. + private static string DownloadString(string url) + { + var client = new HttpClient(); + using var response = client.GetAsync(url).Result; + using var content = response.Content; + return content.ReadAsStringAsync().Result; + } - /// - /// Loads the change log. - /// - /// System.String. - public static string LoadChangeLog() => DownloadString(Constants.ChangeLogUrl); - - /// - /// Loads the base. - /// - /// List<Bank>. - public List LoadBase() + private static List DownloadAndParsePdf( + string url, + string system, + Func> callback + ) + { + var result = new List(); + PdfReader reader; + + try { - Logger.Log("Downloading base", ConsoleColor.Green); - var data = DownloadString(Constants.BaseUrl); - return SerializerFactory - .GetCustomSerializer>(SerializerFormat.Json) - .Deserialize(data); + Logger.Log($"Downloading {system}", ConsoleColor.Green); + reader = new(url); + } + catch (Exception e) + { + Logger.Log($"Error downloading {system}: {e.Message}", ConsoleColor.DarkRed); + return result; } - /// - /// Loads the string. - /// - /// List<Bank>. - public List LoadStr() + for (var currentPage = 1; currentPage <= reader.NumberOfPages; currentPage++) { - Logger.Log("Downloading STR", ConsoleColor.Green); - var data = DownloadString(Constants.StrUrl); - var lines = data.Split("\n").Skip(1).ToArray(); - - return lines - .Select(line => Patterns.CsvPattern.Split(line)) - .Where(columns => columns.Length > 1 && int.TryParse(columns[2], out _)) - .Select( - columns => - new Bank - { - CompeString = columns[2], - IspbString = columns[0], - LongName = columns[5].Replace("\"", "").Replace("?", "-").Trim(), - ShortName = columns[1].Trim(), - DateOperationStarted = DateTime - .ParseExact( - columns[6].Trim(), - "dd/MM/yyyy", - CultureInfo.InvariantCulture - ) - .ToString("yyyy-MM-dd"), - Network = columns[4] - } + var currentText = PdfTextExtractor.GetTextFromPage( + reader, + currentPage, + new SimpleTextExtractionStrategy() + ); + currentText = Encoding.UTF8.GetString( + Encoding.Convert( + Encoding.Default, + Encoding.UTF8, + Encoding.Default.GetBytes(currentText) ) - .ToList(); + ); + result.AddRange(callback(currentText)); } - /// - /// Loads the spi. - /// - /// List<Bank>. - public List LoadSpi() - { - Logger.Log("Downloading SPI", ConsoleColor.Green); - var baseDate = DateTime.Today; + return result; + } - var data = GetPixData(baseDate); + /// + /// Loads the change log. + /// + /// System.String. + public static string LoadChangeLog() => DownloadString(Constants.ChangeLogUrl); - while (string.IsNullOrWhiteSpace(data)) - { - baseDate = baseDate.AddDays(-1); - data = GetPixData(baseDate); - } + /// + /// Loads the base. + /// + /// List<Bank>. + public List LoadBase() + { + Logger.Log("Downloading base", ConsoleColor.Green); + var data = DownloadString(Constants.BaseUrl); + return SerializerFactory + .GetCustomSerializer>(SerializerFormat.Json) + .Deserialize(data); + } - var lines = data.Split("\n").Skip(1).ToArray(); - - return lines - .Select(line => Patterns.SsvPattern.Split(line)) - .Where(columns => columns.Length > 1 && int.TryParse(columns[0], out _)) - .Select( - columns => - new Bank - { - IspbString = columns[0], - LongName = columns[1], - ShortName = columns[2], - PixType = columns[4], - DatePixStarted = DateTime - .Parse( - columns[5].Trim(), - CultureInfo.InvariantCulture, - DateTimeStyles.AssumeLocal - ) - .ToUniversalTime() - .AddHours(-3) - .ToString("yyyy-MM-dd HH:mm:ss") - } - ) - .ToList(); - } + /// + /// Loads the string. + /// + /// List<Bank>. + public List LoadStr() + { + Logger.Log("Downloading STR", ConsoleColor.Green); + var data = DownloadString(Constants.StrUrl); + var lines = data.Split("\n").Skip(1).ToArray(); + + return lines + .Select(line => Patterns.CsvPattern.Split(line)) + .Where(columns => columns.Length > 1 && int.TryParse(columns[2], out _)) + .Select( + columns => + new Bank + { + CompeString = columns[2], + IspbString = columns[0], + LongName = columns[5].Replace("\"", "").Replace("?", "-").Trim(), + ShortName = columns[1].Trim(), + DateOperationStarted = DateTime + .ParseExact( + columns[6].Trim(), + "dd/MM/yyyy", + CultureInfo.InvariantCulture + ) + .ToString("yyyy-MM-dd"), + Network = columns[4] + } + ) + .ToList(); + } - /// - /// Gets the pix data. - /// - /// The date. - /// string. - private static string GetPixData(DateTime date) + /// + /// Loads the spi. + /// + /// List<Bank>. + public List LoadSpi() + { + Logger.Log("Downloading SPI", ConsoleColor.Green); + var baseDate = DateTime.Today; + + var data = GetPixData(baseDate); + + while (string.IsNullOrWhiteSpace(data)) { - try - { - return DownloadString(string.Format(Constants.SpiUrl, date)); - } - catch (WebException) - { - return null; - } + baseDate = baseDate.AddDays(-1); + data = GetPixData(baseDate); } - /// - /// Loads the SLC. - /// - /// List<Bank>. - public List LoadSlc() + var lines = data.Split("\n").Skip(1).ToArray(); + + return lines + .Select(line => Patterns.SsvPattern.Split(line)) + .Where(columns => columns.Length > 1 && int.TryParse(columns[0], out _)) + .Select( + columns => + new Bank + { + IspbString = columns[0], + LongName = columns[1], + ShortName = columns[2], + PixType = columns[4], + DatePixStarted = DateTime + .Parse( + columns[5].Trim(), + CultureInfo.InvariantCulture, + DateTimeStyles.AssumeLocal + ) + .ToUniversalTime() + .AddHours(-3) + .ToString("yyyy-MM-dd HH:mm:ss") + } + ) + .ToList(); + } + + /// + /// Gets the pix data. + /// + /// The date. + /// string. + private static string GetPixData(DateTime date) + { + try { - _countingSlc = 0; - return DownloadAndParsePdf(Constants.SlcUrl, "SLC", ParseLinesSlc); + return DownloadString(string.Format(Constants.SpiUrl, date)); } - - /// - /// Parses the lines SLC. - /// - /// The page. - /// IEnumerable<Bank>. - private IEnumerable ParseLinesSlc(string page) + catch (WebException) { - var result = new List(); - var lines = page.Split("\n"); - - var spliced = string.Empty; + return null; + } + } - foreach (var line in lines) - { - if (!Patterns.SlcPattern.IsMatch(line)) - { - spliced += $" {line}"; - continue; - } + /// + /// Loads the SLC. + /// + /// List<Bank>. + public List LoadSlc() + { + _countingSlc = 0; + return DownloadAndParsePdf(Constants.SlcUrl, "SLC", ParseLinesSlc); + } - Bank bank; + /// + /// Parses the lines SLC. + /// + /// The page. + /// IEnumerable<Bank>. + private IEnumerable ParseLinesSlc(string page) + { + var result = new List(); + var lines = page.Split("\n"); - if (!string.IsNullOrWhiteSpace(spliced)) - { - bank = ParseLineSlc(spliced.Trim()); + var spliced = string.Empty; - if (bank != null) - result.Add(bank); + foreach (var line in lines) + { + if (!Patterns.SlcPattern.IsMatch(line)) + { + spliced += $" {line}"; + continue; + } - spliced = string.Empty; - } + Bank bank; - bank = ParseLineSlc(line); + if (!string.IsNullOrWhiteSpace(spliced)) + { + bank = ParseLineSlc(spliced.Trim()); if (bank != null) result.Add(bank); + + spliced = string.Empty; } - return result; + bank = ParseLineSlc(line); + + if (bank != null) + result.Add(bank); } - /// - /// Parses the line SLC. - /// - /// The line. - /// Bank. - private Bank ParseLineSlc(string line) - { - if (!Patterns.SlcPattern.IsMatch(line)) - return null; + return result; + } - var match = Patterns.SlcPattern.Match(line); + /// + /// Parses the line SLC. + /// + /// The line. + /// Bank. + private Bank ParseLineSlc(string line) + { + if (!Patterns.SlcPattern.IsMatch(line)) + return null; - var code = Convert.ToInt32(match.Groups["code"].Value.Trim()); + var match = Patterns.SlcPattern.Match(line); - _countingSlc++; + var code = Convert.ToInt32(match.Groups["code"].Value.Trim()); - if (_countingSlc != code) - Logger.Log( - $"SLC | Counting: {_countingSlc++} | Code: {code}", - ConsoleColor.DarkYellow - ); + _countingSlc++; - return new() - { - Document = match.Groups["cnpj"].Value.Trim(), - LongName = match.Groups["nome"].Value.Replace("\"", "").Trim() - }; - } + if (_countingSlc != code) + Logger.Log($"SLC | Counting: {_countingSlc++} | Code: {code}", ConsoleColor.DarkYellow); - /// - /// Loads the siloc. - /// - /// List<Bank>. - public List LoadSiloc() + return new() { - return DownloadAndParsePdf(Constants.SilocUrl, "SILOC", ParseLinesSiloc); - } + Document = match.Groups["cnpj"].Value.Trim(), + LongName = match.Groups["nome"].Value.Replace("\"", "").Trim() + }; + } - /// - /// Parses the lines siloc. - /// - /// The page. - /// IEnumerable<Bank>. - private static IEnumerable ParseLinesSiloc(string page) - { - var result = new List(); - var lines = page.Split("\n"); + /// + /// Loads the siloc. + /// + /// List<Bank>. + public List LoadSiloc() + { + return DownloadAndParsePdf(Constants.SilocUrl, "SILOC", ParseLinesSiloc); + } - foreach (var line in lines) - { - var bank = ParseLineSiloc(line); + /// + /// Parses the lines siloc. + /// + /// The page. + /// IEnumerable<Bank>. + private static IEnumerable ParseLinesSiloc(string page) + { + var result = new List(); + var lines = page.Split("\n"); - if (bank != null) - result.Add(bank); - } + foreach (var line in lines) + { + var bank = ParseLineSiloc(line); - return result; + if (bank != null) + result.Add(bank); } - /// - /// Parses the line siloc. - /// - /// The line. - /// Bank. - private static Bank ParseLineSiloc(string line) - { - if (!Patterns.SilocPattern.IsMatch(line)) - return null; - - var match = Patterns.SilocPattern.Match(line); + return result; + } - return new() - { - Compe = Convert.ToInt32(match.Groups["compe"].Value.Trim()), - IspbString = match.Groups["ispb"].Value.Trim(), - LongName = match.Groups["nome"].Value.Replace("\"", "").Trim(), - ChargeStr = match.Groups["cobranca"].Value.Trim(), - CreditDocumentStr = match.Groups["doc"].Value.Trim() - }; - } + /// + /// Parses the line siloc. + /// + /// The line. + /// Bank. + private static Bank ParseLineSiloc(string line) + { + if (!Patterns.SilocPattern.IsMatch(line)) + return null; - /// - /// Loads the sitraf. - /// - /// List<Bank>. - public List LoadSitraf() - { - _countingSitraf = 0; - return DownloadAndParsePdf(Constants.SitrafUrl, "SITRAF", ParseLinesSitraf); - } + var match = Patterns.SilocPattern.Match(line); - /// - /// Parses the lines sitraf. - /// - /// The page. - /// IEnumerable<Bank>. - private IEnumerable ParseLinesSitraf(string page) + return new() { - var result = new List(); - var lines = page.Split("\n"); - - var spliced = string.Empty; + Compe = Convert.ToInt32(match.Groups["compe"].Value.Trim()), + IspbString = match.Groups["ispb"].Value.Trim(), + LongName = match.Groups["nome"].Value.Replace("\"", "").Trim(), + ChargeStr = match.Groups["cobranca"].Value.Trim(), + CreditDocumentStr = match.Groups["doc"].Value.Trim() + }; + } - foreach (var line in lines) - { - if (!Patterns.SitrafPattern.IsMatch(line)) - { - spliced += $" {line}"; - continue; - } + /// + /// Loads the sitraf. + /// + /// List<Bank>. + public List LoadSitraf() + { + _countingSitraf = 0; + return DownloadAndParsePdf(Constants.SitrafUrl, "SITRAF", ParseLinesSitraf); + } - Bank bank; + /// + /// Parses the lines sitraf. + /// + /// The page. + /// IEnumerable<Bank>. + private IEnumerable ParseLinesSitraf(string page) + { + var result = new List(); + var lines = page.Split("\n"); - if (!string.IsNullOrWhiteSpace(spliced)) - { - bank = ParseLineSitraf(spliced.Trim()); + var spliced = string.Empty; - if (bank != null) - result.Add(bank); + foreach (var line in lines) + { + if (!Patterns.SitrafPattern.IsMatch(line)) + { + spliced += $" {line}"; + continue; + } - spliced = string.Empty; - } + Bank bank; - bank = ParseLineSitraf(line); + if (!string.IsNullOrWhiteSpace(spliced)) + { + bank = ParseLineSitraf(spliced.Trim()); if (bank != null) result.Add(bank); + + spliced = string.Empty; } - return result; - } + bank = ParseLineSitraf(line); - /// - /// Parses the line sitraf. - /// - /// The line. - /// Bank. - private Bank ParseLineSitraf(string line) - { - if (!Patterns.SitrafPattern.IsMatch(line)) - return null; + if (bank != null) + result.Add(bank); + } - var match = Patterns.SitrafPattern.Match(line); + return result; + } - var code = Convert.ToInt32(match.Groups["code"].Value.Trim()); + /// + /// Parses the line sitraf. + /// + /// The line. + /// Bank. + private Bank ParseLineSitraf(string line) + { + if (!Patterns.SitrafPattern.IsMatch(line)) + return null; - _countingSitraf++; + var match = Patterns.SitrafPattern.Match(line); - if (_countingSitraf != code) - Logger.Log( - $"SITRAF | Counting: {_countingSitraf++} | Code: {code}", - ConsoleColor.DarkYellow - ); + var code = Convert.ToInt32(match.Groups["code"].Value.Trim()); - return new() - { - Compe = Convert.ToInt32(match.Groups["compe"].Value.Trim()), - IspbString = match.Groups["ispb"].Value.Trim(), - LongName = match.Groups["nome"].Value.Replace("\"", "").Trim() - }; - } + _countingSitraf++; - /// - /// Loads the CTC. - /// - /// List<Bank>. - public List LoadCtc() - { - _countingCtc = 0; - return DownloadAndParsePdf(Constants.CtcUrl, "CTC", ParsePageCtc); - } + if (_countingSitraf != code) + Logger.Log( + $"SITRAF | Counting: {_countingSitraf++} | Code: {code}", + ConsoleColor.DarkYellow + ); - /// - /// Parses the lines CTC. - /// - /// The page. - /// IEnumerable<Bank>. - private IEnumerable ParsePageCtc(string page) + return new() { - var result = new List(); - var lines = page.Split("\n"); - - var spliced = string.Empty; + Compe = Convert.ToInt32(match.Groups["compe"].Value.Trim()), + IspbString = match.Groups["ispb"].Value.Trim(), + LongName = match.Groups["nome"].Value.Replace("\"", "").Trim() + }; + } - foreach (var line in lines) - { - if (!Patterns.CtcPattern.IsMatch(line)) - { - spliced += $" {line}"; - continue; - } + /// + /// Loads the CTC. + /// + /// List<Bank>. + public List LoadCtc() + { + _countingCtc = 0; + return DownloadAndParsePdf(Constants.CtcUrl, "CTC", ParsePageCtc); + } - Bank bank; + /// + /// Parses the lines CTC. + /// + /// The page. + /// IEnumerable<Bank>. + private IEnumerable ParsePageCtc(string page) + { + var result = new List(); + var lines = page.Split("\n"); - if (!string.IsNullOrWhiteSpace(spliced)) - { - bank = ParseLineCtc(spliced.Trim()); + var spliced = string.Empty; - if (bank != null) - result.Add(bank); + foreach (var line in lines) + { + if (!Patterns.CtcPattern.IsMatch(line)) + { + spliced += $" {line}"; + continue; + } - spliced = string.Empty; - } + Bank bank; - bank = ParseLineCtc(line); + if (!string.IsNullOrWhiteSpace(spliced)) + { + bank = ParseLineCtc(spliced.Trim()); if (bank != null) result.Add(bank); + + spliced = string.Empty; } - return result; - } + bank = ParseLineCtc(line); - /// - /// Parses the line CTC. - /// - /// The line. - /// Bank. - private Bank ParseLineCtc(string line) - { - if (!Patterns.CtcPattern.IsMatch(line)) - return null; + if (bank != null) + result.Add(bank); + } - var match = Patterns.CtcPattern.Match(line); + return result; + } - var code = Convert.ToInt32(match.Groups["code"].Value.Trim()); + /// + /// Parses the line CTC. + /// + /// The line. + /// Bank. + private Bank ParseLineCtc(string line) + { + if (!Patterns.CtcPattern.IsMatch(line)) + return null; - _countingCtc++; + var match = Patterns.CtcPattern.Match(line); - if (_countingCtc != code) - Logger.Log( - $"CTC | Counting: {_countingCtc++} | Code: {code}", - ConsoleColor.DarkYellow - ); + var code = Convert.ToInt32(match.Groups["code"].Value.Trim()); - return new() - { - Document = match.Groups["cnpj"].Value.Trim(), - IspbString = match.Groups["ispb"].Value.Trim(), - LongName = match.Groups["nome"].Value.Replace("\"", "").Trim(), - Products = match.Groups["produtos"].Value - .Split(",") - .Select(p => p.Trim()) - .OrderBy(p => p) - .ToArray() - }; - } + _countingCtc++; - /// - /// Loads the PCPS. - /// - /// List<Bank>. - public List LoadPcps() - { - _countingPcps = 0; - return DownloadAndParsePdf(Constants.PcpsUrl, "PCPS", ParsePagePcps); - } + if (_countingCtc != code) + Logger.Log($"CTC | Counting: {_countingCtc++} | Code: {code}", ConsoleColor.DarkYellow); - /// - /// Parses the page PCPS. - /// - /// The page. - /// IEnumerable<Bank>. - private IEnumerable ParsePagePcps(string page) + return new() { - var result = new List(); - var lines = page.Split("\n"); - - var spliced = string.Empty; + Document = match.Groups["cnpj"].Value.Trim(), + IspbString = match.Groups["ispb"].Value.Trim(), + LongName = match.Groups["nome"].Value.Replace("\"", "").Trim(), + Products = match.Groups["produtos"].Value + .Split(",") + .Select(p => p.Trim()) + .OrderBy(p => p) + .ToArray() + }; + } - foreach (var line in lines) - { - if (!Patterns.PcpsPattern.IsMatch(line)) - { - spliced += $" {line}"; - continue; - } + /// + /// Loads the PCPS. + /// + /// List<Bank>. + public List LoadPcps() + { + _countingPcps = 0; + return DownloadAndParsePdf(Constants.PcpsUrl, "PCPS", ParsePagePcps); + } - Bank bank; + /// + /// Parses the page PCPS. + /// + /// The page. + /// IEnumerable<Bank>. + private IEnumerable ParsePagePcps(string page) + { + var result = new List(); + var lines = page.Split("\n"); - if (!string.IsNullOrWhiteSpace(spliced)) - { - bank = ParseLinePcps(spliced.Trim()); + var spliced = string.Empty; - if (bank != null) - result.Add(bank); + foreach (var line in lines) + { + if (!Patterns.PcpsPattern.IsMatch(line)) + { + spliced += $" {line}"; + continue; + } - spliced = string.Empty; - } + Bank bank; - bank = ParseLinePcps(line); + if (!string.IsNullOrWhiteSpace(spliced)) + { + bank = ParseLinePcps(spliced.Trim()); if (bank != null) result.Add(bank); + + spliced = string.Empty; } - return result; + bank = ParseLinePcps(line); + + if (bank != null) + result.Add(bank); } - /// - /// Parses the line PCPS. - /// - /// The line. - /// Bank. - private Bank ParseLinePcps(string line) + return result; + } + + /// + /// Parses the line PCPS. + /// + /// The line. + /// Bank. + private Bank ParseLinePcps(string line) + { + if (!Patterns.PcpsPattern.IsMatch(line)) + return null; + + var match = Patterns.PcpsPattern.Match(line); + + var code = Convert.ToInt32(match.Groups["code"].Value.Trim()); + + _countingPcps++; + + if (_countingPcps != code) + Logger.Log( + $"PCPS | Counting: {_countingPcps++} | Code: {code}", + ConsoleColor.DarkYellow + ); + + return new() { - if (!Patterns.PcpsPattern.IsMatch(line)) - return null; + Document = match.Groups["cnpj"].Value.Trim(), + IspbString = match.Groups["ispb"].Value.Trim(), + LongName = match.Groups["nome"].Value.Replace("\"", "").Trim(), + SalaryPortability = match.Groups["adesao"].Value.Trim() + }; + } - var match = Patterns.PcpsPattern.Match(line); + public List LoadCql() + { + _countingCql = 0; + return DownloadAndParsePdf(Constants.CqlUrl, "CQL", ParsePageCql); + } - var code = Convert.ToInt32(match.Groups["code"].Value.Trim()); + private IEnumerable ParsePageCql(string page) + { + var lines = page.Split("\n"); - _countingPcps++; + return lines.Select(ParseLineCql).Where(bank => bank != null).ToList(); + } - if (_countingPcps != code) - Logger.Log( - $"PCPS | Counting: {_countingPcps++} | Code: {code}", - ConsoleColor.DarkYellow - ); + private Bank ParseLineCql(string line) + { + if (!Patterns.CqlPattern.IsMatch(line)) + return null; - return new() - { - Document = match.Groups["cnpj"].Value.Trim(), - IspbString = match.Groups["ispb"].Value.Trim(), - LongName = match.Groups["nome"].Value.Replace("\"", "").Trim(), - SalaryPortability = match.Groups["adesao"].Value.Trim() - }; - } + var match = Patterns.CqlPattern.Match(line); + var code = Convert.ToInt32(match.Groups["code"].Value.Trim()); + + _countingCql++; + if (_countingCql != code) + Logger.Log($"CQL | Counting: {_countingCql++} | Code: {code}", ConsoleColor.DarkYellow); + return new() + { + IspbString = match.Groups["ispb"].Value.Trim(), + LongName = match.Groups["nome"].Value.Replace("\"", "").Trim(), + Type = match.Groups["tipo"].Value.Trim(), + LegalCheque = true + }; } } diff --git a/BancosBrasileiros.MergeTool/Helpers/Seeder.cs b/BancosBrasileiros.MergeTool/Helpers/Seeder.cs index 487879e5..b13f1ffd 100644 --- a/BancosBrasileiros.MergeTool/Helpers/Seeder.cs +++ b/BancosBrasileiros.MergeTool/Helpers/Seeder.cs @@ -12,316 +12,395 @@ // // *********************************************************************** -namespace BancosBrasileiros.MergeTool.Helpers +namespace BancosBrasileiros.MergeTool.Helpers; + +using System; +using System.Collections.Generic; +using System.Linq; +using CrispyWaffle.Extensions; +using Dto; + +/// +/// Class Seeder. +/// +internal class Seeder { - using System; - using CrispyWaffle.Extensions; - using Dto; - using System.Collections.Generic; - using System.Linq; + /// + /// The source + /// + private readonly IList _source; /// - /// Class Seeder. + /// Initializes a new instance of the class. /// - internal class Seeder + /// The source. + /// source + public Seeder(IList source) => + _source = source ?? throw new ArgumentNullException(nameof(source)); + + /// + /// Generates the missing document. + /// + /// Seeder. + public Seeder GenerateMissingDocument() { - /// - /// The source - /// - private readonly IList _source; - - /// - /// Initializes a new instance of the class. - /// - /// The source. - /// source - public Seeder(IList source) => - _source = source ?? throw new ArgumentNullException(nameof(source)); - - /// - /// Generates the missing document. - /// - /// Seeder. - public Seeder GenerateMissingDocument() - { - var existing = 0; - var missing = 0; + var existing = 0; + var missing = 0; - foreach (var bank in _source) + foreach (var bank in _source) + { + if (bank.Document is { Length: 18 }) { - if (bank.Document is { Length: 18 }) - { - existing++; - continue; - } - - bank.Document = bank.IspbString; - bank.DateUpdated = DateTimeOffset.UtcNow; - missing++; + existing++; + continue; } - Logger.Log( - $"\r\nGenerate document | Existing: {existing} | Missing: {missing}\r\n", - ConsoleColor.DarkYellow - ); - return this; + bank.Document = bank.IspbString; + bank.DateUpdated = DateTimeOffset.UtcNow; + missing++; } - /// - /// Seeds the string. - /// - /// The items. - /// Seeder. - public Seeder SeedStr(IEnumerable items) - { - var updated = 0; - var nameFixed = 0; + Logger.Log( + $"\r\nGenerate document | Existing: {existing} | Missing: {missing}\r\n", + ConsoleColor.DarkYellow + ); + return this; + } - Logger.Log("STR\r\n", ConsoleColor.DarkYellow); + /// + /// Seeds the string. + /// + /// The items. + /// Seeder. + public Seeder SeedStr(IEnumerable items) + { + var updated = 0; + var nameFixed = 0; - foreach (var str in items) - { - var bank = _source.SingleOrDefault(b => b.Compe == str.Compe); + Logger.Log("STR\r\n", ConsoleColor.DarkYellow); - if (bank == null) - { - Logger.Log( - $"Adding bank by STR List | {str.Compe} | {str.LongName}", - ConsoleColor.DarkGreen - ); + foreach (var str in items) + { + var bank = _source.SingleOrDefault(b => b.Compe == str.Compe); - if (str.Document is not { Length: 18 }) - str.Document = str.IspbString; + if (bank == null) + { + Logger.Log( + $"Adding bank by STR List | {str.Compe} | {str.LongName}", + ConsoleColor.DarkGreen + ); - _source.Add(str); - bank = str; - } + if (str.Document is not { Length: 18 }) + str.Document = str.IspbString; - if ( - bank.LongName - .RemoveDiacritics() - .Equals( - str.LongName.RemoveDiacritics(), - StringComparison.InvariantCultureIgnoreCase - ) - && bank.ShortName - .RemoveDiacritics() - .Equals( - str.ShortName.RemoveDiacritics(), - StringComparison.InvariantCultureIgnoreCase - ) - ) - { - Logger.Log($"STR | Bank updated: {str.LongName}", ConsoleColor.DarkGreen); - updated++; - continue; - } + _source.Add(str); + bank = str; + } - Logger.Log( - $"STR | Bank with name different: {bank.LongName} <-> {str.LongName} | {bank.ShortName} <-> {str.ShortName}", - ConsoleColor.DarkYellow - ); - bank.LongName = str.LongName; - bank.ShortName = str.ShortName; - bank.DateUpdated = DateTimeOffset.UtcNow; - nameFixed++; + if ( + bank.LongName + .RemoveDiacritics() + .Equals( + str.LongName.RemoveDiacritics(), + StringComparison.InvariantCultureIgnoreCase + ) + && bank.ShortName + .RemoveDiacritics() + .Equals( + str.ShortName.RemoveDiacritics(), + StringComparison.InvariantCultureIgnoreCase + ) + ) + { + Logger.Log($"STR | Bank updated: {str.LongName}", ConsoleColor.DarkGreen); + updated++; + continue; } Logger.Log( - $"\r\nSTR | Updated: {updated} | Fixed: {nameFixed}\r\n", + $"STR | Bank with name different: {bank.LongName} <-> {str.LongName} | {bank.ShortName} <-> {str.ShortName}", ConsoleColor.DarkYellow ); - return this; + bank.LongName = str.LongName; + bank.ShortName = str.ShortName; + bank.DateUpdated = DateTimeOffset.UtcNow; + nameFixed++; } - /// - /// Seeds the sitraf. - /// - /// The items. - /// Seeder. - public Seeder SeedSitraf(IEnumerable items) - { - var updated = 0; - var nameDifferent = 0; - - Logger.Log("SITRAF\r\n", ConsoleColor.DarkYellow); + Logger.Log( + $"\r\nSTR | Updated: {updated} | Fixed: {nameFixed}\r\n", + ConsoleColor.DarkYellow + ); + return this; + } - foreach (var sitraf in items) - { - var bank = _source.SingleOrDefault(b => b.Compe == sitraf.Compe); + /// + /// Seeds the sitraf. + /// + /// The items. + /// Seeder. + public Seeder SeedSitraf(IEnumerable items) + { + var updated = 0; + var nameDifferent = 0; - if (bank == null) - { - Logger.Log( - $"Adding bank by SITRAF List | {sitraf.Compe} | {sitraf.LongName}", - ConsoleColor.DarkGreen - ); + Logger.Log("SITRAF\r\n", ConsoleColor.DarkYellow); - if (sitraf.Document is not { Length: 18 }) - sitraf.Document = sitraf.IspbString; + foreach (var sitraf in items) + { + var bank = _source.SingleOrDefault(b => b.Compe == sitraf.Compe); - _source.Add(sitraf); - bank = sitraf; - } + if (bank == null) + { + Logger.Log( + $"Adding bank by SITRAF List | {sitraf.Compe} | {sitraf.LongName}", + ConsoleColor.DarkGreen + ); - if ( - !bank.LongName - .RemoveDiacritics() - .Equals( - sitraf.LongName.RemoveDiacritics(), - StringComparison.InvariantCultureIgnoreCase - ) - ) - { - nameDifferent++; - continue; - } + if (sitraf.Document is not { Length: 18 }) + sitraf.Document = sitraf.IspbString; - Logger.Log($"SITRAF | Bank updated: {sitraf.LongName}", ConsoleColor.DarkGreen); - updated++; + _source.Add(sitraf); + bank = sitraf; } - Logger.Log( - $"\r\nSITRAF | Updated: {updated} | Name different: {nameDifferent}\r\n", - ConsoleColor.DarkYellow - ); + if ( + !bank.LongName + .RemoveDiacritics() + .Equals( + sitraf.LongName.RemoveDiacritics(), + StringComparison.InvariantCultureIgnoreCase + ) + ) + { + nameDifferent++; + continue; + } - return this; + Logger.Log($"SITRAF | Bank updated: {sitraf.LongName}", ConsoleColor.DarkGreen); + updated++; } - /// - /// Seeds the SLC. - /// - /// The items. - /// Seeder. - public Seeder SeedSlc(IEnumerable items) - { - var found = 0; - var notFound = 0; + Logger.Log( + $"\r\nSITRAF | Updated: {updated} | Name different: {nameDifferent}\r\n", + ConsoleColor.DarkYellow + ); - Logger.Log("SLC\r\n", ConsoleColor.DarkYellow); + return this; + } - foreach (var slc in items) - { - var bank = _source.SingleOrDefault( - b => b.Document != null && b.Document.Equals(slc.Document) - ); + /// + /// Seeds the SLC. + /// + /// The items. + /// Seeder. + public Seeder SeedSlc(IEnumerable items) + { + var found = 0; + var notFound = 0; - if (bank == null) - { - bank = _source.SingleOrDefault( - b => - b.LongName + Logger.Log("SLC\r\n", ConsoleColor.DarkYellow); + + foreach (var slc in items) + { + var bank = _source.SingleOrDefault( + b => b.Document != null && b.Document.Equals(slc.Document) + ); + + if (bank == null) + { + bank = _source.SingleOrDefault( + b => + b.LongName + .RemoveDiacritics() + .Equals( + slc.LongName.RemoveDiacritics(), + StringComparison.InvariantCultureIgnoreCase + ) + || ( + b.ShortName != null + && b.ShortName .RemoveDiacritics() .Equals( slc.LongName.RemoveDiacritics(), StringComparison.InvariantCultureIgnoreCase ) - || ( - b.ShortName != null - && b.ShortName - .RemoveDiacritics() - .Equals( - slc.LongName.RemoveDiacritics(), - StringComparison.InvariantCultureIgnoreCase - ) - ) - ); - } - - if (bank == null) - { - var ispb = int.Parse(slc.Document.RemoveNonNumeric()[..8]); - - if ( - ispb == 0 - && !slc.LongName.Equals( - "Banco do Brasil", - StringComparison.InvariantCultureIgnoreCase ) - ) - { - Logger.Log( - $"SLC | ISPB nulled: {slc.LongName} | {slc.Document.Trim()}", - ConsoleColor.DarkRed - ); - continue; - } - - bank = _source.SingleOrDefault( - b => - b.Ispb.Equals(ispb) - && b.LongName.Contains( - slc.LongName, - StringComparison.InvariantCultureIgnoreCase - ) - ); - } - - if (bank == null) - { - Logger.Log( - $"SLC | Bank not found: {slc.LongName} | {slc.Document.Trim()}", - ConsoleColor.DarkRed - ); + ); + } - notFound++; - continue; - } + if (bank == null) + { + var ispb = int.Parse(slc.Document.RemoveNonNumeric()[..8]); if ( - (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) - && !string.IsNullOrWhiteSpace(slc.Document) + ispb == 0 + && !slc.LongName.Equals( + "Banco do Brasil", + StringComparison.InvariantCultureIgnoreCase + ) ) - { - bank.Document = slc.Document; - bank.DateUpdated = DateTimeOffset.UtcNow; - } - else if (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) { Logger.Log( - $"SLC | Invalid document {slc.Compe} | {bank.Document} | {slc.Document}", + $"SLC | ISPB nulled: {slc.LongName} | {slc.Document.Trim()}", ConsoleColor.DarkRed ); + continue; } - if (string.IsNullOrWhiteSpace(bank.ShortName)) - { - bank.ShortName = slc.LongName; - bank.DateUpdated = DateTimeOffset.UtcNow; - } + bank = _source.SingleOrDefault( + b => + b.Ispb.Equals(ispb) + && b.LongName.Contains( + slc.LongName, + StringComparison.InvariantCultureIgnoreCase + ) + ); + } - found++; + if (bank == null) + { + Logger.Log( + $"SLC | Bank not found: {slc.LongName} | {slc.Document.Trim()}", + ConsoleColor.DarkRed + ); + + notFound++; + continue; } - Logger.Log( - $"\r\nSLC | Found: {found} | Not found: {notFound}\r\n", - ConsoleColor.DarkYellow - ); - return this; + if ( + (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) + && !string.IsNullOrWhiteSpace(slc.Document) + ) + { + bank.Document = slc.Document; + bank.DateUpdated = DateTimeOffset.UtcNow; + } + else if (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) + { + Logger.Log( + $"SLC | Invalid document {slc.Compe} | {bank.Document} | {slc.Document}", + ConsoleColor.DarkRed + ); + } + + if (string.IsNullOrWhiteSpace(bank.ShortName)) + { + bank.ShortName = slc.LongName; + bank.DateUpdated = DateTimeOffset.UtcNow; + } + + found++; } - /// - /// Seeds the SPI. - /// - /// The items. - /// Seeder. - public Seeder SeedSpi(IEnumerable items) + Logger.Log( + $"\r\nSLC | Found: {found} | Not found: {notFound}\r\n", + ConsoleColor.DarkYellow + ); + return this; + } + + /// + /// Seeds the SPI. + /// + /// The items. + /// Seeder. + public Seeder SeedSpi(IEnumerable items) + { + var found = 0; + var upToDate = 0; + var notFound = 0; + + Logger.Log("SPI\r\n", ConsoleColor.DarkYellow); + + foreach (var spi in items) { - var found = 0; - var upToDate = 0; - var notFound = 0; + var bank = _source.SingleOrDefault( + b => + b.LongName + .RemoveDiacritics() + .Equals( + spi.LongName.RemoveDiacritics(), + StringComparison.InvariantCultureIgnoreCase + ) + || ( + b.ShortName != null + && b.ShortName + .RemoveDiacritics() + .Equals( + spi.LongName.RemoveDiacritics(), + StringComparison.InvariantCultureIgnoreCase + ) + ) + ); - Logger.Log("SPI\r\n", ConsoleColor.DarkYellow); + if (bank == null) + bank = _source.SingleOrDefault(b => b.Ispb.Equals(spi.Ispb)); - foreach (var spi in items) + if (bank == null) { - var bank = _source.SingleOrDefault( + Logger.Log($"SPI | PSP not found: {spi.LongName}", ConsoleColor.DarkRed); + + notFound++; + continue; + } + + if ( + bank.PixType != null + && bank.PixType.Equals(spi.PixType) + && bank.DatePixStarted != null + && bank.DatePixStarted.Equals(spi.DatePixStarted) + ) + { + Logger.Log($"SPI | PSP updated: {spi.LongName}", ConsoleColor.DarkGreen); + + upToDate++; + continue; + } + + bank.PixType = spi.PixType; + bank.DatePixStarted = spi.DatePixStarted; + bank.DateUpdated = DateTimeOffset.UtcNow; + + found++; + } + + Logger.Log( + $"\r\nSPI | Found: {found} | Not found: {notFound} | Up to Date: {upToDate}\r\n", + ConsoleColor.DarkYellow + ); + + return this; + } + + /// + /// Seeds the CTC. + /// + /// The items. + /// Seeder. + public Seeder SeedCtc(IEnumerable items) + { + var found = 0; + var upToDate = 0; + var notFound = 0; + + Logger.Log("CTC\r\n", ConsoleColor.DarkYellow); + + foreach (var ctc in items) + { + var bank = _source.SingleOrDefault( + b => b.Document != null && b.Document.Equals(ctc.Document) + ); + + if (bank == null) + { + bank = _source.SingleOrDefault( b => b.LongName .RemoveDiacritics() .Equals( - spi.LongName.RemoveDiacritics(), + ctc.LongName.RemoveDiacritics(), StringComparison.InvariantCultureIgnoreCase ) || ( @@ -329,381 +408,344 @@ public Seeder SeedSpi(IEnumerable items) && b.ShortName .RemoveDiacritics() .Equals( - spi.LongName.RemoveDiacritics(), + ctc.LongName.RemoveDiacritics(), StringComparison.InvariantCultureIgnoreCase ) ) ); + } - if (bank == null) - bank = _source.SingleOrDefault(b => b.Ispb.Equals(spi.Ispb)); - - if (bank == null) - { - Logger.Log($"SPI | PSP not found: {spi.LongName}", ConsoleColor.DarkRed); - - notFound++; - continue; - } + if (bank == null) + { + var ispb = int.Parse(ctc.Document.RemoveNonNumeric()[..8]); if ( - bank.PixType != null - && bank.PixType.Equals(spi.PixType) - && bank.DatePixStarted != null - && bank.DatePixStarted.Equals(spi.DatePixStarted) + ispb == 0 + && !ctc.LongName.Equals( + "Banco do Brasil", + StringComparison.InvariantCultureIgnoreCase + ) ) { - Logger.Log($"SPI | PSP updated: {spi.LongName}", ConsoleColor.DarkGreen); - - upToDate++; + Logger.Log( + $"CTC | ISPB nulled: {ctc.LongName} | {ctc.Document.Trim()}", + ConsoleColor.DarkRed + ); continue; } - bank.PixType = spi.PixType; - bank.DatePixStarted = spi.DatePixStarted; + bank = _source.SingleOrDefault( + b => + b.Ispb.Equals(ispb) + && b.LongName.Contains( + ctc.LongName, + StringComparison.InvariantCultureIgnoreCase + ) + ); + } + + if (bank == null) + { + Logger.Log( + $"CTC | Bank not found: {ctc.LongName} | {ctc.Document.Trim()}", + ConsoleColor.DarkRed + ); + notFound++; + continue; + } + + if ( + (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) + && !string.IsNullOrWhiteSpace(ctc.Document) + ) + { + bank.Document = ctc.Document; bank.DateUpdated = DateTimeOffset.UtcNow; + } + else if (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) + { + Logger.Log( + $"CTC | Invalid document {ctc.Compe} | {bank.Document} | {ctc.Document}", + ConsoleColor.DarkRed + ); + } - found++; + if (bank.Products != null && !bank.Products.Except(ctc.Products).Any()) + { + Logger.Log($"CTC | Products updated: {ctc.LongName}", ConsoleColor.DarkGreen); + upToDate++; + continue; } - Logger.Log( - $"\r\nSPI | Found: {found} | Not found: {notFound} | Up to Date: {upToDate}\r\n", - ConsoleColor.DarkYellow - ); + bank.Products = ctc.Products; + bank.DateUpdated = DateTimeOffset.UtcNow; - return this; + found++; } - /// - /// Seeds the CTC. - /// - /// The items. - /// Seeder. - public Seeder SeedCtc(IEnumerable items) - { - var found = 0; - var upToDate = 0; - var notFound = 0; + Logger.Log( + $"\r\nCTC | Found: {found} | Not found: {notFound} | Up to date: {upToDate}\r\n", + ConsoleColor.DarkYellow + ); + return this; + } - Logger.Log("CTC\r\n", ConsoleColor.DarkYellow); + /// + /// Seeds the siloc. + /// + /// The items. + /// Seeder. + public Seeder SeedSiloc(IEnumerable items) + { + var found = 0; + var upToDate = 0; + var notFound = 0; - foreach (var ctc in items) - { - var bank = _source.SingleOrDefault( - b => b.Document != null && b.Document.Equals(ctc.Document) - ); + Logger.Log("SILOC\r\n", ConsoleColor.DarkYellow); - if (bank == null) - { - bank = _source.SingleOrDefault( - b => - b.LongName + foreach (var siloc in items) + { + var bank = _source.SingleOrDefault( + b => b.IspbString != null && b.IspbString.Equals(siloc.IspbString) + ); + + if (bank == null) + { + bank = _source.SingleOrDefault( + b => + b.LongName + .RemoveDiacritics() + .Equals( + siloc.LongName.RemoveDiacritics(), + StringComparison.InvariantCultureIgnoreCase + ) + || ( + b.ShortName != null + && b.ShortName .RemoveDiacritics() .Equals( - ctc.LongName.RemoveDiacritics(), + siloc.LongName.RemoveDiacritics(), StringComparison.InvariantCultureIgnoreCase ) - || ( - b.ShortName != null - && b.ShortName - .RemoveDiacritics() - .Equals( - ctc.LongName.RemoveDiacritics(), - StringComparison.InvariantCultureIgnoreCase - ) - ) - ); - } - - if (bank == null) - { - var ispb = int.Parse(ctc.Document.RemoveNonNumeric()[..8]); - - if ( - ispb == 0 - && !ctc.LongName.Equals( - "Banco do Brasil", - StringComparison.InvariantCultureIgnoreCase ) - ) - { - Logger.Log( - $"CTC | ISPB nulled: {ctc.LongName} | {ctc.Document.Trim()}", - ConsoleColor.DarkRed - ); - continue; - } - - bank = _source.SingleOrDefault( - b => - b.Ispb.Equals(ispb) - && b.LongName.Contains( - ctc.LongName, - StringComparison.InvariantCultureIgnoreCase - ) - ); - } - - if (bank == null) - { - Logger.Log( - $"CTC | Bank not found: {ctc.LongName} | {ctc.Document.Trim()}", - ConsoleColor.DarkRed - ); - notFound++; - continue; - } - - if ( - (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) - && !string.IsNullOrWhiteSpace(ctc.Document) - ) - { - bank.Document = ctc.Document; - bank.DateUpdated = DateTimeOffset.UtcNow; - } - else if (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) - { - Logger.Log( - $"CTC | Invalid document {ctc.Compe} | {bank.Document} | {ctc.Document}", - ConsoleColor.DarkRed - ); - } + ); + } - if (bank.Products != null && !bank.Products.Except(ctc.Products).Any()) - { - Logger.Log($"CTC | Products updated: {ctc.LongName}", ConsoleColor.DarkGreen); - upToDate++; - continue; - } + if (bank == null) + { + Logger.Log( + $"SILOC | Bank not found: {siloc.LongName} | {siloc.Document.Trim()}", + ConsoleColor.DarkRed + ); + notFound++; + continue; + } - bank.Products = ctc.Products; + if ( + (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) + && !string.IsNullOrWhiteSpace(siloc.Document) + ) + { + bank.Document = siloc.Document; bank.DateUpdated = DateTimeOffset.UtcNow; + } + else if (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) + { + Logger.Log( + $"SILOC | Invalid document {siloc.Compe} | {bank.Document} | {siloc.Document}", + ConsoleColor.DarkRed + ); + } - found++; + if ( + bank.Charge != null + && bank.Charge.Equals(siloc.Charge) + && bank.CreditDocument != null + && bank.CreditDocument.Equals(siloc.CreditDocument) + ) + { + Logger.Log($"SILOC | COB/DOC updated: {siloc.LongName}", ConsoleColor.DarkGreen); + upToDate++; + continue; } - Logger.Log( - $"\r\nCTC | Found: {found} | Not found: {notFound} | Up to date: {upToDate}\r\n", - ConsoleColor.DarkYellow - ); - return this; + bank.Charge = siloc.Charge; + bank.CreditDocument = siloc.CreditDocument; + bank.DateUpdated = DateTimeOffset.UtcNow; + + found++; } - /// - /// Seeds the siloc. - /// - /// The items. - /// Seeder. - public Seeder SeedSiloc(IEnumerable items) - { - var found = 0; - var upToDate = 0; - var notFound = 0; + Logger.Log( + $"\r\nSILOC | Found: {found} | Not found: {notFound} | Up to date: {upToDate}\r\n", + ConsoleColor.DarkYellow + ); + return this; + } - Logger.Log("SILOC\r\n", ConsoleColor.DarkYellow); + /// + /// Seeds the PCPS. + /// + /// The items. + /// Seeder. + public Seeder SeedPcps(IEnumerable items) + { + var found = 0; + var upToDate = 0; + var notFound = 0; - foreach (var siloc in items) - { - var bank = _source.SingleOrDefault( - b => b.IspbString != null && b.IspbString.Equals(siloc.IspbString) - ); + Logger.Log("PCPS\r\n", ConsoleColor.DarkYellow); - if (bank == null) - { - bank = _source.SingleOrDefault( - b => - b.LongName + foreach (var pcps in items) + { + var bank = _source.SingleOrDefault( + b => b.Document != null && b.Document.Equals(pcps.Document) + ); + + if (bank == null) + { + bank = _source.SingleOrDefault( + b => + b.LongName + .RemoveDiacritics() + .Equals( + pcps.LongName.RemoveDiacritics(), + StringComparison.InvariantCultureIgnoreCase + ) + || ( + b.ShortName != null + && b.ShortName .RemoveDiacritics() .Equals( - siloc.LongName.RemoveDiacritics(), + pcps.LongName.RemoveDiacritics(), StringComparison.InvariantCultureIgnoreCase ) - || ( - b.ShortName != null - && b.ShortName - .RemoveDiacritics() - .Equals( - siloc.LongName.RemoveDiacritics(), - StringComparison.InvariantCultureIgnoreCase - ) - ) - ); - } + ) + ); + } - if (bank == null) - { - Logger.Log( - $"SILOC | Bank not found: {siloc.LongName} | {siloc.Document.Trim()}", - ConsoleColor.DarkRed - ); - notFound++; - continue; - } + if (bank == null) + { + var ispb = int.Parse(pcps.Document.RemoveNonNumeric()[..8]); if ( - (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) - && !string.IsNullOrWhiteSpace(siloc.Document) + ispb == 0 + && !pcps.LongName.Equals( + "Banco do Brasil", + StringComparison.InvariantCultureIgnoreCase + ) ) - { - bank.Document = siloc.Document; - bank.DateUpdated = DateTimeOffset.UtcNow; - } - else if (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) { Logger.Log( - $"SILOC | Invalid document {siloc.Compe} | {bank.Document} | {siloc.Document}", + $"PCPS | ISPB nulled: {pcps.LongName} | {pcps.Document.Trim()}", ConsoleColor.DarkRed ); - } - - if ( - bank.Charge != null - && bank.Charge.Equals(siloc.Charge) - && bank.CreditDocument != null - && bank.CreditDocument.Equals(siloc.CreditDocument) - ) - { - Logger.Log( - $"SILOC | COB/DOC updated: {siloc.LongName}", - ConsoleColor.DarkGreen - ); - upToDate++; continue; } - bank.Charge = siloc.Charge; - bank.CreditDocument = siloc.CreditDocument; - bank.DateUpdated = DateTimeOffset.UtcNow; - - found++; + bank = _source.SingleOrDefault( + b => + b.Ispb.Equals(ispb) + && b.LongName.Contains( + pcps.LongName, + StringComparison.InvariantCultureIgnoreCase + ) + ); } - Logger.Log( - $"\r\nSILOC | Found: {found} | Not found: {notFound} | Up to date: {upToDate}\r\n", - ConsoleColor.DarkYellow - ); - return this; - } - - /// - /// Seeds the PCPS. - /// - /// The items. - /// Seeder. - public void SeedPcps(IEnumerable items) - { - var found = 0; - var upToDate = 0; - var notFound = 0; + if (bank == null) + { + Logger.Log( + $"PCPS | Bank not found: {pcps.LongName} | {pcps.Document.Trim()}", + ConsoleColor.DarkRed + ); + notFound++; + continue; + } - Logger.Log("PCPS\r\n", ConsoleColor.DarkYellow); + if ( + (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) + && !string.IsNullOrWhiteSpace(pcps.Document) + ) + { + bank.Document = pcps.Document; + bank.DateUpdated = DateTimeOffset.UtcNow; + } + else if (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) + { + Logger.Log( + $"SILOC | Invalid document {pcps.Compe} | {bank.Document} | {pcps.Document}", + ConsoleColor.DarkRed + ); + } - foreach (var pcps in items) + if ( + bank.SalaryPortability != null + && bank.SalaryPortability.Equals(pcps.SalaryPortability) + ) { - var bank = _source.SingleOrDefault( - b => b.Document != null && b.Document.Equals(pcps.Document) + Logger.Log( + $"PCPS | Salary portability updated: {pcps.LongName}", + ConsoleColor.DarkGreen ); + upToDate++; + continue; + } - if (bank == null) - { - bank = _source.SingleOrDefault( - b => - b.LongName - .RemoveDiacritics() - .Equals( - pcps.LongName.RemoveDiacritics(), - StringComparison.InvariantCultureIgnoreCase - ) - || ( - b.ShortName != null - && b.ShortName - .RemoveDiacritics() - .Equals( - pcps.LongName.RemoveDiacritics(), - StringComparison.InvariantCultureIgnoreCase - ) - ) - ); - } + bank.SalaryPortability = pcps.SalaryPortability; + bank.DateUpdated = DateTimeOffset.UtcNow; + found++; + } - if (bank == null) - { - var ispb = int.Parse(pcps.Document.RemoveNonNumeric()[..8]); + Logger.Log( + $"\r\nPCPS | Found: {found} | Not found: {notFound} | Up to date: {upToDate}\r\n", + ConsoleColor.DarkYellow + ); - if ( - ispb == 0 - && !pcps.LongName.Equals( - "Banco do Brasil", - StringComparison.InvariantCultureIgnoreCase - ) - ) - { - Logger.Log( - $"PCPS | ISPB nulled: {pcps.LongName} | {pcps.Document.Trim()}", - ConsoleColor.DarkRed - ); - continue; - } - - bank = _source.SingleOrDefault( - b => - b.Ispb.Equals(ispb) - && b.LongName.Contains( - pcps.LongName, - StringComparison.InvariantCultureIgnoreCase - ) - ); - } + return this; + } - if (bank == null) - { - Logger.Log( - $"PCPS | Bank not found: {pcps.LongName} | {pcps.Document.Trim()}", - ConsoleColor.DarkRed - ); - notFound++; - continue; - } + /// + /// Seeds the CQL. + /// + /// The items. + public void SeedCql(IEnumerable items) + { + var found = 0; + var upToDate = 0; + var notFound = 0; - if ( - (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) - && !string.IsNullOrWhiteSpace(pcps.Document) - ) - { - bank.Document = pcps.Document; - bank.DateUpdated = DateTimeOffset.UtcNow; - } - else if (string.IsNullOrWhiteSpace(bank.Document) || bank.Document.Length != 18) - { - Logger.Log( - $"SILOC | Invalid document {pcps.Compe} | {bank.Document} | {pcps.Document}", - ConsoleColor.DarkRed - ); - } + Logger.Log("CQL\r\n", ConsoleColor.DarkYellow); - if ( - bank.SalaryPortability != null - && bank.SalaryPortability.Equals(pcps.SalaryPortability) - ) - { - Logger.Log( - $"PCPS | Salary portability updated: {pcps.LongName}", - ConsoleColor.DarkGreen - ); - upToDate++; - continue; - } + foreach (var cql in items) + { + var bank = _source.SingleOrDefault(b => b.Ispb.Equals(cql.Ispb)); - bank.SalaryPortability = pcps.SalaryPortability; - bank.DateUpdated = DateTimeOffset.UtcNow; - found++; + if (bank == null) + { + Logger.Log( + $"CQL | Bank not found: {cql.LongName} | {cql.Document.Trim()}", + ConsoleColor.DarkRed + ); + notFound++; + continue; } - Logger.Log( - $"\r\nPCPS | Found: {found} | Not found: {notFound} | Up to date: {upToDate}\r\n", - ConsoleColor.DarkYellow - ); + if (bank.LegalCheque) + { + Logger.Log($"CQL | Legal Cheque updated: {cql.LongName}", ConsoleColor.DarkGreen); + upToDate++; + continue; + } + + bank.LegalCheque = true; + bank.DateUpdated = DateTimeOffset.UtcNow; + found++; } + + Logger.Log( + $"\r\nCQL | Found: {found} | Not found: {notFound} | Up to date: {upToDate}\r\n", + ConsoleColor.DarkYellow + ); } } diff --git a/BancosBrasileiros.MergeTool/Helpers/Source.cs b/BancosBrasileiros.MergeTool/Helpers/Source.cs index c83f5110..a0d6a0b2 100644 --- a/BancosBrasileiros.MergeTool/Helpers/Source.cs +++ b/BancosBrasileiros.MergeTool/Helpers/Source.cs @@ -11,51 +11,56 @@ // // // *********************************************************************** -namespace BancosBrasileiros.MergeTool.Helpers + +namespace BancosBrasileiros.MergeTool.Helpers; + +/// +/// Enum Source. +/// +internal enum Source { /// - /// Enum Source - /// - internal enum Source - { - /// - /// The base - /// - Base, - - /// - /// The change log - /// - ChangeLog, - - /// - /// The CTC - /// - Ctc, - - /// - /// The PCPS - /// - Pcps, - - /// - /// The siloc - /// - Siloc, - - /// - /// The sitraf - /// - Sitraf, - - /// - /// The SLC - /// - Slc, - - /// - /// The spi - /// - Spi - } + /// The base. + /// + Base, + + /// + /// The change log. + /// + ChangeLog, + + /// + /// The CTC. + /// + Ctc, + + /// + /// The PCPS. + /// + Pcps, + + /// + /// The siloc. + /// + Siloc, + + /// + /// The sitraf. + /// + Sitraf, + + /// + /// The SLC. + /// + Slc, + + /// + /// The SPI. + /// + Spi, + + /// + /// The CQL. + /// + Cql, } diff --git a/BancosBrasileiros.MergeTool/Helpers/Writer.cs b/BancosBrasileiros.MergeTool/Helpers/Writer.cs index ae3039c3..6edaeb34 100644 --- a/BancosBrasileiros.MergeTool/Helpers/Writer.cs +++ b/BancosBrasileiros.MergeTool/Helpers/Writer.cs @@ -12,137 +12,121 @@ // // *********************************************************************** -namespace BancosBrasileiros.MergeTool.Helpers +namespace BancosBrasileiros.MergeTool.Helpers; + +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using CrispyWaffle.Serialization; +using Dto; + +/// +/// Class Writer. +/// +internal static class Writer { - using CrispyWaffle.Serialization; - using Dto; - using System.Collections.Generic; - using System.IO; - using System.Linq; - using System.Text; - /// - /// Class Writer. + /// Writes the change log. /// - internal static class Writer + /// The change log. + public static void WriteChangeLog(string changeLog) { - /// - /// Writes the change log. - /// - /// The change log. - public static void WriteChangeLog(string changeLog) - { - if (!Directory.Exists("result")) - Directory.CreateDirectory("result"); + if (!Directory.Exists("result")) + Directory.CreateDirectory("result"); - var changeLogFile = Reader.LoadChangeLog(); + var changeLogFile = Reader.LoadChangeLog(); - changeLogFile = changeLogFile.Replace("## Changelog\r\n\r\n", "## Changelog\n\n"); - var result = changeLogFile.Replace( - "## Changelog\n\n", - $"## Changelog\n\n{changeLog}\n" - ); + changeLogFile = changeLogFile.Replace("## Changelog\r\n\r\n", "## Changelog\n\n"); + var result = changeLogFile.Replace("## Changelog\n\n", $"## Changelog\n\n{changeLog}\n"); - File.WriteAllText($"result{Path.DirectorySeparatorChar}CHANGELOG.md", result); - } + File.WriteAllText($"result{Path.DirectorySeparatorChar}CHANGELOG.md", result); + } - /// - /// Saves the specified banks. - /// - /// The banks. - public static void SaveBanks(IList banks) - { - if (!Directory.Exists("result")) - Directory.CreateDirectory("result"); - - banks = banks.OrderBy(b => b.Compe).ToList(); - - SaveCsv(banks); - banks - .GetCustomSerializer(SerializerFormat.Json) - .Save($"result{Path.DirectorySeparatorChar}bancos.json"); - SaveMarkdown(banks); - SaveSql(banks); - new Banks { Bank = banks.ToArray() } - .GetCustomSerializer(SerializerFormat.Xml) - .Save($"result{Path.DirectorySeparatorChar}bancos.xml"); - } - - /// - /// Saves the CSV. - /// - /// The banks. - private static void SaveCsv(IList banks) - { - var lines = new List - { - "COMPE,ISPB,Document,LongName,ShortName,Network,Type,PixType,Charge,CreditDocument,SalaryPortability,Products,Url,DateOperationStarted,DatePixStarted,DateRegistered,DateUpdated" - }; - - lines.AddRange( - banks.Select( - bank => - $"{bank.Compe:000},{bank.Ispb:00000000},{bank.Document},{bank.LongName.Replace(",", "")},{bank.ShortName.Replace(",", "")},{bank.Network},{bank.Type},{bank.PixType},{(string.IsNullOrWhiteSpace(bank.ChargeStr) ? "" : bank.Charge)},{(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) ? "" : bank.CreditDocument)},{bank.SalaryPortability}, {(bank.Products == null ? "NULL" : string.Join("|", bank.Products))},{bank.Url},{bank.DateOperationStarted},{bank.DatePixStarted},{bank.DateRegistered:O},{bank.DateUpdated:O}" - ) - ); - - File.WriteAllLines( - $"result{Path.DirectorySeparatorChar}bancos.csv", - lines, - Encoding.UTF8 - ); - } - - /// - /// Saves the markdown. - /// - /// The banks. - private static void SaveMarkdown(IList banks) + /// + /// Saves the specified banks. + /// + /// The banks. + public static void SaveBanks(IList banks) + { + if (!Directory.Exists("result")) + Directory.CreateDirectory("result"); + + banks = banks.OrderBy(b => b.Compe).ToList(); + + SaveCsv(banks); + banks + .GetCustomSerializer(SerializerFormat.Json) + .Save($"result{Path.DirectorySeparatorChar}bancos.json"); + SaveMarkdown(banks); + SaveSql(banks); + new Banks { Bank = banks.ToArray() } + .GetCustomSerializer(SerializerFormat.Xml) + .Save($"result{Path.DirectorySeparatorChar}bancos.xml"); + } + + /// + /// Saves the CSV. + /// + /// The banks. + private static void SaveCsv(IList banks) + { + var lines = new List { - var lines = new List - { - "# Bancos Brasileiros", - string.Empty, - "COMPE | ISPB | Document | Long Name | Short Name | Network | Type | PIX Type | Charge | Credit Document | Salary Portability | Products | Url | Date Operation Started | Date PIX Started | Date Registered | Date Updated", - "--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- " - }; - - lines.AddRange( - banks.Select( - bank => - $"{bank.Compe:000} | {bank.Ispb:00000000} | {bank.Document} | {bank.LongName} | {bank.ShortName} | {(string.IsNullOrWhiteSpace(bank.Network) ? "-" : bank.Network)} | {(string.IsNullOrWhiteSpace(bank.Type) ? "-" : bank.Type)} | {(string.IsNullOrWhiteSpace(bank.PixType) ? "-" : bank.PixType)} | {(string.IsNullOrWhiteSpace(bank.ChargeStr) ? "-" : bank.Charge)} | {(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) ? "-" : bank.CreditDocument)} | {(string.IsNullOrWhiteSpace(bank.SalaryPortability) ? "-" : bank.SalaryPortability)} | {(bank.Products == null ? "-" : string.Join(",", bank.Products))} | {(string.IsNullOrWhiteSpace(bank.Url) ? "-" : bank.Url)} | {(string.IsNullOrWhiteSpace(bank.DateOperationStarted) ? "-" : bank.DateOperationStarted)} | {(string.IsNullOrWhiteSpace(bank.DatePixStarted) ? "-" : bank.DatePixStarted)} | {bank.DateRegistered:O} | {bank.DateUpdated:O}" - ) - ); - - File.WriteAllLines( - $"result{Path.DirectorySeparatorChar}bancos.md", - lines, - Encoding.UTF8 - ); - } - - /// - /// Saves the SQL. - /// - /// The banks. - private static void SaveSql(IList banks) + "COMPE,ISPB,Document,LongName,ShortName,Network,Type,PixType,Charge,CreditDocument,LegalCheque,SalaryPortability,Products,Url,DateOperationStarted,DatePixStarted,DateRegistered,DateUpdated" + }; + + lines.AddRange( + banks.Select( + bank => + $"{bank.Compe:000},{bank.Ispb:00000000},{bank.Document},{bank.LongName.Replace(",", "")},{bank.ShortName.Replace(",", "")},{bank.Network},{bank.Type},{bank.PixType},{(string.IsNullOrWhiteSpace(bank.ChargeStr) ? "" : bank.ChargeStr)},{(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) ? "" : bank.CreditDocumentStr)},{(bank.LegalCheque ? "sim" : "não")},{bank.SalaryPortability},{(bank.Products == null ? "NULL" : string.Join("|", bank.Products))},{bank.Url},{bank.DateOperationStarted},{bank.DatePixStarted},{bank.DateRegistered:O},{bank.DateUpdated:O}" + ) + ); + + File.WriteAllLines($"result{Path.DirectorySeparatorChar}bancos.csv", lines, Encoding.UTF8); + } + + /// + /// Saves the markdown. + /// + /// The banks. + private static void SaveMarkdown(IList banks) + { + var lines = new List { - var lines = new List(); - - const string prefix = - "INSERT INTO Banks (Compe, Ispb, Document, LongName, ShortName, Network, Type, PixType, Charge, CreditDocument, SalaryPortability, Products, Url, DateOperationStarted, DatePixStarted, DateRegistered, DateUpdated) VALUES("; - lines.AddRange( - banks.Select( - bank => - $"{prefix}'{bank.Compe:000}','{bank.Ispb:00000000}','{bank.Document}','{bank.LongName.Replace("'", "\'")}','{bank.ShortName}',{(string.IsNullOrWhiteSpace(bank.Type) ? "NULL" : $"'{bank.Type}'")},{(string.IsNullOrWhiteSpace(bank.PixType) ? "NULL" : $"'{bank.PixType}'")},{(string.IsNullOrWhiteSpace(bank.Network) ? "NULL" : $"'{bank.Network}'")},{(string.IsNullOrWhiteSpace(bank.ChargeStr) ? "NULL" : $"'{bank.Charge}'")},{(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) ? "NULL" : $"'{bank.CreditDocument}'")},{(string.IsNullOrWhiteSpace(bank.SalaryPortability) ? "NULL" : $"'{bank.SalaryPortability}'")},{(bank.Products == null ? "NULL" : $"'{string.Join(",", bank.Products)}'")},{(string.IsNullOrWhiteSpace(bank.Url) ? "NULL" : $"'{bank.Url}'")},{(string.IsNullOrWhiteSpace(bank.DateOperationStarted) ? "NULL" : $"'{bank.DateOperationStarted}'")},{(string.IsNullOrWhiteSpace(bank.DatePixStarted) ? "NULL" : $"'{bank.DatePixStarted}'")},'{bank.DateRegistered:O}','{bank.DateUpdated:O}');" - ) - ); - - File.WriteAllLines( - $"result{Path.DirectorySeparatorChar}bancos.sql", - lines, - Encoding.UTF8 - ); - } + "# Bancos Brasileiros", + string.Empty, + "COMPE | ISPB | Document | Long Name | Short Name | Network | Type | PIX Type | Charge | Credit Document | Legal Cheque | Salary Portability | Products | Url | Date Operation Started | Date PIX Started | Date Registered | Date Updated", + "--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- " + }; + + lines.AddRange( + banks.Select( + bank => + $"{bank.Compe:000} | {bank.Ispb:00000000} | {bank.Document} | {bank.LongName} | {bank.ShortName} | {(string.IsNullOrWhiteSpace(bank.Network) ? "-" : bank.Network)} | {(string.IsNullOrWhiteSpace(bank.Type) ? "-" : bank.Type)} | {(string.IsNullOrWhiteSpace(bank.PixType) ? "-" : bank.PixType)} | {(string.IsNullOrWhiteSpace(bank.ChargeStr) ? "-" : bank.Charge)} | {(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) ? "-" : bank.CreditDocument)} | {(bank.LegalCheque ? "sim" : "não")} | {(string.IsNullOrWhiteSpace(bank.SalaryPortability) ? "-" : bank.SalaryPortability)} | {(bank.Products == null ? "-" : string.Join(",", bank.Products))} | {(string.IsNullOrWhiteSpace(bank.Url) ? "-" : bank.Url)} | {(string.IsNullOrWhiteSpace(bank.DateOperationStarted) ? "-" : bank.DateOperationStarted)} | {(string.IsNullOrWhiteSpace(bank.DatePixStarted) ? "-" : bank.DatePixStarted)} | {bank.DateRegistered:O} | {bank.DateUpdated:O}" + ) + ); + + File.WriteAllLines($"result{Path.DirectorySeparatorChar}bancos.md", lines, Encoding.UTF8); + } + + /// + /// Saves the SQL. + /// + /// The banks. + private static void SaveSql(IList banks) + { + var lines = new List(); + + const string prefix = + "INSERT INTO Banks (Compe, Ispb, Document, LongName, ShortName, Network, Type, PixType, Charge, CreditDocument, LegalCheque, SalaryPortability, Products, Url, DateOperationStarted, DatePixStarted, DateRegistered, DateUpdated) VALUES("; + lines.AddRange( + banks.Select( + bank => + $"{prefix}'{bank.Compe:000}','{bank.Ispb:00000000}','{bank.Document}','{bank.LongName.Replace("'", "\'")}','{bank.ShortName}',{(string.IsNullOrWhiteSpace(bank.Type) ? "NULL" : $"'{bank.Type}'")},{(string.IsNullOrWhiteSpace(bank.PixType) ? "NULL" : $"'{bank.PixType}'")},{(string.IsNullOrWhiteSpace(bank.Network) ? "NULL" : $"'{bank.Network}'")},{(string.IsNullOrWhiteSpace(bank.ChargeStr) ? "NULL" : $"'{bank.Charge}'")},{(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) ? "NULL" : $"'{bank.CreditDocument}'")},'{bank.SalaryPortability}',{(string.IsNullOrWhiteSpace(bank.SalaryPortability) ? "NULL" : $"'{bank.SalaryPortability}'")},{(bank.Products == null ? "NULL" : $"'{string.Join(",", bank.Products)}'")},{(string.IsNullOrWhiteSpace(bank.Url) ? "NULL" : $"'{bank.Url}'")},{(string.IsNullOrWhiteSpace(bank.DateOperationStarted) ? "NULL" : $"'{bank.DateOperationStarted}'")},{(string.IsNullOrWhiteSpace(bank.DatePixStarted) ? "NULL" : $"'{bank.DatePixStarted}'")},'{bank.DateRegistered:O}','{bank.DateUpdated:O}');" + ) + ); + + File.WriteAllLines($"result{Path.DirectorySeparatorChar}bancos.sql", lines, Encoding.UTF8); } } diff --git a/BancosBrasileiros.MergeTool/Program.cs b/BancosBrasileiros.MergeTool/Program.cs index d63a9171..6490a0fd 100644 --- a/BancosBrasileiros.MergeTool/Program.cs +++ b/BancosBrasileiros.MergeTool/Program.cs @@ -12,27 +12,27 @@ // // *********************************************************************** -namespace BancosBrasileiros.MergeTool -{ - using System; - using BancosBrasileiros.MergeTool.Helpers; +namespace BancosBrasileiros.MergeTool; + +using System; +using System.Text; +using Helpers; +/// +/// Class Program. +/// +static class Program +{ /// - /// Class Program. + /// Defines the entry point of the application. /// - static class Program + static void Main() { - /// - /// Defines the entry point of the application. - /// - static void Main() - { - System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); - Logger.Log("Bancos Brasileiros - Merge tool", ConsoleColor.Cyan); + Logger.Log("Bancos Brasileiros - Merge tool", ConsoleColor.Cyan); - var worker = new Worker(); - worker.Work(); - } + var worker = new Worker(); + worker.Work(); } } diff --git a/BancosBrasileiros.MergeTool/Worker.cs b/BancosBrasileiros.MergeTool/Worker.cs index 87c3268a..1f72ff9d 100644 --- a/BancosBrasileiros.MergeTool/Worker.cs +++ b/BancosBrasileiros.MergeTool/Worker.cs @@ -12,162 +12,160 @@ // // *********************************************************************** -namespace BancosBrasileiros.MergeTool +namespace BancosBrasileiros.MergeTool; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Dto; +using Helpers; +using Newtonsoft.Json; + +/// +/// Class Worker. +/// +internal class Worker { - using Newtonsoft.Json; - using BancosBrasileiros.MergeTool.Helpers; - using BancosBrasileiros.MergeTool.Dto; - using System; - using System.Collections.Generic; - using System.Linq; - using System.Text; - /// - /// Class Worker. + /// Works this instance. /// - internal class Worker + public void Work() { - /// - /// Works this instance. - /// - public void Work() + Logger.Log("Reading data files", ConsoleColor.White); + + var reader = new Reader(); + + var source = reader.LoadBase(); + var original = DeepClone(source); + + var ctc = reader.LoadCtc(); + var siloc = reader.LoadSiloc(); + var sitraf = reader.LoadSitraf(); + var slc = reader.LoadSlc(); + var spi = reader.LoadSpi(); + var str = reader.LoadStr(); + var pcps = reader.LoadPcps(); + var cql = reader.LoadCql(); + + Logger.Log( + $"Source: {source.Count} | CTC: {ctc.Count} | SILOC: {siloc.Count} | SITRAF: {sitraf.Count} | SLC: {slc.Count} | SPI: {spi.Count} | STR: {str.Count} | PCPS: {pcps.Count} | CQL: {cql.Count}\r\n", + ConsoleColor.DarkGreen + ); + + new Seeder(source) + .GenerateMissingDocument() + .SeedStr(str) + .SeedSitraf(sitraf) + .SeedSlc(slc) + .SeedSpi(spi) + .SeedCtc(ctc) + .SeedSiloc(siloc) + .SeedPcps(pcps) + .SeedCql(cql); + + foreach (var bank in source) { - Logger.Log("Reading data files", ConsoleColor.White); - - var reader = new Reader(); + bank.DateRegistered ??= DateTimeOffset.UtcNow; + bank.DateUpdated ??= DateTimeOffset.UtcNow; + } - var source = reader.LoadBase(); - var original = DeepClone(source); + var types = source.GroupBy(b => b.Type); - var ctc = reader.LoadCtc(); - var siloc = reader.LoadSiloc(); - var sitraf = reader.LoadSitraf(); - var slc = reader.LoadSlc(); - var spi = reader.LoadSpi(); - var str = reader.LoadStr(); - var pcps = reader.LoadPcps(); + Logger.Log($"Type: All | Total: {source.Count}", ConsoleColor.Yellow); + foreach (var type in types.OrderBy(g => g.Key)) + { Logger.Log( - $"Source: {source.Count} | CTC: {ctc.Count} | SILOC: {siloc.Count} | SITRAF: {sitraf.Count} | SLC: {slc.Count} | SPI: {spi.Count} | STR: {str.Count} | PCPS: {pcps.Count} \r\n", - ConsoleColor.DarkGreen + $"Type: {(string.IsNullOrWhiteSpace(type.Key) ? "-" : type.Key)} | Total: {type.Count()}", + ConsoleColor.DarkYellow ); + } - new Seeder(source) - .GenerateMissingDocument() - .SeedStr(str) - .SeedSitraf(sitraf) - .SeedSlc(slc) - .SeedSpi(spi) - .SeedCtc(ctc) - .SeedSiloc(siloc) - .SeedPcps(pcps); - - foreach (var bank in source) - { - bank.DateRegistered ??= DateTimeOffset.UtcNow; - bank.DateUpdated ??= DateTimeOffset.UtcNow; - } - - var types = source.GroupBy(b => b.Type); - - Logger.Log($"Type: All | Total: {source.Count}", ConsoleColor.Yellow); - - foreach (var type in types.OrderBy(g => g.Key)) - { - Logger.Log( - $"Type: {(string.IsNullOrWhiteSpace(type.Key) ? "-" : type.Key)} | Total: {type.Count()}", - ConsoleColor.DarkYellow - ); - } + source = source.Where(b => b.Ispb != 0 || b.Compe == 1).ToList(); - source = source.Where(b => b.Ispb != 0 || b.Compe == 1).ToList(); + var except = source.Except(original).ToList(); - var except = source.Except(original).ToList(); + if (!except.Any()) + { + Logger.Log("No new data or updated information", ConsoleColor.DarkMagenta); + Environment.Exit(1); + return; + } - if (!except.Any()) - { - Logger.Log("No new data or updated information", ConsoleColor.DarkMagenta); - Environment.Exit(1); - return; - } + var added = new List(); + var updated = new List(); - var added = new List(); - var updated = new List(); + foreach (var exc in except) + { + var isUpdated = source.Exists(b => b.Ispb == exc.Ispb); - foreach (var exc in except) - { - var isUpdated = source.Any(b => b.Ispb == exc.Ispb); + if (isUpdated) + updated.Add(exc); + else + added.Add(exc); + } - if (isUpdated) - updated.Add(exc); - else - added.Add(exc); - } + var changeLog = new StringBuilder(); - var changeLog = new StringBuilder(); + var color = ConsoleColor.DarkGreen; - var color = ConsoleColor.DarkGreen; + changeLog.AppendLine( + $"### {DateTime.Now:yyyy-MM-dd} - [MergeTool](https://github.com/guibranco/BancosBrasileiros/tree/MergeTool):\r\n" + ); + if (added.Any()) + { changeLog.AppendLine( - $"### {DateTime.Now:yyyy-MM-dd} - [MergeTool](https://github.com/guibranco/BancosBrasileiros/tree/MergeTool):\r\n" + $"- Added {added.Count} bank{(added.Count == 1 ? string.Empty : "s")}" ); - if (added.Any()) + Logger.Log($"\r\nAdded items: {added.Count}\r\n\r\n", ConsoleColor.White); + + foreach (var item in added) { - changeLog.AppendLine( - $"- Added {added.Count} bank{(added.Count == 1 ? string.Empty : "s")}" - ); - - Logger.Log($"\r\nAdded items: {added.Count}\r\n\r\n", ConsoleColor.White); - - foreach (var item in added) - { - changeLog.AppendLine($"\t- {item.Compe} - {item.ShortName} - {item.Document}"); - color = - color == ConsoleColor.DarkGreen - ? ConsoleColor.Cyan - : ConsoleColor.DarkGreen; - Logger.Log($"Added: {item}\r\n", color); - } + changeLog.AppendLine($"\t- {item.Compe} - {item.ShortName} - {item.Document}"); + color = + color == ConsoleColor.DarkGreen ? ConsoleColor.Cyan : ConsoleColor.DarkGreen; + Logger.Log($"Added: {item}\r\n", color); } + } + + color = ConsoleColor.DarkBlue; + + if (updated.Any()) + { + changeLog.AppendLine( + $"- Updated {updated.Count} bank{(updated.Count == 1 ? string.Empty : "s")}" + ); - color = ConsoleColor.DarkBlue; + Logger.Log($"\r\nUpdated items: {updated.Count}\r\n\r\n", ConsoleColor.White); - if (updated.Any()) + foreach (var item in updated) { - changeLog.AppendLine( - $"- Updated {updated.Count} bank{(updated.Count == 1 ? string.Empty : "s")}" - ); - - Logger.Log($"\r\nUpdated items: {updated.Count}\r\n\r\n", ConsoleColor.White); - - foreach (var item in updated) - { - changeLog.AppendLine($"\t- {item.Compe} - {item.ShortName} - {item.Document}"); - color = - color == ConsoleColor.DarkBlue ? ConsoleColor.Blue : ConsoleColor.DarkBlue; - Logger.Log($"Updated: {item}\r\n", color); - } + changeLog.AppendLine($"\t- {item.Compe} - {item.ShortName} - {item.Document}"); + color = color == ConsoleColor.DarkBlue ? ConsoleColor.Blue : ConsoleColor.DarkBlue; + Logger.Log($"Updated: {item}\r\n", color); } + } - Logger.Log("\r\nSaving result files", ConsoleColor.White); + Logger.Log("\r\nSaving result files", ConsoleColor.White); - Writer.WriteChangeLog(changeLog.ToString()); - Writer.SaveBanks(source); + Writer.WriteChangeLog(changeLog.ToString()); + Writer.SaveBanks(source); - Logger.Log($"Merge done. Banks: {source.Count}", ConsoleColor.White); - } + Logger.Log($"Merge done. Banks: {source.Count}", ConsoleColor.White); + } - /// - /// Deeps the clone. - /// - /// - /// The item. - /// T. - private static T DeepClone(T item) - { - var json = JsonConvert.SerializeObject(item); - return JsonConvert.DeserializeObject(json); - } + /// + /// Deeps the clone. + /// + /// + /// The item. + /// T. + private static T DeepClone(T item) + { + var json = JsonConvert.SerializeObject(item); + return JsonConvert.DeserializeObject(json); } } diff --git a/BancosBrasileiros.sln.DotSettings b/BancosBrasileiros.sln.DotSettings index ee3f83e2..b36544dc 100644 --- a/BancosBrasileiros.sln.DotSettings +++ b/BancosBrasileiros.sln.DotSettings @@ -5,6 +5,7 @@ True True True + True True True True