trade.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/growup/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. // TradeDao . r/w
  11. type TradeDao interface {
  12. AddGoods(c context.Context, fields string, values string) (int64, error)
  13. UpdateGoods(c context.Context, set string, where string, IDs []int64) (int64, error)
  14. GoodsCount(c context.Context, where string) (int64, error)
  15. GoodsList(c context.Context, where string, from, limit int) ([]*model.GoodsInfo, error)
  16. OrderList(c context.Context, where string, from, limit int) ([]*model.OrderInfo, error)
  17. OrderCount(c context.Context, where string) (int64, error)
  18. }
  19. const (
  20. _addGoodsSQL = "INSERT INTO creative_goods(%s) VALUES %s"
  21. _updateGoodsSQL = "UPDATE creative_goods SET %s WHERE id in (%s)"
  22. _listGoodsSQL = "SELECT id,goods_type,ex_product_id,ex_resource_id,is_display,display_on_time,discount FROM creative_goods WHERE %s ORDER By id DESC LIMIT ?,?"
  23. _listOrderSQL = "SELECT mid, order_no, order_time, goods_id, goods_type, goods_name, goods_price, goods_cost FROM creative_order WHERE %s ORDER BY order_time DESC LIMIT ?,?"
  24. _countOrderSQL = "SELECT count(id) FROM creative_order WHERE %s"
  25. _countGoodsSQL = "SELECT count(id) FROM creative_goods WHERE %s"
  26. )
  27. // AddGoods .
  28. func (d *Dao) AddGoods(c context.Context, fields string, values string) (int64, error) {
  29. rows, err := d.rddb.Exec(c, fmt.Sprintf(_addGoodsSQL, fields, values))
  30. if err != nil {
  31. log.Error("d.AddGoods db.Exec err(%v)", err)
  32. return 0, err
  33. }
  34. return rows.RowsAffected()
  35. }
  36. // UpdateGoods .
  37. func (d *Dao) UpdateGoods(c context.Context, set string, where string, IDs []int64) (int64, error) {
  38. s := _updateGoodsSQL
  39. if where != "" {
  40. s = s + " AND " + where
  41. }
  42. rows, err := d.rddb.Exec(c, fmt.Sprintf(s, set, xstr.JoinInts(IDs)))
  43. if err != nil {
  44. log.Error("d.UpdateGoods db.Exec err(%v)", err)
  45. return 0, err
  46. }
  47. return rows.RowsAffected()
  48. }
  49. // GoodsList .
  50. func (d *Dao) GoodsList(c context.Context, where string, from, limit int) (res []*model.GoodsInfo, err error) {
  51. res = make([]*model.GoodsInfo, 0)
  52. rows, err := d.rddb.Query(c, fmt.Sprintf(_listGoodsSQL, where), from, limit)
  53. if err != nil {
  54. log.Error("d.GoodsList db.Query err(%v)", err)
  55. return
  56. }
  57. defer rows.Close()
  58. for rows.Next() {
  59. m := new(model.GoodsInfo)
  60. if err = rows.Scan(&m.ID, &m.GoodsType, &m.ProductID, &m.ResourceID, &m.IsDisplay, &m.DisplayOnTime, &m.Discount); err != nil {
  61. log.Error("d.GoodsList rows.Scan err(%v)", err)
  62. return nil, err
  63. }
  64. res = append(res, m)
  65. }
  66. err = rows.Err()
  67. return
  68. }
  69. // GoodsCount .
  70. func (d *Dao) GoodsCount(c context.Context, where string) (total int64, err error) {
  71. err = d.rddb.QueryRow(c, fmt.Sprintf(_countGoodsSQL, where)).Scan(&total)
  72. if err == sql.ErrNoRows {
  73. err = nil
  74. }
  75. return
  76. }
  77. // OrderCount .
  78. func (d *Dao) OrderCount(c context.Context, where string) (total int64, err error) {
  79. err = d.rddb.QueryRow(c, fmt.Sprintf(_countOrderSQL, where)).Scan(&total)
  80. if err == sql.ErrNoRows {
  81. err = nil
  82. }
  83. return
  84. }
  85. // OrderList .
  86. func (d *Dao) OrderList(c context.Context, where string, from, limit int) (res []*model.OrderInfo, err error) {
  87. res = make([]*model.OrderInfo, 0)
  88. rows, err := d.rddb.Query(c, fmt.Sprintf(_listOrderSQL, where), from, limit)
  89. if err != nil {
  90. log.Error("d.OrderList db.Query err(%v)", err)
  91. return
  92. }
  93. defer rows.Close()
  94. for rows.Next() {
  95. v := new(model.OrderInfo)
  96. if err = rows.Scan(&v.MID, &v.OrderNo, &v.OrderTime, &v.GoodsID, &v.GoodsType, &v.GoodsName, &v.GoodsPrice, &v.GoodsCost); err != nil {
  97. log.Error("d.OrderList rows.Scan err(%v)", err)
  98. return
  99. }
  100. res = append(res, v)
  101. }
  102. err = rows.Err()
  103. return
  104. }