panel.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/vip/model"
  6. "go-common/library/database/sql"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _vipPayOrderSuccsSQL = "SELECT id,order_type,buy_months FROM vip_pay_order WHERE mid = ? and status= 2"
  11. _vipPriceConfigsSQL = "SELECT id,platform,product_name,product_id,suit_type,month,sub_type,original_price,selected,remark,ctime,mtime,superscript,start_build,end_build FROM vip_price_config_v2 WHERE status = 0"
  12. _vipPriceDiscountConfigsSQL = "SELECT vpc_id,product_id,discount_price,stime,etime,remark,ctime,mtime,first_price FROM vip_price_discount_config_v2 WHERE stime <= ? AND ((etime > ? AND etime <> '1970-01-01 08:00:00') OR (etime = '1970-01-01 08:00:00'))"
  13. _vipPriceDiscountByProductIDSQL = "SELECT vpc_id,product_id,discount_price,stime,etime,remark,ctime,mtime FROM vip_price_discount_config_v2 WHERE product_id = ? ORDER BY mtime DESC"
  14. _vipPriceByProductIDSQL = "SELECT id,platform,product_name,product_id,suit_type,month,sub_type,original_price,selected,remark,ctime,mtime,superscript FROM vip_price_config_v2 WHERE status = 0 AND product_id = ? ORDER BY mtime DESC LIMIT 1"
  15. _vipPriceByIDSQL = "SELECT id,platform,product_name,product_id,suit_type,month,sub_type,original_price,selected,remark,ctime,mtime,superscript,start_build,end_build FROM vip_price_config_v2 WHERE id = ?"
  16. )
  17. // VipPayOrderSuccs get succ of vip pay orders.
  18. func (d *Dao) VipPayOrderSuccs(c context.Context, mid int64) (mpo map[string]struct{}, err error) {
  19. var rows *sql.Rows
  20. if rows, err = d.db.Query(c, _vipPayOrderSuccsSQL, mid); err != nil {
  21. err = errors.WithStack(err)
  22. return
  23. }
  24. defer rows.Close()
  25. mpo = make(map[string]struct{})
  26. for rows.Next() {
  27. po := new(model.PayOrder)
  28. if err = rows.Scan(&po.ID, &po.OrderType, &po.BuyMonths); err != nil {
  29. if err != sql.ErrNoRows {
  30. err = errors.WithStack(err)
  31. return
  32. }
  33. mpo = nil
  34. err = nil
  35. return
  36. }
  37. mpo[po.DoPayOrderTypeKey()] = struct{}{}
  38. }
  39. err = rows.Err()
  40. return
  41. }
  42. // VipPriceConfigs get vip price configs.
  43. func (d *Dao) VipPriceConfigs(c context.Context) (vpcs []*model.VipPriceConfig, err error) {
  44. var rows *sql.Rows
  45. if rows, err = d.db.Query(c, _vipPriceConfigsSQL); err != nil {
  46. err = errors.WithStack(err)
  47. return
  48. }
  49. defer rows.Close()
  50. for rows.Next() {
  51. vpc := new(model.VipPriceConfig)
  52. if err = rows.Scan(&vpc.ID, &vpc.Plat, &vpc.PdName, &vpc.PdID, &vpc.SuitType, &vpc.Month, &vpc.SubType, &vpc.OPrice,
  53. &vpc.Selected, &vpc.Remark, &vpc.CTime, &vpc.MTime, &vpc.Superscript, &vpc.StartBuild, &vpc.EndBuild); err != nil {
  54. if err != sql.ErrNoRows {
  55. err = errors.WithStack(err)
  56. return
  57. }
  58. vpcs = nil
  59. err = nil
  60. return
  61. }
  62. vpcs = append(vpcs, vpc)
  63. }
  64. err = rows.Err()
  65. return
  66. }
  67. // VipPriceDiscountConfigs get vip price discount configs.
  68. func (d *Dao) VipPriceDiscountConfigs(c context.Context) (mvp map[int64]*model.VipDPriceConfig, err error) {
  69. var (
  70. rows *sql.Rows
  71. now = time.Now()
  72. )
  73. mvp = make(map[int64]*model.VipDPriceConfig)
  74. if rows, err = d.db.Query(c, _vipPriceDiscountConfigsSQL, now, now); err != nil {
  75. err = errors.WithStack(err)
  76. return
  77. }
  78. defer rows.Close()
  79. for rows.Next() {
  80. vpc := new(model.VipDPriceConfig)
  81. if err = rows.Scan(&vpc.ID, &vpc.PdID, &vpc.DPrice, &vpc.STime, &vpc.ETime, &vpc.Remark, &vpc.CTime, &vpc.MTime, &vpc.FirstPrice); err != nil {
  82. if err != sql.ErrNoRows {
  83. err = errors.WithStack(err)
  84. return
  85. }
  86. mvp = nil
  87. err = nil
  88. return
  89. }
  90. mvp[vpc.ID] = vpc
  91. }
  92. err = rows.Err()
  93. return
  94. }
  95. //VipPriceDiscountByProductID select vip price discount by product id.
  96. func (d *Dao) VipPriceDiscountByProductID(c context.Context, productID string) (vpc []*model.VipDPriceConfig, err error) {
  97. var rows *sql.Rows
  98. if rows, err = d.db.Query(c, _vipPriceDiscountByProductIDSQL, productID); err != nil {
  99. err = errors.WithStack(err)
  100. return
  101. }
  102. defer rows.Close()
  103. for rows.Next() {
  104. v := new(model.VipDPriceConfig)
  105. if err = rows.Scan(&v.ID, &v.PdID, &v.DPrice, &v.STime, &v.ETime, &v.Remark, &v.CTime, &v.MTime); err != nil {
  106. err = errors.WithStack(err)
  107. vpc = nil
  108. err = nil
  109. return
  110. }
  111. vpc = append(vpc, v)
  112. }
  113. err = rows.Err()
  114. return
  115. }
  116. //VipPriceByProductID select vip price by product id.
  117. func (d *Dao) VipPriceByProductID(c context.Context, productID string) (vpc *model.VipPriceConfig, err error) {
  118. var row = d.db.QueryRow(c, _vipPriceByProductIDSQL, productID)
  119. vpc = new(model.VipPriceConfig)
  120. if err = row.Scan(&vpc.ID, &vpc.Plat, &vpc.PdName, &vpc.PdID, &vpc.SuitType, &vpc.Month, &vpc.SubType, &vpc.OPrice,
  121. &vpc.Selected, &vpc.Remark, &vpc.CTime, &vpc.MTime, &vpc.Superscript); err != nil {
  122. if err == sql.ErrNoRows {
  123. vpc = nil
  124. err = nil
  125. } else {
  126. err = errors.WithStack(err)
  127. d.errProm.Incr("row_scan_db")
  128. }
  129. }
  130. return
  131. }
  132. //VipPriceByID vip price by id.
  133. func (d *Dao) VipPriceByID(c context.Context, id int64) (vpc *model.VipPriceConfig, err error) {
  134. var row = d.db.QueryRow(c, _vipPriceByIDSQL, id)
  135. vpc = new(model.VipPriceConfig)
  136. if err = row.Scan(&vpc.ID, &vpc.Plat, &vpc.PdName, &vpc.PdID, &vpc.SuitType, &vpc.Month, &vpc.SubType, &vpc.OPrice,
  137. &vpc.Selected, &vpc.Remark, &vpc.CTime, &vpc.MTime, &vpc.Superscript, &vpc.StartBuild, &vpc.EndBuild); err != nil {
  138. if err == sql.ErrNoRows {
  139. vpc = nil
  140. err = nil
  141. } else {
  142. err = errors.WithStack(err)
  143. d.errProm.Incr("row_scan_db")
  144. }
  145. }
  146. return
  147. }