1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- 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 CreateTemplate(ctx context.Context, req *v1.CreateTemplateRequest) (reply *v1.CreateTemplateReply, err error) {
- reply = &v1.CreateTemplateReply{}
- 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()
- // 遍历任务名改为任务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, ",")
- createAt := time.Now().Unix()
- var count int
- err = db.Raw("SELECT count(*) FROM t_adm_data_template where request = ? and task_ids = ? ", requests, taskIds).Find(&count).Error
- if count > 0 {
- return reply, errors.TemplateAlreadyExists
- }
- err = db.Exec("INSERT INTO t_adm_data_template(`template_name`, `request`, `task_ids`, `created_at`, `updated_at`) VALUES(?,?,?,?,?)", req.TemplateName, requests, taskIds, createAt, createAt).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return reply, nil
- }
- return reply, errors.SystemError
- }
- return reply, err
- }
|