Пример #1
0
        public void Write(System.Data.IDataReader reader)
        {
            var organization = db.Organizations.Find(organizationId);

            Dictionary <string, KitMaterial>        materials          = new Dictionary <string, KitMaterial>();
            Dictionary <string, KitOption>          options            = new Dictionary <string, KitOption>();
            Dictionary <int, ValveInterfaceCode>    valveInterfaces    = new Dictionary <int, ValveInterfaceCode>();
            Dictionary <int, ActuatorInterfaceCode> actuatorInterfaces = new Dictionary <int, ActuatorInterfaceCode>();

            while (reader.Read())
            {
                // columns
                // KitNumber, ValveIntCode, ActIntCode, MaterialCode, OptionCode, Price
                string kitNumber = reader.GetString(0);
                var    kit       = db.Kits.Where(k => k.KitNumber == kitNumber).FirstOrDefault();

                if (kit == null)
                {
                    // get the material
                    string materialCode = reader.GetString(3);
                    var    material     = db.KitMaterials.Where(m => m.Code == materialCode).FirstOrDefault();

                    if (material == null)
                    {
                        if (materials.ContainsKey(materialCode))
                        {
                            material = materials[materialCode];
                        }
                        else
                        {
                            material = new KitMaterial()
                            {
                                Code = materialCode,
                                Name = materialCode
                            };
                            db.KitMaterials.Add(material);
                            materials.Add(materialCode, material);
                        }
                    }

                    // get the locking option
                    string optionCode = reader.GetString(4);
                    var    option     = db.KitOptions.Where(o => o.Code == optionCode).FirstOrDefault();

                    if (option == null)
                    {
                        if (options.ContainsKey(optionCode))
                        {
                            option = options[optionCode];
                        }
                        else
                        {
                            option = new KitOption()
                            {
                                Code = optionCode,
                                Name = optionCode
                            };
                            db.KitOptions.Add(option);
                            options.Add(optionCode, option);
                        }
                    }

                    // get the valve interface
                    int valveIntCode   = reader.GetInt32(1);
                    var valveInterface = db.ValveInterfaceCodes.Find(valveIntCode);

                    if (valveInterface == null)
                    {
                        if (valveInterfaces.ContainsKey(valveIntCode))
                        {
                            valveInterface = valveInterfaces[valveIntCode];
                        }
                        else
                        {
                            valveInterface = new ValveInterfaceCode()
                            {
                                InterfaceCode = valveIntCode
                            };
                            db.ValveInterfaceCodes.Add(valveInterface);
                            valveInterfaces.Add(valveIntCode, valveInterface);
                        }
                    }

                    // get the actuator interace
                    int actIntCode   = reader.GetInt32(2);
                    var actInterface = db.ActuatorInterfaceCodes.Find(actIntCode);

                    if (actInterface == null)
                    {
                        if (actuatorInterfaces.ContainsKey(actIntCode))
                        {
                            actInterface = actuatorInterfaces[actIntCode];
                        }
                        else
                        {
                            actInterface = new ActuatorInterfaceCode()
                            {
                                InterfaceCode = actIntCode
                            };
                            db.ActuatorInterfaceCodes.Add(actInterface);
                            actuatorInterfaces.Add(actIntCode, actInterface);
                        }
                    }

                    // add the kit
                    double price = reader.GetDouble(5);

                    kit = new Kit()
                    {
                        KitNumber = reader.GetString(0),
                        ValuveIntefaceCodeEntity    = valveInterface,
                        ActuatorInterfaceCodeEntity = actInterface,
                        Price    = price,
                        Material = material,
                        Option   = option
                    };

                    db.Kits.Add(kit);
                }

                // link this kit to the organization
                if (kit.KitId > 0)
                {
                    if (!organization.Kits.Where(k => k.KitId == kit.KitId).Any())
                    {
                        organization.Kits.Add(kit);
                    }
                }
                else
                {
                    organization.Kits.Add(kit);
                }
            }
        }
Пример #2
0
        public void Write(IDataReader reader)
        {
            var organization = db.Organizations.Find(organizationId);

            HashSet <string> map = new HashSet <string>();

            while (reader.Read())
            {
                // columns
                // mfg, model, size, ActuatorIntCode

                // get the actuator interface
                int    actuatorIntCode = reader.GetInt32(3);
                string mfg             = reader.GetString(0);
                string model           = reader.GetString(1);
                string size            = reader.GetString(2);

                var actuatorInterface = db.ActuatorInterfaceCodes.Find(actuatorIntCode);
                if (actuatorInterface == null)
                {
                    actuatorInterface = new ActuatorInterfaceCode()
                    {
                        InterfaceCode = actuatorIntCode
                    };
                    db.ActuatorInterfaceCodes.Add(actuatorInterface);
                }

                var actuator = db.Actuators
                               .Where(v => v.Manufacturer == mfg)
                               .Where(v => v.Model == model)
                               .Where(v => v.Size == size).FirstOrDefault();

                if (actuator == null)
                {
                    // check local cash
                    if (!map.Contains($"{mfg} {model} {size}"))
                    {
                        map.Add($"{mfg} {model} {size}");
                        actuator = new Actuator()
                        {
                            Manufacturer  = reader.GetString(0),
                            Model         = reader.GetString(1),
                            Size          = reader.GetString(2),
                            InterfaceCode = actuatorIntCode
                        };
                        db.Actuators.Add(actuator);
                        organization.Actuators.Add(actuator);
                    }
                }

                // link this actuator to the organization
                if (actuator != null)
                {
                    if (actuator.ActuatorId > 0)
                    {
                        if (!organization.Actuators.Where(v => v.ActuatorId == actuator.ActuatorId).Any())
                        {
                            organization.Actuators.Add(actuator);
                        }
                    }
                }
            }
        }