123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package utils
- import (
- "context"
- "gd_vehicle/apis"
- "gd_vehicle/errors"
- "gd_vehicle/rpc_apis"
- "strconv"
- "strings"
- "gd_vehicle/common.in/utils"
- "go.uber.org/zap"
- )
- const (
- ProviderDisable = "数据源被禁用"
- ProviderDayCountLimit = "达到当日次数上限"
- ProviderRetainMarginLimit = "达到保有余量次数上限"
- )
- func CheckProviderAvailable(p apis.MerchantProviderLimitInfo) (bool, string) {
- if p.Enable == false || p.State == false {
- return false, ProviderDisable
- }
- if p.DayCount != 0 && p.Count >= p.DayCount {
- return false, ProviderDayCountLimit
- }
- // 设置了每天访问上限
- if p.DayCountMaxLimit != 0 {
- // 设置了保有量,判断是否达到保有量上限
- if p.Inventory != 0 {
- if p.Count < p.Inventory {
- return true, ""
- }
- if p.RetainMarginTotalUsed >= p.RetainMargin {
- return false, ProviderRetainMarginLimit
- }
- } else { // 没有设置保有量,判断是否达到保有量余量上限,保有量为-1表示都没设置过保有量(不做校验)
- if p.RetainMargin >= 0 && p.RetainMarginTotalUsed >= p.RetainMargin {
- return false, ProviderRetainMarginLimit
- }
- }
- }
- if len(p.Filters) == 0 || len(p.Params) == 0 {
- return true, ""
- }
- for _, v := range p.Filters {
- value, ok := p.Params[v.Name]
- if ok == false || value == "" {
- continue
- }
- array := strings.Split(v.Value, ",")
- matched := false
- for _, filterValue := range array {
- if strings.Contains(value, filterValue) == true {
- matched = true
- break
- }
- }
- if matched == false {
- return false, "未通过参数过滤规则"
- }
- }
- return true, ""
- }
- func GetProviderInfo(merchantChildApiId int64, baseApiId int64) ([]apis.MerchantProviderLimitInfo, error) {
- req := apis.ManagementCheckProviderApiReq{}
- req.MerchantChildApiId = merchantChildApiId
- req.BaseApiId = baseApiId
- reply, err := rpc_apis.AuthCheck.ManagementCheckProviderApi(context.Background(), &req)
- if err != nil {
- l.Error("func",
- zap.String("call", "ManagementCheckProviderApi"),
- zap.String("args", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return nil, errors.ServiceError
- }
- return reply.MerchantProviderLimitInfoList, nil
- }
- func GenProviderInfoMap(providerLimitInfo []apis.MerchantProviderLimitInfo) map[string]apis.MerchantProviderLimitInfo {
- providerLimitInfoMap := make(map[string]apis.MerchantProviderLimitInfo)
- for index, _ := range providerLimitInfo {
- providerLimitInfoMap[providerLimitInfo[index].ProviderApiCode+strconv.Itoa(providerLimitInfo[index].GroupNo)] = providerLimitInfo[index]
- }
- return providerLimitInfoMap
- }
- func GenProviderInfoMapNew(providerLimitInfo []apis.MerchantProviderLimitInfo) (map[string]apis.MerchantProviderLimitInfo, [][]string) {
- var providerSort [][]string
- var providerRange []string
- providerLimitInfoMap := make(map[string]apis.MerchantProviderLimitInfo)
- preGroupNo := -1
- for index, _ := range providerLimitInfo {
- if preGroupNo == -1 {
- preGroupNo = providerLimitInfo[index].GroupNo
- } else if providerLimitInfo[index].GroupNo != preGroupNo {
- providerSort = append(providerSort, providerRange)
- providerRange = []string{}
- preGroupNo = providerLimitInfo[index].GroupNo
- }
- providerRange = append(providerRange, providerLimitInfo[index].ProviderApiCode)
- providerLimitInfoMap[providerLimitInfo[index].ProviderApiCode+strconv.Itoa(providerLimitInfo[index].GroupNo)] = providerLimitInfo[index]
- }
- if len(providerRange) != 0 {
- providerSort = append(providerSort, providerRange)
- }
- return providerLimitInfoMap, providerSort
- }
|