/// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(Course_UnitDetail model)
        {
            //新增默认信息
            model.CreateDate = DateTime.Now;
            model.Delflag = false;

            return dal.Add(model) > 0;
        }
        public ActionResult AddCourseUnit(Course_UnitDetail model)
        {
            Course_UnitDetailBLL bll = new Course_UnitDetailBLL();

            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 int Add(Course_UnitDetail model)
 {
     StringBuilder sql = new StringBuilder();
     sql.Append("insert into [dbo].[Course_UnitDetail] ([TrainingId],[Title],[Content],[ParentId],[Display],[Sort],[Delflag],[CreateDate])");
     sql.Append(" values (@TrainingId,@Title,@Content,@ParentId,@Display,@Sort,@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("@Title", SqlDbType.VarChar, 200) { Value = model.Title },
         new SqlParameter("@Content", SqlDbType.VarChar, 8000) { Value = model.Content },
         new SqlParameter("@ParentId", SqlDbType.Int, 4) { Value = model.ParentId },
         new SqlParameter("@Display", SqlDbType.Bit, 1) { Value = model.Display },
         new SqlParameter("@Sort", SqlDbType.Int, 4) { Value = model.Sort },
         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;
 }
        /// <summary>
        /// 找到当前[活动小节]的最外层[章]
        /// </summary>
        /// <param name="iId"></param>
        /// <param name="iTrainingId"></param>
        /// <returns></returns>
        private Course_UnitDetail GetCourse_UnitDetail(int iId, int? iTrainingId)
        {
            var unitBll = new Course_UnitDetailBLL();
            var model = new Course_UnitDetail();

            model = unitBll.GetModel(iId, "TrainingId = " + iTrainingId.Value);

            if (model != null)
            {
                if (model.ParentId != 0)
                {
                    return GetCourse_UnitDetail(model.ParentId.Value, iTrainingId);
                }
                else
                {
                    return model;
                }
            }
            else
            {
                return null;
            }
        }
 /// <summary>
 /// 更新一条记录
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public bool Update(Course_UnitDetail model)
 {
     //更改默认信息
     model.CreateDate = DateTime.Now;
     model.Delflag = false;
     return dal.Update(model) > 0;
 }
 private void ConvertToModel(IDataReader reader, Course_UnitDetail 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["Title"] != DBNull.Value)
         model.Title = reader["Title"].ToString();
     if (reader["Content"] != DBNull.Value)
         model.Content = reader["Content"].ToString();
     if (reader["ParentId"] != DBNull.Value)
         model.ParentId = Convert.ToInt32(reader["ParentId"]);
     if (reader["Display"] != DBNull.Value)
         model.Display = Convert.ToBoolean(reader["Display"]);
     if (reader["Sort"] != DBNull.Value)
         model.Sort = Convert.ToInt32(reader["Sort"]);
     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_UnitDetail model)
 {
     StringBuilder sql = new StringBuilder();
     sql.Append("update [dbo].[Course_UnitDetail] set ");
     sql.Append("[TrainingId]=@TrainingId,[Title]=@Title,[Content]=@Content,[ParentId]=@ParentId,[Display]=@Display,[Sort]=@Sort,[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("@Title", SqlDbType.VarChar, 200) { Value = model.Title },
         new SqlParameter("@Content", SqlDbType.VarChar, 8000) { Value = model.Content },
         new SqlParameter("@ParentId", SqlDbType.Int, 4) { Value = model.ParentId },
         new SqlParameter("@Display", SqlDbType.Bit, 1) { Value = model.Display },
         new SqlParameter("@Sort", SqlDbType.Int, 4) { Value = model.Sort },
         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="id"></param>
 /// <param name="where"></param>
 /// <returns></returns>
 public Course_UnitDetail GetModel(int id, string where)
 {
     string sql = "select * from [dbo].[Course_UnitDetail] 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_UnitDetail model = new Course_UnitDetail();
             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_UnitDetail> 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_UnitDetail]");
     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_UnitDetail]");
     if (!string.IsNullOrEmpty(where))
         sql.Append(" where " + where);
     sql.Append(") as T where [RowNum] between " + start + " and " + end);
     List<Course_UnitDetail> list = new List<Course_UnitDetail>();
     using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql.ToString()))
     {
         while (reader.Read())
         {
             Course_UnitDetail model = new Course_UnitDetail();
             ConvertToModel(reader, model);
             list.Add(model);
         }
     }
     return list;
 }
 /// <summary>
 /// 获取数据集
 /// </summary>
 /// <param name="where"></param>
 /// <param name="orderBy"></param>
 /// <returns></returns>
 public List<Course_UnitDetail> GetList(string where, string orderBy)
 {
     StringBuilder sql = new StringBuilder();
     sql.Append("select * from [dbo].[Course_UnitDetail]");
     if (!string.IsNullOrEmpty(where))
         sql.Append(" where " + where);
     if (!string.IsNullOrEmpty(orderBy))
         sql.Append(" order by " + orderBy);
     List<Course_UnitDetail> list = new List<Course_UnitDetail>();
     using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql.ToString()))
     {
         while (reader.Read())
         {
             Course_UnitDetail model = new Course_UnitDetail();
             ConvertToModel(reader, model);
             list.Add(model);
         }
     }
     return list;
 }