GO基于结构的CRUD(PROTOBUF)
GO基于结构的CRUD(PROTOBUF)
以采购订单为例。
//代码由代码工厂自动生成 //2022-08-20 16:25:57 package model import ( "strconv" "database/sql" "github.com/gin-gonic/gin/binding" "strings" "svrGIN/public" "github.com/gin-gonic/gin" ) func SelectTcgddtcgdd22(ctx *gin.Context) { var arr Tcgddtcgdd2 var rows *sql.Rows var err error var r Res rows, err = db.Query("select * from Tcgdd") if err != nil { r.Status = 500 r.Message = "fail" r.Exception = err.Error() ctx.ProtoBuf(500, &r) public.Log("SelectTcgddtcgdd22()" + err.Error()) return } defer rows.Close() for rows.Next() { var row Tcgdd rows.Scan(&row.Billid,&row.Adddate,&row.Supplierid,&row.Suppliername,&row.Warehouseid,&row.Warehousename,&row.Makerid,&row.Note,&row.Shenhe,&row.Maker) arr.TCGDD = append(arr.TCGDD, &row) } rows, err = db.Query("select * from Tcgdd2") if err != nil { r.Status = 500 r.Message = "fail" r.Exception = err.Error() ctx.ProtoBuf(500, &r) public.Log("SelectTcgddtcgdd22()" + err.Error()) return } defer rows.Close() for rows.Next() { var row Tcgdd2 rows.Scan(&row.Billid,&row.Recno,&row.Goodsid,&row.Goodsname,&row.Gg,&row.Dw,&row.Dj,&row.Qty,&row.Amount) arr.TCGDD2 = append(arr.TCGDD2, &row) } arr.Status = 200 arr.Message = "success" ctx.ProtoBuf(200, &arr) } func InsertTcgddtcgdd22(ctx *gin.Context) { var arr Tcgddtcgdd2 var r Res ctx.ShouldBindBodyWith(&arr, binding.ProtoBuf) tx, _ := db.Begin() for _, row := range arr.TCGDD { s := "insert into Tcgdd(Billid,Adddate,Supplierid,Suppliername,Warehouseid,Warehousename,Makerid,Note,Shenhe,Maker) values ("+public.SqlStr(row.Billid)+","+public.SqlStr(row.Adddate)+","+public.SqlStr(row.Supplierid)+","+public.SqlStr(row.Suppliername)+","+public.SqlStr(row.Warehouseid)+","+public.SqlStr(row.Warehousename)+","+public.SqlStr(row.Makerid)+","+public.SqlStr(row.Note)+","+public.SqlStr(row.Shenhe)+","+public.SqlStr(row.Maker)+")" _, err := tx.Exec(s) if err != nil { tx.Rollback() r.Status = 500 r.Message = "fail" r.Exception = err.Error() ctx.ProtoBuf(500, &r) public.Log("InsertTcgddtcgdd22()" + err.Error()) return } } for _, row := range arr.TCGDD2 { s := "insert into Tcgdd2(Billid,Recno,Goodsid,Goodsname,Gg,Dw,Dj,Qty,Amount) values ("+public.SqlStr(row.Billid)+","+string(row.Recno)+","+public.SqlStr(row.Goodsid)+","+public.SqlStr(row.Goodsname)+","+public.SqlStr(row.Gg)+","+public.SqlStr(row.Dw)+","+strconv.FormatFloat(row.Dj, 'f', 4, 30)+","+strconv.FormatFloat(row.Qty, 'f', 4, 30)+","+strconv.FormatFloat(row.Amount, 'f', 4, 30)+")" _, err := tx.Exec(s) if err != nil { tx.Rollback() r.Status = 500 r.Message = "fail" r.Exception = err.Error() ctx.ProtoBuf(500, &r) public.Log("InsertTcgddtcgdd22()" + err.Error()) return } } tx.Commit() r.Status = 200 r.Message = "success" ctx.ProtoBuf(200, &r) } func UpdateTcgddtcgdd22(ctx *gin.Context) { var arr Tcgddtcgdd2 var r Res ctx.ShouldBindBodyWith(&arr, binding.ProtoBuf) tx, _ := db.Begin() for _, row := range arr.TCGDD { s := "update Tcgdd set Billid="+public.SqlStr(row.Billid)+",Adddate="+public.SqlStr(row.Adddate)+",Supplierid="+public.SqlStr(row.Supplierid)+",Suppliername="+public.SqlStr(row.Suppliername)+",Warehouseid="+public.SqlStr(row.Warehouseid)+",Warehousename="+public.SqlStr(row.Warehousename)+",Makerid="+public.SqlStr(row.Makerid)+",Note="+public.SqlStr(row.Note)+",Shenhe="+public.SqlStr(row.Shenhe)+",Maker="+public.SqlStr(row.Maker)+" where billid="+public.SqlStr(row.Billid) _, err := tx.Exec(s) if err != nil { tx.Rollback() r.Status = 500 r.Message = "fail" r.Exception = err.Error() ctx.ProtoBuf(500, &r) public.Log("UpdateTcgddtcgdd22()" + err.Error()) return } } for _, row := range arr.TCGDD2 { s := "update Tcgdd2 set Billid="+public.SqlStr(row.Billid)+",Recno="+string(row.Recno)+",Goodsid="+public.SqlStr(row.Goodsid)+",Goodsname="+public.SqlStr(row.Goodsname)+",Gg="+public.SqlStr(row.Gg)+",Dw="+public.SqlStr(row.Dw)+",Dj="+strconv.FormatFloat(row.Dj, 'f', 4, 30)+",Qty="+strconv.FormatFloat(row.Qty, 'f', 4, 30)+",Amount="+strconv.FormatFloat(row.Amount, 'f', 4, 30)+" where billid="+public.SqlStr(row.Billid)+" and recno="+string(row.Recno) _, err := tx.Exec(s) if err != nil { tx.Rollback() r.Status = 500 r.Message = "fail" r.Exception = err.Error() ctx.ProtoBuf(500, &r) public.Log("UpdateTcgddtcgdd22()" + err.Error()) return } } tx.Commit() r.Status = 200 r.Message = "success" ctx.ProtoBuf(200, &r) } func DeleteTcgddtcgdd22(ctx *gin.Context) { var r Res var s string var err error where := ctx.Param("where") where = strings.Trim(where, "/") tx, _ := db.Begin() s = "delete from Tcgdd where " + where _, err = tx.Exec(s) if err != nil { tx.Rollback() r.Status = 500 r.Message = "fail" r.Exception = err.Error() ctx.ProtoBuf(500, &r) public.Log("DeleteTcgddtcgdd22()" + err.Error()) return } s = "delete from Tcgdd2 where " + where _, err = tx.Exec(s) if err != nil { tx.Rollback() r.Status = 500 r.Message = "fail" r.Exception = err.Error() ctx.ProtoBuf(500, &r) public.Log("DeleteTcgddtcgdd22()" + err.Error()) return } tx.Commit() r.Status = 200 r.Message = "success" ctx.ProtoBuf(200, &r) }
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/16608033.html