数据库
首页 > 数据库> > 三种处理复杂json存入数据库的方式gorm

三种处理复杂json存入数据库的方式gorm

作者:互联网

func CreateService(s model.ServiceMulti) (err error) {
	pmmulti := s.PmMulti
	servicename := s.ServiceName
	ServiceInfo := s.ServiceInfo

	global.LOG.Info("ServiceMulti is : ", zap.Any("msg", pmmulti))

	err = global.DB.Create(&ServiceInfo).Error
	if err != nil {
		panic(err)
	}
	err = CreateUser(pmmulti, servicename)
	if err != nil {
		panic(err)
	}
	return
}

func CreateUser(pmmulti []string, servicename string)(err error) {
	ServiceUser := new(model.ServiceUser)
	for _, s := range pmmulti {
		ServiceUser.UserName = s
		ServiceUser.ServiceName = servicename
		ServiceUser.Position = "PM"
		err = global.DB.Create(&ServiceUser).Error
	}
	return err
}
func CreateService(s model.ServiceMulti) (err error) {
	ServiceInfo := s.ServiceInfo
	servicename := s.ServiceName
	pmmulti := s.PmMulti
	rdmulti := s.RdMulti
	qamulti := s.QaMulti
	opmulti := s.OpMulti
	err = CreateRela(pmmulti, rdmulti, qamulti, opmulti, servicename)
	if err != nil {
		return err
	}
	err = global.DB.Create(&ServiceInfo).Error
	return err
}

func CreateRela(pmmulti []string, rdmulti []string, qamulti []string, opmulti []string, servicename string)(err error) {
	pm, err := json.Marshal(pmmulti)
	if err != nil {
		return err
	}
	rd, err := json.Marshal(rdmulti)
	if err != nil {
		return err
	}
	qa, err := json.Marshal(qamulti)
	if err != nil {
		return err
	}
	op, err := json.Marshal(opmulti)
	if err != nil {
		return err
	}
	ServiceUser := model.ServiceUser{
		ServiceName: servicename,
		Pm: string(pm),
		Rd: string(rd),
		Qa: string(qa),
		Op: string(op),
	}
	err = global.DB.Create(&ServiceUser).Error
	return err
}

标签:return,string,err,pmmulti,存入,json,servicename,ServiceUser,gorm
来源: https://blog.csdn.net/Isabella327/article/details/118901640