base_api_update.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package base_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "gd_management/impl/pubsub"
  7. "gd_management/rpc_apis"
  8. "gd_management/rpc_apis/gd_access_log"
  9. "encoding/json"
  10. "fmt"
  11. "strings"
  12. "time"
  13. "gd_management/common.in/storage"
  14. "gd_management/common.in/utils"
  15. "github.com/astaxie/beego/orm"
  16. "go.uber.org/zap"
  17. )
  18. type UpdateParams struct {
  19. RequestParam string
  20. ResponseParam string
  21. Id int64
  22. MerchantDataApiId int64
  23. ReqList []apis.ManagementBaseApiParam
  24. RespList []apis.ManagementBaseApiParam
  25. NewReqList []apis.ManagementBaseApiParam
  26. NewRespList []apis.ManagementBaseApiParam
  27. }
  28. func updateBaseApiReqToDBStruct(req *apis.ManagementUpdateBaseApiReq) (*apis.TGdApi, string, string, string, string, error) {
  29. paramPreProcess(&req.BaseApiInfo)
  30. reqParam, err := json.Marshal(req.BaseApiInfo.RequestParam)
  31. if err != nil {
  32. return nil, "", "", "", "", err
  33. }
  34. resParam, err := json.Marshal(req.BaseApiInfo.ResponseParam)
  35. if err != nil {
  36. return nil, "", "", "", "", err
  37. }
  38. errids := ""
  39. for _, id := range req.BaseApiInfo.ErrorCode {
  40. if errids == "" {
  41. errids = fmt.Sprintf("%d", id)
  42. } else {
  43. errids = errids + "," + fmt.Sprintf("%d", id)
  44. }
  45. }
  46. now := time.Now().Format("2006-01-02 15:04:05")
  47. ret := apis.TGdApi{}
  48. err = orm.NewOrm().QueryTable("t_gd_api").Filter("id", req.ApiId).One(&ret)
  49. if err != nil {
  50. return &ret, "", "", "", "", errors.DataBaseError
  51. }
  52. oldRouter := ret.Router
  53. oldMethod := ret.Method
  54. oldName := ret.Name
  55. ret.Id = req.ApiId
  56. ret.ApiType = req.BaseApiInfo.Type
  57. ret.Method = strings.ToUpper(req.BaseApiInfo.Method)
  58. ret.Router = req.BaseApiInfo.Router
  59. ret.Name = req.BaseApiInfo.Name
  60. ret.ErrorCodeIds = errids
  61. ret.RequestParam = string(reqParam)
  62. ret.ResponseParam = string(resParam)
  63. ret.UpdateTime = now
  64. ret.Comment = req.BaseApiInfo.Comment
  65. if strings.HasPrefix(ret.Router, "/") == false {
  66. ret.Router = fmt.Sprintf("/%s", ret.Router)
  67. }
  68. return &ret, oldName, ret.Name, oldRouter, oldMethod, nil
  69. }
  70. func getNewParamList(dst []apis.ManagementBaseApiParam, src []apis.ManagementBaseApiParam) (ret []apis.ManagementBaseApiParam) {
  71. for _, s := range src {
  72. exist := false
  73. for _, d := range dst {
  74. if s.Name == d.Name {
  75. exist = true
  76. item := d
  77. if s.Type != d.Type {
  78. item.Type = s.Type
  79. }
  80. if s.Mean != d.Mean {
  81. item.Mean = s.Mean
  82. }
  83. if s.Example != d.Example {
  84. item.Example = s.Example
  85. }
  86. item.Child = getNewParamList(d.Child, s.Child)
  87. ret = append(ret, item)
  88. break
  89. }
  90. }
  91. if exist == false {
  92. s.Selected = false
  93. ret = append(ret, s)
  94. }
  95. }
  96. return ret
  97. }
  98. func getUpdateParams(reqList []apis.ManagementBaseApiParam, respList []apis.ManagementBaseApiParam, updateParams []UpdateParams) []UpdateParams {
  99. for i, v := range updateParams {
  100. json.Unmarshal([]byte(v.RequestParam), &updateParams[i].ReqList)
  101. json.Unmarshal([]byte(v.ResponseParam), &updateParams[i].RespList)
  102. updateParams[i].NewReqList = getNewParamList(updateParams[i].ReqList, reqList)
  103. updateParams[i].NewRespList = getNewParamList(updateParams[i].RespList, respList)
  104. bytes, _ := json.Marshal(updateParams[i].NewReqList)
  105. updateParams[i].RequestParam = string(bytes)
  106. bytes, _ = json.Marshal(updateParams[i].NewRespList)
  107. updateParams[i].ResponseParam = string(bytes)
  108. }
  109. return updateParams
  110. }
  111. func updateChildDataApi(o orm.Ormer, req *apis.ManagementUpdateBaseApiReq, timeNow string) error {
  112. var updateParams []UpdateParams
  113. _, err := o.Raw("select request_param ,response_param ,id from t_gd_child_data_api where api_id = ?", req.ApiId).QueryRows(&updateParams)
  114. if err != nil && err != orm.ErrNoRows {
  115. return errors.DataBaseError
  116. }
  117. updateParams = getUpdateParams(req.BaseApiInfo.RequestParam, req.BaseApiInfo.ResponseParam, updateParams)
  118. for _, v := range updateParams {
  119. _, err := o.Raw("update t_gd_child_data_api set request_param = ? ,response_param = ?,update_time = ? where id = ?", v.RequestParam, v.ResponseParam, timeNow, v.Id).Exec()
  120. if err != nil {
  121. fmt.Printf("database err:%v\n", err)
  122. return errors.DataBaseError
  123. }
  124. }
  125. return nil
  126. }
  127. func updateMerchantChildDataApi(o orm.Ormer, req *apis.ManagementUpdateBaseApiReq, timeNow string) ([]UpdateParams, error) {
  128. var updateParams []UpdateParams
  129. _, err := o.Raw("select request_param ,response_param ,id, merchant_data_api_id from t_gd_merchant_child_data_api where api_id = ?", req.ApiId).QueryRows(&updateParams)
  130. if err != nil && err != orm.ErrNoRows {
  131. return updateParams, errors.DataBaseError
  132. }
  133. updateParams = getUpdateParams(req.BaseApiInfo.RequestParam, req.BaseApiInfo.ResponseParam, updateParams)
  134. for _, v := range updateParams {
  135. _, err := o.Raw("update t_gd_merchant_child_data_api set request_param = ? ,response_param = ?,update_time = ? where id = ?", v.RequestParam, v.ResponseParam, timeNow, v.Id).Exec()
  136. if err != nil {
  137. fmt.Printf("database err:%v\n", err)
  138. return updateParams, errors.DataBaseError
  139. }
  140. }
  141. return updateParams, nil
  142. }
  143. func updateBaseApi(req *apis.ManagementUpdateBaseApiReq) error {
  144. dbstruct, oldName, newName, oldRouter, oldMethod, err := updateBaseApiReqToDBStruct(req)
  145. if err != nil {
  146. return errors.ArgsError
  147. }
  148. task := func(o orm.Ormer) error {
  149. requestParam := ""
  150. responseParam := ""
  151. o.Raw("select request_param , response_param from t_gd_api where id =?", req.ApiId).QueryRow(&requestParam, &responseParam)
  152. _, err = o.Update(dbstruct, "name", "method", "api_type", "router", "error_code_ids", "request_param", "response_param", "comment", "update_time")
  153. if err != nil {
  154. fmt.Printf("database err:%v\n", err)
  155. return errors.DataBaseError
  156. }
  157. updateParams := []UpdateParams{}
  158. if requestParam != dbstruct.RequestParam || responseParam != dbstruct.ResponseParam {
  159. err := updateChildDataApi(o, req, dbstruct.UpdateTime)
  160. if err != nil {
  161. return errors.DataBaseError
  162. }
  163. updateParams, err = updateMerchantChildDataApi(o, req, dbstruct.UpdateTime)
  164. if err != nil {
  165. return errors.DataBaseError
  166. }
  167. }
  168. if oldName != newName && newName != "" {
  169. if err := pubsub.PublishNameNotify("api_id", dbstruct.Id, 0); err != nil {
  170. return err
  171. }
  172. dreq := gd_access_log.LogUpdateApiNameReq{
  173. Type: 0,
  174. Name: newName,
  175. Id: dbstruct.Id,
  176. }
  177. go func() error {
  178. _, err := rpc_apis.AccessLog.LogUpdateApiName(context.Background(), &dreq)
  179. return err
  180. }()
  181. }
  182. err = utils.RedisSet("t_gd_api", dbstruct.Method+"-"+dbstruct.Router, dbstruct)
  183. if err != nil {
  184. return errors.DataBaseError
  185. }
  186. if err := pubsub.PublishApiNotify(req.ApiId, oldRouter, oldMethod); err != nil {
  187. return err
  188. }
  189. for _, p := range updateParams {
  190. if err := pubsub.PublishMerchantApiNotify(p.MerchantDataApiId, 0, 0, 0); err != nil {
  191. return err
  192. }
  193. }
  194. return nil
  195. }
  196. tasks := []storage.DbaTasker{}
  197. tasks = append(tasks, storage.GenerateDbaTask(task))
  198. storage.ExecTrans(tasks...)
  199. return nil
  200. }
  201. func ManagementUpdateBaseApi(ctx context.Context, req *apis.ManagementUpdateBaseApiReq, reply *apis.ManagementUpdateBaseApiReply) (err error) {
  202. err = updateBaseApi(req)
  203. if err != nil {
  204. l.Error("func",
  205. zap.String("call", "ManagementUpdateBaseApi"),
  206. zap.String("args", utils.MarshalJsonString(req)),
  207. zap.String("error", err.Error()))
  208. }
  209. reply.ApiId = req.ApiId
  210. l.Debug(utils.MarshalJsonString(req, reply))
  211. return
  212. }