using CMS_CORE_Library.Models; using Step.Model.DatabaseModels; using System.Collections; using System.ComponentModel.DataAnnotations; namespace Step.Model.DTOModels.ToolModels { public class DTONcTool { [Required] public short OffsetLength { get; set; } [Required] public int ResidualLife { get; set; } [Required] public int ResidualRevive { get; set; } [Required] // Foreign keys public short FamilyId { get; set; } [Required] public bool Disabled { get; set; } [Required] public bool Broken { get; set; } [Required] public bool Measured { get; set; } [Required] public bool ClockwiseRotation { get; set; } [Required] public bool CounterClockwiseRotation { get; set; } public short? ShankId { get; set; } = null; public static explicit operator DTONcTool(DbNcToolModel obj) { // Get bit values BitArray statusBits = new BitArray(obj.Status); bool[] bits = new bool[8]; statusBits.CopyTo(bits, 0); return new DTONcTool() { FamilyId = obj.FamilyId, ShankId = (short)(obj.ShankId == null ? 0 : obj.ShankId.Value), OffsetLength = obj.OffsetLength, ResidualLife = obj.ResidualLife, ResidualRevive = obj.ResidualRevive, Disabled = bits[0], Broken = bits[1], Measured = bits[2], ClockwiseRotation = bits[3], CounterClockwiseRotation = bits[4], }; } public static explicit operator DbNcToolModel(DTONcTool obj) { // Prepare status byte bool[] result = new bool[8] { obj.Disabled, obj.Broken, obj.Measured, obj.ClockwiseRotation, obj.CounterClockwiseRotation, false, false, false }; byte status = ConvertArrayToByte(result); return new DbNcToolModel() { FamilyId = obj.FamilyId, ShankId = obj.ShankId, OffsetLength = obj.OffsetLength, ResidualLife = obj.ResidualLife, ResidualRevive = obj.ResidualRevive, Status = status }; } protected static byte ConvertArrayToByte(bool[] bits) { BitArray bitField = new BitArray(bits); //BitArray takes a bool[] byte[] bytes = new byte[1]; bitField.CopyTo(bytes, 0); return bytes[0]; } } public class DTONcToolModel : DTONewNcToolModel { public short Id { get; set; } public OffsetModel Offset1 { get; set; } public OffsetModel Offset2 { get; set; } public OffsetModel Offset3 { get; set; } public static explicit operator DTONcToolModel(DbNcToolModel obj) { // Get bit values BitArray statusBits = new BitArray(new byte[] { obj.Status }); bool[] bits = new bool[8]; statusBits.CopyTo(bits, 0); return new DTONcToolModel() { Id = obj.ToolId, FamilyId = obj.FamilyId, ShankId = obj.ShankId, OffsetLength = obj.OffsetLength, ResidualLife = obj.ResidualLife, ResidualRevive = obj.ResidualRevive, Disabled = bits[0], Broken = bits[1], Measured = bits[2], ClockwiseRotation = bits[3], CounterClockwiseRotation = bits[4], OffsetId1 = obj.OffsetId1, OffsetId2 = obj.OffsetId2, OffsetId3 = obj.OffsetId3 }; } public static explicit operator DbNcToolModel(DTONcToolModel obj) { // Prepare status byte bool[] result = new bool[8] { obj.Disabled, obj.Broken, obj.Measured, obj.ClockwiseRotation, obj.CounterClockwiseRotation, false, false, false }; byte status = ConvertArrayToByte(result); return new DbNcToolModel() { ToolId = obj.Id, FamilyId = obj.FamilyId, ShankId = obj.ShankId, OffsetLength = obj.OffsetLength, ResidualLife = obj.ResidualLife, ResidualRevive = obj.ResidualRevive, Status = status, OffsetId1 = obj.OffsetId1, OffsetId2 = obj.OffsetId2, OffsetId3 = obj.OffsetId3 }; } } public class DTONewNcToolModel : DTONcTool { public short? OffsetId1 { get; set; } public short? OffsetId2 { get; set; } public short? OffsetId3 { get; set; } public static explicit operator DTONewNcToolModel(DbNcToolModel obj) { // Get bit values BitArray statusBits = new BitArray(obj.Status); bool[] bits = new bool[8]; statusBits.CopyTo(bits, 0); return new DTONewNcToolModel() { FamilyId = obj.FamilyId, ShankId = (short)(obj.ShankId == null ? 0 : obj.ShankId.Value), OffsetLength = obj.OffsetLength, ResidualLife = obj.ResidualLife, ResidualRevive = obj.ResidualRevive, Disabled = bits[0], Broken = bits[1], Measured = bits[2], ClockwiseRotation = bits[3], CounterClockwiseRotation = bits[4], OffsetId1 = obj.OffsetId1, OffsetId2 = obj.OffsetId2, OffsetId3 = obj.OffsetId3 }; } public static explicit operator DbNcToolModel(DTONewNcToolModel obj) { // Prepare status byte bool[] result = new bool[8] { obj.Disabled, obj.Broken, obj.Measured, obj.ClockwiseRotation, obj.CounterClockwiseRotation, false, false, false}; byte status = ConvertArrayToByte(result); return new DbNcToolModel() { FamilyId = obj.FamilyId, ShankId = obj.ShankId, OffsetLength = obj.OffsetLength, ResidualLife = obj.ResidualLife, ResidualRevive = obj.ResidualRevive, Status = status, OffsetId1 = obj.OffsetId1, OffsetId2 = obj.OffsetId2, OffsetId3 = obj.OffsetId3 }; } } }