info address default
This commit is contained in:
parent
da162f859e
commit
ced3ce8657
@ -22,6 +22,7 @@ type FsAddress struct {
|
|||||||
Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` // 创建时间
|
Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` // 创建时间
|
||||||
Utime *time.Time `gorm:"index;default:'0000-00-00 00:00:00';" json:"utime"` // 更新时间
|
Utime *time.Time `gorm:"index;default:'0000-00-00 00:00:00';" json:"utime"` // 更新时间
|
||||||
Ltime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ltime"` // 上次被使用的时间
|
Ltime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ltime"` // 上次被使用的时间
|
||||||
|
IsDefault *int64 `gorm:"default:0;" json:"is_default"` // 1默认值,0非默认值
|
||||||
}
|
}
|
||||||
type FsAddressModel struct {
|
type FsAddressModel struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
|
@ -14,7 +14,7 @@ func (a *FsAddressModel) GetOne(ctx context.Context, addressId int64, userId int
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *FsAddressModel) GetUserAllAddress(ctx context.Context, userId int64) (resp []FsAddress, err error) {
|
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` = 1", userId).Order("`ltime` DESC").Find(&resp).Error
|
err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = 1", userId).Order("`ctime` DESC").Find(&resp).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -37,26 +37,11 @@ func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (res
|
|||||||
Country: address.Country,
|
Country: address.Country,
|
||||||
ZipCode: address.ZipCode,
|
ZipCode: address.ZipCode,
|
||||||
Status: address.Status,
|
Status: address.Status,
|
||||||
|
IsDefault: address.IsDefault,
|
||||||
Ctime: &now,
|
Ctime: &now,
|
||||||
Utime: &now,
|
Utime: &now,
|
||||||
Ltime: &now,
|
Ltime: &now,
|
||||||
}
|
}
|
||||||
|
|
||||||
// lastOne := &FsAddress{}
|
|
||||||
// err = tx.Where("user_id = ?", lastOne.UserId).Order("ltime ASC").Take(&lastOne).Error
|
|
||||||
// if err == gorm.ErrRecordNotFound {
|
|
||||||
// result.Ltime = &now
|
|
||||||
// return tx.Model(&FsAddress{}).Create(result).Error
|
|
||||||
// }
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 根据lastOne处理时间
|
|
||||||
|
|
||||||
// ltime := (*lastOne.Ltime).Add(-time.Second)
|
|
||||||
// result.Ltime = <ime
|
|
||||||
return tx.Model(&FsAddress{}).Create(result).Error
|
return tx.Model(&FsAddress{}).Create(result).Error
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -80,44 +65,46 @@ func (a *FsAddressModel) UpdateAddress(ctx context.Context, address *FsAddress)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId int64, addressId int64, isDefault int64) (err error) {
|
func (a *FsAddressModel) SettingUserDefaultAddressMutex(ctx context.Context, userId int64, addressId int64, isDefault int64) (err error) {
|
||||||
|
|
||||||
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
|
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
|
||||||
|
|
||||||
logx.Info("address_id:", addressId, " set default ", isDefault)
|
logx.Info("address_id:", addressId, " set default ", isDefault)
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
|
|
||||||
err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1 ", addressId, userId).Take(nil).Error
|
err = tx.Where("`user_id` = ? and `status` = 1 and `is_default` = 1", userId).
|
||||||
|
UpdateColumn("is_default", 0).
|
||||||
|
UpdateColumn("utime", now).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if isDefault == 0 {
|
err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1", addressId, userId).
|
||||||
|
UpdateColumn("is_default", isDefault).
|
||||||
|
UpdateColumn("ltime", now).
|
||||||
|
UpdateColumn("utime", now).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
err = tx.Model(&FsAddress{}).Where("`address_id` = ? and `user_id` = ? and `status` = 1 and `ltime` > ?", addressId, userId, now.AddDate(10, 0, 0)).
|
return nil
|
||||||
UpdateColumn("ltime", now).
|
})
|
||||||
UpdateColumn("utime", now).Error
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1 ", addressId, userId).
|
func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId int64, addressId int64, isDefault int64) (err error) {
|
||||||
UpdateColumn("ltime", now.AddDate(250, 0, 0)).
|
|
||||||
UpdateColumn("utime", now).Error
|
|
||||||
|
|
||||||
if err != nil {
|
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = tx.Model(&FsAddress{}).Where("`address_id` != ? and `user_id` = ? and `status` = 1 and `ltime` > ?", addressId, userId, now.AddDate(10, 0, 0)).
|
|
||||||
UpdateColumn("ltime", now).
|
|
||||||
UpdateColumn("utime", now).Error
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
|
logx.Info("address_id:", addressId, " set default ", isDefault)
|
||||||
|
now := time.Now().UTC()
|
||||||
|
err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1", addressId, userId).
|
||||||
|
UpdateColumn("is_default", isDefault).
|
||||||
|
UpdateColumn("ltime", now).
|
||||||
|
UpdateColumn("utime", now).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -138,7 +125,8 @@ func (a *FsAddressModel) UpdateUsedAddress(ctx context.Context, addressId int64,
|
|||||||
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
|
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
|
||||||
logx.Info("address_id:", addressId, " update used")
|
logx.Info("address_id:", addressId, " update used")
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1 and `ltime` < ?", addressId, userId, now).
|
err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1", addressId, userId).
|
||||||
|
UpdateColumn("utime", now).
|
||||||
UpdateColumn("ltime", now).Error
|
UpdateColumn("ltime", now).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user