/// <summary>
 /// 获取数据集
 /// </summary>
 /// <param name="where"></param>
 /// <param name="orderBy"></param>
 /// <returns></returns>
 public List<Course_UnitReplyDetail> GetList(string where, string orderBy)
 {
     StringBuilder sql = new StringBuilder();
     sql.Append("select * from [dbo].[Course_UnitReplyDetail]");
     if (!string.IsNullOrEmpty(where))
         sql.Append(" where " + where);
     if (!string.IsNullOrEmpty(orderBy))
         sql.Append(" order by " + orderBy);
     List<Course_UnitReplyDetail> list = new List<Course_UnitReplyDetail>();
     using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql.ToString()))
     {
         while (reader.Read())
         {
             Course_UnitReplyDetail model = new Course_UnitReplyDetail();
             ConvertToModel(reader, model);
             list.Add(model);
         }
     }
     return list;
 }
 /// <summary>
 /// 新增一条记录
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public int Add(Course_UnitReplyDetail model)
 {
     StringBuilder sql = new StringBuilder();
     sql.Append("insert into [dbo].[Course_UnitReplyDetail] ([UnitContent],[ClassId],[Content],[AccountId],[ParentReplyId],[AttList],[Display],[Delflag],[CreateDate])");
     sql.Append(" values (@UnitContent,@ClassId,@Content,@AccountId,@ParentReplyId,@AttList,@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("@UnitContent", SqlDbType.Int, 4) { Value = model.UnitContent },
         new SqlParameter("@ClassId", SqlDbType.Int, 4) { Value = model.ClassId },
         new SqlParameter("@Content", SqlDbType.VarChar, 2000) { Value = model.Content },
         new SqlParameter("@AccountId", SqlDbType.Int, 4) { Value = model.AccountId },
         new SqlParameter("@ParentReplyId", SqlDbType.Int, 4) { Value = model.ParentReplyId },
         new SqlParameter("@AttList", SqlDbType.VarChar, 2000) { Value = model.AttList },
         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 JsonResult ReplyUnitContent(int uId, int classId, int accountId, int pId, string Content)
        {
            try
            {
                var bll = new Course_UnitReplyDetailBLL();
                var rootId = 0;
                rootId = GetRootId(pId);

                var model = new Course_UnitReplyDetail();
                model.UnitContent = uId;
                model.ClassId = classId;
                model.Content = Content;
                model.AccountId = accountId;
                model.Display = true;
                model.ParentReplyId = rootId;
                model.Delflag = false;
                model.CreateDate = DateTime.Now;
                bll.Add(model);
                return Json(new { Code = 0, Msg = "操作成功" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { Code = -1, Msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 讨论新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult EditCourseUnitReply(Course_UnitReplyDetail model)
        {
            //此处借用model.AttList传递TrainingId
            if (this.GetCourseIsOver(model.ClassId, int.Parse(model.AttList), model.AccountId))
                return Json(new { Result = false, Msg = "提交失败.当前课程已结束,不能执行该操作!" }, JsonRequestBehavior.AllowGet);

            model.AttList = string.Empty;

            var UnitContentBll = new Course_UnitContentBLL();
            var ReplyBll = new Course_UnitReplyDetailBLL();

            var Model_UnitContent = UnitContentBll.GetModel(model.UnitContent, string.Empty);
            var iUnitType = Model_UnitContent.UnitType;//[1文本,2影音教材,3讨论,4作业,5测试,6结业考试]

            if (model.Id == 0)//新增
            {
                model.Delflag = false;
                model.Display = true;
                model.CreateDate = DateTime.Now;

                if (iUnitType == 3)//仅当活动为讨论时,计分(视频下的讨论不再记分)
                    this.ScoreSet(2, 0, 0, 0, true, model);//设置分数并更新讨论的进度

                if (ReplyBll.Add(model))
                {
                    return Json(new { Result = true, Msg = "提交成功!" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { Result = true, Msg = "提交失败!" }, JsonRequestBehavior.AllowGet);
                }
            }
            else//[删除] 修改DelFlag = 1
            {
                if (model.ParentReplyId == 0)
                {
                    if (ReplyBll.Update(model.Id))
                    {
                        if (iUnitType == 3)//仅当活动为讨论时,计分(视频下的讨论不再记分)
                            this.ScoreSet(2, 0, 0, 0, false, model);//设置分数并更新讨论的进度

                        return Json(new { Result = true, Msg = "话题删除成功!" }, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return Json(new { Result = false, Msg = "话题删除失败!" }, JsonRequestBehavior.AllowGet);
                    }
                }
                else
                {
                    if (ReplyBll.Update(model.Id))
                    {
                        if (iUnitType == 3)//仅当活动为讨论时,计分(视频下的讨论不再记分)
                            this.ScoreSet(2, 0, 0, 0, false, model);//设置分数并更新讨论的进度

                        return Json(new { Result = true, Msg = "回复删除成功!" }, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return Json(new { Result = false, Msg = "回复删除失败!" }, JsonRequestBehavior.AllowGet);
                    }
                }
            }
        }
 /// <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_UnitReplyDetail> 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_UnitReplyDetail]");
     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_UnitReplyDetail]");
     if (!string.IsNullOrEmpty(where))
         sql.Append(" where " + where);
     sql.Append(") as T where [RowNum] between " + start + " and " + end);
     List<Course_UnitReplyDetail> list = new List<Course_UnitReplyDetail>();
     using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql.ToString()))
     {
         while (reader.Read())
         {
             Course_UnitReplyDetail model = new Course_UnitReplyDetail();
             ConvertToModel(reader, model);
             list.Add(model);
         }
     }
     return list;
 }
        private void ConvertToModelOther(IDataReader reader, Course_UnitReplyDetailOther modelOther)
        {
            if (reader["NickName"] != DBNull.Value)
                modelOther.NickName = Convert.ToString(reader["NickName"]);
            if (reader["Pic"] != DBNull.Value)
                modelOther.Pic = Convert.ToString(reader["Pic"]);
            if (reader["OrganDetailId"] != DBNull.Value)
                modelOther.OrganDetailId = Convert.ToInt32(reader["OrganDetailId"]);
            if (reader["OrganDetailTitle"] != DBNull.Value)
                modelOther.OrganDetailTitle = Convert.ToString(reader["OrganDetailTitle"]);

            var model = new Course_UnitReplyDetail();
            if (reader["Id"] != DBNull.Value)
                model.Id = Convert.ToInt32(reader["Id"]);
            if (reader["UnitContent"] != DBNull.Value)
                model.UnitContent = Convert.ToInt32(reader["UnitContent"]);
            if (reader["ClassId"] != DBNull.Value)
                model.ClassId = Convert.ToInt32(reader["ClassId"]);
            if (reader["Content"] != DBNull.Value)
                model.Content = reader["Content"].ToString();
            if (reader["AccountId"] != DBNull.Value)
                model.AccountId = Convert.ToInt32(reader["AccountId"]);
            if (reader["ParentReplyId"] != DBNull.Value)
                model.ParentReplyId = Convert.ToInt32(reader["ParentReplyId"]);
            if (reader["AttList"] != DBNull.Value)
                model.AttList = reader["AttList"].ToString();
            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"]);

            modelOther.CourseUnitReplyDetail = model;
        }
 private void ConvertToModel(IDataReader reader, Course_UnitReplyDetail model)
 {
     if (reader["Id"] != DBNull.Value)
         model.Id = Convert.ToInt32(reader["Id"]);
     if (reader["UnitContent"] != DBNull.Value)
         model.UnitContent = Convert.ToInt32(reader["UnitContent"]);
     if (reader["ClassId"] != DBNull.Value)
         model.ClassId = Convert.ToInt32(reader["ClassId"]);
     if (reader["Content"] != DBNull.Value)
         model.Content = reader["Content"].ToString();
     if (reader["AccountId"] != DBNull.Value)
         model.AccountId = Convert.ToInt32(reader["AccountId"]);
     if (reader["ParentReplyId"] != DBNull.Value)
         model.ParentReplyId = Convert.ToInt32(reader["ParentReplyId"]);
     if (reader["AttList"] != DBNull.Value)
         model.AttList = reader["AttList"].ToString();
     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_UnitReplyDetail model)
 {
     StringBuilder sql = new StringBuilder();
     sql.Append("update [dbo].[Course_UnitReplyDetail] set ");
     sql.Append("[UnitContent]=@UnitContent,[ClassId]=@ClassId,[Content]=@Content,[AccountId]=@AccountId,[ParentReplyId]=@ParentReplyId,[AttList]=@AttList,[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("@UnitContent", SqlDbType.Int, 4) { Value = model.UnitContent },
         new SqlParameter("@ClassId", SqlDbType.Int, 4) { Value = model.ClassId },
         new SqlParameter("@Content", SqlDbType.VarChar, 2000) { Value = model.Content },
         new SqlParameter("@AccountId", SqlDbType.Int, 4) { Value = model.AccountId },
         new SqlParameter("@ParentReplyId", SqlDbType.Int, 4) { Value = model.ParentReplyId },
         new SqlParameter("@AttList", SqlDbType.VarChar, 2000) { Value = model.AttList },
         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="id"></param>
 /// <param name="where"></param>
 /// <returns></returns>
 public Course_UnitReplyDetail GetModel(int id, string where)
 {
     string sql = "select * from [dbo].[Course_UnitReplyDetail] 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_UnitReplyDetail model = new Course_UnitReplyDetail();
             ConvertToModel(reader, model);
             return model;
         }
         else
         {
             return null;
         }
     }
 }
 /// <summary>
 /// 新增一条记录
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public bool Add(Course_UnitReplyDetail model)
 {
     return dal.Add(model) > 0;
 }
 /// <summary>
 /// 更新一条记录
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public bool Update(Course_UnitReplyDetail model)
 {
     return dal.Update(model) > 0;
 }