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

2021/7/19 19:07:26

本文主要是介绍三种处理复杂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
}


这篇关于三种处理复杂json存入数据库的方式gorm的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程