private ChemicalConditionModel SetChemicalConditionModel(GroupChemicalPropertyAll itemProp, IList <string> temperatureReplace, IMaterialsContextUow context) { ChemicalConditionModel condModel = new ChemicalConditionModel(); condModel.RowId = itemProp.RowID; condModel.ValueId = itemProp.ValueID; condModel.HasMultipleTemperatures = false; condModel.Name = ""; if (itemProp.Condition != null && itemProp.Condition != "") { condModel.Name = ElsevierMaterialsMVC.BL.Helpers.TransformFunctions.RemoveLast(itemProp.Condition, ";"); } return(condModel); }
public IList <ChemicalPropertyModel> GetChemicalPropertiesData(int materialId, int subgroupId, int groupId, IMaterialsContextUow context) { IList <ChemicalPropertyModel> model = new List <ChemicalPropertyModel>(); var prop = context.PropertiesWithConvertedValues.AllAsNoTracking.Where(m => m.MaterialId == materialId && m.SubgroupId == subgroupId).Select(m => new { m.GroupId, m.PropertyId, m.ConvValue, m.ConvValueMax, m.ConvValueMin, m.DefaultUnit, m.DefaultUnitId, m.OriginalUnit, m.OrigUnitId, m.OrigValue, m.OrigValueMax, m.OrigValueMin, m.UsUnit, m.Temperature, m.UsValue, m.UsValueMax, m.UsValueMin, m.ValueId, m.RowId }); ChemicalPropertyModel item = new ChemicalPropertyModel(); IList <GroupChemicalPropertyAll> groupChemicalProperties = context.GroupChemicalPropertyAll.AllAsNoTracking.Where(m => m.MaterialID == materialId && m.SubgroupID == subgroupId && m.GroupId == groupId).OrderBy(m => m.PropertyID).ThenBy(m => m.Phase).ThenBy(m => m.X_unit_lbl).ThenBy(m => m.Condition).ToList(); IList <string> temperatureReplace = new List <string>(); //IList<int> propIds=groupChemicalProperties.Select(p=>p.PropertyID).Distinct().ToList(); //IList<string> phasesList = groupChemicalProperties.Select(p => p.Phase).Distinct().ToList(); //var propNames= context.PreferredNames.AllAsNoTracking.Where(p=>propIds.Contains(p.PN_ID) ).Select(p=>new {Key=p.PN_ID, Value=p.PN}); var condModel = new ChemicalConditionModel(); string prevPhase = ""; string prevPropName = ""; foreach (GroupChemicalPropertyAll itemProp in groupChemicalProperties) { var propData = prop.Where(p => p.PropertyId == itemProp.PropertyID && p.RowId == itemProp.RowID && p.GroupId == itemProp.GroupId && p.ValueId == itemProp.ValueID).FirstOrDefault(); if ((prevPhase == "" || prevPropName == "") || (prevPhase != itemProp.Phase || prevPropName != itemProp.PropertyName)) { item = new ChemicalPropertyModel(); item.GroupId = itemProp.GroupId; item.PropertyId = itemProp.PropertyID; //item.PropertyName = propNames.Where(p => p.Key == itemProp.PropertyID).Select(p => p.Value).FirstOrDefault(); item.Phase = itemProp.Phase; item.PhaseId = itemProp.PhaseId; item.PropertyName = itemProp.PropertyName; item.PNameAndPhase = itemProp.PropertyName + ", " + itemProp.Phase; if (item.PNameAndPhase.Trim().EndsWith(",")) { item.PNameAndPhase = ElsevierMaterialsMVC.BL.Helpers.TransformFunctions.RemoveLast(item.PNameAndPhase.Trim(), ","); } prevPhase = itemProp.Phase; prevPropName = itemProp.PropertyName; item.ConditionList = new List <ChemicalConditionModel>(); temperatureReplace = new List <string>(); item.DiagramID = itemProp.DiagramID; condModel = SetChemicalConditionModel(itemProp, temperatureReplace, context); if (itemProp.DiagramID != null) { condModel.PropertyUnits = itemProp.Y_unit_lbl; condModel.USPropertyUnits = itemProp.Y_unit_lbl; condModel.DefaultPropertyUnits = itemProp.Y_unit_lbl; condModel.VariableUnits = itemProp.X_unit_lbl; condModel.DefaultVariableUnits = itemProp.X_unit_lbl; condModel.USVariableUnits = itemProp.X_unit_lbl; condModel.Notes = itemProp.cit_record_id != null ? "note" : ""; condModel.cit_record_id = itemProp.cit_record_id; if (condModel.TemperatureList == null) { condModel.TemperatureList = new List <ChemicalConditionPointModel>(); } var diagPoints = context.DiagramPoints.AllAsNoTracking.Where(p => p.DiagramID == itemProp.DiagramID).OrderBy(p => p.PointOrder); foreach (var point in diagPoints) { condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)point.X_value, Y = (double)point.Y_value, type = ChemicalUnitValuesType.Origin }); condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)point.X_value, Y = (double)point.Y_value, type = ChemicalUnitValuesType.Default }); condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)point.X_value, Y = (double)point.Y_value, type = ChemicalUnitValuesType.Us }); } condModel.PropertyRange = itemProp.Y_range; condModel.VariableRange = itemProp.X_range; //condModel.VariableUnits = ""; if (condModel.TemperatureList != null && condModel.TemperatureList.Count > 1) { condModel.HasMultipleTemperatures = true; foreach (string s in temperatureReplace) { condModel.Name = condModel.Name.Replace(s, ""); } } } else { if (propData.OriginalUnit != null) { condModel.PropertyUnits = propData.OriginalUnit; } if (propData.UsUnit != null) { condModel.USPropertyUnits = propData.UsUnit; } if (propData.DefaultUnit != null) { condModel.DefaultPropertyUnits = propData.DefaultUnit; } if (itemProp.Temperature != null) { if (condModel.TemperatureList == null) { condModel.TemperatureList = new List <ChemicalConditionPointModel>(); } if (propData.OrigValue != null) { condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)itemProp.Temperature, Y = (double)propData.OrigValue, type = ChemicalUnitValuesType.Origin }); } if (propData.ConvValue != null) { condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)itemProp.Temperature, Y = (double)propData.ConvValue, type = ChemicalUnitValuesType.Default }); } if (propData.UsValue != null) { condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)itemProp.Temperature, Y = (double)propData.UsValue, type = ChemicalUnitValuesType.Us }); } } condModel.PropertyRange = propData.OrigValue == null ? "" : ((double)(propData.OrigValue)).ToString("0.####"); condModel.DefaultPropertyRange = propData.ConvValue == null ? "" : ((double)propData.ConvValue).ToString("0.####"); condModel.USPropertyRange = propData.UsValue == null ? "" : ((double)propData.UsValue).ToString("0.####"); condModel.DefaultVariableUnits = "°C"; condModel.VariableUnits = ""; condModel.Notes = ""; } item.ConditionList.Add(condModel); model.Add(item); } else { ChemicalPropertyModel property = model.Where(m => m.PropertyId == itemProp.PropertyID && m.Phase == itemProp.Phase).FirstOrDefault(); if (!property.ConditionList.Where(c => c.RowId == itemProp.RowID).Any()) { condModel = SetChemicalConditionModel(itemProp, temperatureReplace, context); property.ConditionList.Add(condModel); } var condModelPrev = new ChemicalConditionModel(); var condModelNew = new ChemicalConditionModel(); var newCondition = false; condModelPrev = property.ConditionList.Where(c => c.RowId == itemProp.RowID).FirstOrDefault(); condModelNew = SetChemicalConditionModel(itemProp, temperatureReplace, context); if (condModelPrev.Name != condModelNew.Name) { newCondition = true; condModel = condModelNew; } else { condModel = condModelPrev; } if (itemProp.DiagramID != null) { condModel.PropertyUnits = itemProp.Y_unit_lbl; condModel.USPropertyUnits = itemProp.Y_unit_lbl; condModel.DefaultPropertyUnits = itemProp.Y_unit_lbl; condModel.VariableUnits = itemProp.X_unit_lbl; condModel.DefaultVariableUnits = itemProp.X_unit_lbl; condModel.USVariableUnits = itemProp.X_unit_lbl; condModel.Notes = itemProp.cit_record_id != null ? "note" : ""; condModel.cit_record_id = itemProp.cit_record_id; if (condModel.TemperatureList == null) { condModel.TemperatureList = new List <ChemicalConditionPointModel>(); } var diagPoints = context.DiagramPoints.AllAsNoTracking.Where(p => p.DiagramID == itemProp.DiagramID).OrderBy(p => p.PointOrder); foreach (var point in diagPoints) { condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)point.X_value, Y = (double)point.Y_value, type = ChemicalUnitValuesType.Origin }); condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)point.X_value, Y = (double)point.Y_value, type = ChemicalUnitValuesType.Default }); condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)point.X_value, Y = (double)point.Y_value, type = ChemicalUnitValuesType.Us }); } condModel.PropertyRange = itemProp.Y_range; condModel.VariableRange = itemProp.X_range; if (condModel.TemperatureList != null && condModel.TemperatureList.Count > 1) { condModel.HasMultipleTemperatures = true; foreach (string s in temperatureReplace) { condModel.Name = condModel.Name.Replace(s, ""); } } } else { if (propData.OriginalUnit != null) { condModel.PropertyUnits = propData.OriginalUnit; } if (propData.UsUnit != null) { condModel.USPropertyUnits = propData.UsUnit; } if (propData.DefaultUnit != null) { condModel.DefaultPropertyUnits = propData.DefaultUnit; } if (itemProp.Temperature != null) { if (condModel.TemperatureList == null) { condModel.TemperatureList = new List <ChemicalConditionPointModel>(); } if (propData.OrigValue != null) { condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)itemProp.Temperature, Y = (double)propData.OrigValue, type = ChemicalUnitValuesType.Origin }); } if (propData.ConvValue != null) { condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)itemProp.Temperature, Y = (double)propData.ConvValue, type = ChemicalUnitValuesType.Default }); } if (propData.UsValue != null) { condModel.TemperatureList.Add(new ChemicalConditionPointModel() { X = (double)itemProp.Temperature, Y = (double)propData.UsValue, type = ChemicalUnitValuesType.Us }); } } condModel.PropertyRange = propData.OrigValue == null ? "" : ((double)(propData.OrigValue)).ToString("0.####"); condModel.DefaultPropertyRange = propData.ConvValue == null ? "" : ((double)propData.ConvValue).ToString("0.####"); condModel.USPropertyRange = propData.UsValue == null ? "" : ((double)propData.UsValue).ToString("0.####"); condModel.DefaultVariableUnits = "°C"; condModel.VariableUnits = ""; condModel.Notes = ""; if (condModel.TemperatureList != null && condModel.TemperatureList.Count > 1) { condModel.HasMultipleTemperatures = true; foreach (string s in temperatureReplace) { condModel.Name = condModel.Name.Replace(s, ""); } } } if (newCondition) { item.ConditionList.Add(condModel); } } } double propMin = double.MaxValue; double propMax = double.MinValue; double valMin = double.MaxValue; double valMax = double.MinValue; for (int i = 0; i < model.Count; i++) { for (int j = 0; j < model[i].ConditionList.Count; j++) { if (model[i].ConditionList[j].Name == "") { model[i].ConditionList[j].Name = "As received"; } if (model[i].ConditionList[j].TemperatureList != null && (model[i].ConditionList[j].TemperatureList.Where(p => p.type == ChemicalUnitValuesType.Origin).Count()) > 1) { model[i].ConditionList[j].TemperatureList = model[i].ConditionList[j].TemperatureList.OrderBy(p => p.type).ThenBy(p => p.X).ThenBy(P => P.Y).ToList(); foreach (var k in model[i].ConditionList[j].TemperatureList.Where(p => p.type == ChemicalUnitValuesType.Origin)) { if (valMin > k.X) { valMin = k.X; } if (valMax < k.X) { valMax = k.X; } if (propMin > k.Y) { propMin = k.Y; } if (propMax < k.Y) { propMax = k.Y; } } if (model[i].DiagramID == null) { model[i].ConditionList[j].VariableRange = valMin.ToString("0.####") + " — " + valMax.ToString("0.####"); model[i].ConditionList[j].PropertyRange = propMin.ToString("0.####") + " — " + propMax.ToString("0.####"); } model[i].ConditionList[j].VariableUnits = model[i].ConditionList[j].VariableUnits != ""?model[i].ConditionList[j].VariableUnits:"°C"; model[i].ConditionList[j].PropertyUnits = model[i].ConditionList[j].PropertyUnits; propMin = double.MaxValue; propMax = double.MinValue; foreach (var k in model[i].ConditionList[j].TemperatureList.Where(p => p.type == ChemicalUnitValuesType.Default)) { if (propMin > k.Y) { propMin = k.Y; } if (propMax < k.Y) { propMax = k.Y; } } model[i].ConditionList[j].DefaultPropertyRange = propMin.ToString("0.####") + " — " + propMax.ToString("0.####"); propMin = double.MaxValue; propMax = double.MinValue; foreach (var k in model[i].ConditionList[j].TemperatureList.Where(p => p.type == ChemicalUnitValuesType.Us)) { if (propMin > k.Y) { propMin = k.Y; } if (propMax < k.Y) { propMax = k.Y; } } model[i].ConditionList[j].USPropertyRange = propMin.ToString("0.####") + " — " + propMax.ToString("0.####"); } } } return(model); }