public static int Update(CompanyProductInfo model) { string strSQL = "UPDATE CompanyProducts SET CategoryId = @CategoryId,Title = @Title ,Content = @Content,ImageUrl = @ImageUrl,Remark = @Remark,PublishDateTime = @PublishDatetime,ModifyDateTime = GETDATE(),Tags = @Tags,SystemCategoryId = @SystemCategoryId WHERE Id = @Id"; SqlParameter[] parms = { new SqlParameter("Id",SqlDbType.Int), new SqlParameter("CategoryId",SqlDbType.Int), new SqlParameter("CompanyId",SqlDbType.Int), new SqlParameter("Title",SqlDbType.NVarChar), new SqlParameter("Content",SqlDbType.NVarChar), new SqlParameter("ImageUrl",SqlDbType.NVarChar), new SqlParameter("Remark",SqlDbType.NVarChar), new SqlParameter("PublishDateTime",SqlDbType.DateTime), new SqlParameter("Tags",SqlDbType.NVarChar), new SqlParameter("SystemCategoryId",SqlDbType.Int), }; parms[0].Value = model.Id; parms[1].Value = model.CategoryId; parms[2].Value = model.CompanyId; parms[3].Value = string.IsNullOrEmpty(model.Title) ? string.Empty : model.Title; parms[4].Value = string.IsNullOrEmpty(model.Content) ? string.Empty : model.Content; parms[5].Value = string.IsNullOrEmpty(model.ImageUrl) ? string.Empty : model.ImageUrl; parms[6].Value = string.IsNullOrEmpty(model.Remark) ? string.Empty : model.Remark; parms[7].Value = model.PublishDateTime<= DateTime.MinValue ? DateTime.Now : model.PublishDateTime; parms[8].Value = string.IsNullOrEmpty(model.Tags) ? string.Empty : model.Tags; parms[9].Value = model.SystemCategoryId; return SQLPlus.ExecuteNonQuery(CommandType.Text,strSQL,parms); }
public static int Insert(CompanyProductInfo model) { string strSQL = "INSERT INTO dbo.CompanyProducts(CategoryId,CompanyId,Title,Content,ImageUrl,Remark,PublishDateTime,CreateDateTime,ModifyDateTime,IsDeleted,Tags,SystemCategoryId) VALUES(@CategoryId,@CompanyId,@Title,@Content,@ImageUrl,@Remark,@PublishDateTime,GETDATE(),GETDATE(),0,@Tags,@SystemCategoryId);SELECT @@IDENTITY;"; SqlParameter[] parms = { new SqlParameter("Id",SqlDbType.Int), new SqlParameter("CategoryId",SqlDbType.Int), new SqlParameter("CompanyId",SqlDbType.Int), new SqlParameter("Title",SqlDbType.NVarChar), new SqlParameter("Content",SqlDbType.NVarChar), new SqlParameter("ImageUrl",SqlDbType.NVarChar), new SqlParameter("Remark",SqlDbType.NVarChar), new SqlParameter("PublishDateTime",SqlDbType.DateTime), new SqlParameter("Tags",SqlDbType.NVarChar), new SqlParameter("SystemCategoryId",SqlDbType.Int), }; parms[0].Value = model.Id; parms[1].Value = model.CategoryId; parms[2].Value = model.CompanyId; parms[3].Value = string.IsNullOrEmpty(model.Title) ? string.Empty : model.Title; parms[4].Value = string.IsNullOrEmpty(model.Content) ? string.Empty : model.Content; parms[5].Value = string.IsNullOrEmpty(model.ImageUrl) ? string.Empty : model.ImageUrl; parms[6].Value = string.IsNullOrEmpty(model.Remark) ? string.Empty : model.Remark; parms[7].Value = model.PublishDateTime <= DateTime.MinValue ? DateTime.Now : model.PublishDateTime; parms[8].Value = string.IsNullOrEmpty(model.Tags) ? string.Empty : model.Tags; parms[9].Value = model.SystemCategoryId; return Convert.ToInt32(SQLPlus.ExecuteScalar(CommandType.Text,strSQL,parms)); }
/// <summary> /// 更新 /// </summary> /// <param name="model"></param> /// <returns></returns> public static CompanyProductInfo Update(CompanyProductInfo model) { if (model.Id == 0) { int id = CompanyProductManage.Insert(model); model.Id = id; } else { CompanyProductManage.Update(model); } return model; }
public ActionResult ProductEdit(CompanyProductInfo oldModel, FormCollection fc) { bool errors = false; bool isAdd = oldModel.Id == 0; var companyInfo = MemberService.GetCompanyInfoByUserId(PlantEngContext.Current.UserId); if (string.IsNullOrEmpty(oldModel.Title)) { errors = true; ModelState.AddModelError("TitleEmpty", "标题不能为空"); } if (string.IsNullOrEmpty(oldModel.Content)) { errors = true; ModelState.AddModelError("ContentEmpty", "内容不能为空"); } oldModel.SystemCategoryId = Utils.StrToInt(fc["syscat"],0); if(oldModel.SystemCategoryId == 0){ errors = true; ModelState.AddModelError("SysCatEmpty", "请选择产品系统分类"); } //为了在更新的时候,判断新分类是否还是以前的分类,然后为更新新分类产品数做判断 int oldCatId = oldModel.CategoryId; oldModel.CategoryId = Utils.StrToInt(fc["cat"], 0); if(oldModel.CategoryId == 0){ errors = true; ModelState.AddModelError("CatEmpty", "请选择自定义分类"); } if (!errors && ModelState.IsValid) { oldModel.CompanyId = companyInfo.CompanyId; oldModel = CompanyProductService.Update(oldModel); if (isAdd) { //如果是添加,则更新产品分类表中的产品数 + 1 CompanyProductCategoryService.UpdateProductCount(oldModel.CategoryId, companyInfo.CompanyId, true); } else { //如果没有更改分类,则什么都不变 //否则则原来的分类的产品数-1,新分类产品数+ 1 if (oldCatId == oldModel.CategoryId) { //说明还是在原来的分类上更新 } else { //分类改变了 //原来的分类产品数 - 1 ,新分类数 + 1 CompanyProductCategoryService.UpdateProductCount(oldCatId, companyInfo.CompanyId, false); //新分类 + 1 CompanyProductCategoryService.UpdateProductCount(oldModel.CategoryId, companyInfo.CompanyId, true); } } ViewBag.Msg = "保存成功!"; } //产品分类 ViewBag.CategoryList = CompanyProductCategoryService.GetCategoryList(companyInfo.CompanyId); return View(oldModel); }