orders.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package model
  2. import (
  3. "trading-go/common"
  4. )
  5. type Order struct {
  6. Id uint `db:"id"`
  7. GoodsId uint `db:"goods_id"`
  8. BuyerId uint `db:"buyer_id"`
  9. Time uint `db:"time"`
  10. Place string `db:"place"`
  11. Phone string `db:"phone"`
  12. State int `db:"state"`
  13. }
  14. type OrderJson struct {
  15. GoodsId uint `json:"goodsId"`
  16. BuyerId uint `json:"buyerId"`
  17. Time uint `json:"time"`
  18. Place string `json:"place"`
  19. Phone string `json:"phone"`
  20. State int `json:"state"`
  21. }
  22. func (o OrderJson) Change() Order {
  23. return Order{
  24. GoodsId: o.GoodsId,
  25. BuyerId: o.BuyerId,
  26. Time: o.Time,
  27. Place: o.Place,
  28. Phone: o.Phone,
  29. State: o.State,
  30. }
  31. }
  32. func (o Order) GetAll() (orders []Order, err error) {
  33. db := common.DB
  34. sqlStr := "SELECT * FROM orders"
  35. err = db.Select(&orders, sqlStr)
  36. return
  37. }
  38. func (o Order) GetOwner(ownerId uint) (orders []Order, err error) {
  39. db := common.DB
  40. sqlStr := "SELECT * FROM orders WHERE buyer_id = ?"
  41. err = db.Select(&orders, sqlStr, ownerId)
  42. return
  43. }
  44. func (o Order) Revise() error {
  45. db := common.DB
  46. sqlStr := "UPDATE orders SET state = ? WHERE id = ?"
  47. _, err := db.Exec(sqlStr, o.State, o.Id)
  48. return err
  49. }
  50. func (o Order) Create() error {
  51. db := common.DB
  52. sqlStr := "INSERT INTO orders(goods_id, buyer_id, time, place, phone, state) VALUES(:goods_id, :buyer_id, :time, :place, :phone , :state)"
  53. _, err := db.NamedExec(sqlStr, o)
  54. return err
  55. }
  56. func (o Order) Delete() error {
  57. db := common.DB
  58. sqlStr := "DELETE FROM orders WHERE id = ?"
  59. _, err := db.Exec(sqlStr, o.Id)
  60. return err
  61. }