/// <summary> /// 数据转类别实体 /// </summary> public static UserCategoryInfo DataToModel(SqlDataReader rdr) { UserCategoryInfo category = new UserCategoryInfo(); if (!rdr.IsDBNull(0)) category.CategoryTypeID = rdr.GetInt32(0); if (!rdr.IsDBNull(1)) category.CategoryTypeName = rdr.GetString(1); if (!rdr.IsDBNull(2)) category.CategoryTypePrice = rdr.GetDecimal(2); return category; }
/// <summary> /// 用户类别实体转参数 /// </summary> public static SqlParameter[] ModelToParms(UserCategoryInfo category) { SqlParameter[] parms = { new SqlParameter("@CategoryTypeID", SqlDbType.Int), new SqlParameter("@CategoryTypeName", SqlDbType.NVarChar, 20), new SqlParameter("@CategoryTypePrice", SqlDbType.Decimal), new SqlParameter("@UserID", SqlDbType.Int), new SqlParameter("@CategoryTypeLive", SqlDbType.TinyInt), new SqlParameter("@Synchronize", SqlDbType.TinyInt), new SqlParameter("@ModifyDate", SqlDbType.DateTime) }; parms[0].Value = category.CategoryTypeID; parms[1].Value = category.CategoryTypeName; parms[2].Value = category.CategoryTypePrice; parms[3].Value = category.UserID; parms[4].Value = category.CategoryTypeLive; parms[5].Value = category.Synchronize; parms[6].Value = category.ModifyDate; return parms; }
protected void Page_Load(object sender, EventArgs e) { int catId = Convert.ToInt32(Request.Form["catid"]); string catName = Request.Form["catname"].ToString(); string catPrice = Request.Form["catprice"] ?? ""; byte catLive = Convert.ToByte(Request.Form["catlive"]); int userId = Convert.ToInt32(Request.Form["userid"]); UserCategoryInfo category = new UserCategoryInfo(); category.CategoryTypeID = catId; category.CategoryTypeName = catName; if (catPrice != "") category.CategoryTypePrice = Convert.ToInt32(catPrice); category.UserID = userId; category.CategoryTypeLive = catLive; category.Synchronize = 0; bool success = bll.UserCategoryExistsWithSync(userId, catId); if (success) { success = bll.UpdateUserCategory(category); } else { success = bll.InsertUserCategory(category); } string result = "{"; if (success) { result += "\"result\":\"ok\""; } else { result += "\"result\":\"error\""; } result += "}"; Response.Write(result); Response.End(); }
//取类别ID private int GetCategoryTypeId(string catName) { catTypeList = cat_bll.GetUserCategoryList(userId); foreach (DataRow dr in catTypeList.Rows) { if (catName == dr["CategoryTypeName"].ToString()) { return Convert.ToInt32(dr["CategoryTypeID"]); } } UserCategoryInfo category = new UserCategoryInfo(); category.CategoryTypeID = cat_bll.GetMaxCategoryTypeId(userId); category.CategoryTypeName = catName; category.CategoryTypePrice = 0m; category.UserID = userId; category.CategoryTypeLive = 1; category.Synchronize = 1; category.ModifyDate = DateTime.Now; bool success = cat_bll.InsertUserCategory(category); if (success) { return category.CategoryTypeID; } else { throw new Exception(); } }
/// <summary> /// 插入用户类别 /// </summary> public bool InsertUserCategory(UserCategoryInfo category) { return dal.InsertUserCategory(category); }
/// <summary> /// 删除用户类别 /// </summary> public bool DeleteUserCategory(UserCategoryInfo category) { return dal.DeleteUserCategory(category); }
/// <summary> /// 修改用户类别 /// </summary> public bool UpdateUserCategory(UserCategoryInfo category) { return dal.UpdateUserCategory(category); }
/// <summary> /// 删除用户类别 /// </summary> public bool DeleteUserCategory(UserCategoryInfo category) { SqlParameter[] parms = ModelToParms(category); int result = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.StoredProcedure, SQL_DELETE_USER_CATEGORY_PROCEDURE, parms); return result > 0; }
/// <summary> /// 根据类别名称取用户类别 /// </summary> public UserCategoryInfo GetUserCategoryByName(int userId, string catTypeName) { UserCategoryInfo category = new UserCategoryInfo(); SqlParameter[] parms = { new SqlParameter(PARM_USER_ID, SqlDbType.Int), new SqlParameter(PARM_CATEGORY_TYPE_NAME, SqlDbType.NVarChar, 20) }; parms[0].Value = userId; parms[1].Value = catTypeName; using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, SQL_SELECT_USER_CATEGORY_BY_NAME, parms)) { while (rdr.Read()) { category = DataToModel(rdr); } } return category; }
//类别删除操作 protected void CatTypeList_RowDeleting(object sender, GridViewDeleteEventArgs e) { int catTypeId = Convert.ToInt32(CatTypeList.DataKeys[e.RowIndex].Value); string catTypeName = ((Label)CatTypeList.Rows[e.RowIndex].FindControl("CatTypeNameLab")).Text; string catTypePrice = ((Label)CatTypeList.Rows[e.RowIndex].FindControl("CatTypePriceLab")).Text; DataTable items = item_bll.GetItemListByCategoryId(userId, catTypeId); if (items.Rows.Count > 0) { Utility.Alert(this, "不能删除已使用的类别!"); return; } if(this.CatTypeList.Rows.Count == 1) { Utility.Alert(this, "不能删除最后一个类别!"); return; } UserCategoryInfo category = new UserCategoryInfo(); category.CategoryTypeID = catTypeId; category.CategoryTypeName = catTypeName; category.CategoryTypePrice = Convert.ToInt32(catTypePrice); category.UserID = userId; category.CategoryTypeLive = 0; category.Synchronize = 1; category.ModifyDate = DateTime.Now; bool success = bll.DeleteUserCategory(category); if (success) { Utility.Alert(this, "删除成功。"); CatTypeList.EditIndex = -1; BindGrid(); } else { Utility.Alert(this, "删除失败!"); } }
//类别更新操作 protected void CatTypeList_RowUpdating(object sender, GridViewUpdateEventArgs e) { int catTypeId = Convert.ToInt32(CatTypeList.DataKeys[e.RowIndex].Value); string catTypeName = ((TextBox)CatTypeList.Rows[e.RowIndex].FindControl("CatTypeNameBox")).Text.Trim(); string catTypeNameHid = ((HiddenField)CatTypeList.Rows[e.RowIndex].FindControl("CatTypeNameHid")).Value; string catTypePrice = ((TextBox)CatTypeList.Rows[e.RowIndex].FindControl("CatTypePriceBox")).Text.Trim(); if (catTypeName == "") { Utility.Alert(this, "类别名称未填写!"); return; } if (catTypePrice != "") { if (!ValidHelper.CheckNumber(catTypePrice)) { Utility.Alert(this, "类别预算填写错误!"); return; } } else { catTypePrice = "0"; } bool success = false; UserCategoryInfo category = new UserCategoryInfo(); if (catTypeName != catTypeNameHid) { category = bll.GetUserCategoryByName(userId, catTypeName); if (category.CategoryTypeID > 0) { Utility.Alert(this, "类别已存在,不能重复添加!"); return; } } category.CategoryTypeID = catTypeId; category.CategoryTypeName = catTypeName; category.CategoryTypePrice = Convert.ToInt32(catTypePrice); category.UserID = userId; category.CategoryTypeLive = 1; category.Synchronize = 1; category.ModifyDate = DateTime.Now; using (TransactionScope ts = new TransactionScope()) { success = bll.DeleteUserCategory(userId, catTypeId); success = bll.InsertUserCategory(category); ts.Complete(); } if (success) { Utility.Alert(this, "更新成功。"); CatTypeList.EditIndex = -1; BindGrid(); } else { Utility.Alert(this, "更新失败!"); } }