orders.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package model
  2. import (
  3. "trading-go/common"
  4. "trading-go/response"
  5. )
  6. type Order struct {
  7. Id uint `db:"id"`
  8. GoodsId uint `db:"goods_id"`
  9. BuyerId uint `db:"buyer_id"`
  10. Time uint `db:"time"`
  11. Place string `db:"place"`
  12. Phone string `db:"phone"`
  13. State int `db:"state"`
  14. }
  15. type OrderJson struct {
  16. GoodsId uint `json:"goodsId"`
  17. BuyerId uint `json:"buyerId"`
  18. Time uint `json:"time"`
  19. Place string `json:"place"`
  20. Phone string `json:"phone"`
  21. State int `json:"state"`
  22. }
  23. type OrderSurface struct {
  24. GoodsImg string `json:"goodsImg" db:"pic"`
  25. Price uint `json:"price" db:"price"`
  26. State int `json:"state" db:"state"`
  27. Title string `json:"title" db:"title"`
  28. BName string `json:"bName" db:"name"`
  29. Bid uint `json:"bid,string" db:"uid"`
  30. }
  31. func (o OrderJson) Change() Order {
  32. return Order{
  33. GoodsId: o.GoodsId,
  34. BuyerId: o.BuyerId,
  35. Time: o.Time,
  36. Place: o.Place,
  37. Phone: o.Phone,
  38. State: o.State,
  39. }
  40. }
  41. func (o Order) GetAll() (orders []Order, err error) {
  42. db := common.DB
  43. sqlStr := "SELECT * FROM orders"
  44. err = db.Select(&orders, sqlStr)
  45. return
  46. }
  47. func (o Order) GetOwner(ownerId uint) (orderSurface []OrderSurface, err error) {
  48. db := common.DB
  49. sqlStr := `SELECT g.pic, g.price, o.state, g.title, u.name, u.uid
  50. FROM orders as o, goods_surfaces as g, users as u
  51. WHERE o.buyer_id = ? AND o.goods_id = g.id AND g.owner_id = u.uid ;`
  52. err = db.Select(&orderSurface, sqlStr, ownerId)
  53. return
  54. }
  55. func (o Order) Revise() error {
  56. db := common.DB
  57. sqlStr := "UPDATE orders SET state = ? WHERE id = ?"
  58. _, err := db.Exec(sqlStr, o.State, o.Id)
  59. return err
  60. }
  61. func (o Order) Create() error {
  62. db := common.DB
  63. sqlStr := "INSERT INTO orders(goods_id, buyer_id, time, place, phone, state) VALUES(:goods_id, :buyer_id, :time, :place, :phone , :state)"
  64. _, err := db.NamedExec(sqlStr, o)
  65. return err
  66. }
  67. func (o Order) Delete() error {
  68. db := common.DB
  69. sqlStr := "DELETE FROM orders WHERE id = ?"
  70. _, err := db.Exec(sqlStr, o.Id)
  71. return err
  72. }
  73. func (o Order) GetPaged(page, pageSize int) (rsp response.PageResponse, err error) {
  74. var orders []Order
  75. db := common.DB
  76. sqlStr := "SELECT * FROM orders LIMIT ?,?"
  77. rsp.Page = page
  78. rsp.PageSize = pageSize
  79. err = db.Select(&orders, sqlStr, (page-1)*pageSize, pageSize)
  80. rsp.Data = orders
  81. return
  82. }
  83. func (o Order) GetCnt() (cnt int, err error) {
  84. db := common.DB
  85. sqlStr := "SELECT COUNT(1) FROM orders"
  86. err = db.Get(&cnt, sqlStr)
  87. return
  88. }