private bool DoAdd() { bool result = false; Model.forum_board model = new Model.forum_board(); BLL.forum_board bll = new BLL.forum_board(); model.parent_id = int.Parse(ddlParentId.SelectedValue); model.is_lock = int.Parse(rblStatus.SelectedValue); model.boardname = txtBoardName.Text; model.img_url = txtImgUrl.Text; model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99); model.content = txtContent.Text; model.moderator_list = txtModeratorList.Text; for (int i = 0; i < cblAllowUserGroupID.Items.Count; i++) { if (cblAllowUserGroupID.Items[i].Selected) { model.allow_usergroupid_list += cblAllowUserGroupID.Items[i].Value + ","; } } model.allow_usergroupid_list = model.allow_usergroupid_list.TrimEnd(','); if (bll.Add(model) > 0) { AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加论坛版块:" + model.boardname); //记录日志 result = true; } return(result); }
/// <summary> /// 将对象转换为实体 /// </summary> public Model.forum_board DataRowToModel(DataRow row) { Model.forum_board model = new Model.forum_board(); if (row != null) { if (row["id"] != null && row["id"].ToString() != "") { model.id = int.Parse(row["id"].ToString()); } if (row["boardname"] != null) { model.boardname = row["boardname"].ToString(); } if (row["parent_id"] != null && row["parent_id"].ToString() != "") { model.parent_id = int.Parse(row["parent_id"].ToString()); } if (row["class_list"] != null) { model.class_list = row["class_list"].ToString(); } if (row["class_layer"] != null && row["class_layer"].ToString() != "") { model.class_layer = int.Parse(row["class_layer"].ToString()); } if (row["sort_id"] != null && row["sort_id"].ToString() != "") { model.sort_id = int.Parse(row["sort_id"].ToString()); } if (row["img_url"] != null) { model.img_url = row["img_url"].ToString(); } if (row["content"] != null) { model.content = row["content"].ToString(); } if (row["is_lock"] != null && row["is_lock"].ToString() != "") { model.is_lock = int.Parse(row["is_lock"].ToString()); } if (row["allow_usergroupid_list"] != null) { model.allow_usergroupid_list = row["allow_usergroupid_list"].ToString(); } if (row["moderator_list"] != null) { model.moderator_list = row["moderator_list"].ToString(); } if (row["subject_count"] != null && row["subject_count"].ToString() != "") { model.subject_count = int.Parse(row["subject_count"].ToString()); } if (row["post_count"] != null && row["post_count"].ToString() != "") { model.post_count = int.Parse(row["post_count"].ToString()); } } return model; }
private void ShowInfo(int _id) { BLL.forum_board bll = new BLL.forum_board(); Model.forum_board model = bll.GetModel(_id); ddlParentId.SelectedValue = model.parent_id.ToString(); rblStatus.SelectedValue = model.is_lock.ToString(); txtBoardName.Text = model.boardname; txtImgUrl.Text = model.img_url; txtSortId.Text = model.sort_id.ToString(); txtContent.Text = model.content; txtModeratorList.Text = model.moderator_list; //赋值授予权限 if (model.allow_usergroupid_list != null) { for (int i = 0; i < cblAllowUserGroupID.Items.Count; i++) { string[] allowusergroupid = model.allow_usergroupid_list.Split(','); foreach (string id in allowusergroupid) { if (id != "" && id == cblAllowUserGroupID.Items[i].Value) { cblAllowUserGroupID.Items[i].Selected = true; } } } } }
/// <summary> /// 得到一个对象实体(重载,带事务) /// </summary> public Model.forum_board GetModel(SqlConnection conn, SqlTransaction trans, int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,boardname,parent_id,class_list,class_layer,sort_id,img_url,content,is_lock,allow_usergroupid_list,moderator_list,subject_count,post_count"); strSql.Append(" from " + databaseprefix + "forum_board "); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = id; Model.forum_board model = new Model.forum_board(); DataSet ds = DbHelperSQL.Query(conn, trans, strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }
//批量移动 protected void ddlMoveId_SelectedIndexChanged(object sender, EventArgs e) { ChkAdminLevel("plugin_forum_post", DTEnums.ActionEnum.Edit.ToString()); //检查权限 int sucCount = 0; //成功数量 int postcount = 0; int replycount = 0; if (ddlMoveId.SelectedValue == "") { ddlMoveId.SelectedIndex = 0; JscriptMsg("请选择要移动的类别!", string.Empty); return; } BLL.forum_posts bll = new BLL.forum_posts(); BLL.forum_board bbll = new BLL.forum_board(); Model.forum_board bmodel = new Model.forum_board(); Repeater rptList = new Repeater(); rptList = this.rptList1; for (int i = 0; i < rptList.Items.Count; i++) { int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value); int oldboardid = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidBid")).Value); CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId"); if (cb.Checked) { DataTable dt = bll.GetList(0, "id=" + id + " or parent_post_id=" + id, "id desc").Tables[0]; foreach (DataRow dr in dt.Rows) { if (int.Parse(dr["parent_post_id"].ToString()) == 0) { postcount += 1; replycount += 1; } else { replycount += 1; } sucCount++; bll.UpdateField(int.Parse(dr["id"].ToString()), "board_id=" + ddlMoveId.SelectedValue); } bmodel = bbll.GetModel(oldboardid); bmodel.subject_count -= postcount; bmodel.post_count -= replycount; bbll.Update(bmodel); bmodel = bbll.GetModel(int.Parse(ddlMoveId.SelectedValue)); bmodel.subject_count += postcount; bmodel.post_count += replycount; bbll.Update(bmodel); } } AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "批量移动频道内容分类"); //记录日志 JscriptMsg("批量移动成功" + sucCount + "条", Utils.CombUrlTxt("post_list.aspx", "board_id={0}&keywords={1}&property={2}", this.board_id.ToString(), this.keywords, this.property)); }
/// <summary> /// 修改子节点的ID列表及深度(自身迭代) /// </summary> /// <param name="parent_id"></param> private void UpdateChilds(SqlConnection conn, SqlTransaction trans, int parent_id) { //查找父节点信息 Model.forum_board model = GetModel(conn, trans, parent_id); if (model != null) { //查找子节点 string strSql = "select id from " + databaseprefix + "forum_board where parent_id=" + parent_id; DataSet ds = DbHelperSQL.Query(conn, trans, strSql); //带事务 foreach (DataRow dr in ds.Tables[0].Rows) { //修改子节点的ID列表及深度 int id = int.Parse(dr["id"].ToString()); string class_list = model.class_list + id + ","; int class_layer = model.class_layer + 1; DbHelperSQL.ExecuteSql(conn, trans, "update " + databaseprefix + "forum_board set class_list='" + class_list + "', class_layer=" + class_layer + " where id=" + id); //带事务 //调用自身迭代 this.UpdateChilds(conn, trans, id); //带事务 } } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.forum_board model) { return(dal.Update(model)); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.forum_board model) { return(dal.Add(model)); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.forum_board model) { using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into " + databaseprefix + "forum_board("); strSql.Append("boardname,parent_id,class_list,class_layer,sort_id,img_url,content,is_lock,allow_usergroupid_list,moderator_list,subject_count,post_count)"); strSql.Append(" values ("); strSql.Append("@boardname,@parent_id,@class_list,@class_layer,@sort_id,@img_url,@content,@is_lock,@allow_usergroupid_list,@moderator_list,@subject_count,@post_count)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@boardname", SqlDbType.NVarChar,100), new SqlParameter("@parent_id", SqlDbType.Int,4), new SqlParameter("@class_list", SqlDbType.NVarChar,500), new SqlParameter("@class_layer", SqlDbType.Int,4), new SqlParameter("@sort_id", SqlDbType.Int,4), new SqlParameter("@img_url", SqlDbType.NVarChar,255), new SqlParameter("@content", SqlDbType.NText), new SqlParameter("@is_lock", SqlDbType.Int,4), new SqlParameter("@allow_usergroupid_list", SqlDbType.NVarChar,255), new SqlParameter("@moderator_list", SqlDbType.NVarChar,255), new SqlParameter("@subject_count", SqlDbType.Int,4), new SqlParameter("@post_count", SqlDbType.Int,4)}; parameters[0].Value = model.boardname; parameters[1].Value = model.parent_id; parameters[2].Value = model.class_list; parameters[3].Value = model.class_layer; parameters[4].Value = model.sort_id; parameters[5].Value = model.img_url; parameters[6].Value = model.content; parameters[7].Value = model.is_lock; parameters[8].Value = model.allow_usergroupid_list; parameters[9].Value = model.moderator_list; parameters[10].Value = model.subject_count; parameters[11].Value = model.post_count; object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务 model.id = Convert.ToInt32(obj); if (model.parent_id > 0) { Model.forum_board model2 = GetModel(conn, trans, model.parent_id); //带事务 model.class_list = model2.class_list + model.id + ","; model.class_layer = model2.class_layer + 1; } else { model.class_list = "," + model.id + ","; model.class_layer = 1; } //修改节点列表和深度 DbHelperSQL.ExecuteSql(conn, trans, "update " + databaseprefix + "forum_board set class_list='" + model.class_list + "', class_layer=" + model.class_layer + " where id=" + model.id); //带事务 trans.Commit(); } catch { trans.Rollback(); return 0; } } } return model.id; }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.forum_board model) { using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { //先判断选中的父节点是否被包含 if (IsContainNode(model.id, model.parent_id)) { //查找旧数据 Model.forum_board oldModel = GetModel(model.id); //查找旧父节点数据 string class_list = "," + model.parent_id + ","; int class_layer = 1; if (oldModel.parent_id > 0) { Model.forum_board oldParentModel = GetModel(conn, trans, oldModel.parent_id); //带事务 class_list = oldParentModel.class_list + model.parent_id + ","; class_layer = oldParentModel.class_layer + 1; } //先提升选中的父节点 DbHelperSQL.ExecuteSql(conn, trans, "update " + databaseprefix + "forum_board set parent_id=" + oldModel.parent_id + ",class_list='" + class_list + "', class_layer=" + class_layer + " where id=" + model.parent_id); //带事务 UpdateChilds(conn, trans, model.parent_id); //带事务 } //更新子节点 if (model.parent_id > 0) { Model.forum_board model2 = GetModel(conn, trans, model.parent_id); //带事务 model.class_list = model2.class_list + model.id + ","; model.class_layer = model2.class_layer + 1; } else { model.class_list = "," + model.id + ","; model.class_layer = 1; } StringBuilder strSql = new StringBuilder(); strSql.Append("update " + databaseprefix + "forum_board set "); strSql.Append("boardname=@boardname,"); strSql.Append("parent_id=@parent_id,"); strSql.Append("class_list=@class_list,"); strSql.Append("class_layer=@class_layer,"); strSql.Append("sort_id=@sort_id,"); strSql.Append("img_url=@img_url,"); strSql.Append("content=@content,"); strSql.Append("is_lock=@is_lock,"); strSql.Append("allow_usergroupid_list=@allow_usergroupid_list,"); strSql.Append("moderator_list=@moderator_list,"); strSql.Append("subject_count=@subject_count,"); strSql.Append("post_count=@post_count"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@boardname", SqlDbType.NVarChar,100), new SqlParameter("@parent_id", SqlDbType.Int,4), new SqlParameter("@class_list", SqlDbType.NVarChar,500), new SqlParameter("@class_layer", SqlDbType.Int,4), new SqlParameter("@sort_id", SqlDbType.Int,4), new SqlParameter("@img_url", SqlDbType.NVarChar,255), new SqlParameter("@content", SqlDbType.NText), new SqlParameter("@is_lock", SqlDbType.Int,4), new SqlParameter("@allow_usergroupid_list", SqlDbType.NVarChar,255), new SqlParameter("@moderator_list", SqlDbType.NVarChar,255), new SqlParameter("@subject_count", SqlDbType.Int,4), new SqlParameter("@post_count", SqlDbType.Int,4), new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = model.boardname; parameters[1].Value = model.parent_id; parameters[2].Value = model.class_list; parameters[3].Value = model.class_layer; parameters[4].Value = model.sort_id; parameters[5].Value = model.img_url; parameters[6].Value = model.content; parameters[7].Value = model.is_lock; parameters[8].Value = model.allow_usergroupid_list; parameters[9].Value = model.moderator_list; parameters[10].Value = model.subject_count; parameters[11].Value = model.post_count; parameters[12].Value = model.id; DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters); //更新子节点 UpdateChilds(conn, trans, model.id); trans.Commit(); } catch { trans.Rollback(); return false; } } } return true; }