orders.go 2.6 KB

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