edit_template.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package data_import
  2. import (
  3. "adm-management/errors"
  4. "adm-management/model"
  5. v1 "adm-management/pb/v1"
  6. "context"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "encoding/json"
  11. "fmt"
  12. "git.getensh.com/common/gopkgsv2/database"
  13. "git.getensh.com/common/gopkgsv2/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. "gorm.io/gorm"
  17. )
  18. func EditTemplate(ctx context.Context, req *v1.EditTemplateRequest) (reply *v1.EditTemplateReply, err error) {
  19. reply = &v1.EditTemplateReply{}
  20. defer func() {
  21. if r := recover(); r != nil {
  22. err = fmt.Errorf("%+v", r)
  23. e := &status.Status{}
  24. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  25. logger.Error("err",
  26. zap.String("system_err", err.Error()),
  27. zap.Stack("stacktrace"))
  28. }
  29. }
  30. }()
  31. if len(req.TemplateName) < 1 || len(req.Request) < 1 || len(req.TaskName) < 1 {
  32. return reply, errors.ParamsError
  33. }
  34. db := database.DB()
  35. templateInfo := model.AdmDataTemplate{}
  36. err = db.Raw("SELECT * FROM t_adm_data_template where id = ? ", req.Id).Find(&templateInfo).Error
  37. if err != nil {
  38. if err == gorm.ErrRecordNotFound {
  39. return reply, err
  40. }
  41. return reply, errors.SystemError
  42. }
  43. // 遍历任务名改为任务id
  44. var taskIds string
  45. var ids []string
  46. for _, i := range req.TaskName {
  47. tasks, err := model.NewTaskList().Get(db.Where("task_name = ?", i))
  48. if err != nil && err != gorm.ErrRecordNotFound {
  49. return nil, err
  50. }
  51. if err == gorm.ErrRecordNotFound {
  52. fmt.Println("not found")
  53. return nil, err
  54. }
  55. ids = append(ids, strconv.Itoa(int(tasks.TaskId)))
  56. }
  57. taskIds = strings.Join(ids, ",")
  58. requests := strings.Join(req.Request, ",")
  59. updateAt := time.Now().Unix()
  60. if req.TemplateName == templateInfo.TemplateName && requests == templateInfo.Request && taskIds == templateInfo.TaskIds {
  61. return reply, errors.TemplateNoChange
  62. }
  63. err = db.Exec("UPDATE t_adm_data_template SET `template_name` = ?, `request` = ?, `task_ids` = ?, `updated_at` = ? where id = ?", req.TemplateName, requests, taskIds, updateAt, req.Id).Error
  64. if err != nil {
  65. if err == gorm.ErrRecordNotFound {
  66. return reply, nil
  67. }
  68. return reply, errors.SystemError
  69. }
  70. return reply, err
  71. }