| | 1 | | // Ignore Spelling: Naics Bal |
| | 2 | |
|
| | 3 | | using System.ComponentModel.DataAnnotations; |
| | 4 | | using System.ComponentModel.DataAnnotations.Schema; |
| | 5 | | using System.Data; |
| | 6 | | using System.Text.Json; |
| | 7 | | using System.Text.Json.Serialization; |
| | 8 | | using Common.Core.Classes; |
| | 9 | | using Common.Core.Converters; |
| | 10 | | using Common.Core.Interfaces; |
| | 11 | |
|
| | 12 | | namespace Common.Core.Models; |
| | 13 | |
|
| | 14 | | /// <summary>This class contains details of a Company.</summary> |
| | 15 | | public class Company : ModelEdit, ICompany |
| | 16 | | { |
| | 17 | | /// <summary>Default name of the Company data file.</summary> |
| | 18 | | public const string cDefaultFile = "Company.json"; |
| | 19 | |
|
| | 20 | | #region Private Variables |
| | 21 | |
|
| | 22 | | private int _id; |
| | 23 | | private string? _name; |
| 29 | 24 | | private Address _address = new(); |
| | 25 | | private string? _governmentNumber; |
| | 26 | | private string? _primaryPhone; |
| | 27 | | private string? _secondaryPhone; |
| | 28 | | private string? _email; |
| | 29 | | private string? _naicsCode; |
| | 30 | | private bool? _private; |
| | 31 | | private int? _depositsCount; |
| | 32 | | private decimal? _depositsBal; |
| | 33 | |
|
| | 34 | | #endregion |
| | 35 | |
|
| | 36 | | #region ICompany Properties |
| | 37 | |
|
| | 38 | | /// <inheritdoc/> |
| | 39 | | public override int Id |
| | 40 | | { |
| 56 | 41 | | get => _id; |
| | 42 | | set |
| 30 | 43 | | { |
| 30 | 44 | | if( value != _id ) |
| 29 | 45 | | { |
| 29 | 46 | | _id = value; |
| 29 | 47 | | OnPropertyChanged( nameof( Id ) ); |
| 29 | 48 | | } |
| 30 | 49 | | } |
| | 50 | | } |
| | 51 | |
|
| | 52 | | /// <inheritdoc/> |
| | 53 | | [Required] |
| | 54 | | [MaxLength( 100 )] |
| | 55 | | public string Name |
| | 56 | | { |
| 54 | 57 | | get => ( _name is not null ) ? _name : string.Empty; |
| | 58 | | set |
| 30 | 59 | | { |
| 30 | 60 | | if( !value.Equals( _name ) ) |
| 30 | 61 | | { |
| 30 | 62 | | _name = value; |
| 30 | 63 | | OnPropertyChanged( nameof( Name ) ); |
| 30 | 64 | | } |
| 30 | 65 | | } |
| | 66 | | } |
| | 67 | |
|
| | 68 | | /// <inheritdoc/> |
| | 69 | | public Address Address |
| | 70 | | { |
| 44 | 71 | | get => _address; |
| | 72 | | set |
| 27 | 73 | | { |
| 27 | 74 | | if( value != _address ) |
| 27 | 75 | | { |
| 27 | 76 | | _address = value; |
| 27 | 77 | | OnPropertyChanged( nameof( Address ) ); |
| 27 | 78 | | } |
| 27 | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| | 82 | | /// <inheritdoc/> |
| | 83 | | [MaxLength( 50 )] |
| | 84 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 85 | | public string? GovernmentNumber |
| | 86 | | { |
| 23 | 87 | | get => _governmentNumber; |
| | 88 | | set |
| 30 | 89 | | { |
| 30 | 90 | | if( value != _governmentNumber ) |
| 30 | 91 | | { |
| 30 | 92 | | _governmentNumber = SetNullString( value ); |
| 30 | 93 | | OnPropertyChanged( nameof( GovernmentNumber ) ); |
| 30 | 94 | | } |
| 30 | 95 | | } |
| | 96 | | } |
| | 97 | |
|
| | 98 | | /// <inheritdoc/> |
| | 99 | | [MaxLength( 50 )] |
| | 100 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 101 | | public string? PrimaryPhone |
| | 102 | | { |
| 23 | 103 | | get => _primaryPhone; |
| | 104 | | set |
| 30 | 105 | | { |
| 30 | 106 | | if( value != _primaryPhone ) |
| 30 | 107 | | { |
| 30 | 108 | | _primaryPhone = SetNullString( value ); |
| 30 | 109 | | OnPropertyChanged( nameof( PrimaryPhone ) ); |
| 30 | 110 | | } |
| 30 | 111 | | } |
| | 112 | | } |
| | 113 | |
|
| | 114 | | /// <inheritdoc/> |
| | 115 | | [MaxLength( 50 )] |
| | 116 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 117 | | public string? SecondaryPhone |
| | 118 | | { |
| 23 | 119 | | get => _secondaryPhone; |
| | 120 | | set |
| 30 | 121 | | { |
| 30 | 122 | | if( value != _secondaryPhone ) |
| 30 | 123 | | { |
| 30 | 124 | | _secondaryPhone = SetNullString( value ); |
| 30 | 125 | | OnPropertyChanged( nameof( SecondaryPhone ) ); |
| 30 | 126 | | } |
| 30 | 127 | | } |
| | 128 | | } |
| | 129 | |
|
| | 130 | | /// <inheritdoc/> |
| | 131 | | [MaxLength( 50 )] |
| | 132 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 133 | | public string? Email |
| | 134 | | { |
| 22 | 135 | | get => _email; |
| | 136 | | set |
| 28 | 137 | | { |
| 28 | 138 | | if( value != _email ) |
| 28 | 139 | | { |
| 28 | 140 | | _email = SetNullString( value ); |
| 28 | 141 | | OnPropertyChanged( nameof( Email ) ); |
| 28 | 142 | | } |
| 28 | 143 | | } |
| | 144 | | } |
| | 145 | |
|
| | 146 | | /// <inheritdoc/> |
| | 147 | | [MaxLength( 20 )] |
| | 148 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 149 | | public string? NaicsCode |
| | 150 | | { |
| 23 | 151 | | get => _naicsCode; |
| | 152 | | set |
| 30 | 153 | | { |
| 30 | 154 | | if( value != _naicsCode ) |
| 30 | 155 | | { |
| 30 | 156 | | _naicsCode = SetNullString( value ); |
| 30 | 157 | | OnPropertyChanged( nameof( NaicsCode ) ); |
| 30 | 158 | | } |
| 30 | 159 | | } |
| | 160 | | } |
| | 161 | |
|
| | 162 | | /// <inheritdoc/> |
| | 163 | | [Column( TypeName = "char(1)" )] |
| | 164 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 165 | | [JsonConverter( typeof( JsonBooleanString ) )] |
| | 166 | | public bool? Private |
| | 167 | | { |
| 30 | 168 | | get => _private; |
| | 169 | | set |
| 34 | 170 | | { |
| 34 | 171 | | if( value != _private ) |
| 33 | 172 | | { |
| 33 | 173 | | _private = value; |
| 33 | 174 | | OnPropertyChanged( nameof( Private ) ); |
| 33 | 175 | | } |
| 34 | 176 | | } |
| | 177 | | } |
| | 178 | |
|
| | 179 | | /// <inheritdoc/> |
| | 180 | | [Range( 0, 100 )] |
| | 181 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 182 | | [JsonConverter( typeof( JsonIntegerString ) )] |
| | 183 | | public int? DepositsCount |
| | 184 | | { |
| 23 | 185 | | get => _depositsCount; |
| | 186 | | set |
| 30 | 187 | | { |
| 30 | 188 | | if( value != _depositsCount ) |
| 30 | 189 | | { |
| 30 | 190 | | _depositsCount = value; |
| 30 | 191 | | OnPropertyChanged( nameof( DepositsCount ) ); |
| 30 | 192 | | } |
| 30 | 193 | | } |
| | 194 | | } |
| | 195 | |
|
| | 196 | | /// <inheritdoc/> |
| | 197 | | [Column( TypeName = "decimal(18,2)" )] |
| | 198 | | [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )] |
| | 199 | | [JsonConverter( typeof( JsonDecimalString ) )] |
| | 200 | | public decimal? DepositsBal |
| | 201 | | { |
| 24 | 202 | | get => _depositsBal; |
| | 203 | | set |
| 31 | 204 | | { |
| 31 | 205 | | if( value != _depositsBal ) |
| 24 | 206 | | { |
| 24 | 207 | | _depositsBal = value; |
| 24 | 208 | | OnPropertyChanged( nameof( DepositsBal ) ); |
| 24 | 209 | | } |
| 31 | 210 | | } |
| | 211 | | } |
| | 212 | |
|
| | 213 | | #endregion |
| | 214 | |
|
| | 215 | | #region Public Methods |
| | 216 | |
|
| | 217 | | /// <inheritdoc/> |
| | 218 | | public override object Clone() |
| 2 | 219 | | { |
| 2 | 220 | | return ReflectionHelper.CreateDeepCopy( this ) as Company ?? new Company(); |
| 2 | 221 | | } |
| | 222 | |
|
| | 223 | | /// <inheritdoc/> |
| | 224 | | /// <param name="obj">An object implementing the ICompany interface to compare with this object.</param> |
| | 225 | | public override bool Equals( object? obj ) |
| 19 | 226 | | { |
| 23 | 227 | | if( obj is null || obj is not ICompany other ) { return false; } |
| 17 | 228 | | return ReflectionHelper.IsEqual( this, other ); |
| 19 | 229 | | } |
| | 230 | |
|
| | 231 | | /// <inheritdoc/> |
| | 232 | | /// <param name="obj">An object implementing the ICompany interface with the changed values.</param> |
| | 233 | | public override void Update( object? obj ) |
| 3 | 234 | | { |
| 7 | 235 | | if( obj is null || obj is not ICompany other || other is not Company company ) { return; } |
| | 236 | |
|
| 1 | 237 | | ReflectionHelper.ApplyChanges( company, this ); |
| 3 | 238 | | } |
| | 239 | |
|
| | 240 | | /// <summary>Gets the Json serializer options for Company objects.</summary> |
| | 241 | | /// <returns>A JsonSerializerOptions object.</returns> |
| | 242 | | public static JsonSerializerOptions GetSerializerOptions() |
| 1 | 243 | | { |
| 1 | 244 | | var rtn = JsonHelper.DefaultSerializerOptions(); |
| 1 | 245 | | rtn.Converters.Add( new InterfaceFactory( typeof( Company ), typeof( ICompany ) ) ); |
| 1 | 246 | | rtn.NumberHandling = JsonNumberHandling.AllowReadingFromString; |
| | 247 | |
|
| 1 | 248 | | return rtn; |
| 1 | 249 | | } |
| | 250 | |
|
| | 251 | | /// <summary>Builds a Company object from a database table row.</summary> |
| | 252 | | /// <param name="row">Database row containing the Company columns.</param> |
| | 253 | | /// <param name="addPrefix">Table column prefix for Address fields if required.</param> |
| | 254 | | /// <returns>Company object containing the database values.</returns> |
| | 255 | | /// <remarks>This method assumes that the table column names are the same as the property names.</remarks> |
| | 256 | | public static Company Read( DataRow row, string addPrefix = "" ) |
| 10 | 257 | | { |
| 10 | 258 | | return new Company() |
| 10 | 259 | | { |
| 10 | 260 | | Id = row.Field<int>( nameof( Id ) ), |
| 10 | 261 | | Name = row.Field<string>( nameof( Name ) )!, |
| 10 | 262 | | Address = Address.BuildAddress( row, addPrefix ), |
| 10 | 263 | | GovernmentNumber = row.Field<string?>( nameof( GovernmentNumber ) ), |
| 10 | 264 | | PrimaryPhone = row.Field<string?>( nameof( PrimaryPhone ) ), |
| 10 | 265 | | SecondaryPhone = row.Field<string?>( nameof( SecondaryPhone ) ), |
| 10 | 266 | | Email = row.Field<string?>( nameof( Email ) ), |
| 10 | 267 | | NaicsCode = row.Field<string?>( nameof( NaicsCode ) ), |
| 10 | 268 | | Private = Generic.CharToBool( row[nameof( Private )] ), |
| 10 | 269 | | DepositsCount = row.Field<int?>( nameof( DepositsCount ) ), |
| 10 | 270 | | DepositsBal = row.Field<decimal?>( nameof( DepositsBal ) ), |
| 10 | 271 | | }; |
| 10 | 272 | | } |
| | 273 | |
|
| | 274 | | /// <summary>Builds the SQL script for any value changes.</summary> |
| | 275 | | /// <param name="row">Database row containing the current Company data.</param> |
| | 276 | | /// <param name="obj">ICompany object containing the original values.</param> |
| | 277 | | /// <param name="mod">ICompany object containing the modified values.</param> |
| | 278 | | /// <param name="addPrefix">Table column name prefix for Address fields (if required).</param> |
| | 279 | | /// <returns>An empty string is returned if no changes were found.</returns> |
| | 280 | | /// <remarks>This method assumes that the table column names are the same as the property names.</remarks> |
| | 281 | | public static string UpdateSQL( DataRow row, ICompany obj, ICompany mod, string addPrefix = "" ) |
| 6 | 282 | | { |
| 6 | 283 | | IList<string> sql = new List<string>(); |
| 6 | 284 | | Company cur = Read( row, addPrefix ); |
| 8 | 285 | | if( cur.Id != mod.Id ) { return string.Empty; } |
| | 286 | |
|
| 5 | 287 | | if( !cur.Equals( obj ) ) |
| 1 | 288 | | { |
| 1 | 289 | | mod.Name = cur.Name; |
| 1 | 290 | | mod.Address = cur.Address; |
| 1 | 291 | | mod.GovernmentNumber = cur.GovernmentNumber; |
| 1 | 292 | | mod.PrimaryPhone = cur.PrimaryPhone; |
| 1 | 293 | | mod.SecondaryPhone = cur.SecondaryPhone; |
| 1 | 294 | | mod.Email = cur.Email; |
| 1 | 295 | | mod.NaicsCode = cur.NaicsCode; |
| 1 | 296 | | mod.DepositsCount = cur.DepositsCount; |
| 1 | 297 | | mod.DepositsBal = cur.DepositsBal; |
| 1 | 298 | | mod.Private = cur.Private; |
| 1 | 299 | | return string.Empty; |
| | 300 | | } |
| | 301 | |
|
| 16 | 302 | | if( obj.Name != mod.Name ) { SetSQLColumn( nameof( Name ), mod.Name, sql ); } |
| 4 | 303 | | _ = Address.UpdateAddress( obj.Address, mod.Address, cur.Address, sql, addPrefix ); |
| 4 | 304 | | UpdateOthers( obj, mod, sql ); |
| 4 | 305 | | if( obj.Private != mod.Private ) |
| 4 | 306 | | { |
| | 307 | | // Special handling for boolean as char |
| 4 | 308 | | char? val = mod.Private is null ? null : mod.Private.Value ? 'Y' : 'N'; |
| 4 | 309 | | SetSQLColumn( nameof( Private ), val, sql ); |
| 4 | 310 | | } |
| | 311 | |
|
| 4 | 312 | | return string.Join( ", ", sql ); |
| 6 | 313 | | } |
| | 314 | |
|
| | 315 | | #endregion |
| | 316 | |
|
| | 317 | | private static void UpdateOthers( ICompany obj, ICompany mod, IList<string> sql ) |
| 4 | 318 | | { |
| 16 | 319 | | if( obj.GovernmentNumber != mod.GovernmentNumber ) { SetSQLColumn( nameof( GovernmentNumber ), mod.GovernmentNumber, |
| 16 | 320 | | if( obj.PrimaryPhone != mod.PrimaryPhone ) { SetSQLColumn( nameof( PrimaryPhone ), mod.PrimaryPhone, sql ); } |
| 16 | 321 | | if( obj.SecondaryPhone != mod.SecondaryPhone ) { SetSQLColumn( nameof( SecondaryPhone ), mod.SecondaryPhone, sql ); |
| 16 | 322 | | if( obj.Email != mod.Email ) { SetSQLColumn( nameof( Email ), mod.Email, sql ); } |
| 16 | 323 | | if( obj.NaicsCode != mod.NaicsCode ) { SetSQLColumn( nameof( NaicsCode ), mod.NaicsCode, sql ); } |
| 16 | 324 | | if( obj.DepositsCount != mod.DepositsCount ) { SetSQLColumn( nameof( DepositsCount ), mod.DepositsCount.ToString(), |
| 16 | 325 | | if( obj.DepositsBal != mod.DepositsBal ) { SetSQLColumn( nameof( DepositsBal ), mod.DepositsBal.ToString(), sql ); } |
| 4 | 326 | | } |
| | 327 | | } |