try_api_task_create.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package base_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "fmt"
  7. "strings"
  8. "time"
  9. "github.com/astaxie/beego/orm"
  10. )
  11. func ManagementCreateTryApiTask(ctx context.Context, req *apis.ManagementCreateTryApiTaskReq, reply *apis.ManagementCreateTryApiTaskReply) (err error) {
  12. if req.Router == "" || req.FileName == "" || req.Method == "" {
  13. return errors.ArgsError
  14. }
  15. now := time.Now().Format("2006-01-02 15:04:05")
  16. o := orm.NewOrm()
  17. count := 0
  18. sql := "select count(1) from t_gd_try_api_task where file_name=? and type=?"
  19. err = o.Raw(sql, req.FileName, 1).QueryRow(&count)
  20. if count > 0 {
  21. return errors.TaskExist
  22. } else if err != nil && err != orm.ErrNoRows {
  23. return errors.DataBaseError
  24. }
  25. merchantName := ""
  26. o.Raw("select merchant_name from t_gd_merchants where app_key=?", req.AppKey).QueryRow(&merchantName)
  27. task := apis.TryApi{}
  28. task.Type = 1
  29. task.ApiName = req.ApiName
  30. task.StatusCode = 0
  31. task.ApiRouter = req.Router
  32. task.FileName = req.FileName
  33. task.DownloadFile = req.DownloadFile
  34. task.OriginFile = req.OriginFile
  35. task.RequestInfo = req.RequestInfo
  36. task.UpdatedAt = now
  37. task.CreatedAt = now
  38. task.TaskName = req.TaskName
  39. task.MerchantId = req.MerchantId
  40. task.Merchant = fmt.Sprintf("%s(使用内部账号)", merchantName)
  41. if req.IsMerchant {
  42. task.Merchant = fmt.Sprintf("%s(使用商户账号)", merchantName)
  43. }
  44. if task.TaskName == "" {
  45. task.TaskName = strings.TrimRight(req.FileName, ".xlsx")
  46. }
  47. id, err := o.Insert(&task)
  48. if err != nil {
  49. return errors.DataBaseError
  50. }
  51. reply.Id = id
  52. return nil
  53. }