一.需求分析
- 商品修改和商品新增类似
- 本小节商品修改使用上小节中编写的工具函数,让多表操作组成一个事务
二.代码实现
//修改商品表数据
func updItemByIdWithTx(t TbItem) int{
return commons.PrepareWithTx("update tb_item set title=?,sell_point=?,price=?,num=?,barcode=?,image=?,cid=?,status=?,updated=? where id=?",
t.Title,t.SellPoint,t.Price,t.Num,t.Barcode,t.Image,t.Cid,t.Status,t.Updated,t.Id)
}
- 在/item/desc/TbItemDesc.go中添加函数
//根据主键修改商品描述,带有事务
func UpdDescByIdWithTxDao(t TbItemDesc) int{
return commons.PrepareWithTx("update tb_item_desc set item_desc=?,updated=? where item_id=?",t.ItemDesc,t.Updated,t.ItemId)
}
- 在/item/TbItemService.go中添加函数
func updateService(v url.Values) (e commons.EgoResult) {
commons.OpenConnWithTx()
var t TbItem
id, _ := strconv.Atoi(v["Id"][0])
t.Id = id
cid, _ := strconv.Atoi(v["Cid"][0])
t.Cid = cid
t.Title = v["Title"][0]
t.SellPoint = v["SellPoint"][0]
price, _ := strconv.Atoi(v["Price"][0])
t.Price = price
num, _ := strconv.Atoi(v["Num"][0])
t.Num = num
t.Image = v["Image"][0]
status, _ := strconv.Atoi(v["Status"][0])
t.Status = int8(status)
date := time.Now().Format("2006-01-02 15:04:05")
t.Updated = date
count := updItemByIdWithTx(t)
if count > 0 {
var itemDesc desc.TbItemDesc
itemDesc.ItemId = id
itemDesc.ItemDesc = v["Desc"][0]
itemDesc.Updated = date
count = desc.UpdDescByIdWithTxDao(itemDesc)
if count > 0 {
commons.CloseConnWithTx(true)
e.Status = 200
return
}
}
commons.CloseConnWithTx(false)
return
}
- 在/item/TbItemController.go中添加函数,和配置映射
//修改
func updateController(w http.ResponseWriter, r *http.Request){
r.ParseForm()
er:=updateService(r.Form)
b,_:=json.Marshal(er)
w.Header().Set(commons.HEADER_CONTENT_TYPE,commons.JSON_HEADER)
w.Write(b)
}
func ItemHandler() {
commons.Router.HandleFunc("/showItem", showItemController)
commons.Router.HandleFunc("/item/delete", delByIdsController)
commons.Router.HandleFunc("/item/instock", instockController)
commons.Router.HandleFunc("/item/offstock", offstockController)
commons.Router.HandleFunc("/item/imageupload", imagesUploadController)
commons.Router.HandleFunc("/item/add", insertControllew)
commons.Router.HandleFunc("/item/showItemById", showItemDescCatController)
commons.Router.HandleFunc("/item/update", updateController) //商品修改页面信息显示
}