/// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(Course_UnitTest model)
        {
            if (model.Question == null)
            {
                model.Question = "";
            }

            model.Verson = "-1";

            model.Display = true;
            model.Delflag = false;
            model.CreateDate = DateTime.Now;

            return dal.Add(model) > 0;
        }
        /// <summary>
        /// 获取指定课程的结业考试试题信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Course_UnitTest> GetExamQuesInfo(int id)
        {
            string sql = @"select * from dbo.Course_UnitTest where TrainingId=@TrainingId and Display = 1 and Delflag = 0";
            SqlParameter[] cmdParams = new SqlParameter[]{
                new SqlParameter("@TrainingId", SqlDbType.Int, 4) { Value = id }
            };

            List<Course_UnitTest> list = new List<Course_UnitTest>();
            using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql, cmdParams))
            {
                while (reader.Read())
                {
                    Course_UnitTest model = new Course_UnitTest();
                    ConvertToModel(reader, model);
                    list.Add(model);
                }
            }

            return list;
        }
 /// <summary>
 /// 新增一条记录
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public int Add(Course_UnitTest model)
 {
     StringBuilder sql = new StringBuilder();
     sql.Append("insert into [dbo].[Course_UnitTest] ([TrainingId],[Verson],[Content],[QTtype],[Question],[Answer],[Credit],[Display],[Delflag],[CreateDate])");
     sql.Append(" values (@TrainingId,@Verson,@Content,@QTtype,@Question,@Answer,@Credit,@Display,@Delflag,@CreateDate)");
     sql.Append(" set @Id=@@IDENTITY");
     SqlParameter[] cmdParams = new SqlParameter[]{
         new SqlParameter("@Id", SqlDbType.Int, 4) { Value = model.Id, Direction = ParameterDirection.Output },
         new SqlParameter("@TrainingId", SqlDbType.Int, 4) { Value = model.TrainingId },
         new SqlParameter("@Verson", SqlDbType.VarChar, 20) { Value = model.Verson },
         new SqlParameter("@Content", SqlDbType.VarChar, 2000) { Value = model.Content },
         new SqlParameter("@QTtype", SqlDbType.Int, 4) { Value = model.QTtype },
         new SqlParameter("@Question", SqlDbType.VarChar, 8000) { Value = model.Question },
         new SqlParameter("@Answer", SqlDbType.VarChar, 100) { Value = model.Answer },
         new SqlParameter("@Credit", SqlDbType.Float, 8) { Value = model.Credit },
         new SqlParameter("@Display", SqlDbType.Bit, 1) { Value = model.Display },
         new SqlParameter("@Delflag", SqlDbType.Bit, 1) { Value = model.Delflag },
         new SqlParameter("@CreateDate", SqlDbType.DateTime, 8) { Value = model.CreateDate }
     };
     int result = Convert.ToInt32(MSEntLibSqlHelper.ExecuteNonQueryBySql(sql.ToString(), cmdParams));
     model.Id = Convert.ToInt32(cmdParams[0].Value);
     return result;
 }
        public ActionResult ExemExeclTemp(int TrainingId)
        {
            Course_UnitTestBLL course_UnitTestBLL = new Course_UnitTestBLL();
            string msg = "";
            HttpPostedFileBase excel = Request.Files[0];
            string fileName = Server.MapPath(Code.UploadCore.UploadAttach(excel, ref msg));
            int GroupId = Code.SiteCache.Instance.GroupId;
            int OrganId = Code.SiteCache.Instance.ManageOrganId;

            DataTable dt = ReadExcel(fileName);
            if (dt.Columns[0].ColumnName.ToString() != "类型" || dt.Columns[1].ColumnName.ToString() != "题目内容" || dt.Columns[2].ColumnName.ToString() != "答案内容" || dt.Columns[3].ColumnName.ToString() != "正确答案序号" || dt.Columns[4].ColumnName.ToString() != "权重")
            {
                return Content("no:导入的格式不正确!");
            }
            else
            {
                List<Course_UnitTest> list = new List<Course_UnitTest>();
                if (dt.Rows.Count == 0)
                {
                    return Content("no:无导入数据!");
                }
                else
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        try
                        {
                            for (int k = 0; k < dt.Columns.Count; k++)
                            {
                                if (string.IsNullOrEmpty(dt.Rows[i][k].ToString()))
                                {
                                    return Content("no:导入的数据有空值!");
                                }
                            }

                            Course_UnitTest model = new Course_UnitTest();
                            model.TrainingId = TrainingId;
                            model.Verson = "-1";
                            if (dt.Rows[i][2].ToString().Trim().IndexOf(',') > 0)
                            {
                                string[] Questions = dt.Rows[i][2].ToString().Trim().Split(',');
                                model.Question = "[";
                                for (int c = 0; c < Questions.Length; c++)
                                {
                                    model.Question += "{Id:" + (c + 1) + ",Content:'" + Questions[c] + "'},";
                                }

                                model.Question = model.Question.TrimEnd(',') + "]";
                            }
                            else
                            {
                                model.Question = dt.Rows[i][2].ToString().Trim();
                            }

                            model.Answer = dt.Rows[i][3].ToString().Trim();

                            switch (dt.Rows[i][0].ToString().Trim())
                            {
                                case "单选题":
                                    model.QTtype = 1;
                                    break;
                                case "多选题":
                                    model.QTtype = 2;
                                    break;
                                case "判断题":
                                    model.QTtype = 3;
                                    model.Question = "[{Id:1,Content:'1'},{Id:2,Content:'0'}]";
                                    break;
                                case "问答题":
                                    model.QTtype = 4;
                                    model.Question = "";
                                    model.Answer = "";
                                    break;
                                default:
                                    break;
                            }
                            model.Content = dt.Rows[i][1].ToString().Trim();

                            if (Regex.IsMatch(dt.Rows[i][4].ToString(), @"^[+-]?\d*[.]?\d*$"))
                            {
                                model.Credit = Convert.ToDouble(dt.Rows[i][4]);
                            }
                            else
                            {
                                return Content("no:权重必须是数字类型!");
                            }

                            if (dt.Rows[i][4].ToString().Length > 2)
                            {
                                return Content("no:权重数字长度不能超过五位数!");
                            }

                            if (dt.Rows[i][4].ToString() == "0")
                            {
                                return Content("no:第" + (i + 1) + "行的权重不能为0!");
                            }

                            model.Display = true;
                            model.Delflag = false;
                            model.CreateDate = DateTime.Now;
                            list.Add(model);
                        }
                        catch (Exception ex)
                        {
                            return Content("no:" + ex);
                        }
                    }

                    int count = course_UnitTestBLL.AddExem(list);
                    if (count > 0)
                    {
                        return Content("yes:成功导入" + list.Count + "行数据");
                    }
                    else
                    {
                        return Content("no:导入失败");
                    }

                }
            }
        }
        public ActionResult ExamExeclTemp(int TrainingId)
        {
            string msg = "";
            HttpPostedFileBase excel = Request.Files[0];
            string fileName = Server.MapPath(Code.UploadCore.UploadAttach(excel, ref msg));
            int GroupId = Code.SiteCache.Instance.GroupId;
            int OrganId = Code.SiteCache.Instance.ManageOrganId;

            DataTable dt = ReadExcel(fileName);
            if (dt.Columns[0].ColumnName.ToString() != "类型" || dt.Columns[1].ColumnName.ToString() != "题目内容" || dt.Columns[2].ColumnName.ToString() != "答案内容" || dt.Columns[3].ColumnName.ToString() != "正确答案序号" || dt.Columns[4].ColumnName.ToString() != "权重")
            {
                return Content("no:导入的格式不正确!!!");
            }
            else
            {
                List<Course_UnitTest> list = new List<Course_UnitTest>();
                if (dt.Rows.Count == 0)
                {
                    return Content("no:无导入数据!!!");
                }
                else
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        try
                        {
                            for (int k = 0; k < dt.Columns.Count; k++)
                            {
                                if (string.IsNullOrEmpty(dt.Rows[i][k].ToString()))
                                {
                                    return Content("no:导入的数据有空值!!!");
                                }
                            }

                            Course_UnitTest model = new Course_UnitTest();
                            model.TrainingId = TrainingId;
                            switch (dt.Rows[i][0].ToString().Trim())
                            {
                                case "单选题":
                                    model.QTtype = 1;
                                    break;
                                case "多选题":
                                    model.QTtype = 2;
                                    break;
                                case "判断题":
                                    model.QTtype = 3;
                                    break;
                                case "问答题":
                                    model.QTtype = 4;
                                    break;
                                default:
                                    break;
                            }
                            model.Content = dt.Rows[i][1].ToString().Trim();
                            model.Question = dt.Rows[i][2].ToString().Trim();
                            model.Answer = dt.Rows[i][3].ToString().Trim();

                            if (Regex.IsMatch(dt.Rows[i][4].ToString(), @"^[+-]?\d*[.]?\d*$"))
                            {
                                model.Credit = Convert.ToDouble(dt.Rows[i][4]);
                            }
                            else
                            {
                                return Content("no:权重必须是数字类型!");
                            }

                            if (dt.Rows[i][4].ToString().Length > 2)
                            {
                                return Content("no:权重数字长度不能超过五位数!");
                            }

                            if (dt.Rows[i][4].ToString() == "0")
                            {
                                return Content("no:第" + (i + 1) + "行的权重不能为0!");
                            }

                            model.Display = true;
                            model.Delflag = false;
                            model.CreateDate = DateTime.Now;
                            list.Add(model);
                        }
                        catch (Exception ex)
                        {
                            return Content("no:" + ex);
                        }
                    }

                    //int count = bll.AddExem(list);
                    //if (count > 0)
                    //{
                    //    return Content("yes:成功导入" + count + "行数据");
                    //}
                    //else
                    //{
                    //    return Content("no:导入失败");
                    //}

                    return Content("no:导入成功!");
                }
            }
        }
        public ActionResult AddEaxmQues(Course_UnitTest model)
        {
            Course_UnitTestBLL bll = new Course_UnitTestBLL();

            if (bll.Add(model))
            {
                return Json(new { Result = true, Msg = "新增成功!" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Result = false, Msg = "新增失败!" }, JsonRequestBehavior.AllowGet);
            }
        }
 /// <summary>
 /// 更新一条记录
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public bool Update(Course_UnitTest model)
 {
     return dal.Update(model) > 0;
 }
 private void ConvertToModel(IDataReader reader, Course_UnitTest model)
 {
     if (reader["Id"] != DBNull.Value)
         model.Id = Convert.ToInt32(reader["Id"]);
     if (reader["TrainingId"] != DBNull.Value)
         model.TrainingId = Convert.ToInt32(reader["TrainingId"]);
     if (reader["Verson"] != DBNull.Value)
         model.Verson = reader["Verson"].ToString();
     if (reader["Content"] != DBNull.Value)
         model.Content = reader["Content"].ToString();
     if (reader["QTtype"] != DBNull.Value)
         model.QTtype = Convert.ToInt32(reader["QTtype"]);
     if (reader["Question"] != DBNull.Value)
         model.Question = reader["Question"].ToString();
     if (reader["Answer"] != DBNull.Value)
         model.Answer = reader["Answer"].ToString();
     if (reader["Credit"] != DBNull.Value)
         model.Credit = Convert.ToDouble(reader["Credit"]);
     if (reader["Display"] != DBNull.Value)
         model.Display = Convert.ToBoolean(reader["Display"]);
     if (reader["Delflag"] != DBNull.Value)
         model.Delflag = Convert.ToBoolean(reader["Delflag"]);
     if (reader["CreateDate"] != DBNull.Value)
         model.CreateDate = Convert.ToDateTime(reader["CreateDate"]);
 }
 /// <summary>
 /// 更新一条记录
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public int Update(Course_UnitTest model)
 {
     StringBuilder sql = new StringBuilder();
     sql.Append("update [dbo].[Course_UnitTest] set ");
     sql.Append("[TrainingId]=@TrainingId,[Verson]=@Verson,[Content]=@Content,[QTtype]=@QTtype,[Question]=@Question,[Answer]=@Answer,[Credit]=@Credit,[Display]=@Display,[Delflag]=@Delflag,[CreateDate]=@CreateDate");
     sql.Append(" where [Id]=@Id");
     SqlParameter[] cmdParams = new SqlParameter[] {
         new SqlParameter("@Id", SqlDbType.Int, 4) { Value = model.Id },
         new SqlParameter("@TrainingId", SqlDbType.Int, 4) { Value = model.TrainingId },
         new SqlParameter("@Verson", SqlDbType.VarChar, 20) { Value = model.Verson },
         new SqlParameter("@Content", SqlDbType.VarChar, 2000) { Value = model.Content },
         new SqlParameter("@QTtype", SqlDbType.Int, 4) { Value = model.QTtype },
         new SqlParameter("@Question", SqlDbType.VarChar, 8000) { Value = model.Question },
         new SqlParameter("@Answer", SqlDbType.VarChar, 100) { Value = model.Answer },
         new SqlParameter("@Credit", SqlDbType.Float, 8) { Value = model.Credit },
         new SqlParameter("@Display", SqlDbType.Bit, 1) { Value = model.Display },
         new SqlParameter("@Delflag", SqlDbType.Bit, 1) { Value = model.Delflag },
         new SqlParameter("@CreateDate", SqlDbType.DateTime, 8) { Value = model.CreateDate }
     };
     return MSEntLibSqlHelper.ExecuteNonQueryBySql(sql.ToString(), cmdParams);
 }
        /// <summary>
        /// 更新指定课程结业考试试题版本号
        /// </summary>
        /// <param name="TrainingId"></param>
        /// <param name="Verson"></param>
        /// <returns></returns>
        public int SetVerson(int TrainingId, string Verson)
        {
            int result = -1;

            //查询指定课程下的结业考试试题信息
            string sql = @"select * from [dbo].[Course_UnitTest] where TrainingId=@TrainingId and Display = 1 and Delflag = 0";
            SqlParameter[] cmdParams = new SqlParameter[]{
                new SqlParameter("@TrainingId", SqlDbType.Int, 4) { Value = TrainingId }
            };

            List<Course_UnitTest> list = new List<Course_UnitTest>();
            using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql, cmdParams))
            {
                while (reader.Read())
                {
                    Course_UnitTest model1 = new Course_UnitTest();
                    ConvertToModel(reader, model1);
                    model1.Verson = Verson;
                    list.Add(model1);
                }
            }

            //将指定课程下的结业考试试题display置为0
            StringBuilder sqlBuild = new StringBuilder();
            sqlBuild.Append("update [dbo].[Course_UnitTest] set display = 0 where TrainingId=@TrainingId and display = 1 and delflag = 0;");
            cmdParams = new SqlParameter[] {
                new SqlParameter("@TrainingId", SqlDbType.Int, 4) { Value = TrainingId }
            };
            result = MSEntLibSqlHelper.ExecuteNonQueryBySql(sqlBuild.ToString(), cmdParams);

            //新增新版本号的数据
            for (int i = 0; i < list.Count; i++)
            {
                sqlBuild = new StringBuilder();
                sqlBuild.Append("insert into [dbo].[Course_UnitTest] ([TrainingId],[Verson],[Content],[QTtype],[Question],[Answer],[Credit],[Display],[Delflag],[CreateDate])");
                sqlBuild.Append(" values (@TrainingId,@Verson,@Content,@QTtype,@Question,@Answer,@Credit,@Display,@Delflag,@CreateDate)");
                sqlBuild.Append(" set @Id=@@IDENTITY");
                cmdParams = new SqlParameter[]{
                new SqlParameter("@Id", SqlDbType.Int, 4) { Value = list[i].Id, Direction = ParameterDirection.Output },
                new SqlParameter("@TrainingId", SqlDbType.Int, 4) { Value = list[i].TrainingId },
                new SqlParameter("@Verson", SqlDbType.VarChar, 20) { Value = list[i].Verson },
                new SqlParameter("@Content", SqlDbType.VarChar, 2000) { Value = list[i].Content },
                new SqlParameter("@QTtype", SqlDbType.Int, 4) { Value = list[i].QTtype },
                new SqlParameter("@Question", SqlDbType.VarChar, 8000) { Value = list[i].Question },
                new SqlParameter("@Answer", SqlDbType.VarChar, 100) { Value = list[i].Answer },
                new SqlParameter("@Credit", SqlDbType.Float, 8) { Value = list[i].Credit },
                new SqlParameter("@Display", SqlDbType.Bit, 1) { Value = list[i].Display },
                new SqlParameter("@Delflag", SqlDbType.Bit, 1) { Value = list[i].Delflag },
                new SqlParameter("@CreateDate", SqlDbType.DateTime, 8) { Value = list[i].CreateDate }
                };
                result = Convert.ToInt32(MSEntLibSqlHelper.ExecuteNonQueryBySql(sqlBuild.ToString(), cmdParams));
            }

            return result;
        }
 /// <summary>
 /// 取得一条记录
 /// </summary>
 /// <param name="id"></param>
 /// <param name="where"></param>
 /// <returns></returns>
 public Course_UnitTest GetModel(int id, string where)
 {
     string sql = "select * from [dbo].[Course_UnitTest] where [Id]=@Id";
     if (!string.IsNullOrEmpty(where))
         sql += " and " + where;
     SqlParameter[] cmdParams = new SqlParameter[]{
         new SqlParameter("@Id", SqlDbType.Int, 4) { Value = id }
     };
     using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql, cmdParams))
     {
         if (reader.Read())
         {
             Course_UnitTest model = new Course_UnitTest();
             ConvertToModel(reader, model);
             return model;
         }
         else
         {
             return null;
         }
     }
 }
 /// <summary>
 /// 获取分页数据集
 /// </summary>
 /// <param name="pageSize"></param>
 /// <param name="pageIndex"></param>
 /// <param name="where"></param>
 /// <param name="orderBy"></param>
 /// <param name="recordCount"></param>
 /// <returns></returns>
 public List<Course_UnitTest> GetList(int pageSize, int pageIndex, string where, string orderBy, out int recordCount)
 {
     if (string.IsNullOrEmpty(orderBy))
         throw new ArgumentNullException();
     StringBuilder sb = new StringBuilder();
     sb.Append("select count(1) from [dbo].[Course_UnitTest]");
     if (!string.IsNullOrEmpty(where))
         sb.Append(" where " + where);
     recordCount = Convert.ToInt32(MSEntLibSqlHelper.ExecuteScalarBySql(sb.ToString()));
     int start = (pageIndex - 1) * pageSize + 1;
     int end = pageIndex * pageSize;
     StringBuilder sql = new StringBuilder();
     sql.Append("select * from (select *,ROW_NUMBER() over (order by " + orderBy + ") as [RowNum] from [dbo].[Course_UnitTest]");
     if (!string.IsNullOrEmpty(where))
         sql.Append(" where " + where);
     sql.Append(") as T where [RowNum] between " + start + " and " + end);
     List<Course_UnitTest> list = new List<Course_UnitTest>();
     using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql.ToString()))
     {
         while (reader.Read())
         {
             Course_UnitTest model = new Course_UnitTest();
             ConvertToModel(reader, model);
             list.Add(model);
         }
     }
     return list;
 }
 /// <summary>
 /// 获取数据集
 /// </summary>
 /// <param name="where"></param>
 /// <param name="orderBy"></param>
 /// <returns></returns>
 public List<Course_UnitTest> GetList(string where, string orderBy)
 {
     StringBuilder sql = new StringBuilder();
     sql.Append("select * from [dbo].[Course_UnitTest]");
     if (!string.IsNullOrEmpty(where))
         sql.Append(" where " + where);
     if (!string.IsNullOrEmpty(orderBy))
         sql.Append(" order by " + orderBy);
     List<Course_UnitTest> list = new List<Course_UnitTest>();
     using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql.ToString()))
     {
         while (reader.Read())
         {
             Course_UnitTest model = new Course_UnitTest();
             ConvertToModel(reader, model);
             list.Add(model);
         }
     }
     return list;
 }