Example #1
0
func AddTerminal(obj Terminal) (int64, error) {
	var id int64
	var profileId int64
	var carrierId int64
	var err error
	orm := GetOrm()
	if err = orm.Begin(); err == nil {
		obj.CreateOn = time.Now()
		obj.ModifyOn = time.Now()
		obj.OfflineOn = time.Now()
		//profile
		if obj.TerminalProfile == nil {
			obj.TerminalProfile = &TerminalProfile{
				CreateOn: time.Now(),
				ModifyOn: time.Now(),
			}
		} else {
			obj.TerminalProfile.CreateOn = time.Now()
			obj.TerminalProfile.ModifyOn = time.Now()
		}

		if profileId, err = orm.Insert(obj.TerminalProfile); err == nil {
			obj.TerminalProfile.Id = profileId
		} else {
			orm.Rollback()
			return 0, err
		}
		//profile end

		//carrier
		if obj.TerminalCarrier == nil {
			obj.TerminalCarrier = &TerminalCarrier{
				CreateOn: time.Now(),
				ModifyOn: time.Now(),
			}
		} else {
			obj.TerminalCarrier.CreateOn = time.Now()
			obj.TerminalCarrier.ModifyOn = time.Now()
		}

		if carrierId, err = orm.Insert(obj.TerminalCarrier); err == nil {
			obj.TerminalCarrier.Id = carrierId
		} else {
			orm.Rollback()
			return 0, err
		}
		//carrier end

		if id, err = orm.Insert(&obj); err == nil {
			orm.Commit()
		} else {
			orm.Rollback()
			return 0, err
		}
	} else {
		return 0, err
	}

	return id, nil
}
Example #2
0
func AddUser(obj User) (int64, error) {
	var id int64
	var profileId int64
	var err error
	orm := GetOrm()
	if err = orm.Begin(); err == nil {
		obj.CreateOn = time.Now()
		obj.ModifyOn = time.Now()
		if obj.UserProfile == nil {
			obj.UserProfile = &UserProfile{
				CreateOn: time.Now(),
				ModifyOn: time.Now(),
			}
		} else {
			obj.UserProfile.CreateOn = time.Now()
			obj.UserProfile.ModifyOn = time.Now()
		}
		if profileId, err = orm.Insert(obj.UserProfile); err == nil {
			obj.UserProfile.Id = profileId
			if id, err = orm.Insert(&obj); err == nil {
				orm.Commit()
			} else {
				orm.Rollback()
				return 0, err
			}
		} else {
			orm.Rollback()
			return 0, err
		}
	} else {
		return 0, err
	}

	return id, nil
}