一.需求分析

  • 商品修改和商品新增类似
  • 本小节商品修改使用上小节中编写的工具函数,让多表操作组成一个事务

二.代码实现

  • 在TbItemDao.go中编写代码
  1. //修改商品表数据
  2. func updItemByIdWithTx(t TbItem) int{
  3. return commons.PrepareWithTx("update tb_item set title=?,sell_point=?,price=?,num=?,barcode=?,image=?,cid=?,status=?,updated=? where id=?",
  4. t.Title,t.SellPoint,t.Price,t.Num,t.Barcode,t.Image,t.Cid,t.Status,t.Updated,t.Id)
  5. }
  • 在/item/desc/TbItemDesc.go中添加函数
  1. //根据主键修改商品描述,带有事务
  2. func UpdDescByIdWithTxDao(t TbItemDesc) int{
  3. return commons.PrepareWithTx("update tb_item_desc set item_desc=?,updated=? where item_id=?",t.ItemDesc,t.Updated,t.ItemId)
  4. }
  • 在/item/TbItemService.go中添加函数
  1. func updateService(v url.Values) (e commons.EgoResult) {
  2. commons.OpenConnWithTx()
  3. var t TbItem
  4. id, _ := strconv.Atoi(v["Id"][0])
  5. t.Id = id
  6. cid, _ := strconv.Atoi(v["Cid"][0])
  7. t.Cid = cid
  8. t.Title = v["Title"][0]
  9. t.SellPoint = v["SellPoint"][0]
  10. price, _ := strconv.Atoi(v["Price"][0])
  11. t.Price = price
  12. num, _ := strconv.Atoi(v["Num"][0])
  13. t.Num = num
  14. t.Image = v["Image"][0]
  15. status, _ := strconv.Atoi(v["Status"][0])
  16. t.Status = int8(status)
  17. date := time.Now().Format("2006-01-02 15:04:05")
  18. t.Updated = date
  19. count := updItemByIdWithTx(t)
  20. if count > 0 {
  21. var itemDesc desc.TbItemDesc
  22. itemDesc.ItemId = id
  23. itemDesc.ItemDesc = v["Desc"][0]
  24. itemDesc.Updated = date
  25. count = desc.UpdDescByIdWithTxDao(itemDesc)
  26. if count > 0 {
  27. commons.CloseConnWithTx(true)
  28. e.Status = 200
  29. return
  30. }
  31. }
  32. commons.CloseConnWithTx(false)
  33. return
  34. }
  • 在/item/TbItemController.go中添加函数,和配置映射
  1. //修改
  2. func updateController(w http.ResponseWriter, r *http.Request){
  3. r.ParseForm()
  4. er:=updateService(r.Form)
  5. b,_:=json.Marshal(er)
  6. w.Header().Set(commons.HEADER_CONTENT_TYPE,commons.JSON_HEADER)
  7. w.Write(b)
  8. }
  9. func ItemHandler() {
  10. commons.Router.HandleFunc("/showItem", showItemController)
  11. commons.Router.HandleFunc("/item/delete", delByIdsController)
  12. commons.Router.HandleFunc("/item/instock", instockController)
  13. commons.Router.HandleFunc("/item/offstock", offstockController)
  14. commons.Router.HandleFunc("/item/imageupload", imagesUploadController)
  15. commons.Router.HandleFunc("/item/add", insertControllew)
  16. commons.Router.HandleFunc("/item/showItemById", showItemDescCatController)
  17. commons.Router.HandleFunc("/item/update", updateController) //商品修改页面信息显示
  18. }