goods.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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" json:"ownerId,string"`
  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,string"`
  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,string"`
  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" json:"ownerId,string"`
  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. result, err := db.Exec(sqlStr, g.OwnerId, g.Desc, g.Title, g.Price, g.Place, g.State)
  59. if err != nil {
  60. return
  61. }
  62. insertId, err := result.LastInsertId()
  63. if err != nil {
  64. return
  65. }
  66. sqlStr = "INSERT INTO goods_surfaces(id, pic, owner_id, title, price, state) VALUES (?, ?, ?, ?, ?, ?)"
  67. exec, err := db.Exec(sqlStr, insertId, g.Pic[0], g.OwnerId, g.Title, g.Price, 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 owner_id = ?, `desc` = ?, title = ?, price = ?, place = ?, state = ? WHERE id = ?"
  111. _, err := db.Exec(sqlStr, g.OwnerId, g.Desc, g.Title, g.Price, g.Place, g.State, g.Id)
  112. if err != nil {
  113. return err
  114. }
  115. sqlStr = "UPDATE goods_surfaces SET pic = ?, owner_id = ?, title = ?, price = ?, state = ? WHERE id = ?"
  116. _, err = db.Exec(sqlStr, g.Pic[0], g.OwnerId, g.Title, g.Price, 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. fmt.Println(goodsIds)
  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 := 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. }
  200. func (g *GoodsSurface) GetGoodsList(id []uint) (goods []GoodsSurface, err error) {
  201. db := common.DB
  202. sqlStr := "SELECT * FROM goods_surfaces WHERE id IN (?)"
  203. query, args, err := sqlx.In(sqlStr, id)
  204. if err != nil {
  205. return nil, err
  206. }
  207. err = db.Select(&goods, query, args...)
  208. return
  209. }
  210. func getGoodsIds() (ids []uint, err error) {
  211. db := common.DB
  212. sqlStr := "SELECT id FROM goods_surfaces"
  213. err = db.Select(&ids, sqlStr)
  214. return
  215. }