| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 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)
- }
|