package gmodel import ( "context" "errors" "gorm.io/gorm" ) type FsAddress struct { Id int64 `gorm:"primary_key" json:"id"` UserId *int64 `gorm:"default:0" json:"user_id"` // 用户ID Name *string `gorm:"default:''" json:"name"` // 地址名称 FirstName *string `gorm:"default:''" json:"first_name"` // FirstName LastName *string `gorm:"default:''" json:"last_name"` // LastName Mobile *string `gorm:"default:''" json:"mobile"` // 手机号码 Street *string `gorm:"default:''" json:"street"` // 街道 Suite *string `gorm:"default:''" json:"suite"` // 房号 City *string `gorm:"default:''" json:"city"` // 城市 State *string `gorm:"default:''" json:"state"` // 州名 Country *string `gorm:"default:''" json:"country"` // 国家 ZipCode *string `gorm:"default:''" json:"zip_code"` // 邮编 Status *int64 `gorm:"default:1" json:"status"` // 1正常 0异常 IsDefault *int64 `gorm:"default:0" json:"is_default"` // 1默认地址,0非默认地址 } type FsAddressModel struct { db *gorm.DB } func NewFsAddressModel(db *gorm.DB) *FsAddressModel { return &FsAddressModel{db} } func (a *FsAddressModel) GetOne(ctx context.Context, id int64, userId int64) (resp FsAddress, err error) { err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`id` = ? and `user_id` = ? and `status` = ? ", id, userId, 1).First(&resp).Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return FsAddress{}, err } return resp, nil } func (a *FsAddressModel) GetUserAllAddress(ctx context.Context, userId int64) (resp []FsAddress, err error) { err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = ?", userId, 1).Order("`id` DESC").Find(&resp).Error if err != nil { return nil, err } return }