goods.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if err != nil {
  113. return err
  114. }
  115. sqlStr = "UPDATE goods_surfaces SET pic_id = ?, owner_id = ?, title = ?, price = ?, integrity = ?, state = ? WHERE id = ?"
  116. _, err = db.Exec(sqlStr, g.PicId, g.OwnerId, g.Title, g.Price, g.Integrity, g.State, g.Id)
  117. return err
  118. }
  119. func (g *Goods) GetDetail() error {
  120. db := common.DB
  121. sqlStr := "SELECT * FROM goods WHERE id = ?"
  122. err := db.Get(g, sqlStr, g.Id)
  123. return err
  124. }
  125. func (g *Goods) GetUserGoods() (goods []Goods, err error) {
  126. db := common.DB
  127. fmt.Println(g.OwnerId)
  128. sqlStr := "SELECT * FROM goods WHERE owner_id = ?"
  129. err = db.Select(&goods, sqlStr, g.OwnerId)
  130. return
  131. }
  132. func (g *GoodsSurface) GetCategoryPaged(page, pageSize int, category uint) (rsp response.PageResponse, err error) {
  133. db := common.DB
  134. var goods []GoodsSurface
  135. var goodsIds []uint
  136. rsp.Page = page
  137. rsp.PageSize = pageSize
  138. sqlStr := "SELECT goods_id FROM category_of_goods WHERE category_id = ?"
  139. err = db.Select(&goodsIds, sqlStr, category)
  140. if err != nil {
  141. return
  142. }
  143. sqlStr = "SELECT * FROM goods_surfaces WHERE id IN (?) LIMIT ?,?"
  144. query, args, err := sqlx.In(sqlStr, goodsIds, (page-1)*pageSize, pageSize)
  145. if err != nil {
  146. return
  147. }
  148. err = db.Select(&goods, query, args...)
  149. rsp.Data = goods
  150. return
  151. }
  152. func (g *GoodsSurface) GetCategoryCnt(category uint) (cnt int, err error) {
  153. db := common.DB
  154. sqlStr := "SELECT COUNT(1) FROM goods_surfaces WHERE id IN (SELECT goods_id FROM category_of_goods WHERE category_id = ?)"
  155. err = db.Get(&cnt, sqlStr, category)
  156. return
  157. }