user_merchant_base_api_update.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package user_merchant
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "gd_management/impl/pubsub"
  7. "encoding/json"
  8. "strings"
  9. "time"
  10. "gd_management/common.in/storage"
  11. "gd_management/common.in/utils"
  12. "github.com/astaxie/beego/orm"
  13. "go.uber.org/zap"
  14. )
  15. func updateMerchantBaseApi(req *apis.ManagementUpdateMerchantBaseApiReq) error {
  16. publish := false
  17. updateTask := func(o orm.Ormer) error {
  18. tab := apis.TGdMerchantChildDataApi{}
  19. countChanged := false
  20. err := o.QueryTable("t_gd_merchant_child_data_api").Filter("id", req.MerchantChildApiId).One(&tab)
  21. if err != nil {
  22. if err == orm.ErrNoRows {
  23. return errors.MerchantDataApiChildApiNotExist
  24. } else {
  25. return errors.DataBaseError
  26. }
  27. }
  28. merchantDataApiId := tab.MerchantDataApiId
  29. if tab.CountType != req.CountType {
  30. countChanged = true
  31. tab.CountType = req.CountType
  32. }
  33. if tab.ForceUpdate != req.ForceUpdate {
  34. tab.ForceUpdate = req.ForceUpdate
  35. }
  36. if tab.IsCrypto != req.IsCrypto {
  37. tab.IsCrypto = req.IsCrypto
  38. }
  39. if tab.ReuseTime != req.ReuseTime {
  40. tab.ReuseTime = req.ReuseTime
  41. }
  42. if tab.Timeout != req.Timeout {
  43. countChanged = true
  44. tab.Timeout = req.Timeout
  45. }
  46. if tab.IsRawErrorCode != req.IsRawErrorCode {
  47. countChanged = true
  48. tab.IsRawErrorCode = req.IsRawErrorCode
  49. }
  50. if req.IpWhitelist != "" {
  51. ip := strings.Split(req.IpWhitelist, ",")
  52. for _, v := range ip {
  53. ips := strings.Split(v, "-")
  54. for _, realIp := range ips {
  55. if !utils.VerifyIp(realIp) {
  56. return errors.IpAddressErr
  57. }
  58. }
  59. }
  60. }
  61. if tab.IpWhitelist != req.IpWhitelist {
  62. tab.IpWhitelist = req.IpWhitelist
  63. }
  64. if len(req.RequestParam) != 0 {
  65. bytes, err := json.Marshal(req.RequestParam)
  66. if err != nil {
  67. return errors.ArgsError
  68. }
  69. tab.RequestParam = string(bytes)
  70. }
  71. if len(req.ResponseParam) != 0 {
  72. bytes, err := json.Marshal(req.ResponseParam)
  73. if err != nil {
  74. return errors.ArgsError
  75. }
  76. tab.ResponseParam = string(bytes)
  77. }
  78. if len(req.Filters) != 0 {
  79. bytes, err := json.Marshal(req.Filters)
  80. if err != nil {
  81. return errors.ArgsError
  82. }
  83. tab.Filter = string(bytes)
  84. } else {
  85. tab.Filter = ""
  86. }
  87. if tab.CountCode != req.CountCode {
  88. countChanged = true
  89. }
  90. if tab.MinimalTimeConsuming != req.MinimalTimeConsuming {
  91. tab.MinimalTimeConsuming = req.MinimalTimeConsuming
  92. }
  93. if tab.RandomPercentage != req.RandomPercentage {
  94. tab.RandomPercentage = req.RandomPercentage
  95. }
  96. if tab.RateLimit != req.RateLimit {
  97. tab.RateLimit = req.RateLimit
  98. publish = true
  99. }
  100. tab.CountCode = req.CountCode
  101. tab.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  102. _, err = o.Update(&tab)
  103. if err != nil {
  104. l.Error("mysql",
  105. zap.String("sql", "update t_gd_merchant_child_data_api"),
  106. zap.String("args", utils.MarshalJsonString(req)),
  107. zap.String("error", err.Error()))
  108. return errors.DataBaseError
  109. }
  110. for _, group := range req.MerchantProviderGroupList {
  111. for _, papi := range group.MerchantProviderApiList {
  112. var id int64
  113. 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)
  114. if err != nil {
  115. return errors.DataBaseError
  116. }
  117. relation := apis.MerchantApiProviderRelation{}
  118. relation.Id = id
  119. relation.MerchantChildApiId = req.MerchantChildApiId
  120. relation.ProviderApiId = papi.ProviderApiId
  121. relation.Enable = papi.Enable
  122. relation.DayCount = papi.DayCount
  123. relation.GroupNo = group.GroupNo
  124. _, err = o.Update(&relation, "enable", "day_count")
  125. if err != nil {
  126. return errors.DataBaseError
  127. }
  128. }
  129. }
  130. if err := pubsub.PublishMerchantApiNotify(merchantDataApiId, 0, 0, 0); err != nil {
  131. return err
  132. }
  133. if countChanged {
  134. return pubsub.PublishApiExportInfoNotify()
  135. }
  136. return nil
  137. }
  138. tasks := []storage.DbaTasker{}
  139. tasks = append(tasks, storage.GenerateDbaTask(updateTask))
  140. storage.ExecTrans(tasks...)
  141. if publish {
  142. pubsub.PublishRateLimit()
  143. }
  144. return nil
  145. }
  146. func ManagementUpdateMerchantBaseApi(ctx context.Context, req *apis.ManagementUpdateMerchantBaseApiReq, reply *apis.ManagementUpdateMerchantBaseApiReply) (err error) {
  147. err = updateMerchantBaseApi(req)
  148. if err != nil {
  149. l.Error("func",
  150. zap.String("call", "ManagementUpdateMerchantBaseApi"),
  151. zap.String("args", utils.MarshalJsonString(req)),
  152. zap.String("error", err.Error()))
  153. }
  154. l.Debug(utils.MarshalJsonString(req, reply))
  155. return
  156. }