From 04106851382f78670bcccc64ac1ed84533bfc518 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Sun, 8 Oct 2023 11:23:22 +0800 Subject: [PATCH] info address default --- model/gmodel/fs_address_logic.go | 53 ++++++++++++-------------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 154c9d88..83ff4e7c 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -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("`ctime` DESC").Find(&resp).Error + err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = 1", userId).Order("`ltime` DESC").Find(&resp).Error if err != nil { return nil, err } @@ -65,44 +65,31 @@ func (a *FsAddressModel) UpdateAddress(ctx context.Context, address *FsAddress) return err } -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("`user_id` = ? and `status` = 1 and `is_default` = 1", userId). - UpdateColumn("is_default", 0). - UpdateColumn("utime", now).Error - if err != nil { - return err - } - - 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 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() - 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 isDefault == 1 { + err = tx.Where("`user_id` = ? and `status` = 1 and `ltime` > ? and `address_id` != ?", userId, now.AddDate(10, 0, 0), addressId). + UpdateColumn("ltime", now). + UpdateColumn("utime", now).Error + if err != nil { + return err + } + } + + tr := tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1", addressId, userId). + UpdateColumn("utime", now) + if isDefault == 1 { + tr.UpdateColumn("ltime", now.AddDate(250, 0, 0)) + } else { + tr.UpdateColumn("ltime", now) + } + + err = tr.Error if err != nil { return err }