goods.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package model
  2. import (
  3. "fmt"
  4. "github.com/jmoiron/sqlx"
  5. "trading-go/common"
  6. "trading-go/response"
  7. )
  8. type Goods struct {
  9. Id uint `db:"id"`
  10. PicId uint `db:"pic_id"`
  11. OwnerId uint `db:"owner_id"`
  12. Desc string `db:"desc"`
  13. Title string `db:"title"`
  14. Price uint `db:"price"`
  15. Integrity int `db:"integrity"`
  16. Place string `db:"place"`
  17. State int `db:"state"`
  18. }
  19. type GoodsJson struct {
  20. PicId uint `json:"pic_id"`
  21. OwnerId uint `json:"ownerId"`
  22. Desc string `json:"desc"`
  23. Title string `json:"title"`
  24. Price uint `json:"price"`
  25. Integrity int `json:"integrity"`
  26. Place string `json:"place"`
  27. Categories []uint `json:"categories"`
  28. State int `json:"state"`
  29. }
  30. type GoodsSurfaceJson struct {
  31. PicId uint `json:"picId"`
  32. OwnerId uint `json:"ownerId"`
  33. Title string `json:"title"`
  34. Price uint `json:"price"`
  35. Integrity int `json:"integrity"`
  36. State int `json:"state"`
  37. }
  38. type GoodsSurface struct {
  39. Id uint `db:"id"`
  40. PicId uint `db:"pic_id"`
  41. OwnerId uint `db:"owner_id"`
  42. Title string `db:"title"`
  43. Price uint `db:"price"`
  44. Integrity int `db:"integrity"`
  45. State int `db:"state"`
  46. }
  47. func (g *GoodsJson) Change() Goods {
  48. return Goods{
  49. PicId: g.PicId,
  50. OwnerId: g.OwnerId,
  51. Desc: g.Desc,
  52. Title: g.Title,
  53. Price: g.Price,
  54. Integrity: g.Integrity,
  55. Place: g.Place,
  56. State: g.State,
  57. }
  58. }
  59. func (g *Goods) Create() (id uint, err error) {
  60. db := common.DB
  61. sqlStr := "INSERT INTO goods(pic_id, owner_id, `desc`, title, price, integrity, place, state) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
  62. _, err = db.Exec(sqlStr, g.PicId, g.OwnerId, g.Desc, g.Title, g.Price, g.Integrity, g.Place, g.State)
  63. if err != nil {
  64. return
  65. }
  66. sqlStr = "INSERT INTO goods_surfaces(pic_id, owner_id, title, price, integrity, state) VALUES (?, ?, ?, ?, ?, ?)"
  67. exec, err := db.Exec(sqlStr, g.PicId, g.OwnerId, g.Title, g.Price, g.Integrity, g.State)
  68. if err != nil {
  69. return
  70. }
  71. ids, err := exec.LastInsertId()
  72. id = uint(ids)
  73. if err != nil {
  74. return
  75. }
  76. return
  77. }
  78. func (g *Goods) Delete() error {
  79. db := common.DB
  80. sqlStr := "DELETE FROM goods WHERE id = ?"
  81. _, err := db.Exec(sqlStr, g.Id)
  82. if err != nil {
  83. return err
  84. }
  85. sqlStr = "DELETE FROM goods_surfaces WHERE id = ?"
  86. _, err = db.Exec(sqlStr, g.Id)
  87. if err != nil {
  88. return err
  89. }
  90. return nil
  91. }
  92. func (g *GoodsSurface) GetPaged(page, pageSize int) (rsp response.PageResponse, err error) {
  93. var goods []GoodsSurface
  94. db := common.DB
  95. sqlStr := "SELECT * FROM goods_surfaces LIMIT ?,?"
  96. rsp.Page = page
  97. rsp.PageSize = pageSize
  98. err = db.Select(&goods, sqlStr, (page-1)*pageSize, pageSize)
  99. rsp.Data = goods
  100. return
  101. }
  102. func (g *GoodsSurface) GetCnt() (cnt int, err error) {
  103. db := common.DB
  104. sqlStr := "SELECT COUNT(1) FROM goods_surfaces"
  105. err = db.Get(&cnt, sqlStr)
  106. return
  107. }
  108. func (g *Goods) Revise() error {
  109. db := common.DB
  110. sqlStr := "UPDATE goods SET pic_id = ?, owner_id = ?, `desc` = ?, title = ?, price = ?, integrity = ?, place = ?, state = ? WHERE id = ?"
  111. _, err := db.Exec(sqlStr, g.PicId, g.OwnerId, g.Desc, g.Title, g.Price, g.Integrity, g.Place, g.State, g.Id)
  112. return err
  113. }
  114. func (g *Goods) GetDetail() error {
  115. db := common.DB
  116. fmt.Println(g.Id)
  117. sqlStr := "SELECT * FROM goods WHERE id = ?"
  118. err := db.Get(&g, sqlStr, g.Id)
  119. return err
  120. }
  121. func (g *Goods) GetUserGoods() (goods []Goods, err error) {
  122. db := common.DB
  123. sqlStr := "SELECT * FROM goods WHERE owner_id = ?"
  124. err = db.Select(&goods, sqlStr, g.OwnerId)
  125. return
  126. }
  127. func (g *GoodsSurface) GetCategoryPaged(page, pageSize int, category uint) (rsp response.PageResponse, err error) {
  128. db := common.DB
  129. var goods []GoodsSurface
  130. var goodsIds []uint
  131. rsp.Page = page
  132. rsp.PageSize = pageSize
  133. sqlStr := "SELECT goods_id FROM category_of_goods WHERE category_id = ?"
  134. err = db.Select(&goodsIds, sqlStr, category)
  135. if err != nil {
  136. return
  137. }
  138. sqlStr = "SELECT * FROM goods_surfaces WHERE id IN (?) LIMIT ?,?"
  139. query, args, err := sqlx.In(sqlStr, goodsIds, (page-1)*pageSize, pageSize)
  140. if err != nil {
  141. return
  142. }
  143. fmt.Println(query, args)
  144. err = db.Select(&goods, query, args...)
  145. rsp.Data = goods
  146. return
  147. }
  148. func (g *GoodsSurface) GetCategoryCnt(category uint) (cnt int, err error) {
  149. db := common.DB
  150. sqlStr := "SELECT COUNT(1) FROM goods_surfaces WHERE id IN (SELECT goods_id FROM category_of_goods WHERE category_id = ?)"
  151. err = db.Get(&cnt, sqlStr, category)
  152. return
  153. }