user_merchant_h5_list.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package user_merchant
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/common.in/utils"
  6. "gd_management/errors"
  7. "fmt"
  8. "github.com/astaxie/beego/orm"
  9. "go.uber.org/zap"
  10. "time"
  11. )
  12. //var PAGESIZE = 10
  13. func getMerchantH5List(req *apis.ManagementGetMerchantH5ServiceReq, reply *apis.ManagementGetMerchantH5ServiceReply) error {
  14. var maps []orm.Params
  15. o := orm.NewOrm()
  16. err := o.Raw("select count(1) from t_gd_h5_service_merchant where merchant_id=?", req.MerchantId).QueryRow(&reply.Total)
  17. if err != nil {
  18. l.Error("mysql",
  19. zap.String("sql", "select count(1) from t_gd_merchant_data_api"),
  20. zap.String("args", utils.MarshalJsonString(req)),
  21. zap.String("error", err.Error()))
  22. if err == orm.ErrNoRows {
  23. return nil
  24. }
  25. return errors.DataBaseError
  26. }
  27. sql := "SELECT a.count, a.id, a.state," +
  28. "( SELECT service_name FROM t_gd_h5_service WHERE id = a.h5_service_id) AS h5_service_name," +
  29. "( SELECT b.count FROM t_gd_h5_service_access_count AS b WHERE b.h5_service_merchant_id = a.id AND create_time =? ) AS total_used_count," +
  30. "( SELECT b.count FROM t_gd_h5_service_access_count AS b WHERE b.h5_service_merchant_id = a.id AND create_time = ? ) AS day_used_count," +
  31. "a.end_time, a.start_time FROM t_gd_h5_service_merchant as a" +
  32. " where a.merchant_id=?"
  33. zeroDate := "0000-00-00"
  34. nowDate := time.Now().Format("2006-01-02")
  35. if req.IsAll == true {
  36. _, err = o.Raw(sql, zeroDate, nowDate, req.MerchantId).Values(&maps)
  37. } else {
  38. sql = fmt.Sprintf("%s limit ? offset ?", sql)
  39. _, err = o.Raw(sql, zeroDate, nowDate, req.MerchantId, PAGESIZE, req.PageNumber*PAGESIZE).Values(&maps)
  40. }
  41. if err != nil {
  42. l.Error("mysql",
  43. zap.String("sql", sql),
  44. zap.String("args", utils.MarshalJsonString(req)),
  45. zap.String("error", err.Error()))
  46. if err == orm.ErrNoRows {
  47. return nil
  48. }
  49. return errors.DataBaseError
  50. }
  51. var start_time, end_time int64
  52. for _, v := range maps {
  53. item := apis.MerchantH5ServiceItem{}
  54. item.H5ServiceName, _ = v["h5_service_name"].(string)
  55. item.MerchantH5ServiceId = int64(ormStringToInt(v, "id"))
  56. if int64(ormStringToInt(v, "state")) == 0 {
  57. item.State = "正常"
  58. } else {
  59. item.State = "过期"
  60. }
  61. start_time = ormStringToInt(v, "start_time")
  62. end_time = ormStringToInt(v, "end_time")
  63. if end_time == 0 && int(ormStringToInt(v, "count")) == 0 {
  64. item.ComboType = 0
  65. }else if end_time == 0 {
  66. item.ComboType = 1
  67. } else {
  68. item.ComboType = 2
  69. }
  70. if item.ComboType == 1 {
  71. //按总次数
  72. item.TotalCount = int(ormStringToInt(v, "count"))
  73. item.RemainCount = item.TotalCount - int(ormStringToInt(v, "total_used_count"))
  74. if item.RemainCount <= 0 {
  75. item.State = "过期"
  76. }
  77. } else if item.ComboType == 2 {
  78. //按天
  79. item.DayCount = int(ormStringToInt(v, "count"))
  80. item.DayRemainCount = item.DayCount - int(ormStringToInt(v, "day_used_count"))
  81. item.DayNumberCount = getDayNumber(start_time, end_time)
  82. item.DayNumberRemain = getRemainDayNumber(end_time)
  83. if item.DayNumberCount < 0 || item.DayRemainCount <= 0 {
  84. item.State = "过期"
  85. }
  86. }
  87. reply.MerchantH5ServiceList = append(reply.MerchantH5ServiceList, item)
  88. }
  89. reply.PageNumber = req.PageNumber + 1
  90. reply.PageSize = PAGESIZE
  91. return nil
  92. }
  93. func ManagementGetMerchantH5Service(ctx context.Context, req *apis.ManagementGetMerchantH5ServiceReq, reply *apis.ManagementGetMerchantH5ServiceReply) (err error) {
  94. if req.PageNumber > 0 {
  95. req.PageNumber -= 1
  96. } else {
  97. req.PageNumber = 0
  98. }
  99. err = getMerchantH5List(req, reply)
  100. if err != nil {
  101. l.Error("func",
  102. zap.String("call", "ManagementGetMerchantH5Service"),
  103. zap.String("args", utils.MarshalJsonString(req)),
  104. zap.String("error", err.Error()))
  105. }
  106. l.Debug(utils.MarshalJsonString(req, reply))
  107. return
  108. }