public Boolean Edit(ExaminationPaper model)
        {
            try
            {
                olsEni.Entry(model).State = EntityState.Modified;
                olsEni.SaveChanges();

                return true;
            }
            catch (Exception ex)
            {
                StaticHelper.RecordSystemLog(ex);
                return false;
            }
        }
        public Boolean Create(ExaminationPaper model)
        {
            try
            {

                Int32 id;

                id = GetEPId();

                model.EP_Id = id;
                olsEni.ExaminationPapers.Add(model);
                olsEni.SaveChanges();

                return true;
            }
            catch (Exception ex)
            {
                StaticHelper.RecordSystemLog(ex);
                return false;
            }
        }
        public ExaminationPaper GetNew(Int32 userId)
        {
            ExaminationPaper model;
            User u;

            u = olsEni.Users.Single(m =>
                m.U_Id == userId
                && m.U_Status == (Byte)Status.Available);

            model = new ExaminationPaper()
            {
                EP_Id = 0,
                EP_UserId = userId,
                EP_UserName = u.U_Name,
                EP_Remark = "",
                EP_AddTime = DateTime.Now,
                EP_Status = (Byte)Status.Available
            };

            return model;
        }
        /// <summary>
        /// 计算成绩
        /// </summary>
        /// <param name="ep"></param>
        public String GradePaper(ExaminationPaper ep)
        {
            Int32 score, number, eptqCount;
            Double ratio;
            ExaminationPaperTemplateQuestion eptq;
            ExaminationTask et;
            List<ExaminationPaperQuestion> epqs;
            String scoreString;

            score = 0;
            number = 0;
            scoreString = "";

            epqs = olsEni.ExaminationPaperQuestions.Where(m => m.EP_Id == ep.EP_Id).ToList();
            foreach (var epq in epqs)
            {

                eptq = olsEni.ExaminationPaperTemplateQuestions.Single(m => m.EPTQ_Id == epq.EPTQ_Id);
                switch (eptq.EPTQ_Type)
                {
                    case "单选题":
                    case "多选题":
                    case "判断题":

                        if (eptq.EPTQ_ModelAnswer == epq.EPQ_Answer)
                        {
                            epq.EPQ_Exactness = (Byte)AnswerStatus.Exactness;
                            score += eptq.EPTQ_Score;
                            number += 1;
                        }
                        else
                        {
                            epq.EPQ_Exactness = (Byte)AnswerStatus.Wrong;
                        }
                        break;
                    default:
                        break;
                }
            }

            // 如果试题只有单选题、多选题、判断题,则自动设置分数
            eptqCount =
                olsEni
                .ExaminationPaperTemplateQuestions
                .Where(m =>
                    m.EPT_Id == ep.EPT_Id
                    && m.EPTQ_Type != "单选题"
                    && m.EPTQ_Type != "多选题"
                    && m.EPTQ_Type != "判断题"
                    && m.EPTQ_Status == (Byte)Status.Available)
                .Count();
            et = olsEni.ExaminationTasks.Single(m => m.ET_Id == ep.ET_Id);

            if ((Byte)StatisticType.Score == et.ET_StatisticType && eptqCount == 0)
            {
                ep.EP_Score = score;
                olsEni.Entry(ep).State = EntityState.Modified;
                scoreString = score + "分";
            }
            else if ((Byte)StatisticType.Number == et.ET_StatisticType && eptqCount == 0)
            {
                ratio = Math.Round((Double)number / (Double)et.ET_TotalNumber, 2, MidpointRounding.AwayFromZero);
                ep.EP_Score = (Int32)(ratio * 100);
                olsEni.Entry(ep).State = EntityState.Modified;
                scoreString = score + "%";
            }

            return scoreString;
        }
        private ResponseJson addExaminationPaper(ExaminationPaperTemplate ept, Int32 uId)
        {
            Int32 epId, epqId;
            ExaminationPaper ep;
            ResponseJson resJson;
            List<ExaminationPaperTemplateQuestion> eptqs;

            resJson = new ResponseJson(ResponseStatus.Error, now);

            // 添加试卷
            epId = GetEPId();

            ep = new ExaminationPaper
            {
                EP_Id = epId,
                ET_Id = ept.ET_Id,
                EPT_Id = ept.EPT_Id,
                EP_PaperStatus = (Byte)PaperStatus.Doing,
                EP_EndTime = now.AddMinutes(ept.EPT_TimeSpan), // 进入考试时开始计算考试时间
                EP_TimeSpan = ept.EPT_TimeSpan,
                EP_UserId = uId,
                EP_UserName = "",
                EP_Score = -1,
                EP_Remark = "",
                EP_AddTime = now,
                EP_Status = (Byte)Status.Available
            };

            // 添加试卷试题数据
            eptqs =
                olsEni.ExaminationPaperTemplateQuestions
                .Where(m =>
                    m.EPT_Id == ept.EPT_Id
                    && m.EPTQ_Status == (Byte)Status.Available)
                .ToList();
            epqId = GetEPQId();
            foreach (var eptq in eptqs)
            {
                var epq = new ExaminationPaperQuestion
                {
                    EPQ_Id = epqId,
                    EP_Id = epId,
                    EPTQ_Id = eptq.EPTQ_Id,
                    EPQ_Answer = "",
                    EPQ_Exactness = 0,
                    EPQ_Critique = "",
                    EPQ_AddTime = now
                };
                olsEni.Entry(epq).State = EntityState.Added;
                epqId += 1;
            }

            olsEni.Entry(ep).State = EntityState.Added;
            if (0 == olsEni.SaveChanges())
            {
                resJson.message = ResponseMessage.SaveChangesError;
                return resJson;
            }

            resJson.status = ResponseStatus.Success;
            resJson.data = epId;
            return resJson;
        }
        public void ChangeTest()
        {
            Int32 id, epId, epqId;
            DateTime now, startTime;
            ExaminationTask et;
            ExaminationPaperTemplate ept;
            UExaminationTask uet;
            GeneratePaperTemplate_Accessor gpt = new GeneratePaperTemplate_Accessor();
            ChangePaperStatus target = new ChangePaperStatus();
            ResponseJson resJson;
            ExaminationPaper ep;
            ExaminationPaperQuestion epq;
            Object expected, actual;
            List<ExaminationPaperTemplateQuestion> eptqs;

            #region 部署测试数据

            now = DateTime.Now;
            uet = new UExaminationTask();

            startTime = new DateTime(1970, 1, 1, now.AddHours(1).Hour, now.Minute, 0);
            id = new Utility().GetETId();
            et = new ExaminationTask
            {
                ET_Id = id,
                ET_Name = "单元测试每日任务" + id,
                ET_Enabled = (Byte)ExaminationTaskStatus.Enabled,
                ET_Type = (Byte)ExaminationTaskType.Examination,
                ET_ParticipatingDepartment = "[6,9]",
                ET_Attendee = "[6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,39,40,41,42,43,44,45]",
                ET_StatisticType = (Byte)StatisticType.Score,
                ET_TotalScore = 100,
                ET_TotalNumber = 10,
                ET_Mode = (Byte)ExaminationTaskMode.Auto,
                ET_AutoType = (Byte)AutoType.Day,
                ET_AutoOffsetDay = 0,
                ET_DifficultyCoefficient = 0,
                ET_AutoClassifies = "[\"综合、公文、绩效知识(90题)\",\"所得税知识(180题)\",\"营业税知识(60题)\",\"其他地方税知识(180题)\",\"税收征管法、相关法律法规及征管制度(253题)\",\"规费知识(130题)\",\"纳税服务知识(95题)\"]",
                ET_AutoRatio = "[{\"type\":\"单选题\",\"percent\":0.2},{\"type\":\"多选题\",\"percent\":0.2},{\"type\":\"判断题\",\"percent\":0.2},{\"type\":\"公文改错题\",\"percent\":0},{\"type\":\"计算题\",\"percent\":0},{\"type\":\"案例分析题\",\"percent\":0},{\"type\":\"问答题\",\"percent\":0}]",
                ET_StartTime = startTime,
                ET_EndTime = startTime,
                ET_TimeSpan = 0,
                ET_PaperTemplates = "[]",
                ET_Questions = "[]",
                ET_Remark = "",
                ET_AddTime = now,
                ET_Status = (Byte)Status.Available
            };
            olsEni.Entry(et).State = EntityState.Added;

            if (0 == olsEni.SaveChanges())
            {
                Assert.Fail("部署测试数据失败1。");
            }

            // 生成试卷模板数据
            uet = new UExaminationTask();
            resJson = gpt.Generate();
            if (ResponseStatus.Error == resJson.status || resJson.message != "")
            {
                Assert.Fail("部署测试数据失败3。" + resJson.message);
            }

            Thread.Sleep(10 * 1000);

            ept = olsEni.ExaminationPaperTemplates.SingleOrDefault(m => m.ET_Id == id);
            if (null == ept)
            {
                Assert.Fail("部署测试数据失败4。");
            }

            ept.EPT_PaperTemplateStatus = (Byte)PaperTemplateStatus.Done;

            if (0 == olsEni.SaveChanges())
            {
                Assert.Fail("部署测试数据失败2。");
            }

            // 添加试卷
            epId = new Utility().GetEPId();
            ep = new ExaminationPaper
            {
                EP_Id = epId,
                ET_Id = ept.ET_Id,
                EPT_Id = ept.EPT_Id,
                EP_PaperStatus = (Byte)PaperStatus.Doing,
                EP_EndTime = ept.EPT_EndTime,
                EP_TimeSpan = ept.EPT_TimeSpan,
                EP_UserId = 1,
                EP_UserName = "",
                EP_Score = -1,
                EP_Remark = "",
                EP_AddTime = now,
                EP_Status = (Byte)Status.Available
            };

            olsEni.Entry(ep).State = EntityState.Added;
            olsEni.SaveChanges();

            // 添加答题数据
            epqId = new Utility().GetEPQId();
            eptqs = olsEni.ExaminationPaperTemplateQuestions.Where(m => m.EPT_Id == ept.EPT_Id).ToList();

            foreach (var eptq in eptqs)
            {

                epq = new ExaminationPaperQuestion
                {
                    EPQ_Id = epqId,
                    EPQ_Answer = eptq.EPTQ_ModelAnswer,
                    EPQ_Exactness = (Byte)AnswerStatus.Unset,
                    EPQ_Critique = null,
                    EPQ_AddTime = now,
                    EP_Id = epId,
                    EPTQ_Id = eptq.EPTQ_Id
                };
                olsEni.Entry(epq).State = EntityState.Added;

                epqId += 1;
            }
            olsEni.SaveChanges();
            #endregion

            expected = ResponseStatus.Success;
            actual = target.Change();

            Assert.AreEqual(expected, ((ResponseJson)actual).status);

            olsEni.Entry(ep).State = EntityState.Detached;
            ep = null;

            ep = olsEni.ExaminationPapers.Single(m => m.EP_Id == epId);

            expected = (Byte)PaperStatus.Done;
            actual = ep.EP_PaperStatus;

            Assert.AreEqual(expected, actual);

            expected = 100;
            actual = ep.EP_Score;

            Assert.AreEqual(expected, actual);
        }