public BodyScanPlanet(XScanClass scan, SystemBody sysbody, int sysbodyid, int id = 0) : base(scan, sysbody, sysbodyid, id)
        {
            XScanPlanet pscan = scan.Planet;

            PlanetClassId    = pscan.PlanetClassId;
            MassEM           = pscan.MassEM;
            SurfaceGravity   = pscan.SurfaceGravity;
            VolcanismId      = pscan.VolcanismId == 0 ? (byte?)null : pscan.VolcanismId;
            VolcanismMinor   = pscan.VolcanismMinor;
            VolcanismMajor   = pscan.VolcanismMajor;
            IsLandable       = pscan.HasLandable ? pscan.IsLandable : (bool?)null;
            HasComposition   = pscan.HasComposition;
            CompositionMetal = pscan.CompositionMetal;
            CompositionRock  = pscan.CompositionRock;
            CompositionIce   = pscan.CompositionIce;

            if (pscan.HasAtmosphere)
            {
                Atmosphere = new BodyScanAtmosphere(scan.Atmosphere, id);
            }

            if (pscan.HasMaterials)
            {
                Materials = new BodyScanMaterials(scan.Materials, id);
            }
        }
        public void Insert(SystemBody body)
        {
            if (bodycmd == null)
            {
                Context.Set <SystemBody>().Add(body);
                Context.SaveChanges();
            }
            else
            {
                bodycmd.Transaction     = Context.Database.CurrentTransaction?.UnderlyingTransaction;
                namecmd.Transaction     = Context.Database.CurrentTransaction?.UnderlyingTransaction;
                paramSystemId.Value     = body.SystemId;
                paramBodyID.Value       = body.BodyID;
                paramStars.Value        = body.Stars;
                paramIsBelt.Value       = body.IsBelt;
                paramPlanet.Value       = body.Planet;
                paramMoon1.Value        = body.Moon1;
                paramMoon2.Value        = body.Moon2;
                paramMoon3.Value        = body.Moon3;
                paramScanBaseHash.Value = body.ScanBaseHash;
                paramCustomNameId.Value = body.CustomNameId;
                body.Id = execbodycmd(bodycmd);

                if (body.CustomName != null)
                {
                    body.CustomName.Id      = body.Id;
                    paramNameId.Value       = body.Id;
                    paramNameSystemId.Value = body.SystemId;
                    paramNameBodyID.Value   = body.CustomName.BodyID;
                    paramCustomName.Value   = body.CustomName.CustomName;
                    namecmd.ExecuteNonQuery();
                }
            }
        }
        public BodyScanStar(XModels.XScanClass scan, SystemBody sysbody, int sysbodyid, int id = 0) : base(scan, sysbody, sysbodyid, id)
        {
            XScanStar sscan = scan.Star;

            AbsoluteMagnitude = sscan.AbsoluteMagnitude;
            StellarMass       = sscan.StellarMass;
            Age_MY            = sscan.Age_MY;
            StarTypeId        = sscan.StarTypeId;
            LuminosityId      = sscan.LuminosityId == 0 ? (byte?)null : sscan.LuminosityId;
        }
        public void Update(XModels.XScanClass scandata, SystemBody dbbody, BodyScan dbscan)
        {
            if (dbbody.BodyID == -1 && scandata.Body.BodyID >= 0)
            {
                short bodyid = scandata.Body.BodyID;
                paramUpdateBodyId.Value     = dbbody.Id;
                paramUpdateBodyBodyID.Value = bodyid;
                cmdUpdateBody.ExecuteNonQuery();
                dbbody.BodyID = bodyid;
            }

            if ((dbscan.ParentSetId == null && scandata.Scan.ParentSetId != 0) ||
                (dbscan.AxialTilt == null && scandata.Scan.HasAxialTilt) ||
                (dbscan.ReserveLevelId == null && scandata.Scan.ReserveLevelId != 0) ||
                (dbscan.TidalLock == null && scandata.Scan.HasTidalLock))
            {
                paramUpdateScanId.Value           = dbscan.Id;
                paramUpdateScanAxialTilt.Value    = (object)dbscan.AxialTilt ?? (scandata.Scan.HasAxialTilt ? (object)scandata.Scan.AxialTilt : DBNull.Value);
                paramUpdateScanReserveLevel.Value = (object)dbscan.ReserveLevelId ?? (scandata.Scan.ReserveLevelId != 0 ? (object)scandata.Scan.ReserveLevelId : DBNull.Value);
                paramUpdateScanTidalLock.Value    = (object)dbscan.TidalLock ?? (scandata.Scan.HasTidalLock ? (object)scandata.Scan.TidalLock : DBNull.Value);
                paramUpdateScanParents.Value      = (object)dbscan.ParentSetId ?? (scandata.Scan.HasParents ? (object)scandata.Parents.DbId : DBNull.Value);
                cmdUpdateScan.ExecuteNonQuery();
                dbscan.AxialTilt      = dbscan.AxialTilt ?? (scandata.Scan.HasAxialTilt ? (float?)scandata.Scan.AxialTilt : null);
                dbscan.TidalLock      = dbscan.TidalLock ?? (scandata.Scan.HasTidalLock ? (bool?)scandata.Scan.TidalLock : null);
                dbscan.ReserveLevelId = dbscan.ReserveLevelId ?? (scandata.Scan.ReserveLevelId != 0 ? (byte?)scandata.Scan.ReserveLevelId : null);
                dbscan.ParentSetId    = dbscan.ParentSetId ?? (scandata.Scan.ParentSetId != 0 ? (byte?)scandata.Scan.ParentSetId : null);
            }

            if (dbscan is BodyScanPlanet pscan && scandata.Scan.IsPlanet)
            {
                if ((!pscan.HasComposition && scandata.Planet.HasComposition) ||
                    (pscan.VolcanismId == null && scandata.Planet.VolcanismId != 0) ||
                    (pscan.IsLandable == null && scandata.Planet.HasLandable) ||
                    (pscan.TerraformStateId == null && scandata.Planet.TerraformStateId != 0))
                {
                    pscan.CompositionMetal                = pscan.HasComposition ? pscan.CompositionMetal : scandata.Planet.CompositionMetal;
                    pscan.CompositionRock                 = pscan.HasComposition ? pscan.CompositionRock : scandata.Planet.CompositionRock;
                    pscan.CompositionIce                  = pscan.HasComposition ? pscan.CompositionIce : scandata.Planet.CompositionIce;
                    pscan.HasComposition                 |= scandata.Planet.HasComposition;
                    pscan.IsLandable                      = pscan.IsLandable ?? (scandata.Planet.HasLandable ? (bool?)scandata.Planet.IsLandable : null);
                    pscan.TerraformStateId                = pscan.TerraformStateId ?? (scandata.Planet.TerraformStateId != 0 ? (byte?)scandata.Planet.TerraformStateId : null);
                    pscan.VolcanismMajor                  = pscan.VolcanismId != null ? pscan.VolcanismMajor : scandata.Planet.VolcanismMajor;
                    pscan.VolcanismMinor                  = pscan.VolcanismId != null ? pscan.VolcanismMinor : scandata.Planet.VolcanismMinor;
                    pscan.VolcanismId                     = pscan.VolcanismId ?? (scandata.Planet.VolcanismId != 0 ? (byte?)scandata.Planet.VolcanismId : null);
                    paramUpdatePlanetScanId.Value         = pscan.Id;
                    paramUpdatePlanetComposIce.Value      = pscan.CompositionIce;
                    paramUpdatePlanetComposRock.Value     = pscan.CompositionRock;
                    paramUpdatePlanetComposMetal.Value    = pscan.CompositionMetal;
                    paramUpdatePlanetHasCompos.Value      = pscan.HasComposition;
                    paramUpdatePlanetLandable.Value       = (object)pscan.IsLandable ?? DBNull.Value;
                    paramUpdatePlanetTerraformState.Value = (object)pscan.TerraformStateId ?? DBNull.Value;
                    paramUpdatePlanetVolcanismId.Value    = (object)pscan.VolcanismId ?? DBNull.Value;
                    paramUpdatePlanetVolcanismMajor.Value = pscan.VolcanismMajor;
                    paramUpdatePlanetVolcanismMinor.Value = pscan.VolcanismMinor;
                    cmdUpdatePlanet.ExecuteNonQuery();
                }

                if (pscan.Atmosphere == null)
                {
                    if (scandata.Planet.HasAtmosphere)
                    {
                        pscan.Atmosphere = new BodyScanAtmosphere(scandata.Atmosphere, pscan.Id);

                        paramAtmosScanId.Value     = pscan.Atmosphere.Id;
                        paramSurfacePressure.Value = pscan.Atmosphere.SurfacePressure;
                        paramAtmosComp1Id.Value    = (object)pscan.Atmosphere.AtmosphereComponent1Id ?? DBNull.Value;
                        paramAtmosComp1Amt.Value   = pscan.Atmosphere.AtmosphereComponent1Amt;
                        paramAtmosComp2Id.Value    = (object)pscan.Atmosphere.AtmosphereComponent2Id ?? DBNull.Value;
                        paramAtmosComp2Amt.Value   = pscan.Atmosphere.AtmosphereComponent2Amt;
                        paramAtmosComp3Id.Value    = (object)pscan.Atmosphere.AtmosphereComponent3Id ?? DBNull.Value;
                        paramAtmosComp3Amt.Value   = pscan.Atmosphere.AtmosphereComponent3Amt;
                        paramAtmosNameId.Value     = pscan.Atmosphere.AtmosphereId;
                        paramAtmosTypeId.Value     = (object)pscan.Atmosphere.AtmosphereTypeId ?? DBNull.Value;
                        paramAtmosHot.Value        = pscan.Atmosphere.AtmosphereHot;
                        paramAtmosThin.Value       = pscan.Atmosphere.AtmosphereThin;
                        paramAtmosThick.Value      = pscan.Atmosphere.AtmosphereThick;
                        cmdInsertAtmos.ExecuteNonQuery();
                    }
                }
                else if ((pscan.Atmosphere.AtmosphereTypeId == null && scandata.Atmosphere.AtmosphereTypeId != 0) ||
                         (pscan.Atmosphere.AtmosphereComponent1Id == null && scandata.Atmosphere.Component1Id != 0))
                {
                    pscan.Atmosphere.AtmosphereTypeId = pscan.Atmosphere.AtmosphereTypeId ?? (scandata.Atmosphere.AtmosphereTypeId != 0 ? (byte?)scandata.Atmosphere.AtmosphereTypeId : null);
                    if (pscan.Atmosphere.AtmosphereComponent1Id == null && scandata.Atmosphere.Component1Id != 0)
                    {
                        pscan.Atmosphere.AtmosphereComponent1Id  = scandata.Atmosphere.Component1Id != 0 ? (byte?)scandata.Atmosphere.Component1Id : null;
                        pscan.Atmosphere.AtmosphereComponent1Amt = scandata.Atmosphere.Component1Amt;
                        pscan.Atmosphere.AtmosphereComponent2Id  = scandata.Atmosphere.Component2Id != 0 ? (byte?)scandata.Atmosphere.Component2Id : null;
                        pscan.Atmosphere.AtmosphereComponent2Amt = scandata.Atmosphere.Component2Amt;
                        pscan.Atmosphere.AtmosphereComponent3Id  = scandata.Atmosphere.Component3Id != 0 ? (byte?)scandata.Atmosphere.Component3Id : null;
                        pscan.Atmosphere.AtmosphereComponent3Amt = scandata.Atmosphere.Component3Amt;
                    }

                    paramAtmosUpdateScanId.Value   = pscan.Atmosphere.Id;
                    paramAtmosUpdateTypeId.Value   = (object)pscan.Atmosphere.AtmosphereTypeId ?? DBNull.Value;
                    paramAtmosUpdateComp1Id.Value  = (object)pscan.Atmosphere.AtmosphereComponent1Id ?? DBNull.Value;
                    paramAtmosUpdateComp1Amt.Value = pscan.Atmosphere.AtmosphereComponent1Amt;
                    paramAtmosUpdateComp2Id.Value  = (object)pscan.Atmosphere.AtmosphereComponent2Id ?? DBNull.Value;
                    paramAtmosUpdateComp2Amt.Value = pscan.Atmosphere.AtmosphereComponent2Amt;
                    paramAtmosUpdateComp3Id.Value  = (object)pscan.Atmosphere.AtmosphereComponent3Id ?? DBNull.Value;
                    paramAtmosUpdateComp3Amt.Value = pscan.Atmosphere.AtmosphereComponent3Amt;
                    cmdUpdateAtmos.ExecuteNonQuery();
                }

                if (pscan.Materials == null && scandata.Planet.HasMaterials)
                {
                    pscan.Materials           = new BodyScanMaterials(scandata.Materials, pscan.Id);
                    paramMatsId.Value         = pscan.Materials.Id;
                    paramMatsCarbon.Value     = pscan.Materials.MaterialCarbon;
                    paramMatsIron.Value       = pscan.Materials.MaterialIron;
                    paramMatsNickel.Value     = pscan.Materials.MaterialNickel;
                    paramMatsPhosphorus.Value = pscan.Materials.MaterialPhosphorus;
                    paramMatsSulphur.Value    = pscan.Materials.MaterialSulphur;
                    paramMat1Id.Value         = pscan.Materials.Material1Id;
                    paramMat1Amt.Value        = pscan.Materials.Material1Amt;
                    paramMat2Id.Value         = pscan.Materials.Material2Id;
                    paramMat2Amt.Value        = pscan.Materials.Material2Amt;
                    paramMat3Id.Value         = pscan.Materials.Material3Id;
                    paramMat3Amt.Value        = pscan.Materials.Material3Amt;
                    paramMat4Id.Value         = pscan.Materials.Material4Id;
                    paramMat4Amt.Value        = pscan.Materials.Material4Amt;
                    paramMat5Id.Value         = pscan.Materials.Material5Id;
                    paramMat5Amt.Value        = pscan.Materials.Material5Amt;
                    paramMat6Id.Value         = pscan.Materials.Material6Id;
                    paramMat6Amt.Value        = pscan.Materials.Material6Amt;
                    cmdInsertMats.ExecuteNonQuery();
                }
            }