goods.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package goods
  2. import (
  3. "net/http"
  4. "strconv"
  5. "mtp2_if/db"
  6. "github.com/gin-gonic/gin"
  7. )
  8. // Goods 商品表结构定义
  9. type Goods struct {
  10. Goodsid int `json:"goodsid" xorm:"'GOODSID'"`
  11. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"`
  12. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'"`
  13. Marketid int `json:"marketid" xorm:"'MARKETID'"`
  14. }
  15. // TableName 返回表名接口
  16. func (Goods) TableName() string {
  17. return "GOODS"
  18. }
  19. // QueryReq 商品查询请求
  20. type QueryReq struct {
  21. Token string `json:"token" binding:"required"`
  22. GoodsID int `json:"goodsid" binding:"required"`
  23. }
  24. // QueryRsp 商品查询结果
  25. type QueryRsp struct {
  26. GoodsID int `json:"goodsid" binding:"required"`
  27. GoodsCode string `json:"goodscode" binding:"required"`
  28. GoodsName string `json:"goodsname" binding:"required"`
  29. MarketID int `json:"marketid" binding:"required"`
  30. }
  31. // QueryAllReq 所有商品查询请求
  32. type QueryAllReq struct {
  33. Token string `json:"token" binding:"required"`
  34. //MarketID int `json:"marketid" binding:"required"`
  35. }
  36. // QueryAllRsp 所有商品查询结果
  37. type QueryAllRsp struct {
  38. Goodss []Goods `json:"goodss"`
  39. }
  40. // QueryGoods 商品信息查询
  41. func QueryGoods(c *gin.Context) {
  42. var goods QueryReq
  43. err := c.ShouldBind(&goods)
  44. if err != nil {
  45. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  46. }
  47. sql := "select goodscode, goodsname, marketid from goods where goodsid = ?"
  48. engine := db.GetEngine()
  49. results, err := engine.QueryString(sql, goods.GoodsID)
  50. if err != nil {
  51. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  52. }
  53. if len(results) == 0 {
  54. c.JSON(http.StatusBadRequest, gin.H{"error": "cont find"})
  55. }
  56. goodscode := results[0]["GOODSCODE"]
  57. goodsname := results[0]["GOODSNAME"]
  58. marketid, _ := strconv.Atoi(results[0]["MARKETID"])
  59. ret := QueryRsp{
  60. GoodsID: goods.GoodsID,
  61. GoodsCode: goodscode,
  62. GoodsName: goodsname,
  63. MarketID: marketid,
  64. }
  65. c.SecureJSON(http.StatusOK, ret)
  66. }
  67. // GetGoods 商品信息查询
  68. func GetGoods(c *gin.Context) {
  69. var goods QueryReq
  70. err := c.ShouldBind(&goods)
  71. if err != nil {
  72. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  73. }
  74. engine := db.GetEngine()
  75. goodsinfo := new(Goods)
  76. has, err := engine.Where("goodsid=?", goods.GoodsID).And("marketid=?", 200).Get(goodsinfo)
  77. if err != nil {
  78. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  79. return
  80. }
  81. if !has {
  82. c.JSON(http.StatusBadRequest, gin.H{"error": "not find record"})
  83. return
  84. }
  85. ret := QueryRsp{
  86. GoodsID: goods.GoodsID,
  87. GoodsCode: goodsinfo.Goodscode,
  88. GoodsName: goodsinfo.Goodsname,
  89. MarketID: goodsinfo.Marketid,
  90. }
  91. c.SecureJSON(http.StatusOK, ret)
  92. }
  93. // GetAllGoods 所有商品信息查询
  94. func GetAllGoods(c *gin.Context) {
  95. var goods QueryAllReq
  96. err := c.ShouldBind(&goods)
  97. if err != nil {
  98. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  99. }
  100. engine := db.GetEngine()
  101. goodss := make([]Goods, 0)
  102. err = engine.Find(&goodss)
  103. if err != nil {
  104. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  105. return
  106. }
  107. ret := QueryAllRsp{
  108. Goodss: goodss,
  109. }
  110. c.SecureJSON(http.StatusOK, ret)
  111. }