user_merchant_api_convert.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package user_merchant
  4. import (
  5. "context"
  6. "gd_management/apis"
  7. "gd_management/errors"
  8. "encoding/json"
  9. "fmt"
  10. "strings"
  11. "github.com/astaxie/beego/orm"
  12. )
  13. // TGdApi 提供文档导出所需信息
  14. type TGdApi struct {
  15. Id int64 `json:"id"`
  16. ApiType int `json:"api_type"`
  17. Name string `json:"name"`
  18. Method string `json:"method" description:"方法,GET POST等"`
  19. Router string `json:"router" description:"路由"`
  20. RequestParam string `json:"request_param" description:"请求参数"`
  21. ResponseParam string `json:"response_param" description:"响应参数"`
  22. //ProviderApiIds string `json:"provider_api_ids" description:"三方api列表 "`
  23. OriginRequestParam string `json:"origin_request_param"`
  24. OriginResponseParam string `json:"origin_response_param"`
  25. Enable bool `json:"enable"`
  26. ErrorCodeIds string `json:"error_code_ids"`
  27. CreateTime string `json:"create_time"`
  28. UpdateTime string `json:"update_time"`
  29. IsCrypto bool `json:"is_crypto"`
  30. Comment string `json:"comment"`
  31. }
  32. func parseJsonExample(array []apis.ManagementBaseApiParam) string {
  33. ret := "{\n"
  34. for _, v := range array {
  35. if v.Selected == false {
  36. continue
  37. }
  38. value := v.Example
  39. switch v.Type {
  40. case "int":
  41. if value == "" {
  42. value = "0"
  43. }
  44. case "int64":
  45. if value == "" {
  46. value = "0"
  47. }
  48. case "float":
  49. if value == "" {
  50. value = "0.00"
  51. }
  52. case "float64":
  53. if value == "" {
  54. value = "0.00"
  55. }
  56. case "string":
  57. if value == "" {
  58. value = "\"string\""
  59. } else {
  60. value = fmt.Sprintf("\"%s\"", value)
  61. }
  62. case "bool":
  63. value = "false"
  64. case "struct":
  65. value = parseJsonExample(v.Child)
  66. case "list-int":
  67. value = "[0,0]"
  68. case "list-float":
  69. value = "[0.00,0.00]"
  70. case "list-bool":
  71. value = "[false, true, false]"
  72. case "list-string":
  73. value = "[\"string\", \"string\"]"
  74. case "list-struct":
  75. sub := parseJsonExample(v.Child)
  76. value = fmt.Sprintf("[%s,%s]", sub, sub)
  77. }
  78. name := v.Name
  79. if v.In != "" {
  80. name = v.In
  81. }
  82. ret = fmt.Sprintf("%s\"%s\":%s,\n", ret, name, value)
  83. }
  84. ret = strings.TrimRight(ret, ",\n")
  85. ret = ret + "\n}"
  86. return ret
  87. }
  88. func getParamArray(array []apis.ManagementBaseApiParam, structName string) []apis.ParamStruct {
  89. paramStructs := []apis.ParamStruct{}
  90. if len(array) == 0 {
  91. return paramStructs
  92. }
  93. first := apis.ParamStruct{}
  94. first.StructName = structName
  95. for _, v := range array {
  96. if v.Selected == false {
  97. continue
  98. }
  99. item := apis.ManagementBaseApiParam{}
  100. item.Type = v.Type
  101. item.Name = v.Name
  102. item.Mean = v.Mean
  103. item.Selected = v.Selected
  104. item.Required = v.Required
  105. item.Example = v.Example
  106. if v.In != "" {
  107. item.Name = v.In
  108. }
  109. if v.Type == "struct" && len(v.Child) > 0 {
  110. subParams := getParamArray(v.Child, item.Name)
  111. paramStructs = append(paramStructs, subParams...)
  112. } else if v.Type == "list-struct" && len(v.Child) > 0 {
  113. subParams := getParamArray(v.Child, "列表"+item.Name+"元素结构")
  114. paramStructs = append(paramStructs, subParams...)
  115. }
  116. first.Params = append(first.Params, item)
  117. }
  118. paramStructs = append(paramStructs, first)
  119. return paramStructs
  120. }
  121. func getErrorList(errorCodeIds string) []apis.ErrorCode {
  122. o := orm.NewOrm()
  123. if errorCodeIds == "" {
  124. return nil
  125. }
  126. sql := "select code, msg from t_gd_error_code where id in(" + errorCodeIds + ")"
  127. ret := []apis.ErrorCode{}
  128. o.Raw(sql).QueryRows(&ret)
  129. return ret
  130. }
  131. func ManagementConvertMerchantDataApi(ctx context.Context, req *apis.ManagementConvertMerchantDataApiReq, reply *apis.ManagementConvertMerchantDataApiReply) (err error) {
  132. originApis := []TGdApi{}
  133. sql := "select a.name, a.method, a.router, a.comment, a.error_code_ids," +
  134. " a.request_param as origin_request_param, a.response_param as" +
  135. " origin_response_param, b.request_param, b.response_param,b.is_crypto from" +
  136. " t_gd_api as a left join t_gd_merchant_child_data_api" +
  137. " as b on a.id = b.api_id left join t_gd_merchant_data_api as c on c.id = b.merchant_data_api_id " +
  138. "left join t_gd_child_data_api as d on d.query_type_id=c.query_type_id and a.id=d.api_id where b.merchant_data_api_id =? ORDER BY d.priority"
  139. o := orm.NewOrm()
  140. _, err = o.Raw(sql, req.MerchantDataApiId).QueryRows(&originApis)
  141. if err != nil {
  142. return errors.DataBaseError
  143. }
  144. for _, v := range originApis {
  145. item := apis.ApiParamStructInfo{}
  146. item.ApiName = v.Name
  147. item.ApiRouter = v.Router
  148. item.ApiMethod = v.Method
  149. item.IsCrypto = v.IsCrypto
  150. item.Comment = v.Comment
  151. reqList := []apis.ManagementBaseApiParam{}
  152. err := json.Unmarshal([]byte(v.RequestParam), &reqList)
  153. if err == nil {
  154. item.RequestParams = getParamArray(reqList, "data")
  155. }
  156. respList := []apis.ManagementBaseApiParam{}
  157. err = json.Unmarshal([]byte(v.ResponseParam), &respList)
  158. if err == nil {
  159. item.JsonExample = parseJsonExample(respList)
  160. item.JsonExample = fmt.Sprintf("{\"code\":0, \"msg\":\"success\", \"data\":%s}", item.JsonExample)
  161. item.ResponseParams = getParamArray(respList, "data")
  162. }
  163. item.ErrorList = getErrorList(v.ErrorCodeIds)
  164. reply.Apis = append(reply.Apis, item)
  165. }
  166. sql = "select data_api_name from t_gd_data_api " +
  167. "where id=(select data_api_id from t_gd_data_api_query_type" +
  168. " where id=(select query_type_id from t_gd_merchant_data_api where id=?))"
  169. o.Raw(sql, req.MerchantDataApiId).QueryRow(&reply.DataApiName)
  170. return nil
  171. }