package gmodel import ( "context" "time" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" ) func (a *FsAddressModel) GetOne(ctx context.Context, addressId int64, userId int64) (resp *FsAddress, err error) { err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`address_id` = ? and `user_id` = ? and `status` = ? ", addressId, userId, 1).Take(&resp).Error return resp, err } func (a *FsAddressModel) GetUserAllAddress(ctx context.Context, userId int64) (resp []*FsAddressWithDefault, err error) { resp = make([]*FsAddressWithDefault, 0) var dbresp []*FsAddress err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = 1", userId).Order("`ltime` DESC").Find(&dbresp).Error if err != nil { return nil, err } now := time.Now().UTC().AddDate(10, 0, 0).Unix() for _, r := range dbresp { rd := &FsAddressWithDefault{ FsAddress: r, } if r.Ltime.UTC().Unix() > now { rd.IsDefault = 1 } else { rd.IsDefault = 0 } resp = append(resp, rd) } return } func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (result *FsAddress, err error) { err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error { now := time.Now().UTC() result = &FsAddress{ UserId: address.UserId, FirstName: address.FirstName, LastName: address.LastName, Mobile: address.Mobile, Street: address.Street, Suite: address.Suite, City: address.City, State: address.State, Country: address.Country, ZipCode: address.ZipCode, Status: address.Status, Ctime: &now, Utime: &now, Ltime: &now, } return tx.Model(&FsAddress{}).Create(result).Error }) if err != nil { return nil, err } return result, nil } func (a *FsAddressModel) UpdateAddress(ctx context.Context, address *FsAddress) (err error) { err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error { err = tx. Where("address_id = ? and user_id = ? and status = 1 ", address.AddressId, address.UserId). Updates(address).Error if err != nil { return err } return err }) return err } func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId int64, addressId int64, isDefault int64) (err error) { err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error { logx.Info("address_id:", addressId, " set default ", isDefault) now := time.Now().UTC() var updates = map[string]interface{}{ "ltime": now, "utime": now, } if isDefault == 1 { err = tx.Where("`user_id` = ? and `status` = 1 and `ltime` > ? and `address_id` != ?", userId, now.AddDate(10, 0, 0), addressId). UpdateColumns(updates).Error if err != nil { return err } updates["ltime"] = now.AddDate(250, 0, 0) } tr := tx.Model(&FsAddress{}).Where("`address_id` = ? and `user_id` = ? and `status` = 1", addressId, userId). UpdateColumns(updates) err = tr.Error if err != nil { return err } return nil }) return err } func (a *FsAddressModel) DeleteOne(ctx context.Context, addressId int64, userId int64) (err error) { err = a.db.WithContext(ctx).Model(&FsAddress{}). Where("`address_id` = ? and `user_id` = ? and `status` = 1 ", addressId, userId). UpdateColumn("status", 0).Error return err } // UpdateUsedAddress 当每次订单成功后, 更新一次地址使用时间 func (a *FsAddressModel) UpdateUsedAddress(ctx context.Context, addressId int64, userId int64) (err error) { err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error { logx.Info("address_id:", addressId, " update used") now := time.Now().UTC() var updates = map[string]interface{}{ "ltime": now, "utime": now, } err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1", addressId, userId). UpdateColumns(updates).Error if err != nil { return err } return nil }) return err }