diff --git a/model/gmodel/fs_address_gen.go b/model/gmodel/fs_address_gen.go index 979d03ab..67ec691d 100644 --- a/model/gmodel/fs_address_gen.go +++ b/model/gmodel/fs_address_gen.go @@ -19,7 +19,7 @@ type FsAddress struct { Country *string `gorm:"default:'';" json:"country"` // 国家 ZipCode *string `gorm:"default:'';" json:"zip_code"` // 邮编 Status *int64 `gorm:"default:0;" json:"status"` // 1正常 0异常 - IsDefault *int64 `gorm:"default:0;" json:"is_default"` // 1默认地址,0非默认地址 + IsDefault *int64 `gorm:"index;default:0;" json:"is_default"` // 1默认地址,0非默认地址 } type FsAddressModel struct{ db *gorm.DB } diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 734a712f..1c56e5ef 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -15,6 +15,14 @@ func (a *FsAddressModel) GetOne(ctx context.Context, id int64, userId int64) (re return resp, nil } +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` = ?", userId, 1).Order("`id` DESC").Find(&resp).Error + if err != nil { + return nil, err + } + return +} + func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (resp *FsAddress, err error) { err = a.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { @@ -45,10 +53,15 @@ func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (res return resp, nil } -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` = ?", userId, 1).Order("`id` DESC").Find(&resp).Error - if err != nil { - return nil, err - } - return +func (a *FsAddressModel) UpdateAddAddress(ctx context.Context, address *FsAddress) (err error) { + err = a.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + if *address.IsDefault > 0 { + err = tx.Where("user_id = ? and is_default = 1", *address.UserId).Update("is_default", 0).Error + if err != nil { + return err + } + } + return a.db.WithContext(ctx).Where("id = ? and user_id = ?", address.Id, address.UserId).Omit("id", "user_id").Updates(address).Error + }) + return err } diff --git a/server/home-user-auth/internal/logic/useraddaddresslogic.go b/server/home-user-auth/internal/logic/useraddaddresslogic.go index 7f1c42b0..e4956921 100644 --- a/server/home-user-auth/internal/logic/useraddaddresslogic.go +++ b/server/home-user-auth/internal/logic/useraddaddresslogic.go @@ -1,6 +1,7 @@ package logic import ( + "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" @@ -38,5 +39,62 @@ func (l *UserAddAddressLogic) UserAddAddress(req *types.RequestAddAddress, useri return resp.SetStatus(basic.CodeSafeValueRangeErr) } - return resp.SetStatus(basic.CodeOK) + m := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn) + var status int64 = 1 + + if req.Id == 0 { + // $address->country = 'USA'; + // $address->status = 1; + // $address->user_id = $uid; + var ( + country string = "USA" + + isDefautl int64 = 1 + ) + createOne := &gmodel.FsAddress{ + Name: &req.Name, + FirstName: &req.FirstName, + LastName: &req.LastName, + Mobile: &req.Mobile, + Street: &req.Street, + Suite: &req.Suite, + City: &req.City, + State: &req.State, + Country: &country, + Status: &status, + UserId: &userinfo.UserId, + ZipCode: &req.ZipCode, + IsDefault: &isDefautl, + } + _, err := m.CreateOne(l.ctx, createOne) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeDbCreateErr) + } + return resp.SetStatus(basic.CodeOK, map[string]int64{"id": createOne.Id}) + } + + address := &gmodel.FsAddress{ + Id: req.Id, + Name: &req.Name, + FirstName: &req.FirstName, + LastName: &req.LastName, + Mobile: &req.Mobile, + Street: &req.Street, + Suite: &req.Suite, + City: &req.City, + State: &req.State, + Status: &status, + UserId: &userinfo.UserId, + ZipCode: &req.ZipCode, + IsDefault: &req.IsDefault, + } + + err := m.UpdateAddAddress(l.ctx, address) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeDbUpdateErr) + } + + return resp.SetStatus(basic.CodeOK, map[string]int64{"id": address.Id}) } diff --git a/server/home-user-auth/internal/logic/usersavebasicinfologic.go b/server/home-user-auth/internal/logic/usersavebasicinfologic.go index 5f16dcff..bd1ac35a 100644 --- a/server/home-user-auth/internal/logic/usersavebasicinfologic.go +++ b/server/home-user-auth/internal/logic/usersavebasicinfologic.go @@ -54,7 +54,7 @@ func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoFo case gmodel.ErrRecordNotFound: return resp.SetStatus(basic.CodeUserIdNotFoundErr) default: - return resp.SetStatusWithMessage(basic.CodeUpdateErr, err.Error()) + return resp.SetStatusWithMessage(basic.CodeDbUpdateErr, err.Error()) } } diff --git a/server/home-user-auth/internal/types/types.go b/server/home-user-auth/internal/types/types.go index 14bd5b86..e9e54607 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -31,7 +31,7 @@ type RequestUserLogin struct { type RequestAddAddress struct { Id int64 `json:"id"` // address_id 地址id - IsDefault int `json:"is_default"` //是否默认 + IsDefault int64 `json:"is_default"` //是否默认 Name string `json:"name"` //收货人 FirstName string `json:"first_name"` //first_name LastName string `json:"last_name"` //last_name diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index d0bfcfde..1a6b5a3e 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -69,7 +69,7 @@ type RequestUserLogin { type RequestAddAddress { Id int64 `json:"id"` // address_id 地址id - IsDefault int `json:"is_default"` //是否默认 + IsDefault int64 `json:"is_default"` //是否默认 Name string `json:"name"` //收货人 FirstName string `json:"first_name"` //first_name LastName string `json:"last_name"` //last_name diff --git a/utils/basic/basic.go b/utils/basic/basic.go index 1e6c7b7d..7ded9cc0 100644 --- a/utils/basic/basic.go +++ b/utils/basic/basic.go @@ -22,10 +22,11 @@ var ( CodeGuestDupErr = &StatusResponse{5010, "the user is already a guest user and does not need to apply again"} // 用户已经是guest用户不需要重复申请 错误 CodeGuestGenErr = &StatusResponse{5011, "serialization failed for guest_id of the visitor"} // 游客guest_id序列化失败 - CodeUpdateErr = &StatusResponse{5000, "update database error"} // update database logic 错误 + CodeDbUpdateErr = &StatusResponse{5000, "update database error"} // update database logic 错误 CodeDupGuestErr = &StatusResponse{5001, "the user is already a guest user and does not need to apply again"} // 用户已经是guest用户不需要重复申请 错误 CodeRequestParamsErr = &StatusResponse{5002, "invalid request param"} // api参数校验 错误 CodeDbRecordNotFoundErr = &StatusResponse{5003, "db record not found"} + CodeDbCreateErr = &StatusResponse{5004, "create one in database error"} ) type Response struct { @@ -58,7 +59,11 @@ func (resp *Response) SetStatus(sr *StatusResponse, data ...interface{}) *Respon Message: sr.Message, } if len(data) == 1 { - newResp.Data = data[0] + if err, ok := data[0].(error); ok { + newResp.Message = err.Error() + } else { + newResp.Data = data[0] + } } return newResp }