package data_import import ( "adm-management/errors" "adm-management/model" v1 "adm-management/pb/v1" "context" "strconv" "strings" "time" "encoding/json" "fmt" "git.getensh.com/common/gopkgsv2/database" "git.getensh.com/common/gopkgsv2/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "gorm.io/gorm" ) func EditTemplate(ctx context.Context, req *v1.EditTemplateRequest) (reply *v1.EditTemplateReply, err error) { reply = &v1.EditTemplateReply{} defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() if len(req.TemplateName) < 1 || len(req.Request) < 1 || len(req.TaskName) < 1 { return reply, errors.ParamsError } db := database.DB() templateInfo := model.AdmDataTemplate{} err = db.Raw("SELECT * FROM t_adm_data_template where id = ? ", req.Id).Find(&templateInfo).Error if err != nil { if err == gorm.ErrRecordNotFound { return reply, err } return reply, errors.SystemError } // 遍历任务名改为任务id var taskIds string var ids []string for _, i := range req.TaskName { tasks, err := model.NewTaskList().Get(db.Where("task_name = ?", i)) if err != nil && err != gorm.ErrRecordNotFound { return nil, err } if err == gorm.ErrRecordNotFound { fmt.Println("not found") return nil, err } ids = append(ids, strconv.Itoa(int(tasks.TaskId))) } taskIds = strings.Join(ids, ",") requests := strings.Join(req.Request, ",") updateAt := time.Now().Unix() if req.TemplateName == templateInfo.TemplateName && requests == templateInfo.Request && taskIds == templateInfo.TaskIds { return reply, errors.TemplateNoChange } 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 if err != nil { if err == gorm.ErrRecordNotFound { return reply, nil } return reply, errors.SystemError } return reply, err }