From 586e02c842926f7efccdedaf7275bbad9bbbae26 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Sat, 7 Oct 2023 16:59:52 +0800 Subject: [PATCH] info address used --- model/gmodel/fs_address_logic.go | 40 +++++++++++++------ .../internal/handler/addressdefaulthandler.go | 2 +- server/info/internal/logic/addressaddlogic.go | 2 +- .../internal/logic/addressdefaultlogic.go | 9 ++++- .../info/internal/logic/addressupdatelogic.go | 2 +- server/info/internal/types/types.go | 5 +++ server_api/info.api | 7 +++- 7 files changed, 48 insertions(+), 19 deletions(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index c8f47563..24aacb0a 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -80,12 +80,11 @@ func (a *FsAddressModel) UpdateAddress(ctx context.Context, address *FsAddress) return err } -func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId int64, addressId int64) (err error) { +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 ") - + 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 @@ -93,17 +92,32 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i 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 - if err != nil { - return err - } + if isDefault == 0 { + + 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 + } + + } else { + + err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1 ", addressId, userId). + UpdateColumn("ltime", now.AddDate(250, 0, 0)). + 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 + } - 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).Error - if err != nil { - logx.Error(err) } return nil diff --git a/server/info/internal/handler/addressdefaulthandler.go b/server/info/internal/handler/addressdefaulthandler.go index 38920ca7..28bbe7eb 100644 --- a/server/info/internal/handler/addressdefaulthandler.go +++ b/server/info/internal/handler/addressdefaulthandler.go @@ -14,7 +14,7 @@ import ( func AddressDefaultHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.AddressIdRequest + var req types.AddressDefaultRequest userinfo, err := basic.RequestParse(w, r, svcCtx, &req) if err != nil { return diff --git a/server/info/internal/logic/addressaddlogic.go b/server/info/internal/logic/addressaddlogic.go index b53a3978..f173fdc7 100644 --- a/server/info/internal/logic/addressaddlogic.go +++ b/server/info/internal/logic/addressaddlogic.go @@ -73,7 +73,7 @@ func (l *AddressAddLogic) AddressAdd(req *types.AddressRequest, userinfo *auth.U } if req.IsDefault > 0 { - m.SettingUserDefaultAddress(l.ctx, userinfo.UserId, address.AddressId) + m.SettingUserDefaultAddress(l.ctx, userinfo.UserId, address.AddressId, req.IsDefault) } addresses, err := m.GetUserAllAddress(l.ctx, userinfo.UserId) diff --git a/server/info/internal/logic/addressdefaultlogic.go b/server/info/internal/logic/addressdefaultlogic.go index 903a5dd5..6df93f27 100644 --- a/server/info/internal/logic/addressdefaultlogic.go +++ b/server/info/internal/logic/addressdefaultlogic.go @@ -30,7 +30,7 @@ func NewAddressDefaultLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ad // func (l *AddressDefaultLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { // } -func (l *AddressDefaultLogic) AddressDefault(req *types.AddressIdRequest, userinfo *auth.UserInfo) (resp *basic.Response) { +func (l *AddressDefaultLogic) AddressDefault(req *types.AddressDefaultRequest, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null @@ -38,7 +38,12 @@ func (l *AddressDefaultLogic) AddressDefault(req *types.AddressIdRequest, userin return resp.SetStatus(basic.CodeUnAuth) } - err := l.svcCtx.AllModels.FsAddress.SettingUserDefaultAddress(l.ctx, userinfo.UserId, req.AddressId) + // 确认这个IsDefault的值范围 + if !auth.CheckValueRange(req.IsDefault, 0, 1) { + return resp.SetStatus(basic.CodeSafeValueRangeErr) // IsDefault值超出范围, 返回安全值范围错误 + } + + err := l.svcCtx.AllModels.FsAddress.SettingUserDefaultAddress(l.ctx, userinfo.UserId, req.AddressId, req.IsDefault) if err != nil { return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error()) } diff --git a/server/info/internal/logic/addressupdatelogic.go b/server/info/internal/logic/addressupdatelogic.go index 377021c2..d6ef1953 100644 --- a/server/info/internal/logic/addressupdatelogic.go +++ b/server/info/internal/logic/addressupdatelogic.go @@ -71,7 +71,7 @@ func (l *AddressUpdateLogic) AddressUpdate(req *types.AddressRequest, userinfo * } if req.IsDefault > 0 { - l.svcCtx.AllModels.FsAddress.SettingUserDefaultAddress(l.ctx, userinfo.UserId, address.AddressId) + l.svcCtx.AllModels.FsAddress.SettingUserDefaultAddress(l.ctx, userinfo.UserId, address.AddressId, req.IsDefault) } addresses, err := l.svcCtx.AllModels.FsAddress.GetUserAllAddress(l.ctx, userinfo.UserId) diff --git a/server/info/internal/types/types.go b/server/info/internal/types/types.go index 581340aa..abe5f37b 100644 --- a/server/info/internal/types/types.go +++ b/server/info/internal/types/types.go @@ -18,6 +18,11 @@ type AddressIdRequest struct { AddressId int64 `json:"address_id"` // 地址id } +type AddressDefaultRequest struct { + AddressId int64 `json:"address_id"` // 地址id + IsDefault int64 `json:"is_default"` // 是否默认 +} + type AddressRequest struct { AddressId int64 `json:"address_id,optional"` IsDefault int64 `json:"is_default"` //是否默认 diff --git a/server_api/info.api b/server_api/info.api index 821a46e0..f467dce8 100644 --- a/server_api/info.api +++ b/server_api/info.api @@ -20,7 +20,7 @@ service info { post /api/info/user/profile/base/update(ProfileRequest) returns (response); @handler AddressDefaultHandler - post /api/info/address/default(AddressIdRequest) returns (response); + post /api/info/address/default(AddressDefaultRequest) returns (response); @handler AddressAddHandler post /api/info/address/add(AddressRequest) returns (response); @@ -52,6 +52,11 @@ type ( AddressId int64 `json:"address_id"` // 地址id } + AddressDefaultRequest { + AddressId int64 `json:"address_id"` // 地址id + IsDefault int64 `json:"is_default"` // 是否默认 + } + AddressRequest { AddressId int64 `json:"address_id,optional"` IsDefault int64 `json:"is_default"` //是否默认