三种处理复杂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
}
上一篇:go学习-gorm


下一篇:gorm学习随笔