(三)goweb千锋教育练习项目

本文介绍了如何在商品管理系统中实现商品的新增、修改和删除操作,包括事务处理和多表操作。同时详细阐述了规格参数的管理,包括查询、新增和删除功能,涉及数据库查询、事务管理和API接口设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.商品新增

  • 商品描述表(tb_item_desc)和商品表(tb_item)具有主外键关系,商品的主键也是商品描述的主键,使用工具函数生成的主键也当作商品描述表的主键
  • 商品描述中信息来源于页面中KindEditor的富文本编辑框,里面带有HTML代码直接保存就可以
  • 多表新增时要考虑事务的问题,本功能中使用最原始的方式实现(多个DML,多个事务,效率低),后面多表新增使用标准的事务方式,让同学们有对比,看看哪个方式较好.
  • 只需要在DAO和Service中添加代码即可.
//新增
func insertItemDao(t TbItem) int{
	count,err:=commons.Dml("insert into tb_item values(?,?,?,?,?,?,?,?,?,?,?)",t.Id,t.Title,t.SellPoint,t.Price,t.Num,t.Barcode,t.Image,t.Cid,t.Status,t.Created,t.Updated)
	if err!=nil{
		return -1
	}
	return int(count)
}
  • 在commons文件夹下新建Commons.go文件,并编写工具函数,实现生成主键
package commons

import (
	"time"
	"math/rand"
	"strconv"
)
//生成数据库主键
func GenId() int{
	rand.Seed(time.Now().UnixNano())
	id,_:=strconv.Atoi(strconv.Itoa(rand.Intn(10000))+strconv.Itoa(int(time.Now().Unix())))
	return id
}
  • 在TbItemService.go中编写业务实现商品新增
//商品新增
func insetService(f url.Values) (e commons.EgoResult){
	var t TbItem
	cid,_:=strconv.Atoi(f["Cid"][0])
	t.Cid =cid
	t.Title = f["Title"][0]
	t.SellPoint = f["SellPoint"][0]
	price,_:=strconv.Atoi(f["Price"][0])
	t.Price = price
	num,_:=strconv.Atoi(f["Num"][0])
	t.Num=num
	t.Image = f["Image"][0]
	t.Status = 1
	date:=time.Now().Format("2006-01-02 15:04:05")
	t.Created =date
	t.Updated = date
	id:=commons.GenId()
	t.Id = id



	count :=insertItemDao(t)
	if count>0{
		e.Status = 200
	}
	return
}
  • 在TbItemController.go中添加函数实现商品新增,并添加映射
//商品新增
func insertControllew (w http.ResponseWriter, r *http.Request) {
	//需要先进行解析
	r.ParseForm()
	er:=insetService(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) //商品新增
}

  • 在/item文件夹下新建desc文件夹,并在desc文件夹下新建TbItemDesc.go编写实体
package desc

//商品描述
type TbItemDesc struct {
	ItemId int
	ItemDesc string
	Created string
	Updated string
}

  • 在/item/desc下新建TbItemDescDao.go
package desc

import (
	"commons"
	"fmt"
)
//新增描述
func insertDescDao(t TbItemDesc ) int{
	count,err:=commons.Dml("insert into tb_item_desc values(?,?,?,?)",t.ItemId,t.ItemDesc,t.Created,t.Updated)
	if err!=nil{
		fmt.Println(err)
		return -1
	}
	return int(count)
}

  • 在/item/desc下新建TbItemDescService.go,并把新增暴露给其他package
package desc

//新增
func Insert(t TbItemDesc) int{
	return insertDescDao(t)
}

  • 在/item/TbItemDao.go中添加删除函数
//根据id删除
func delById(id int) int{
	count,err:=commons.Dml("delete from tb_item where id=?",id)
	if err!=nil{
		fmt.Println(err)
		return -1
	}
	return int(count)
}
  • 修改/item/TbItemService.go中新增商品业务代码
//商品新增
func insetService(f url.Values) (e commons.EgoResult){
	var t TbItem
	cid,_:=strconv.Atoi(f["Cid"][0])
	t.Cid =cid
	t.Title = f["Title"][0]
	t.SellPoint = f["SellPoint"][0]
	price,_:=strconv.Atoi(f["Price"][0])
	t.Price = price
	num,_:=strconv.Atoi(f["Num"][0])
	t.Num=num
	t.Image = f["Image"][0]
	t.Status = 1
	date:=time.Now().Format("2006-01-02 15:04:05")
	t.Created =date
	t.Updated = date
	id:=commons.GenId()
	t.Id = id


	//商品表新增执行
	count :=insertItemDao(t)
	if count>0{
        //商品描述新增
		var tbItemDesc desc.TbItemDesc
		tbItemDesc.ItemId = id
		tbItemDesc.Created = date
		tbItemDesc.Updated = date
		tbItemDesc.ItemDesc = f["Desc"][0]
		countDesc:=desc.Insert(tbItemDesc)
		if countDesc>0{
			e.Status = 200
		}else{
			//删除商品中数据
			delById(id)
			e.Status = 400
		}
	}
	return
}

2.商品修改

页面显示的数据是服务端根据客户端传递过来的商品id进行查询,查询时除了查询商品表(tb_item)以外还需要查询商品描述表(tb_item_desc)和商品类目表(tb_item_cat)把这些数据返回json

  • 在TbItemDao.go中编写代码
//修改商品表数据
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) //商品修改页面信息显示
}

3.规格参数删除

  • 在/item文件夹下新建文件param
  • 在/item/param中新建TbItemParam.go
package param
//模版
type TbItemParam struct {
	Id int `json:"id"`
	ItemCatId int `json:"itemCatId"`
	ParamData string `json:"paramData"`
	Created string `json:"created"`
	Updated string `json:"updated"`
}

type TbItemParamCat struct {
	TbItemParam
	CatName string `json:"catName"`
}

  • 在/item/param中新建TbItemParamDao.go
package param

import (
	"commons"
	"fmt"
)
//分页查询
func selByPageDao(page,rows int) []TbItemParam{
	r,err:=commons.Dql("select * from tb_item_param limit ?,?",rows*(page-1),rows)
	if err!=nil{
		fmt.Println(err)
		return nil
	}
	t := make([]TbItemParam,0)
	for r.Next(){
		var param TbItemParam
		r.Scan(&param.Id,&param.ItemCatId,&param.ParamData,&param.Created,&param.Updated)
		t=append(t,param)
	}
	return t
}

//查询总个数
func selCount() int{
	r,err:=commons.Dql("select count(*) from tb_item_param")
	if err!=nil{
		fmt.Println(err)
		return -1
	}
	if r.Next(){
		var count int
		r.Scan(&count)
		return count
	}
	return -1
}

  • 在/item/param中新建TbItemParamService.go
package param

import "commons"
import (
	c "item/cat"
)

//显示规格参数
func showParamService(page, rows int) (d commons.Datagrid) {
	t := selByPageDao(page, rows)
	d.Total = selCount()
	cats := make([]TbItemParamCat, 0)
	for i := 0; i < len(t); i++ {
		var cat TbItemParamCat
		cat.Id = t[i].Id
		cat.Updated = t[i].Updated
		cat.Created = t[i].Created
		cat.ParamData = t[i].ParamData
		cat.ItemCatId = t[i].ItemCatId
		cat.CatName = c.ShowCatByIdService(t[i].ItemCatId).Name
		cats = append(cats, cat)
	}
	d.Rows = cats
	return
}

  • 在/item/param中新建TbItemParamController.go
package param

import "commons"
import (
	c "item/cat"
)

//显示规格参数
func showParamService(page, rows int) (d commons.Datagrid) {
	t := selByPageDao(page, rows)
	d.Total = selCount()
	cats := make([]TbItemParamCat, 0)
	for i := 0; i < len(t); i++ {
		var cat TbItemParamCat
		cat.Id = t[i].Id
		cat.Updated = t[i].Updated
		cat.Created = t[i].Created
		cat.ParamData = t[i].ParamData
		cat.ItemCatId = t[i].ItemCatId
		cat.CatName = c.ShowCatByIdService(t[i].ItemCatId).Name
		cats = append(cats, cat)
	}
	d.Rows = cats
	return
}

  • 在main.go中添加函数引用
	//规格参数
	param.ParamHandler()

4.规格参数查询

  • 不同类型商品有不同的规格参数模版,在设计数据库时直接把模版都放入到一个列中,类型为json,保证无论怎么变化都可以实现规格参数的处理
  • tb_item_param是规格参数模版表,这个表中存储了不同类型对应的模版信息
  • 页面显示时除了规格参数模版表中内容还需要显示对应类目的名称
  • 在/item/param/TbItemParamDao.go中添加函数,实现多条删除
//多条删除
func delByIdsDao(ids []int) int{
	sql:="delete from tb_item_param where id in ("
	for i:=0;i<len(ids);i++{
		sql+=strconv.Itoa(ids[i])
		if i <len(ids)-1{
			sql+=","
		}
	}
	sql+=")"
	count,err:=commons.Dml(sql)
	if err!=nil{
		fmt.Println(err)
		return -1
	}
	return int(count)
}
  • 在/item/param/TbItemParamService.go中添加业务
//删除规格参数
func delByIdsService(ids string) (e commons.EgoResult){
	idStr:=strings.Split(ids,",")
	idInt:=make([]int,0)
	for _,n:=range idStr{
		id,_:=strconv.Atoi(n)
		idInt= append(idInt,id)
	}
	count:=delByIdsDao(idInt)
	if count>0{
		e.Status=200
	}
	return
}
  • 在/item/param/TbItemParamController.go中添加控制器函数
//删除规格参数
func delByIdsController(w http.ResponseWriter,r *http.Request){
	er:=delByIdsService(r.FormValue("ids"))
	b,_:=json.Marshal(er)
	w.Header().Set(commons.HEADER_CONTENT_TYPE,commons.JSON_HEADER)
	w.Write(b)
}
func ParamHandler(){
	commons.Router.HandleFunc("/item/param/show",showParamController)
	commons.Router.HandleFunc("/item/param/delete",delByIdsController)//删除规格参数
}

5.规格参数新增

  • 在/item/param/TbItemParamDao.go中添加新增(主键自增)
//新增
func insertParamDao(param TbItemParam) int{
	count,err:=commons.Dml("insert into tb_item_param values(default,?,?,?,?)",param.ItemCatId,param.ParamData,param.Created,param.Updated)
	if err!=nil{
		fmt.Println(err)
		return -1
	}
	return int(count)
}
  • 在/item/param/TbItemParamService.go中添加新增业务
//新增规格参数
func insertParamService(catid int ,paramData string) (e commons.EgoResult){
	date:=time.Now().Format("2006-01-02 15:04:05")
	param:=TbItemParam{ItemCatId:catid,ParamData:paramData,Created:date,Updated:date}
	count:=insertParamDao(param)
	if count>0{
		e.Status=200

	}
	return
}
  • 在src/item/param/TbItemParamController.go中添加控制器函数,和url映射
//规格参数新增
func insertParamController(w http.ResponseWriter,r *http.Request){
	catid,_:=strconv.Atoi(r.FormValue("itemCatId"))
	paramData:=r.FormValue("paramData")
	er:=insertParamService(catid,paramData)
	b,_:=json.Marshal(er)
	w.Header().Set(commons.HEADER_CONTENT_TYPE,commons.JSON_HEADER)
	w.Write(b)
}
commons.Router.HandleFunc("/item/param/add",insertParamController)//规格参数新增

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值