provider.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package utils
  4. import (
  5. "context"
  6. "gd_service/apis"
  7. "gd_service/errors"
  8. "gd_service/rpc_apis"
  9. "strconv"
  10. "strings"
  11. "gd_service/common.in/utils"
  12. "go.uber.org/zap"
  13. )
  14. const (
  15. ProviderDisable = "数据源被禁用"
  16. ProviderDayCountLimit = "达到当日次数上限"
  17. ProviderRetainMarginLimit = "达到保有余量次数上限"
  18. )
  19. func CheckProviderAvailable(p apis.MerchantProviderLimitInfo) (bool, string) {
  20. if p.Enable == false || p.State == false {
  21. return false, ProviderDisable
  22. }
  23. if p.DayCount != 0 && p.Count >= p.DayCount {
  24. return false, ProviderDayCountLimit
  25. }
  26. // 设置了每天访问上限
  27. if p.DayCountMaxLimit != 0 {
  28. // 设置了保有量,判断是否达到保有量上限
  29. if p.Inventory != 0 {
  30. if p.Count < p.Inventory {
  31. return true, ""
  32. }
  33. if p.RetainMarginTotalUsed >= p.RetainMargin {
  34. return false, ProviderRetainMarginLimit
  35. }
  36. } else { // 没有设置保有量,判断是否达到保有量余量上限,保有量为-1表示都没设置过保有量(不做校验)
  37. if p.RetainMargin >= 0 && p.RetainMarginTotalUsed >= p.RetainMargin {
  38. return false, ProviderRetainMarginLimit
  39. }
  40. }
  41. }
  42. if len(p.Filters) == 0 || len(p.Params) == 0 {
  43. return true, ""
  44. }
  45. for _, v := range p.Filters {
  46. value, ok := p.Params[v.Name]
  47. if ok == false || value == "" {
  48. continue
  49. }
  50. array := strings.Split(v.Value, ",")
  51. matched := false
  52. for _, filterValue := range array {
  53. if strings.Contains(value, filterValue) == true {
  54. matched = true
  55. break
  56. }
  57. }
  58. if matched == false {
  59. return false, "未通过参数过滤规则"
  60. }
  61. }
  62. return true, ""
  63. }
  64. func GetProviderInfo(merchantChildApiId int64, baseApiId int64) ([]apis.MerchantProviderLimitInfo, error) {
  65. req := apis.ManagementCheckProviderApiReq{}
  66. req.MerchantChildApiId = merchantChildApiId
  67. req.BaseApiId = baseApiId
  68. reply, err := rpc_apis.AuthCheck.ManagementCheckProviderApi(context.Background(), &req)
  69. if err != nil {
  70. l.Error("func",
  71. zap.String("call", "ManagementCheckProviderApi"),
  72. zap.String("args", utils.MarshalJsonString(req)),
  73. zap.String("error", err.Error()))
  74. return nil, errors.ServiceError
  75. }
  76. return reply.MerchantProviderLimitInfoList, nil
  77. }
  78. func GenProviderInfoMap(providerLimitInfo []apis.MerchantProviderLimitInfo) map[string]apis.MerchantProviderLimitInfo {
  79. providerLimitInfoMap := make(map[string]apis.MerchantProviderLimitInfo)
  80. for index, _ := range providerLimitInfo {
  81. providerLimitInfoMap[providerLimitInfo[index].ProviderApiCode+strconv.Itoa(providerLimitInfo[index].GroupNo)] = providerLimitInfo[index]
  82. }
  83. return providerLimitInfoMap
  84. }
  85. func GenProviderInfoMapNew(providerLimitInfo []apis.MerchantProviderLimitInfo) (map[string]apis.MerchantProviderLimitInfo, [][]string) {
  86. var providerSort [][]string
  87. var providerRange []string
  88. providerLimitInfoMap := make(map[string]apis.MerchantProviderLimitInfo)
  89. preGroupNo := -1
  90. for index, _ := range providerLimitInfo {
  91. if preGroupNo == -1 {
  92. preGroupNo = providerLimitInfo[index].GroupNo
  93. } else if providerLimitInfo[index].GroupNo != preGroupNo {
  94. providerSort = append(providerSort, providerRange)
  95. providerRange = []string{}
  96. preGroupNo = providerLimitInfo[index].GroupNo
  97. }
  98. providerRange = append(providerRange, providerLimitInfo[index].ProviderApiCode)
  99. providerLimitInfoMap[providerLimitInfo[index].ProviderApiCode+strconv.Itoa(providerLimitInfo[index].GroupNo)] = providerLimitInfo[index]
  100. }
  101. if len(providerRange) != 0 {
  102. providerSort = append(providerSort, providerRange)
  103. }
  104. return providerLimitInfoMap, providerSort
  105. }