goods.go 5.7 KB

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