public void AuditSomeoneInvestigationRecord(Guid recordId, string AuditorName)
 {
     using (NutritionalResearchDatabaseEntities mydb = new NutritionalResearchDatabaseEntities())
     {
         var record = mydb.InvestigationRecord.Where(nObj => nObj.Id == recordId).SingleOrDefault();
         if (record == null)
         {
             throw new ArgumentException("无效记录Id");
         }
         if ((InvestigationRecordStateType)record.State != InvestigationRecordStateType.FinishedAndNoAudit)
         {
             throw new InvalidOperationException("该记录状态无效,不能完成审核");
         }
         record.AuditorName = AuditorName;
         record.AuditTime   = DateTime.Now;
         record.State       = (int)InvestigationRecordStateType.FinishedAndAudited;
         try
         {
             mydb.SaveChanges();
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }
 public Guid CreateNewInvestigationRecord(NewInvestigationRecordDto newrecord)
 {
     if (newrecord == null)
     {
         throw new ArgumentNullException("记录参数不能为空!");
     }
     using (NutritionalResearchDatabaseEntities mydb = new NutritionalResearchDatabaseEntities())
     {
         var queneId = mydb.InvestigationRecord.Where(nObj => nObj.QueueId == newrecord.QueueId).FirstOrDefault();
         if (queneId != null)
         {
             throw new ArgumentException("输入的队列编码重复");
         }
         int _stage = 1;
         if (newrecord.Week < 13)
         {
             _stage = 1;
         }
         else if (newrecord.Week >= 13 && newrecord.Week <= 28)
         {
             _stage = 2;
         }
         else
         {
             _stage = 3;
         }
         InvestigationRecord record = new InvestigationRecord()
         {
             Id               = Guid.NewGuid(),
             BeforeWeight     = newrecord.BeforeWeight,
             BeforeBMI        = 10000 * newrecord.BeforeWeight / (newrecord.Height * newrecord.Height),
             Birthday         = newrecord.Birthday,
             CreationTime     = DateTime.Now,
             CurrentWeight    = newrecord.CurrentWeight,
             HealthBookId     = newrecord.HealthBookId,
             Height           = newrecord.Height,
             InvestigatorName = newrecord.InvestigatorName,
             Name             = newrecord.Name,
             QueueId          = newrecord.QueueId,
             Stage            = _stage,
             State            = (int)InvestigationRecordStateType.NoFinish,
             Week             = newrecord.Week
         };
         mydb.InvestigationRecord.Add(record);
         try
         {
             mydb.SaveChanges();
             return(record.Id);
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }
 public void AddOrUpdateSomeoneInvestigationAnswer(InvestigationAnswerInputDto answer)
 {
     if (answer == null)
     {
         throw new ArgumentNullException("答案参数不能为空!");
     }
     using (NutritionalResearchDatabaseEntities mydb = new NutritionalResearchDatabaseEntities())
     {
         var record = mydb.InvestigationRecord.Where(nObj => nObj.Id == answer.InvestigationRecordId).SingleOrDefault();
         if (record == null)
         {
             throw new ArgumentException("无效的调查记录!");
         }
         if (record.State == (int)InvestigationRecordStateType.FinishedAndAudited)
         {
             throw new InvalidOperationException("不能修改已审核的调查记录答案!");
         }
         var _answer = (from nObj in mydb.InvestigationAnswer
                        where nObj.InvestigationRecordId == answer.InvestigationRecordId &&
                        nObj.QuestionId == answer.QuestionId
                        select nObj).SingleOrDefault();
         if (_answer == null)
         {
             InvestigationAnswer _newAnswer = new InvestigationAnswer()
             {
                 AnswerType            = (int)answer.Answer_Type,
                 AnswerValue1          = answer.AnswerValue1,
                 AnswerValue2          = answer.AnswerValue2,
                 CreationTime          = DateTime.Now,
                 Id                    = Guid.NewGuid(),
                 InvestigationRecordId = answer.InvestigationRecordId,
                 QuestionId            = answer.QuestionId,
                 QuestionSerialNumber  = answer.QuestionSerialNumber,
                 QuestionType          = (int)answer.Question_Type
             };
             mydb.InvestigationAnswer.Add(_newAnswer);
         }
         else
         {
             _answer.AnswerType   = (int)answer.Answer_Type;
             _answer.AnswerValue1 = answer.AnswerValue1;
             _answer.AnswerValue2 = answer.AnswerValue2;
             _answer.UpdationTime = DateTime.Now;
         }
         try
         {
             mydb.SaveChanges();
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }
 public void FinishSomeoneInvestigationRecord(Guid recordId)
 {
     using (NutritionalResearchDatabaseEntities mydb = new NutritionalResearchDatabaseEntities())
     {
         var record = mydb.InvestigationRecord.Where(nObj => nObj.Id == recordId).SingleOrDefault();
         if (record == null)
         {
             throw new ArgumentException("无效记录Id");
         }
         if ((InvestigationRecordStateType)record.State == InvestigationRecordStateType.FinishedAndAudited)
         {
             throw new InvalidOperationException("该记录已审核,不能进行相关操作");
         }
         else if ((InvestigationRecordStateType)record.State == InvestigationRecordStateType.NoFinish)
         {
             record.State = (int)InvestigationRecordStateType.FinishedAndNoAudit;
         }
         //try
         //{
         //    mydb.SaveChanges();
         //}
         //catch (Exception ex)
         //{
         //    throw ex;
         //}
         GenerateOrUpdateReport(record, mydb);
         try
         {
             mydb.SaveChanges();
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }
        private void GenerateOrUpdateReport(InvestigationRecord record, NutritionalResearchDatabaseEntities mydb)
        {
            Dictionary <FoodCategory, double> foodCategoryAverageDailyIntake = new Dictionary <FoodCategory, double>();

            //计算并生成膳食构成报告
            try
            {
                var foodGroup = mydb.FoodCategory
                                .Where(nObj => nObj.StatisticsCategoryCode != null)
                                .OrderBy(nObj => nObj.StatisticsCategoryCode)
                                .GroupBy(nObj => nObj.StatisticsCategoryCode)
                                .ToList();
                foodGroup.ForEach(item =>
                {
                    double totalAverageDailyIntake = 0;
                    item.ToList().ForEach(cate =>
                    {
                        //var _answer = (from nObj in mydb.Question
                        //               join nObj2 in record.InvestigationAnswer on nObj.Id equals nObj2.QuestionId
                        //               where nObj.CategoryId == cate.Id
                        //               select nObj2).SingleOrDefault();
                        var _answer = (from nObj in record.InvestigationAnswer
                                       join nObj2 in mydb.Question on nObj.QuestionId equals nObj2.Id
                                       where nObj2.CategoryId == cate.Id
                                       select nObj).SingleOrDefault();
                        if (_answer != null && _answer.AnswerValue1.HasValue && _answer.AnswerValue2.HasValue)
                        {
                            var _intake = ComputerAverageDailyIntake((AnswerType)_answer.AnswerType, _answer.AnswerValue1.Value, _answer.AnswerValue2.Value);
                            foodCategoryAverageDailyIntake.Add(cate, _intake);
                            totalAverageDailyIntake += _intake;
                        }
                    });
                    var _structure = mydb.StructureOfMeals.Where(nObj => nObj.RecordId == record.Id && nObj.StructureCode == item.Key).SingleOrDefault();
                    if (_structure == null)
                    {
                        StructureOfMeals structure = new StructureOfMeals()
                        {
                            Id                   = Guid.NewGuid(),
                            CreationTime         = DateTime.Now,
                            RecordId             = record.Id,
                            StructureCode        = item.Key,
                            StructureDescription = item.First().StatisticsCategoryName,
                            Unit                 = "g",
                            Intake               = totalAverageDailyIntake
                        };
                        mydb.StructureOfMeals.Add(structure);
                    }
                    else
                    {
                        _structure.Intake       = totalAverageDailyIntake;
                        _structure.UpdationTime = DateTime.Now;
                    }
                });
                mydb.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("计算并生成膳食构成报告出错!", ex);
            }
            //计算并生成营业元素摄入量报告
            try
            {
                Dictionary <Foods, double> foodAverageDailyIntake = new Dictionary <Foods, double>();
                foreach (var item in foodCategoryAverageDailyIntake)
                {
                    foreach (var food in item.Key.Foods)
                    {
                        foodAverageDailyIntake.Add(food, item.Value * food.Proportion);
                    }
                }
                mydb.NuritiveElement.ToList().ForEach(item =>
                {
                    double nutrtiveElementIntake = 0;
                    foreach (var item2 in foodAverageDailyIntake)
                    {
                        nutrtiveElementIntake += ComputerNutrtiveElementIntake(item.Id, item2.Key, item2.Value);
                    }
                    double?_RNI_AI   = null;
                    double?_RNIRatio = null;
                    if (item.Stage1StandardValue != null && item.Stage2StandardValue != null && item.Stage3StandardValue != null)
                    {
                        if (record.Stage == 1)
                        {
                            _RNI_AI = item.Stage1StandardValue.Value;
                        }
                        else if (record.Stage == 2)
                        {
                            _RNI_AI = item.Stage2StandardValue.Value;
                        }
                        else
                        {
                            _RNI_AI = item.Stage3StandardValue.Value;
                        }
                        _RNIRatio = 100 * nutrtiveElementIntake / _RNI_AI.Value;
                    }
                    var _statistics = mydb.NutrtiveElementIntakeStatistics.Where(nObj => nObj.RecordId == record.Id && nObj.NutritiveElementId == item.Id).SingleOrDefault();
                    if (_statistics == null)
                    {
                        NutrtiveElementIntakeStatistics statistics = new NutrtiveElementIntakeStatistics()
                        {
                            Id                 = Guid.NewGuid(),
                            CreationTime       = DateTime.Now,
                            RecordId           = record.Id,
                            NutritiveElementId = item.Id,
                            IntakeValue        = nutrtiveElementIntake,
                            RNI_AIValue        = _RNI_AI,
                            RNIRatio           = _RNIRatio
                        };
                        mydb.NutrtiveElementIntakeStatistics.Add(statistics);
                    }
                    else
                    {
                        _statistics.IntakeValue  = nutrtiveElementIntake;
                        _statistics.RNI_AIValue  = _RNI_AI;
                        _statistics.RNIRatio     = _RNIRatio;
                        _statistics.UpdationTime = DateTime.Now;
                    }
                });
                mydb.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("计算并生成营业元素摄入量报告出错!", ex);
            }
        }