/// <summary> /// 根据微用户所在行业给微账户添加默认模块 /// </summary> public void addMouduleByRoleid(int roleid, int wid, IArticleCategoryRepository repository) { var acBll = new ArticleCategoryService(repository); //得到模型的实体类集合 var idList = getModelList(" role_id=" + roleid + " order by sort_id asc"); //循环给为账户添加行业模块 for (int i = 0; i < idList.Count; i++) { var acModel = new ArticleCategoryInfo() { title = idList[i].mName, call_index = "mubanpinyin", wid = wid, link_url = idList[i].url, channel_id = 1, sort_id = MyCommFun.Obj2Int(idList[i].sort_id) }; int resId = acBll.Add(acModel); var upModel = acBll.GetModel(resId); upModel.class_list = "," + resId + ","; acBll.Update(upModel); } }
/// <summary> /// 增加一条数据 /// </summary> public int Add(ArticleCategoryInfo 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 + "article_category("); strSql.Append("channel_id,title,call_index,parent_id,class_list,class_layer,sort_id,link_url,img_url,content,seo_title,seo_keywords,seo_description,wid,ico_url,cStatus)"); strSql.Append(" values ("); strSql.Append("@channel_id,@title,@call_index,@parent_id,@class_list,@class_layer,@sort_id,@link_url,@img_url,@content,@seo_title,@seo_keywords,@seo_description,@wid,@ico_url,@cStatus)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@channel_id", SqlDbType.Int,4), new SqlParameter("@title", SqlDbType.NVarChar,100), new SqlParameter("@call_index", SqlDbType.NVarChar,50), 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("@link_url", SqlDbType.NVarChar,255), new SqlParameter("@img_url", SqlDbType.NVarChar,255), new SqlParameter("@content", SqlDbType.NText), new SqlParameter("@seo_title", SqlDbType.NVarChar,255), new SqlParameter("@seo_keywords", SqlDbType.NVarChar,255), new SqlParameter("@seo_description", SqlDbType.NVarChar,255), new SqlParameter("@wid", SqlDbType.Int,4), new SqlParameter("@ico_url", SqlDbType.NVarChar,500), new SqlParameter("@cStatus", SqlDbType.Int,4)}; parameters[0].Value = model.channel_id; parameters[1].Value = model.title; parameters[2].Value = model.call_index; parameters[3].Value = model.parent_id; parameters[4].Value = model.class_list; parameters[5].Value = model.class_layer; parameters[6].Value = model.sort_id; parameters[7].Value = model.link_url; parameters[8].Value = model.img_url; parameters[9].Value = model.content; parameters[10].Value = model.seo_title; parameters[11].Value = model.seo_keywords; parameters[12].Value = model.seo_description; parameters[13].Value = model.wid; parameters[14].Value = model.ico_url; parameters[15].Value = model.cStatus; object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务 model.id = Convert.ToInt32(obj); //添加自定义图片尺寸 if (model.imagesize_values != null) { StringBuilder strSqlimage; foreach (var modelimagesize in model.imagesize_values) { strSqlimage = new StringBuilder(); strSqlimage.Append("insert into " + databaseprefix + "article_images_size("); strSqlimage.Append("category_id,height,width)"); strSqlimage.Append(" values ("); strSqlimage.Append("@category_id,@height,@width)"); SqlParameter[] parametersimages = { new SqlParameter("@category_id", SqlDbType.Int,4), new SqlParameter("@height", SqlDbType.NVarChar,50), new SqlParameter("@width", SqlDbType.NVarChar,50)}; parametersimages[0].Value = model.id; parametersimages[1].Value = modelimagesize.height; parametersimages[2].Value = modelimagesize.width; DbHelperSQL.ExecuteSql(conn, trans, strSqlimage.ToString(), parametersimages); } } 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 + "article_category 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 ArticleCategoryInfo GetModel(SqlConnection conn, SqlTransaction trans, int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,channel_id,title,call_index,parent_id,class_list,class_layer,sort_id,link_url,img_url,content,seo_title,seo_keywords,seo_description,ico_url,wid,cStatus from " + databaseprefix + "article_category "); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = id; var model = new ArticleCategoryInfo(); DataSet ds = DbHelperSQL.Query(conn, trans, strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "") { model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString()); } if (ds.Tables[0].Rows[0]["channel_id"] != null && ds.Tables[0].Rows[0]["channel_id"].ToString() != "") { model.channel_id = int.Parse(ds.Tables[0].Rows[0]["channel_id"].ToString()); } if (ds.Tables[0].Rows[0]["title"] != null && ds.Tables[0].Rows[0]["title"].ToString() != "") { model.title = ds.Tables[0].Rows[0]["title"].ToString(); } if (ds.Tables[0].Rows[0]["call_index"] != null && ds.Tables[0].Rows[0]["call_index"].ToString() != "") { model.call_index = ds.Tables[0].Rows[0]["call_index"].ToString(); } if (ds.Tables[0].Rows[0]["parent_id"] != null && ds.Tables[0].Rows[0]["parent_id"].ToString() != "") { model.parent_id = int.Parse(ds.Tables[0].Rows[0]["parent_id"].ToString()); } if (ds.Tables[0].Rows[0]["class_list"] != null && ds.Tables[0].Rows[0]["class_list"].ToString() != "") { model.class_list = ds.Tables[0].Rows[0]["class_list"].ToString(); } if (ds.Tables[0].Rows[0]["class_layer"] != null && 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]["sort_id"] != null && 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]["link_url"] != null && ds.Tables[0].Rows[0]["link_url"].ToString() != "") { model.link_url = ds.Tables[0].Rows[0]["link_url"].ToString(); } if (ds.Tables[0].Rows[0]["img_url"] != null && ds.Tables[0].Rows[0]["img_url"].ToString() != "") { model.img_url = ds.Tables[0].Rows[0]["img_url"].ToString(); } if (ds.Tables[0].Rows[0]["content"] != null && ds.Tables[0].Rows[0]["content"].ToString() != "") { model.content = ds.Tables[0].Rows[0]["content"].ToString(); } if (ds.Tables[0].Rows[0]["seo_title"] != null && ds.Tables[0].Rows[0]["seo_title"].ToString() != "") { model.seo_title = ds.Tables[0].Rows[0]["seo_title"].ToString(); } if (ds.Tables[0].Rows[0]["seo_keywords"] != null && ds.Tables[0].Rows[0]["seo_keywords"].ToString() != "") { model.seo_keywords = ds.Tables[0].Rows[0]["seo_keywords"].ToString(); } if (ds.Tables[0].Rows[0]["seo_description"] != null && ds.Tables[0].Rows[0]["seo_description"].ToString() != "") { model.seo_description = ds.Tables[0].Rows[0]["seo_description"].ToString(); } if (ds.Tables[0].Rows[0]["ico_url"] != null && ds.Tables[0].Rows[0]["ico_url"].ToString() != "") { model.ico_url = ds.Tables[0].Rows[0]["ico_url"].ToString(); } if (ds.Tables[0].Rows[0]["wid"] != null && ds.Tables[0].Rows[0]["wid"].ToString() != "") { model.wid = int.Parse(ds.Tables[0].Rows[0]["wid"].ToString()); } if (ds.Tables[0].Rows[0]["cStatus"] != null && ds.Tables[0].Rows[0]["cStatus"].ToString() != "") { model.cStatus = int.Parse(ds.Tables[0].Rows[0]["cStatus"].ToString()); } return model; } else { return null; } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(ArticleCategoryInfo model) { using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { //先判断选中的父节点是否被包含 if (IsContainNode(model.id, model.parent_id)) { //查找旧数据 var oldModel = GetModel(model.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 + "article_category 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 + "article_category set "); strSql.Append("channel_id=@channel_id,"); strSql.Append("title=@title,"); strSql.Append("call_index=@call_index,"); 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("link_url=@link_url,"); strSql.Append("img_url=@img_url,"); strSql.Append("content=@content,"); strSql.Append("seo_title=@seo_title,"); strSql.Append("seo_keywords=@seo_keywords,"); strSql.Append("seo_description=@seo_description,"); strSql.Append("wid=@wid,"); strSql.Append("ico_url=@ico_url,"); strSql.Append("cStatus=@cStatus"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@channel_id", SqlDbType.Int,4), new SqlParameter("@title", SqlDbType.NVarChar,100), new SqlParameter("@call_index", SqlDbType.NVarChar,50), 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("@link_url", SqlDbType.NVarChar,255), new SqlParameter("@img_url", SqlDbType.NVarChar,255), new SqlParameter("@content", SqlDbType.NText), new SqlParameter("@seo_title", SqlDbType.NVarChar,255), new SqlParameter("@seo_keywords", SqlDbType.NVarChar,255), new SqlParameter("@seo_description", SqlDbType.NVarChar,255), new SqlParameter("@wid", SqlDbType.Int,4), new SqlParameter("@ico_url", SqlDbType.NVarChar,500), new SqlParameter("@cStatus", SqlDbType.Int,4), new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = model.channel_id; parameters[1].Value = model.title; parameters[2].Value = model.call_index; parameters[3].Value = model.parent_id; parameters[4].Value = model.class_list; parameters[5].Value = model.class_layer; parameters[6].Value = model.sort_id; parameters[7].Value = model.link_url; parameters[8].Value = model.img_url; parameters[9].Value = model.content; parameters[10].Value = model.seo_title; parameters[11].Value = model.seo_keywords; parameters[12].Value = model.seo_description; parameters[13].Value = model.wid; parameters[14].Value = model.ico_url; parameters[15].Value = model.cStatus; parameters[16].Value = model.id; DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters); //添加自定义图片尺寸 if (model.imagesize_values != null) { //删除已删除的图片 new ArticleImageSizeRepository(databaseprefix).DeleteList(conn, trans, model.imagesize_values, model.id); StringBuilder strSqlimage; foreach (var modelimagesize in model.imagesize_values) { strSqlimage = new StringBuilder(); if (modelimagesize.id > 0) { strSqlimage.Append("update dt_article_images_size set "); strSqlimage.Append("category_id=@category_id,"); strSqlimage.Append("height=@height,"); strSqlimage.Append("width=@width"); strSqlimage.Append(" where id=@id"); SqlParameter[] parametersimages = { new SqlParameter("@category_id", SqlDbType.Int,4), new SqlParameter("@height", SqlDbType.NVarChar,50), new SqlParameter("@width", SqlDbType.NVarChar,50), new SqlParameter("@id", SqlDbType.Int,4)}; parametersimages[0].Value = model.id; parametersimages[1].Value = modelimagesize.height; parametersimages[2].Value = modelimagesize.width; parametersimages[3].Value = model.id; DbHelperSQL.ExecuteSql(conn, trans, strSqlimage.ToString(), parametersimages); } else { strSqlimage.Append("insert into " + databaseprefix + "article_images_size("); strSqlimage.Append("category_id,height,width)"); strSqlimage.Append(" values ("); strSqlimage.Append("@category_id,@height,@width)"); SqlParameter[] parametersimages = { new SqlParameter("@category_id", SqlDbType.Int,4), new SqlParameter("@height", SqlDbType.NVarChar,50), new SqlParameter("@width", SqlDbType.NVarChar,50)}; parametersimages[0].Value = model.id; parametersimages[1].Value = modelimagesize.height; parametersimages[2].Value = modelimagesize.width; DbHelperSQL.ExecuteSql(conn, trans, strSqlimage.ToString(), parametersimages); } } } //更新子节点 UpdateChilds(conn, trans, model.id); trans.Commit(); } catch (Exception ex) { trans.Rollback(); return false; } } } return true; }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(ArticleCategoryInfo model) { return this._repository.Update(model); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(ArticleCategoryInfo model) { return this._repository.Add(model); }