fusenapi/model/fsaddressmodel.go
2023-06-08 15:41:49 +08:00

68 lines
1.9 KiB
Go
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import (
"context"
"fmt"
"strings"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
var _ FsAddressModel = (*customFsAddressModel)(nil)
type (
// FsAddressModel is an interface to be customized, add more methods here,
// and implement the added methods in customFsAddressModel.
FsAddressModel interface {
fsAddressModel
FindDataAddressList(ctx context.Context, userid int64) (*DataAddressList, error)
}
customFsAddressModel struct {
*defaultFsAddressModel
}
DataAddressList struct {
Id int64 `db:"id"`
UserId int64 `db:"user_id"` // 用户ID
Name string `db:"name"` // 地址名称
FirstName string `db:"first_name"` // FirstName
LastName string `db:"last_name"` // LastName
Mobile string `db:"mobile"` // 手机号码
Street string `db:"street"` // 街道
Suite string `db:"suite"` // 房号
City string `db:"city"` // 城市
State string `db:"state"` // 州名
ZipCode string `db:"zip_code"` // 邮编
IsDefault int64 `db:"is_default"` // 1默认地址0非默认地址
}
)
var (
// DataAddressList 结构需要的字段
fsDataAddressListRows = strings.Join(stringx.Remove(fsAddressFieldNames, "`status`", "`country`"), ",")
)
// NewFsAddressModel returns a model for the database table.
func NewFsAddressModel(conn sqlx.SqlConn) FsAddressModel {
return &customFsAddressModel{
defaultFsAddressModel: newFsAddressModel(conn),
}
}
func (m *defaultFsAddressModel) FindDataAddressList(ctx context.Context, userid int64) (*DataAddressList, error) {
query := fmt.Sprintf("select %s from %s where `user_id` = ? ", fsDataAddressListRows, m.table)
var resp DataAddressList
err := m.conn.QueryRowCtx(ctx, &resp, query, userid)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}