provider_add_api.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package provider
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "gd_management/impl/pubsub"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "github.com/astaxie/beego/orm"
  11. )
  12. func ManagementAddProviderApi(ctx context.Context, req *apis.ManagementAddProviderApiReq, reply *apis.ManagementAddProviderApiReply) error {
  13. fmt.Println(req)
  14. if req.ProviderId == 0 || req.ThirdpartApiName == "" || req.ThirdpartApiRouter == "" || req.ThirdpartHost == "" || req.ProviderApiCode == "" {
  15. return errors.ArgsError
  16. }
  17. var provider apis.ProviderPlatform
  18. err := orm.NewOrm().QueryTable("t_gd_provider").Filter("id", req.ProviderId).One(&provider)
  19. if err != nil {
  20. if err == orm.ErrNoRows {
  21. return errors.PorviderPlatformNotExist
  22. } else {
  23. return errors.DataBaseError
  24. }
  25. }
  26. var providerApi apis.ProviderApi
  27. if strings.Contains(req.ProviderApiCode, "-") == false {
  28. providerApi.ProviderApiCode = provider.PlatformCode + "-" + req.ProviderApiCode
  29. } else {
  30. providerApi.ProviderApiCode = req.ProviderApiCode
  31. }
  32. if strings.HasPrefix(req.ThirdpartApiRouter, "/") == true {
  33. providerApi.ThirdpartApiRouter = req.ThirdpartApiRouter[1:]
  34. } else {
  35. providerApi.ThirdpartApiRouter = req.ThirdpartApiRouter
  36. }
  37. if strings.HasSuffix(req.ThirdpartHost, "/") == true {
  38. providerApi.ThirdpartHost = req.ThirdpartHost[0 : len(req.ThirdpartHost)-1]
  39. } else {
  40. providerApi.ThirdpartHost = req.ThirdpartHost
  41. }
  42. providerApi.Count = req.Count
  43. //providerApi.ThirdpartApiRouter = req.ThirdpartApiRouter
  44. providerApi.ThirdpartApiName = req.ThirdpartApiName
  45. providerApi.ProviderId = req.ProviderId
  46. providerApi.Cost = req.Cost
  47. providerApi.ThirdpartApiMethod = strings.ToUpper(req.ThirdpartApiMethod)
  48. providerApi.State = req.State
  49. providerApi.Timeout = req.Timeout
  50. var timeLayout = "2006-01-02 15:04:05"
  51. timeNow := time.Now().Format(timeLayout)
  52. providerApi.CreateTime = timeNow
  53. providerApi.UpdateTime = timeNow
  54. id, err := orm.NewOrm().Insert(&providerApi)
  55. if err != nil {
  56. return errors.DataBaseError
  57. }
  58. pubsub.PublishProviderExportInfoNotify()
  59. reply.ProviderApiId = id
  60. return nil
  61. }