exchange.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "net/url"
  8. "strconv"
  9. "go-common/app/interface/main/growup/model"
  10. vip "go-common/app/service/main/vip/model"
  11. xsql "go-common/library/database/sql"
  12. "go-common/library/log"
  13. )
  14. const (
  15. _panelType = "incentive"
  16. )
  17. const (
  18. _goodsInfoSQL = "SELECT ex_product_id,ex_resource_id,display_on_time,goods_type,discount FROM creative_goods WHERE is_display = ?"
  19. _goodsResourceIDSQL = "SELECT ex_resource_id,discount,goods_type FROM creative_goods WHERE ex_product_id = ? AND goods_type = ? AND is_display = 2"
  20. _goodsOrderSQL = "SELECT order_time,goods_name,goods_price,goods_cost FROM creative_order WHERE mid = ? ORDER BY order_time DESC LIMIT ?,?"
  21. _goodsOrderCountSQL = "SELECT count(*) FROM creative_order WHERE mid = ?"
  22. // insert
  23. _txInGoodsOrderSQL = "INSERT INTO creative_order(mid,order_no,order_time,goods_type,goods_id,goods_name,goods_price,goods_cost) VALUES(?,?,?,?,?,?,?,?)"
  24. )
  25. // GetGoodsByProductID get goods by product id
  26. func (d *Dao) GetGoodsByProductID(c context.Context, productID string, goodsType int) (goods *model.GoodsInfo, err error) {
  27. goods = &model.GoodsInfo{}
  28. err = d.db.QueryRow(c, _goodsResourceIDSQL, productID, goodsType).Scan(&goods.ResourceID, &goods.Discount, &goods.GoodsType)
  29. if err == sql.ErrNoRows {
  30. err = nil
  31. }
  32. return
  33. }
  34. // GetGoodsOrderCount get order count
  35. func (d *Dao) GetGoodsOrderCount(c context.Context, mid int64) (count int, err error) {
  36. err = d.db.QueryRow(c, _goodsOrderCountSQL, mid).Scan(&count)
  37. if err == sql.ErrNoRows {
  38. err = nil
  39. }
  40. return
  41. }
  42. // GetGoodsOrders get orders
  43. func (d *Dao) GetGoodsOrders(c context.Context, mid int64, start, end int) (orders []*model.GoodsOrder, err error) {
  44. orders = make([]*model.GoodsOrder, 0)
  45. rows, err := d.db.Query(c, _goodsOrderSQL, mid, start, end)
  46. if err != nil {
  47. log.Error("d.db.Query error(%v)", err)
  48. return
  49. }
  50. defer rows.Close()
  51. for rows.Next() {
  52. g := &model.GoodsOrder{}
  53. err = rows.Scan(&g.OrderTime, &g.GoodsName, &g.GoodsPrice, &g.GoodsCost)
  54. if err != nil {
  55. log.Error("rows.Scan error(%v)", err)
  56. return
  57. }
  58. orders = append(orders, g)
  59. }
  60. err = rows.Err()
  61. return
  62. }
  63. // GetDisplayGoods get up from up_activity
  64. func (d *Dao) GetDisplayGoods(c context.Context, isDisplay int) (goods []*model.GoodsInfo, err error) {
  65. goods = make([]*model.GoodsInfo, 0)
  66. rows, err := d.db.Query(c, _goodsInfoSQL, isDisplay)
  67. if err != nil {
  68. log.Error("d.db.Query error(%v)", err)
  69. return
  70. }
  71. defer rows.Close()
  72. for rows.Next() {
  73. g := &model.GoodsInfo{}
  74. err = rows.Scan(&g.ProductID, &g.ResourceID, &g.DisplayOnTime, &g.GoodsType, &g.Discount)
  75. if err != nil {
  76. log.Error("rows.Scan error(%v)", err)
  77. return
  78. }
  79. goods = append(goods, g)
  80. }
  81. err = rows.Err()
  82. return
  83. }
  84. // TxInsertGoodsOrder insert goods order
  85. func (d *Dao) TxInsertGoodsOrder(tx *xsql.Tx, o *model.GoodsOrder) (rows int64, err error) {
  86. res, err := tx.Exec(_txInGoodsOrderSQL, o.MID, o.OrderNo, o.OrderTime, o.GoodsType, o.GoodsID, o.GoodsName, o.GoodsPrice, o.GoodsCost)
  87. if err != nil {
  88. log.Error("d.db.Exec(%s) error(%v)", _txInGoodsOrderSQL, err)
  89. return
  90. }
  91. return res.RowsAffected()
  92. }
  93. // ListVipProducts list vip products
  94. func (d *Dao) ListVipProducts(c context.Context, mid int64) (r map[string]*model.GoodsInfo, err error) {
  95. r = make(map[string]*model.GoodsInfo)
  96. res, err := d.vip.VipPanelInfo5(c, &vip.ArgPanel{PanelType: _panelType, Mid: mid})
  97. if err != nil {
  98. log.Error("vipRPC.VipPanelInfo5 err(%v)", err)
  99. return
  100. }
  101. if _, err = json.Marshal(res.Vps); err != nil {
  102. log.Error("json.Marshal err(%v)", err)
  103. return
  104. }
  105. for _, v := range res.Vps {
  106. m := new(model.GoodsInfo)
  107. m.ProductID = v.PdID
  108. m.ProductName = v.PdName
  109. // 大会员实时价格 = 激励兑换实时成本价; 单位元转换为单位分
  110. m.OriginPrice = int64(Round(Mul(v.DPrice, float64(100)), 0))
  111. m.Month = v.Month
  112. r[v.PdID] = m
  113. }
  114. return
  115. }
  116. // ExchangeBigVIP exchange big vip
  117. func (d *Dao) ExchangeBigVIP(c context.Context, mid, resourceID, orderNo int64, remark string) (err error) {
  118. params := url.Values{}
  119. params.Set("batchId", strconv.FormatInt(resourceID, 10))
  120. params.Set("mid", strconv.FormatInt(mid, 10))
  121. params.Set("orderNo", strconv.FormatInt(orderNo, 10))
  122. params.Set("remark", remark)
  123. var res struct {
  124. Code int `json:"code"`
  125. Message string `json:"message"`
  126. }
  127. url := d.c.Host.VipURI
  128. if err = d.httpRead.Post(c, url, "", params, &res); err != nil {
  129. log.Error("d.client.Post url(%s) error(%v)", url+"?"+params.Encode(), err)
  130. return
  131. }
  132. if res.Code != 0 {
  133. log.Error("ExchangeBigVIP code != 0. res.Code(%d) | url(%s) error(%v)", res.Code, url+"?"+params.Encode(), res.Message)
  134. err = fmt.Errorf("ExchangeBigVIP error(%s)", res.Message)
  135. }
  136. return
  137. }