fusenapi/server/home-user-auth/internal/logic/useraddaddresslogic.go
2023-06-19 20:10:32 +08:00

101 lines
2.4 KiB
Go

package logic
import (
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"context"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UserAddAddressLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserAddAddressLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAddAddressLogic {
return &UserAddAddressLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UserAddAddressLogic) UserAddAddress(req *types.RequestAddAddress, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
if !userinfo.IsUser() {
return resp.SetStatus(basic.CodeUnAuth)
}
// 确认这个IsDefault的值范围
if !auth.CheckValueRange(req.IsDefault, 0, 1) {
return resp.SetStatus(basic.CodeSafeValueRangeErr)
}
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})
}