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"` // 创建时间
|
||||
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"` // 上次被使用的时间
|
||||
IsDefault *int64 `gorm:"default:0;" json:"is_default"` // 1默认值,0非默认值
|
||||
}
|
||||
type FsAddressModel struct {
|
||||
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) {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -37,26 +37,11 @@ func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (res
|
|||
Country: address.Country,
|
||||
ZipCode: address.ZipCode,
|
||||
Status: address.Status,
|
||||
|
||||
Ctime: &now,
|
||||
Utime: &now,
|
||||
Ltime: &now,
|
||||
IsDefault: address.IsDefault,
|
||||
Ctime: &now,
|
||||
Utime: &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
|
||||
})
|
||||
|
||||
|
@ -80,44 +65,46 @@ func (a *FsAddressModel) UpdateAddress(ctx context.Context, address *FsAddress)
|
|||
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 {
|
||||
|
||||
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).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 {
|
||||
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)).
|
||||
UpdateColumn("ltime", now).
|
||||
UpdateColumn("utime", now).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1 ", addressId, userId).
|
||||
UpdateColumn("ltime", now.AddDate(250, 0, 0)).
|
||||
UpdateColumn("utime", now).Error
|
||||
func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId int64, addressId int64, isDefault int64) (err 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)).
|
||||
UpdateColumn("ltime", now).
|
||||
UpdateColumn("utime", now).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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()
|
||||
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
|
||||
|
@ -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 {
|
||||
logx.Info("address_id:", addressId, " update used")
|
||||
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
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
Loading…
Reference in New Issue
Block a user