286 lines
11 KiB
C#
286 lines
11 KiB
C#
// Copyright (c) 2020-present schick Informatik
|
|
// Description:
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace bsmd.database
|
|
{
|
|
|
|
/// <summary>
|
|
/// Extra balcony: Data stored in Maersk Excel sheets must be stored to be exported
|
|
/// at a later date. In order to do that (and to be a little more flexible in the future)
|
|
/// we use a generic storage class called "XtraData" that hopefully might be useful in another
|
|
/// future scenario as well
|
|
/// </summary>
|
|
public class MaerskData : DatabaseEntity, IEquatable<MaerskData>, IComparable<MaerskData>
|
|
{
|
|
|
|
#region Construction
|
|
public MaerskData()
|
|
{
|
|
this.tablename = "[dbo].[XtraData]";
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Enums
|
|
|
|
public enum MDStatus
|
|
{
|
|
[Description("No id and is in the future")]
|
|
NO_ID,
|
|
[Description("Updated")]
|
|
UPDATED,
|
|
[Description("Requesting id")]
|
|
REQUESTING_ID,
|
|
[Description("Id assigned")]
|
|
ID_ASSIGNED,
|
|
[Description("upcoming without an id")]
|
|
NO_ID_AND_DUE,
|
|
[Description("Has id and is in the future")]
|
|
ID,
|
|
[Description("In the past, id or not")]
|
|
DONE,
|
|
[Description("no ETA found on data record")]
|
|
NO_ETA,
|
|
[Description("VISIT-ID was cancelled")]
|
|
CANCELLED
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// reference id as set in the database
|
|
/// </summary>
|
|
public Guid MessageCoreId { get; set; }
|
|
|
|
/// <summary>
|
|
/// Core instance reference once matched / set
|
|
/// </summary>
|
|
public MessageCore Core { get; set; }
|
|
|
|
public MDStatus Status { get; set; }
|
|
|
|
public DateTime? ETA { get; set; }
|
|
|
|
public string ColA { get; set; }
|
|
|
|
public string ColB { get; set; }
|
|
/// <summary>
|
|
/// Shipping area?
|
|
/// </summary>
|
|
public string ColC { get; set; }
|
|
|
|
public string ColD { get; set; }
|
|
/// <summary>
|
|
/// Vessel name
|
|
/// </summary>
|
|
public string ColE { get; set; }
|
|
/// <summary>
|
|
/// IMO
|
|
/// </summary>
|
|
public string ColF { get; set; }
|
|
/// <summary>
|
|
/// Voyage-No incoming
|
|
/// </summary>
|
|
public string ColG { get; set; }
|
|
/// <summary>
|
|
/// Voyage-No outgoing
|
|
/// </summary>
|
|
public string ColH { get; set; }
|
|
/// <summary>
|
|
/// Terminal
|
|
/// </summary>
|
|
public string ColI { get; set; }
|
|
/// <summary>
|
|
/// Operator
|
|
/// </summary>
|
|
public string ColJ { get; set; }
|
|
/// <summary>
|
|
/// ETA
|
|
/// </summary>
|
|
public string ColK { get; set; }
|
|
/// <summary>
|
|
/// ETD
|
|
/// </summary>
|
|
public string ColL { get; set; }
|
|
/// <summary>
|
|
/// Visit-Id
|
|
/// </summary>
|
|
public string ColM { get; set; }
|
|
|
|
[MaxLength(100)]
|
|
public string Remark { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region abstract DatabaseEntity method implementation
|
|
|
|
public override List<DatabaseEntity> LoadList(IDataReader reader)
|
|
{
|
|
List<DatabaseEntity> result = new List<DatabaseEntity>();
|
|
if (reader != null)
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
result.Add(ReadRowFromReader(reader));
|
|
}
|
|
reader.Close();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public async Task<List<MaerskData>> LoadListAsync(SqlDataReader reader)
|
|
{
|
|
List<MaerskData> result = new List<MaerskData>();
|
|
if (reader != null)
|
|
{
|
|
while (await reader.ReadAsync())
|
|
{
|
|
result.Add(ReadRowFromReader(reader));
|
|
}
|
|
reader.Close();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private MaerskData ReadRowFromReader(IDataReader reader)
|
|
{
|
|
MaerskData md = null;
|
|
if (reader != null)
|
|
{
|
|
md = new MaerskData();
|
|
|
|
md.id = reader.GetGuid(0);
|
|
if (!reader.IsDBNull(1)) md.MessageCoreId = reader.GetGuid(1);
|
|
if (!reader.IsDBNull(2)) md.ColA = reader.GetString(2);
|
|
if (!reader.IsDBNull(3)) md.ColB = reader.GetString(3);
|
|
if (!reader.IsDBNull(4)) md.ColC = reader.GetString(4);
|
|
if (!reader.IsDBNull(5)) md.ColD = reader.GetString(5);
|
|
if (!reader.IsDBNull(6)) md.ColE = reader.GetString(6);
|
|
if (!reader.IsDBNull(7)) md.ColF = reader.GetString(7);
|
|
if (!reader.IsDBNull(8)) md.ColG = reader.GetString(8);
|
|
if (!reader.IsDBNull(9)) md.ColH = reader.GetString(9);
|
|
if (!reader.IsDBNull(10)) md.ColI = reader.GetString(10);
|
|
if (!reader.IsDBNull(11)) md.ColJ = reader.GetString(11);
|
|
if (!reader.IsDBNull(12)) md.ColK = reader.GetString(12);
|
|
if (!reader.IsDBNull(13)) md.ColL = reader.GetString(13);
|
|
if (!reader.IsDBNull(14)) md.ColM = reader.GetString(14);
|
|
if (!reader.IsDBNull(15)) md.Remark = reader.GetString(15);
|
|
|
|
// try parsing the column to datetime
|
|
if (DateTime.TryParse(md.ColA, out DateTime eta))
|
|
md.ETA = eta;
|
|
}
|
|
return md;
|
|
}
|
|
|
|
public override void PrepareLoadCommand(IDbCommand cmd, Message.LoadFilter filter, params object[] criteria)
|
|
{
|
|
string query = string.Format("SELECT Id, ReferenceId, Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12 , Field13, Field14 FROM {0} ", this.Tablename);
|
|
|
|
switch (filter)
|
|
{
|
|
case Message.LoadFilter.BY_ID:
|
|
query += " WHERE Id = @ID";
|
|
((SqlCommand)cmd).Parameters.AddWithValue("@ID", criteria[0]);
|
|
break;
|
|
case Message.LoadFilter.BY_CORE:
|
|
query += " WHERE ReferenceId = @ID";
|
|
((SqlCommand)cmd).Parameters.AddWithValue("@ID", criteria[0]);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
cmd.CommandText = query;
|
|
}
|
|
|
|
public override void PrepareSave(IDbCommand cmd)
|
|
{
|
|
SqlCommand scmd = cmd as SqlCommand;
|
|
|
|
scmd.Parameters.AddWithNullableValue("@REF", this.MessageCoreId);
|
|
scmd.Parameters.AddWithNullableValue("@COLA", this.ColA);
|
|
scmd.Parameters.AddWithNullableValue("@COLB", this.ColB);
|
|
scmd.Parameters.AddWithNullableValue("@COLC", this.ColC);
|
|
scmd.Parameters.AddWithNullableValue("@COLD", this.ColD);
|
|
scmd.Parameters.AddWithNullableValue("@COLE", this.ColE);
|
|
scmd.Parameters.AddWithNullableValue("@COLF", this.ColF);
|
|
scmd.Parameters.AddWithNullableValue("@COLG", this.ColG);
|
|
scmd.Parameters.AddWithNullableValue("@COLH", this.ColH);
|
|
scmd.Parameters.AddWithNullableValue("@COLI", this.ColI);
|
|
scmd.Parameters.AddWithNullableValue("@COLJ", this.ColJ);
|
|
scmd.Parameters.AddWithNullableValue("@COLK", this.ColK);
|
|
scmd.Parameters.AddWithNullableValue("@COLL", this.ColL);
|
|
scmd.Parameters.AddWithNullableValue("@COLM", this.ColM);
|
|
scmd.Parameters.AddWithNullableValue("@REMARK", this.Remark);
|
|
|
|
if (this.IsNew)
|
|
{
|
|
this.CreateId();
|
|
scmd.Parameters.AddWithValue("@ID", this.Id);
|
|
scmd.CommandText = string.Format("INSERT INTO {0} (Id, ReferenceId, Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12, Field13, Field14) " +
|
|
"VALUES (@ID, @REF, @COLA, @COLB, @COLC, @COLD, @COLE, @COLF, @COLG, @COLH, @COLI, @COLJ, @COLK, @COLL, @COLM, @REMARK)", this.Tablename);
|
|
}
|
|
else
|
|
{
|
|
scmd.Parameters.AddWithValue("@ID", this.Id);
|
|
scmd.CommandText = string.Format("UPDATE {0} SET Field1 = @COLA, Field2 = @COLB, Field3 = @COLC, Field4 = @COLD, Field5 = @COLE, Field6 = @COLF, Field7 = @COLG, Field8 = @COLH, " +
|
|
"Field9 = @COLI, Field10 = @COLJ, Field11 = @COLK, Field12 = @COLL, Field13 = @COLM, Field14 = @Remark WHERE Id = @ID", this.Tablename);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Equality means if IMO and Voyage no. In/Out are the same
|
|
/// </summary>
|
|
public bool Equals(MaerskData other)
|
|
{
|
|
if (other == null) return false;
|
|
if (this.ColG == null) return other.ColG == null;
|
|
if (this.ColH == null) return other.ColH == null;
|
|
if (this.ColF == null) return other.ColF == null;
|
|
return this.ColF.Equals(other.ColF) && this.ColG.Equals(other.ColG) && this.ColH.Equals(other.ColH);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sort-Order is by ETA
|
|
/// </summary>
|
|
public int CompareTo(MaerskData other)
|
|
{
|
|
if (this.ETA == null) return 0;
|
|
if ((other == null) || (other.ETA == null)) return 0;
|
|
return this.ETA.Value.CompareTo(other.ETA.Value);
|
|
}
|
|
|
|
public bool Update(MaerskData md)
|
|
{
|
|
bool result = false;
|
|
if (!md.ColA.IsNullOrEmpty() && !md.ColA.Equals(ColA)) { ColA = md.ColA; result = true; }
|
|
if (!md.ColB.IsNullOrEmpty() && !md.ColB.Equals(ColB)) { ColB = md.ColB; result = true; }
|
|
if (!md.ColC.IsNullOrEmpty() && !md.ColC.Equals(ColC)) { ColC = md.ColC; result = true; }
|
|
if (!md.ColD.IsNullOrEmpty() && !md.ColD.Equals(ColD)) { ColD = md.ColD; result = true; }
|
|
if (!md.ColE.IsNullOrEmpty() && !md.ColE.Equals(ColE)) { ColE = md.ColE; result = true; }
|
|
if (!md.ColI.IsNullOrEmpty() && !md.ColI.Equals(ColI)) { ColI = md.ColI; result = true; }
|
|
if (!md.ColJ.IsNullOrEmpty() && !md.ColJ.Equals(ColJ)) { ColJ = md.ColJ; result = true; }
|
|
if (!md.ColK.IsNullOrEmpty() && !md.ColK.Equals(ColK)) { ColK = md.ColK; result = true; }
|
|
if (!md.ColL.IsNullOrEmpty() && !md.ColL.Equals(ColL)) { ColL = md.ColL; result = true; }
|
|
if (!md.ColM.IsNullOrEmpty() && !md.ColM.Equals(ColM)) { ColM = md.ColM; result = true; }
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|