• go基于结构的CRUD(JSON)


    go基于结构的CRUD(JSON)

    以采购订单为例。

    //代码由代码工厂自动生成
    //2022-08-20 16:24:24
    package model
    
    import (
        "strconv"
        "database/sql"
        "github.com/gin-gonic/gin/binding"
        "strings"
        "svrGIN/public"
        "github.com/gin-gonic/gin"
    )
    
    func SelectTcgddtcgdd2(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.JSON(500, &r)
            public.Log("SelectTcgddtcgdd2()" + 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.JSON(500, &r)
            public.Log("SelectTcgddtcgdd2()" + 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.JSON(200, &arr)
    }
    
    func InsertTcgddtcgdd2(ctx *gin.Context) {
        var arr Tcgddtcgdd2
        var r Res
        ctx.ShouldBindBodyWith(&arr, binding.JSON)
        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.JSON(500, &r)
                public.Log("InsertTcgddtcgdd2()" + 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.JSON(500, &r)
                public.Log("InsertTcgddtcgdd2()" + err.Error())
                return
            }
        }
        tx.Commit()
        r.Status = 200
        r.Message = "success"
        ctx.JSON(200, &r)
    }
    
    func UpdateTcgddtcgdd2(ctx *gin.Context) {
        var arr Tcgddtcgdd2
        var r Res
        var s string
        var err error
        ctx.ShouldBindBodyWith(&arr, binding.JSON)
        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.JSON(500, &r)
                public.Log("UpdateTcgddtcgdd2()" + 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.JSON(500, &r)
                public.Log("UpdateTcgddtcgdd2()" + err.Error())
                return
            }
        }
        tx.Commit()
        r.Status = 200
        r.Message = "success"
        ctx.JSON(200, &r)
    }
    
    func DeleteTcgddtcgdd2(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.JSON(500, &r)
            public.Log("DeleteTcgddtcgdd2()" + 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.JSON(500, &r)
            public.Log("DeleteTcgddtcgdd2()" + err.Error())
            return
        }
        tx.Commit()
        r.Status = 200
        r.Message = "success"
        ctx.JSON(200, &r)
    }
  • 相关阅读:
    listbox,tree定位item后显示出来
    dbgrideh导出Excel
    ---注册job 设置时间 必须在命令窗口内执行
    四舍五入函数 function MyRound2
    解决delphi7注册过期方法
    Trunc错误浮点计算处理
    BGridEh,同时也用了DBGrid。在OnDrawColmnCell事件中调用DefaultDrawColumnCell,编译时却提示Incom
    不能借助DLL的全局变量来达到两个应用程序间的数据传递,除非使用内存映像文件
    JavaScript实现换肤功能
    echarts图表隐藏之后再展示出现变形
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/16608027.html
Copyright © 2020-2023  润新知