新增了rpc服务定义和工具命令

This commit is contained in:
Rzy 2024-06-07 18:47:40 +08:00
parent 981f73ec8f
commit c771e3b559
10 changed files with 7 additions and 606 deletions

View File

@ -10,17 +10,6 @@ import (
type Config struct {
zrpc.RpcServerConf
etc.ExtraConfig
DB struct {
Master struct {
DataSource string
}
Slave struct {
DataSource string
}
MarketReseller struct {
DataSource string
}
}
Cache cache.CacheConf
ZLTX types.ZLTXConf
Market types.MarketConf

View File

@ -2,23 +2,16 @@ package svc
import (
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"trasfer_middleware/cmd/rpc/internal/config"
"trasfer_middleware/cmd/rpc/internal/logic/po/market"
"trasfer_middleware/cmd/rpc/internal/logic/po/zltx"
"trasfer_middleware/genModel"
"trasfer_middleware/genModel/marketReseller"
)
type ServiceContext struct {
Config config.Config
RedisClient *redis.Redis
DbRead *Model
DbWrite *Model
DbReseller *MarkerReseller
ZltxOrder *zltx.ZltxOrder
Market *market.Market
ZltxOrder *zltx.ZltxOrder
Market *market.Market
}
func NewServiceContext(c config.Config) *ServiceContext {
@ -29,32 +22,18 @@ func NewServiceContext(c config.Config) *ServiceContext {
Type: c.Redis.Type,
Pass: c.Redis.Pass,
}),
DbRead: DbModel(c.DB.Master.DataSource, c),
DbWrite: DbModel(c.DB.Slave.DataSource, c),
DbReseller: ResellerModel(c.DB.MarketReseller.DataSource, c),
ZltxOrder: zltx.NewZltxOrder(c.ZLTX),
Market: market.NewMarket(c.Market),
ZltxOrder: zltx.NewZltxOrder(c.ZLTX),
Market: market.NewMarket(c.Market),
}
}
func DbModel(datasource string, c config.Config) *Model {
sqlConn := sqlx.NewMysql(datasource)
//sqlConn := sqlx.NewMysql(datasource)
return &Model{
ClientModel: genModel.NewServerClientModel(sqlConn, c.Cache),
//ClientModel: genModel.NewServerClientModel(sqlConn, c.Cache),
}
}
type Model struct {
ClientModel genModel.ServerClientModel
}
func ResellerModel(datasource string, c config.Config) *MarkerReseller {
sqlConn := sqlx.NewMysql(datasource)
return &MarkerReseller{
ResellerMerchant: marketReseller.NewResellerMerchantModel(sqlConn, c.Cache),
}
}
type MarkerReseller struct {
ResellerMerchant marketReseller.ResellerMerchantModel
//ClientModel genModel.ServerClientModel
}

View File

@ -1,27 +0,0 @@
package marketReseller
import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ ResellerMerchantModel = (*customResellerMerchantModel)(nil)
type (
// ResellerMerchantModel is an interface to be customized, add more methods here,
// and implement the added methods in customResellerMerchantModel.
ResellerMerchantModel interface {
resellerMerchantModel
}
customResellerMerchantModel struct {
*defaultResellerMerchantModel
}
)
// NewResellerMerchantModel returns a model for the database table.
func NewResellerMerchantModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) ResellerMerchantModel {
return &customResellerMerchantModel{
defaultResellerMerchantModel: newResellerMerchantModel(conn, c, opts...),
}
}

View File

@ -1,208 +0,0 @@
// Code generated by goctl. DO NOT EDIT.
package marketReseller
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
var (
resellerMerchantFieldNames = builder.RawFieldNames(&ResellerMerchant{})
resellerMerchantRows = strings.Join(resellerMerchantFieldNames, ",")
resellerMerchantRowsExpectAutoSet = strings.Join(stringx.Remove(resellerMerchantFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
resellerMerchantRowsWithPlaceHolder = strings.Join(stringx.Remove(resellerMerchantFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
cacheResellerResellerMerchantIdPrefix = "cache:reseller:resellerMerchant:id:"
cacheResellerResellerMerchantAppIdPrefix = "cache:reseller:resellerMerchant:appId:"
cacheResellerResellerMerchantMerchantIdPrefix = "cache:reseller:resellerMerchant:merchantId:"
cacheResellerResellerMerchantPosIdPrefix = "cache:reseller:resellerMerchant:posId:"
)
type (
resellerMerchantModel interface {
Insert(ctx context.Context, data *ResellerMerchant) (sql.Result, error)
FindOne(ctx context.Context, id uint64) (*ResellerMerchant, error)
FindOneByAppId(ctx context.Context, appId string) (*ResellerMerchant, error)
FindOneByMerchantId(ctx context.Context, merchantId string) (*ResellerMerchant, error)
FindOneByPosId(ctx context.Context, posId string) (*ResellerMerchant, error)
Update(ctx context.Context, data *ResellerMerchant) error
Delete(ctx context.Context, id uint64) error
}
defaultResellerMerchantModel struct {
sqlc.CachedConn
table string
}
ResellerMerchant struct {
Id uint64 `db:"id"`
MerchantId string `db:"merchant_id"` // 商户号
ResellerId uint64 `db:"reseller_id"` // 分销商id
PosId string `db:"pos_id"` // 平台id 目前都是单平台
AppId string `db:"app_id"` // appid。目前都是单应用 通知地址 事件地址都针对于这一个应用配置
StoreId string `db:"store_id"` // 没有门店,兴业银行需求。 当作后期的扩展字段
ApiMode int64 `db:"api_mode"` // 对接类型 1-RSA+标准接口返回【默认】2-兴业银行模式
PublicKey sql.NullString `db:"public_key"` // 公钥
PrivateKey sql.NullString `db:"private_key"` // 私钥
MerchantPublicKey sql.NullString `db:"merchant_public_key"` // 客户侧公钥
SecretKey string `db:"secret_key"` // body加解密密钥
NotifyUrl sql.NullString `db:"notify_url"` // 通知地址配置
SubscribeEvent sql.NullString `db:"subscribe_event"` // 订阅事件
Status uint64 `db:"status"` // 状态 1正常 2禁用
UpdateTime sql.NullTime `db:"update_time"` // 编辑时间
CreateTime time.Time `db:"create_time"` // 创建时间
}
)
func newResellerMerchantModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultResellerMerchantModel {
return &defaultResellerMerchantModel{
CachedConn: sqlc.NewConn(conn, c, opts...),
table: "`reseller_merchant`",
}
}
func (m *defaultResellerMerchantModel) Delete(ctx context.Context, id uint64) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
resellerResellerMerchantAppIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantAppIdPrefix, data.AppId)
resellerResellerMerchantIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantIdPrefix, id)
resellerResellerMerchantMerchantIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantMerchantIdPrefix, data.MerchantId)
resellerResellerMerchantPosIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantPosIdPrefix, data.PosId)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
return conn.ExecCtx(ctx, query, id)
}, resellerResellerMerchantAppIdKey, resellerResellerMerchantIdKey, resellerResellerMerchantMerchantIdKey, resellerResellerMerchantPosIdKey)
return err
}
func (m *defaultResellerMerchantModel) FindOne(ctx context.Context, id uint64) (*ResellerMerchant, error) {
resellerResellerMerchantIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantIdPrefix, id)
var resp ResellerMerchant
err := m.QueryRowCtx(ctx, &resp, resellerResellerMerchantIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", resellerMerchantRows, m.table)
return conn.QueryRowCtx(ctx, v, query, id)
})
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultResellerMerchantModel) FindOneByAppId(ctx context.Context, appId string) (*ResellerMerchant, error) {
resellerResellerMerchantAppIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantAppIdPrefix, appId)
var resp ResellerMerchant
err := m.QueryRowIndexCtx(ctx, &resp, resellerResellerMerchantAppIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where `app_id` = ? limit 1", resellerMerchantRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, appId); err != nil {
return nil, err
}
return resp.Id, nil
}, m.queryPrimary)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultResellerMerchantModel) FindOneByMerchantId(ctx context.Context, merchantId string) (*ResellerMerchant, error) {
resellerResellerMerchantMerchantIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantMerchantIdPrefix, merchantId)
var resp ResellerMerchant
err := m.QueryRowIndexCtx(ctx, &resp, resellerResellerMerchantMerchantIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where `merchant_id` = ? limit 1", resellerMerchantRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, merchantId); err != nil {
return nil, err
}
return resp.Id, nil
}, m.queryPrimary)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultResellerMerchantModel) FindOneByPosId(ctx context.Context, posId string) (*ResellerMerchant, error) {
resellerResellerMerchantPosIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantPosIdPrefix, posId)
var resp ResellerMerchant
err := m.QueryRowIndexCtx(ctx, &resp, resellerResellerMerchantPosIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where `pos_id` = ? limit 1", resellerMerchantRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, posId); err != nil {
return nil, err
}
return resp.Id, nil
}, m.queryPrimary)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultResellerMerchantModel) Insert(ctx context.Context, data *ResellerMerchant) (sql.Result, error) {
resellerResellerMerchantAppIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantAppIdPrefix, data.AppId)
resellerResellerMerchantIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantIdPrefix, data.Id)
resellerResellerMerchantMerchantIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantMerchantIdPrefix, data.MerchantId)
resellerResellerMerchantPosIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantPosIdPrefix, data.PosId)
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, resellerMerchantRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.MerchantId, data.ResellerId, data.PosId, data.AppId, data.StoreId, data.ApiMode, data.PublicKey, data.PrivateKey, data.MerchantPublicKey, data.SecretKey, data.NotifyUrl, data.SubscribeEvent, data.Status)
}, resellerResellerMerchantAppIdKey, resellerResellerMerchantIdKey, resellerResellerMerchantMerchantIdKey, resellerResellerMerchantPosIdKey)
return ret, err
}
func (m *defaultResellerMerchantModel) Update(ctx context.Context, newData *ResellerMerchant) error {
data, err := m.FindOne(ctx, newData.Id)
if err != nil {
return err
}
resellerResellerMerchantAppIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantAppIdPrefix, data.AppId)
resellerResellerMerchantIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantIdPrefix, data.Id)
resellerResellerMerchantMerchantIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantMerchantIdPrefix, data.MerchantId)
resellerResellerMerchantPosIdKey := fmt.Sprintf("%s%v", cacheResellerResellerMerchantPosIdPrefix, data.PosId)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, resellerMerchantRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, newData.MerchantId, newData.ResellerId, newData.PosId, newData.AppId, newData.StoreId, newData.ApiMode, newData.PublicKey, newData.PrivateKey, newData.MerchantPublicKey, newData.SecretKey, newData.NotifyUrl, newData.SubscribeEvent, newData.Status, newData.Id)
}, resellerResellerMerchantAppIdKey, resellerResellerMerchantIdKey, resellerResellerMerchantMerchantIdKey, resellerResellerMerchantPosIdKey)
return err
}
func (m *defaultResellerMerchantModel) formatPrimary(primary any) string {
return fmt.Sprintf("%s%v", cacheResellerResellerMerchantIdPrefix, primary)
}
func (m *defaultResellerMerchantModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", resellerMerchantRows, m.table)
return conn.QueryRowCtx(ctx, v, query, primary)
}
func (m *defaultResellerMerchantModel) tableName() string {
return m.table
}

View File

@ -1,27 +0,0 @@
package marketReseller
import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ ResellerModel = (*customResellerModel)(nil)
type (
// ResellerModel is an interface to be customized, add more methods here,
// and implement the added methods in customResellerModel.
ResellerModel interface {
resellerModel
}
customResellerModel struct {
*defaultResellerModel
}
)
// NewResellerModel returns a model for the database table.
func NewResellerModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) ResellerModel {
return &customResellerModel{
defaultResellerModel: newResellerModel(conn, c, opts...),
}
}

View File

@ -1,134 +0,0 @@
// Code generated by goctl. DO NOT EDIT.
package marketReseller
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
var (
resellerFieldNames = builder.RawFieldNames(&Reseller{})
resellerRows = strings.Join(resellerFieldNames, ",")
resellerRowsExpectAutoSet = strings.Join(stringx.Remove(resellerFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
resellerRowsWithPlaceHolder = strings.Join(stringx.Remove(resellerFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
cacheResellerResellerIdPrefix = "cache:reseller:reseller:id:"
)
type (
resellerModel interface {
Insert(ctx context.Context, data *Reseller) (sql.Result, error)
FindOne(ctx context.Context, id uint64) (*Reseller, error)
Update(ctx context.Context, data *Reseller) error
Delete(ctx context.Context, id uint64) error
}
defaultResellerModel struct {
sqlc.CachedConn
table string
}
Reseller struct {
Id uint64 `db:"id"` // 主键
Creator int64 `db:"creator"` // 操作人员
Code string `db:"code"` // 分销商编号
Name string `db:"name"` // 分销商名称
Subject string `db:"subject"` // 分销商主体
Account string `db:"account"` // 登录账号
Password string `db:"password"` // 登录密码
Status uint64 `db:"status"` // 分销商账号状态
HeadImg string `db:"head_img"` // 头像
SalesmanId uint64 `db:"salesman_id"` // 销售人员ID,后台用户表ID
SalesmanName string `db:"salesman_name"` // 销售人员名称
CompanyId uint64 `db:"company_id"` // 所属公司公司表ID
CompanyName string `db:"company_name"` // 公司名称
IdentityType uint64 `db:"identity_type"` // 身份类型
Balance float64 `db:"balance"` // 分销商余额
BalanceWarning float64 `db:"balance_warning"` // 余额预警
FrozenAmount float64 `db:"frozen_amount"` // 冻结金额
CreditAmount float64 `db:"credit_amount"` // 授信金额
TotalConsumption float64 `db:"total_consumption"` // 累计消费
ContactName string `db:"contact_name"` // 联系人名称
ContactPhone string `db:"contact_phone"` // 联系人电话
ContactEmail string `db:"contact_email"` // 联系人Email多个逗号(,)用逗号分开
CreateTime time.Time `db:"create_time"` // 创建时间
UpdateTime sql.NullTime `db:"update_time"` // 编辑时间
DeleteTime sql.NullTime `db:"delete_time"` // 删除时间
DirectResellerId uint64 `db:"direct_reseller_id"` // 直连天下分销商映射ID
MapTime sql.NullTime `db:"map_time"` // 映射时间
}
)
func newResellerModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultResellerModel {
return &defaultResellerModel{
CachedConn: sqlc.NewConn(conn, c, opts...),
table: "`reseller`",
}
}
func (m *defaultResellerModel) Delete(ctx context.Context, id uint64) error {
resellerResellerIdKey := fmt.Sprintf("%s%v", cacheResellerResellerIdPrefix, id)
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
return conn.ExecCtx(ctx, query, id)
}, resellerResellerIdKey)
return err
}
func (m *defaultResellerModel) FindOne(ctx context.Context, id uint64) (*Reseller, error) {
resellerResellerIdKey := fmt.Sprintf("%s%v", cacheResellerResellerIdPrefix, id)
var resp Reseller
err := m.QueryRowCtx(ctx, &resp, resellerResellerIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", resellerRows, m.table)
return conn.QueryRowCtx(ctx, v, query, id)
})
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultResellerModel) Insert(ctx context.Context, data *Reseller) (sql.Result, error) {
resellerResellerIdKey := fmt.Sprintf("%s%v", cacheResellerResellerIdPrefix, data.Id)
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, resellerRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.Creator, data.Code, data.Name, data.Subject, data.Account, data.Password, data.Status, data.HeadImg, data.SalesmanId, data.SalesmanName, data.CompanyId, data.CompanyName, data.IdentityType, data.Balance, data.BalanceWarning, data.FrozenAmount, data.CreditAmount, data.TotalConsumption, data.ContactName, data.ContactPhone, data.ContactEmail, data.DeleteTime, data.DirectResellerId, data.MapTime)
}, resellerResellerIdKey)
return ret, err
}
func (m *defaultResellerModel) Update(ctx context.Context, data *Reseller) error {
resellerResellerIdKey := fmt.Sprintf("%s%v", cacheResellerResellerIdPrefix, data.Id)
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, resellerRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, data.Creator, data.Code, data.Name, data.Subject, data.Account, data.Password, data.Status, data.HeadImg, data.SalesmanId, data.SalesmanName, data.CompanyId, data.CompanyName, data.IdentityType, data.Balance, data.BalanceWarning, data.FrozenAmount, data.CreditAmount, data.TotalConsumption, data.ContactName, data.ContactPhone, data.ContactEmail, data.DeleteTime, data.DirectResellerId, data.MapTime, data.Id)
}, resellerResellerIdKey)
return err
}
func (m *defaultResellerModel) formatPrimary(primary any) string {
return fmt.Sprintf("%s%v", cacheResellerResellerIdPrefix, primary)
}
func (m *defaultResellerModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", resellerRows, m.table)
return conn.QueryRowCtx(ctx, v, query, primary)
}
func (m *defaultResellerModel) tableName() string {
return m.table
}

View File

@ -1,5 +0,0 @@
package marketReseller
import "github.com/zeromicro/go-zero/core/stores/sqlx"
var ErrNotFound = sqlx.ErrNotFound

View File

@ -1,27 +0,0 @@
package genModel
import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ ServerClientModel = (*customServerClientModel)(nil)
type (
// ServerClientModel is an interface to be customized, add more methods here,
// and implement the added methods in customServerClientModel.
ServerClientModel interface {
serverClientModel
}
customServerClientModel struct {
*defaultServerClientModel
}
)
// NewServerClientModel returns a model for the database table.
func NewServerClientModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) ServerClientModel {
return &customServerClientModel{
defaultServerClientModel: newServerClientModel(conn, c, opts...),
}
}

View File

@ -1,134 +0,0 @@
// Code generated by goctl. DO NOT EDIT.
package genModel
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
var (
serverClientFieldNames = builder.RawFieldNames(&ServerClient{})
serverClientRows = strings.Join(serverClientFieldNames, ",")
serverClientRowsExpectAutoSet = strings.Join(stringx.Remove(serverClientFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
serverClientRowsWithPlaceHolder = strings.Join(stringx.Remove(serverClientFieldNames, "`client_id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
cacheTransferServerClientClientIdPrefix = "cache:transfer:serverClient:clientId:"
cacheTransferServerClientClientIndexPrefix = "cache:transfer:serverClient:clientIndex:"
)
type (
serverClientModel interface {
Insert(ctx context.Context, data *ServerClient) (sql.Result, error)
FindOne(ctx context.Context, clientId int64) (*ServerClient, error)
Update(ctx context.Context, data *ServerClient) error
Delete(ctx context.Context, clientId int64) error
FindOneByIndex(ctx context.Context, index string) (*ServerClient, error)
}
defaultServerClientModel struct {
sqlc.CachedConn
table string
}
ServerClient struct {
ClientId int64 `db:"client_id"`
ClientName string `db:"client_name"` // 客户名称
ClientIndex string `db:"client_index"` // 客户索引
ZltxMerchantId int64 `db:"zltx_merchant_id"`
ZltxSecret string `db:"zltx_secret"`
CreateTime time.Time `db:"create_time"`
UpdateTime time.Time `db:"update_time"`
Status int64 `db:"status"`
}
)
func newServerClientModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultServerClientModel {
return &defaultServerClientModel{
CachedConn: sqlc.NewConn(conn, c, opts...),
table: "`server_client`",
}
}
func (m *defaultServerClientModel) Delete(ctx context.Context, clientId int64) error {
transferServerClientClientIdKey := fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, clientId)
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("delete from %s where `client_id` = ?", m.table)
return conn.ExecCtx(ctx, query, clientId)
}, transferServerClientClientIdKey)
return err
}
func (m *defaultServerClientModel) FindOne(ctx context.Context, clientId int64) (*ServerClient, error) {
transferServerClientClientIdKey := fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, clientId)
var resp ServerClient
err := m.QueryRowCtx(ctx, &resp, transferServerClientClientIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `client_id` = ? limit 1", serverClientRows, m.table)
return conn.QueryRowCtx(ctx, v, query, clientId)
})
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultServerClientModel) FindOneByIndex(ctx context.Context, index string) (*ServerClient, error) {
transferServerClientClientIndexKey := fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, index)
var resp ServerClient
err := m.QueryRowCtx(ctx, &resp, transferServerClientClientIndexKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `client_id` = ? limit 1", serverClientRows, m.table)
return conn.QueryRowCtx(ctx, v, query, index)
})
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultServerClientModel) Insert(ctx context.Context, data *ServerClient) (sql.Result, error) {
transferServerClientClientIdKey := fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, data.ClientId)
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, serverClientRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.ClientId, data.ClientName, data.ClientIndex, data.ZltxMerchantId, data.ZltxSecret, data.Status)
}, transferServerClientClientIdKey)
return ret, err
}
func (m *defaultServerClientModel) Update(ctx context.Context, data *ServerClient) error {
transferServerClientClientIdKey := fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, data.ClientId)
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `client_id` = ?", m.table, serverClientRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, data.ClientName, data.ClientIndex, data.ZltxMerchantId, data.ZltxSecret, data.Status, data.ClientId)
}, transferServerClientClientIdKey)
return err
}
func (m *defaultServerClientModel) formatPrimary(primary any) string {
return fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, primary)
}
func (m *defaultServerClientModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
query := fmt.Sprintf("select %s from %s where `client_id` = ? limit 1", serverClientRows, m.table)
return conn.QueryRowCtx(ctx, v, query, primary)
}
func (m *defaultServerClientModel) tableName() string {
return m.table
}

View File

@ -1,5 +0,0 @@
package genModel
import "github.com/zeromicro/go-zero/core/stores/sqlx"
var ErrNotFound = sqlx.ErrNotFound