goods.go 5.7 KB

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