/// <summary> /// 快捷添加系统默认导航 /// </summary> public int Add(int parent_id, string nav_name, string title, string link_url, int sort_id, int channel_id, string action_type) { if (parent_id < 1) { return 0; } //开始赋值 var model = new NavigationInfo { nav_type = MXEnums.NavigationEnum.System.ToString(), name = nav_name, title = title, link_url = link_url }; if (sort_id > 0) { model.sort_id = sort_id; } model.parent_id = parent_id; if (channel_id > 0) { model.channel_id = channel_id; } model.action_type = action_type; model.is_sys = 1; return Add(model); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(NavigationInfo 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 + "navigation("); strSql.Append("nav_type,name,title,sub_title,link_url,sort_id,is_lock,remark,parent_id,class_list,class_layer,channel_id,action_type,is_sys)"); strSql.Append(" values ("); strSql.Append("@nav_type,@name,@title,@sub_title,@link_url,@sort_id,@is_lock,@remark,@parent_id,@class_list,@class_layer,@channel_id,@action_type,@is_sys)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@nav_type", SqlDbType.NVarChar,50), new SqlParameter("@name", SqlDbType.NVarChar,50), new SqlParameter("@title", SqlDbType.NVarChar,100), new SqlParameter("@sub_title", SqlDbType.NVarChar,100), new SqlParameter("@link_url", SqlDbType.NVarChar,255), new SqlParameter("@sort_id", SqlDbType.Int,4), new SqlParameter("@is_lock", SqlDbType.TinyInt,1), new SqlParameter("@remark", SqlDbType.NVarChar,500), new SqlParameter("@parent_id", SqlDbType.Int,4), new SqlParameter("@class_list", SqlDbType.NVarChar,500), new SqlParameter("@class_layer", SqlDbType.Int,4), new SqlParameter("@channel_id", SqlDbType.Int,4), new SqlParameter("@action_type", SqlDbType.NVarChar,500), new SqlParameter("@is_sys", SqlDbType.TinyInt,1)}; parameters[0].Value = model.nav_type; parameters[1].Value = model.name; parameters[2].Value = model.title; parameters[3].Value = model.sub_title; parameters[4].Value = model.link_url; parameters[5].Value = model.sort_id; parameters[6].Value = model.is_lock; parameters[7].Value = model.remark; parameters[8].Value = model.parent_id; parameters[9].Value = model.class_list; parameters[10].Value = model.class_layer; parameters[11].Value = model.channel_id; parameters[12].Value = model.action_type; parameters[13].Value = model.is_sys; object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务 model.id = Convert.ToInt32(obj); //得到刚插入的新ID if (model.parent_id > 0) { var 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 + "navigation 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 NavigationInfo GetModel(SqlConnection conn, SqlTransaction trans, int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,nav_type,name,title,sub_title,link_url,sort_id,is_lock,remark,parent_id,class_list,class_layer,channel_id,action_type,is_sys"); strSql.Append(" from " + databaseprefix + "navigation "); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = id; var model = new NavigationInfo(); DataSet ds = DbHelperSQL.Query(conn, trans, strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["id"].ToString() != "") { model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString()); } model.nav_type = ds.Tables[0].Rows[0]["nav_type"].ToString(); model.name = ds.Tables[0].Rows[0]["name"].ToString(); model.title = ds.Tables[0].Rows[0]["title"].ToString(); model.sub_title = ds.Tables[0].Rows[0]["sub_title"].ToString(); model.link_url = ds.Tables[0].Rows[0]["link_url"].ToString(); if (ds.Tables[0].Rows[0]["sort_id"].ToString() != "") { model.sort_id = int.Parse(ds.Tables[0].Rows[0]["sort_id"].ToString()); } if (ds.Tables[0].Rows[0]["is_lock"].ToString() != "") { model.is_lock = int.Parse(ds.Tables[0].Rows[0]["is_lock"].ToString()); } model.remark = ds.Tables[0].Rows[0]["remark"].ToString(); if (ds.Tables[0].Rows[0]["parent_id"].ToString() != "") { model.parent_id = int.Parse(ds.Tables[0].Rows[0]["parent_id"].ToString()); } model.class_list = ds.Tables[0].Rows[0]["class_list"].ToString(); if (ds.Tables[0].Rows[0]["class_layer"].ToString() != "") { model.class_layer = int.Parse(ds.Tables[0].Rows[0]["class_layer"].ToString()); } if (ds.Tables[0].Rows[0]["channel_id"].ToString() != "") { model.channel_id = int.Parse(ds.Tables[0].Rows[0]["channel_id"].ToString()); } model.action_type = ds.Tables[0].Rows[0]["action_type"].ToString(); if (ds.Tables[0].Rows[0]["is_sys"].ToString() != "") { model.is_sys = int.Parse(ds.Tables[0].Rows[0]["is_sys"].ToString()); } return model; } else { return null; } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(NavigationInfo model) { var oldModel = GetModel(model.id); //旧的数据 using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { //先判断选中的父节点是否被包含 if (IsContainNode(model.id, model.parent_id)) { //查找旧父节点数据 string class_list = "," + model.parent_id + ","; int class_layer = 1; if (oldModel.parent_id > 0) { var 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 + "navigation 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) { var 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 + "navigation set "); strSql.Append("nav_type=@nav_type,"); strSql.Append("name=@name,"); strSql.Append("title=@title,"); strSql.Append("sub_title=@sub_title,"); strSql.Append("link_url=@link_url,"); strSql.Append("sort_id=@sort_id,"); strSql.Append("is_lock=@is_lock,"); strSql.Append("remark=@remark,"); strSql.Append("parent_id=@parent_id,"); strSql.Append("class_list=@class_list,"); strSql.Append("class_layer=@class_layer,"); strSql.Append("channel_id=@channel_id,"); strSql.Append("action_type=@action_type,"); strSql.Append("is_sys=@is_sys"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@nav_type", SqlDbType.NVarChar,50), new SqlParameter("@name", SqlDbType.NVarChar,50), new SqlParameter("@title", SqlDbType.NVarChar,100), new SqlParameter("@sub_title", SqlDbType.NVarChar,100), new SqlParameter("@link_url", SqlDbType.NVarChar,255), new SqlParameter("@sort_id", SqlDbType.Int,4), new SqlParameter("@is_lock", SqlDbType.TinyInt,1), new SqlParameter("@remark", SqlDbType.NVarChar,500), new SqlParameter("@parent_id", SqlDbType.Int,4), new SqlParameter("@class_list", SqlDbType.NVarChar,500), new SqlParameter("@class_layer", SqlDbType.Int,4), new SqlParameter("@channel_id", SqlDbType.Int,4), new SqlParameter("@action_type", SqlDbType.NVarChar,500), new SqlParameter("@is_sys", SqlDbType.TinyInt,1), new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = model.nav_type; parameters[1].Value = model.name; parameters[2].Value = model.title; parameters[3].Value = model.sub_title; parameters[4].Value = model.link_url; parameters[5].Value = model.sort_id; parameters[6].Value = model.is_lock; parameters[7].Value = model.remark; parameters[8].Value = model.parent_id; parameters[9].Value = model.class_list; parameters[10].Value = model.class_layer; parameters[11].Value = model.channel_id; parameters[12].Value = model.action_type; parameters[13].Value = model.is_sys; parameters[14].Value = model.id; DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters); //更新子节点 UpdateChilds(conn, trans, model.id); //如无发生错误则提交事务 trans.Commit(); } catch { trans.Rollback(); return false; } } } return true; }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(NavigationInfo model) { return this._repository.Update(model); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(NavigationInfo model) { return this._repository.Add(model); }