예제 #1
0
        public void CreateSoilSampleNutrient(SoilSampleNutrientModel model)
        {
            using (IDbConnection connection = new SqlConnection(GlobalConfig.CnnString(db)))
            {
                var p = new DynamicParameters();
                p.Add("@SoilSampleId", model.SoilSampleId);
                p.Add("@NutrientId", model.NutrientId);
                p.Add("@Amount", model.Amount);
                p.Add("@Goal", model.Goal);
                p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);

                connection.Execute("dbo.spSoilSamplesNutrients_Insert", p, commandType: CommandType.StoredProcedure);
            }
        }
예제 #2
0
        private SoilSampleNutrientModel CreateSampleNutrientRecord(List <string> sample, string key)
        {
            int index = NutrientMappings[key];
            SoilSampleNutrientModel newNutrientRecord = new SoilSampleNutrientModel();

            newNutrientRecord.Amount         = null;
            newNutrientRecord.Recommendation = null;

            newNutrientRecord.NutrientId = Convert.ToInt32(key);

            NutrientModel nutrient = GlobalConfig.Connection.GetNutrient_ById(newNutrientRecord.NutrientId);


            if (sample[index] != "NULL")
            {
                newNutrientRecord.Amount = Convert.ToDecimal(sample[index]);

                string nutrientUnit = Units.Where <UnitModel>(unit => unit.Id == nutrient.UnitId).Select(unit => unit.Unit).First();

                //Makes sure it's a quantity that needs to be converted
                if (nutrientUnit != "None")
                {
                    newNutrientRecord.Amount *= SoilSampleNutrientModel.PPMConversionFactor;
                }
            }

            Regex recommendationRegex   = new Regex($"^{key}\\sRec\\s\\d");
            var   recommendationColumns = RecommendationMappings
                                          .Where(pair => recommendationRegex.IsMatch(pair.Key))
                                          .Select(pair => pair.Value);

            if (HasRecommendations(recommendationColumns))
            {
                decimal totalRec = recommendationColumns
                                   .Where(i => decimal.TryParse(sample[i], out decimal recommendation))
                                   .Select(i => decimal.Parse(sample[i]))
                                   .Sum();
                if (NotAllNull(sample, recommendationColumns))
                {
                    newNutrientRecord.Recommendation = totalRec;
                }
            }

            return(newNutrientRecord);
        }
예제 #3
0
        private void ProcessSample(List <string> sampleRow)
        {
            SoilSampleModel newSampleModel = new SoilSampleModel
            {
                FieldId    = Convert.ToInt32(sampleRow[FieldIndex]),
                SampleYear = Convert.ToInt32(sampleRow[YearIndex])
            };

            foreach (var key in NutrientMappings.Keys)
            {
                SoilSampleNutrientModel newNutrientRecord = CreateSampleNutrientRecord(sampleRow, key);
                newSampleModel.Nutrients.Add(newNutrientRecord);
            }
            SoilSampleData sampleData = new SoilSampleData();

            sampleData.Sample = newSampleModel;
            sampleData.WriteSoilSample();
            Samples.Add(newSampleModel);
        }
예제 #4
0
        public void CreateFieldNutrient(SoilSampleNutrientModel model, int fieldId, int lastSampledYear)
        {
            using (IDbConnection connection = new SqlConnection(GlobalConfig.CnnString(db)))
            {
                var p = new DynamicParameters();
                p.Add("@FieldId", fieldId);
                p.Add("@NutrientId", model.NutrientId);
                p.Add("@Amount", model.Amount);
                p.Add("@Goal", model.Goal);
                p.Add("@SampleYear", lastSampledYear);
                p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);

                connection.Execute("dbo.spFieldsNutrients_Upsert", p, commandType: CommandType.StoredProcedure);

                p = new DynamicParameters();
                p.Add("@FieldId", fieldId);
                p.Add("@NutrientId", model.NutrientId);
                p.Add("@NewAmount", model.Amount);
                p.Add("@SoilSampleId", model.SoilSampleId);
                p.Add("@id", model.Id, dbType: DbType.Int32, direction: ParameterDirection.Output);

                connection.Execute("dbo.spLedger_Update", p, commandType: CommandType.StoredProcedure);
            }
        }