package goods import ( "net/http" "strconv" "mtp2_if/db" "github.com/gin-gonic/gin" ) // Goods 商品表结构定义 type Goods struct { Goodsid int `json:"goodsid" xorm:"'GOODSID'"` Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` Goodsname string `json:"goodsname" xorm:"'GOODSNAME'"` Marketid int `json:"marketid" xorm:"'MARKETID'"` } // TableName 返回表名接口 func (Goods) TableName() string { return "GOODS" } // QueryReq 商品查询请求 type QueryReq struct { Token string `json:"token" binding:"required"` GoodsID int `json:"goodsid" binding:"required"` } // QueryRsp 商品查询结果 type QueryRsp struct { GoodsID int `json:"goodsid" binding:"required"` GoodsCode string `json:"goodscode" binding:"required"` GoodsName string `json:"goodsname" binding:"required"` MarketID int `json:"marketid" binding:"required"` } // QueryAllReq 所有商品查询请求 type QueryAllReq struct { Token string `json:"token" binding:"required"` //MarketID int `json:"marketid" binding:"required"` } // QueryAllRsp 所有商品查询结果 type QueryAllRsp struct { Goodss []Goods `json:"goodss"` } // QueryGoods 商品信息查询 func QueryGoods(c *gin.Context) { var goods QueryReq err := c.ShouldBind(&goods) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } sql := "select goodscode, goodsname, marketid from goods where goodsid = ?" engine := db.GetEngine() results, err := engine.QueryString(sql, goods.GoodsID) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } if len(results) == 0 { c.JSON(http.StatusBadRequest, gin.H{"error": "cont find"}) } goodscode := results[0]["GOODSCODE"] goodsname := results[0]["GOODSNAME"] marketid, _ := strconv.Atoi(results[0]["MARKETID"]) ret := QueryRsp{ GoodsID: goods.GoodsID, GoodsCode: goodscode, GoodsName: goodsname, MarketID: marketid, } c.SecureJSON(http.StatusOK, ret) } // GetGoods 商品信息查询 func GetGoods(c *gin.Context) { var goods QueryReq err := c.ShouldBind(&goods) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } engine := db.GetEngine() goodsinfo := new(Goods) has, err := engine.Where("goodsid=?", goods.GoodsID).And("marketid=?", 200).Get(goodsinfo) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if !has { c.JSON(http.StatusBadRequest, gin.H{"error": "not find record"}) return } ret := QueryRsp{ GoodsID: goods.GoodsID, GoodsCode: goodsinfo.Goodscode, GoodsName: goodsinfo.Goodsname, MarketID: goodsinfo.Marketid, } c.SecureJSON(http.StatusOK, ret) } // GetAllGoods 所有商品信息查询 func GetAllGoods(c *gin.Context) { var goods QueryAllReq err := c.ShouldBind(&goods) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } engine := db.GetEngine() goodss := make([]Goods, 0) err = engine.Find(&goodss) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } ret := QueryAllRsp{ Goodss: goodss, } c.SecureJSON(http.StatusOK, ret) }