123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- package user_merchant
- import (
- "context"
- "gd_management/apis"
- "gd_management/errors"
- "gd_management/impl/pubsub"
- "encoding/json"
- "strings"
- "time"
- "gd_management/common.in/storage"
- "gd_management/common.in/utils"
- "github.com/astaxie/beego/orm"
- "go.uber.org/zap"
- )
- func updateMerchantBaseApi(req *apis.ManagementUpdateMerchantBaseApiReq) error {
- publish := false
- updateTask := func(o orm.Ormer) error {
- tab := apis.TGdMerchantChildDataApi{}
- countChanged := false
- err := o.QueryTable("t_gd_merchant_child_data_api").Filter("id", req.MerchantChildApiId).One(&tab)
- if err != nil {
- if err == orm.ErrNoRows {
- return errors.MerchantDataApiChildApiNotExist
- } else {
- return errors.DataBaseError
- }
- }
- merchantDataApiId := tab.MerchantDataApiId
- if tab.CountType != req.CountType {
- countChanged = true
- tab.CountType = req.CountType
- }
- if tab.ForceUpdate != req.ForceUpdate {
- tab.ForceUpdate = req.ForceUpdate
- }
- if tab.IsCrypto != req.IsCrypto {
- tab.IsCrypto = req.IsCrypto
- }
- if tab.ReuseTime != req.ReuseTime {
- tab.ReuseTime = req.ReuseTime
- }
- if tab.Timeout != req.Timeout {
- countChanged = true
- tab.Timeout = req.Timeout
- }
- if tab.IsRawErrorCode != req.IsRawErrorCode {
- countChanged = true
- tab.IsRawErrorCode = req.IsRawErrorCode
- }
- if req.IpWhitelist != "" {
- ip := strings.Split(req.IpWhitelist, ",")
- for _, v := range ip {
- ips := strings.Split(v, "-")
- for _, realIp := range ips {
- if !utils.VerifyIp(realIp) {
- return errors.IpAddressErr
- }
- }
- }
- }
- if tab.IpWhitelist != req.IpWhitelist {
- tab.IpWhitelist = req.IpWhitelist
- }
- if len(req.RequestParam) != 0 {
- bytes, err := json.Marshal(req.RequestParam)
- if err != nil {
- return errors.ArgsError
- }
- tab.RequestParam = string(bytes)
- }
- if len(req.ResponseParam) != 0 {
- bytes, err := json.Marshal(req.ResponseParam)
- if err != nil {
- return errors.ArgsError
- }
- tab.ResponseParam = string(bytes)
- }
- if len(req.Filters) != 0 {
- bytes, err := json.Marshal(req.Filters)
- if err != nil {
- return errors.ArgsError
- }
- tab.Filter = string(bytes)
- } else {
- tab.Filter = ""
- }
- if tab.CountCode != req.CountCode {
- countChanged = true
- }
- if tab.MinimalTimeConsuming != req.MinimalTimeConsuming {
- tab.MinimalTimeConsuming = req.MinimalTimeConsuming
- }
- if tab.RandomPercentage != req.RandomPercentage {
- tab.RandomPercentage = req.RandomPercentage
- }
- if tab.RateLimit != req.RateLimit {
- tab.RateLimit = req.RateLimit
- publish = true
- }
- tab.CountCode = req.CountCode
- tab.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
- _, err = o.Update(&tab)
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "update t_gd_merchant_child_data_api"),
- zap.String("args", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- for _, group := range req.MerchantProviderGroupList {
- for _, papi := range group.MerchantProviderApiList {
- var id int64
- err := o.Raw("select id from t_gd_merchant_api_provider_api_relation where merchant_child_api_id= ? and provider_api_id=? and group_no = ?", req.MerchantChildApiId, papi.ProviderApiId, group.GroupNo).QueryRow(&id)
- if err != nil {
- return errors.DataBaseError
- }
- relation := apis.MerchantApiProviderRelation{}
- relation.Id = id
- relation.MerchantChildApiId = req.MerchantChildApiId
- relation.ProviderApiId = papi.ProviderApiId
- relation.Enable = papi.Enable
- relation.DayCount = papi.DayCount
- relation.GroupNo = group.GroupNo
- _, err = o.Update(&relation, "enable", "day_count")
- if err != nil {
- return errors.DataBaseError
- }
- }
- }
- if err := pubsub.PublishMerchantApiNotify(merchantDataApiId, 0, 0, 0); err != nil {
- return err
- }
- if countChanged {
- return pubsub.PublishApiExportInfoNotify()
- }
- return nil
- }
- tasks := []storage.DbaTasker{}
- tasks = append(tasks, storage.GenerateDbaTask(updateTask))
- storage.ExecTrans(tasks...)
- if publish {
- pubsub.PublishRateLimit()
- }
- return nil
- }
- func ManagementUpdateMerchantBaseApi(ctx context.Context, req *apis.ManagementUpdateMerchantBaseApiReq, reply *apis.ManagementUpdateMerchantBaseApiReply) (err error) {
- err = updateMerchantBaseApi(req)
- if err != nil {
- l.Error("func",
- zap.String("call", "ManagementUpdateMerchantBaseApi"),
- zap.String("args", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- }
- l.Debug(utils.MarshalJsonString(req, reply))
- return
- }
|