feat: gorm
parent
1417e654a4
commit
9a4978e1e9
@ -1,20 +0,0 @@
|
||||
@echo off
|
||||
@echo ??????????????
|
||||
|
||||
set tables=gift
|
||||
set targetDir=.\model
|
||||
set templateDir=..\..\doc\template
|
||||
|
||||
::set host=127.0.0.1
|
||||
::set port=23306
|
||||
set host=192.168.1.100
|
||||
set port=3306
|
||||
::set dbname=dcg
|
||||
set dbname=dmgame
|
||||
set username=root
|
||||
set password=root
|
||||
|
||||
for %%i in (%tables%) do (
|
||||
echo ????????? %dbname% ??? %%i
|
||||
goctl model mysql datasource --url "%username%:%password%@tcp(%host%:%port%)/%dbname%" -t %%i -d %targetDir% --style go_zero --home %templateDir%
|
||||
)
|
@ -1,12 +0,0 @@
|
||||
package user_center
|
||||
|
||||
// protoc 生成
|
||||
//go:generate goctl rpc protoc ./rpc/pb/gift.proto --style=go_zero --go_out=./rpc --go-grpc_out=./rpc --zrpc_out=./rpc
|
||||
|
||||
// api 生成
|
||||
//go:generate goctl api go -api ./api/doc/user_center.api -dir ./api --style go_zero
|
||||
|
||||
// api swagger 文档生成 需要sh
|
||||
//go:generate goctl api plugin -plugin goctl-swagger="swagger -filename user_center.json -host 127.0.0.1" -api ./api/doc/user_center.api -dir ./api/doc
|
||||
|
||||
// model 生成 配合genModel.sh (可能需要复制到terminal执行)
|
@ -1,44 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ GiftModel = (*customGiftModel)(nil)
|
||||
|
||||
type (
|
||||
// GiftModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customGiftModel.
|
||||
GiftModel interface {
|
||||
giftModel
|
||||
FindOneByGiftId(ctx context.Context, giftId string) (*Gift, error)
|
||||
}
|
||||
|
||||
customGiftModel struct {
|
||||
*defaultGiftModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewGiftModel returns a model for the database table.
|
||||
func NewGiftModel(conn sqlx.SqlConn) GiftModel {
|
||||
return &customGiftModel{
|
||||
defaultGiftModel: newGiftModel(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customGiftModel) FindOneByGiftId(ctx context.Context, giftId string) (*Gift, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `gift_id` = ? limit 1", giftRows, m.table)
|
||||
var resp Gift
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, giftId)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
@ -1,17 +0,0 @@
|
||||
Name: gift.rpc
|
||||
ListenOn: 127.0.0.1:10002
|
||||
DB:
|
||||
DataSource: root:root@tcp(127.0.0.1:3306)/dmgame?charset=utf8mb4&loc=Asia%2FShanghai&parseTime=true
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: gift.rpc
|
||||
Gift:
|
||||
Kafka:
|
||||
Addr: [ "127.0.0.1:9093" ]
|
||||
Topic: "gift"
|
||||
ConsumerGroupId: "msgToDb"
|
||||
Log:
|
||||
Mode: file
|
||||
KeepDays: 7
|
||||
Level: error
|
@ -1,45 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"live-service/app/gift/rpc/msg_transfer"
|
||||
|
||||
"live-service/app/gift/rpc/internal/config"
|
||||
"live-service/app/gift/rpc/internal/server"
|
||||
"live-service/app/gift/rpc/internal/svc"
|
||||
"live-service/app/gift/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/gift.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
ctx := svc.NewServiceContext(c)
|
||||
svr := server.NewGiftServer(ctx)
|
||||
|
||||
msg_transfer.Init(context.Background(), ctx, svr)
|
||||
msg_transfer.Run()
|
||||
|
||||
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||
pb.RegisterGiftServer(grpcServer, svr)
|
||||
|
||||
if c.Mode == service.DevMode || c.Mode == service.TestMode {
|
||||
reflection.Register(grpcServer)
|
||||
}
|
||||
})
|
||||
defer s.Stop()
|
||||
|
||||
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
|
||||
s.Start()
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: gift.proto
|
||||
|
||||
package gift
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"live-service/app/gift/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type (
|
||||
CollectGift = pb.CollectGift
|
||||
Empty = pb.Empty
|
||||
|
||||
Gift interface {
|
||||
// collectGift 收集礼物,仅作为收集使用
|
||||
CollectGift(ctx context.Context, in *CollectGift, opts ...grpc.CallOption) (*Empty, error)
|
||||
}
|
||||
|
||||
defaultGift struct {
|
||||
cli zrpc.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewGift(cli zrpc.Client) Gift {
|
||||
return &defaultGift{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
// collectGift 收集礼物,仅作为收集使用
|
||||
func (m *defaultGift) CollectGift(ctx context.Context, in *CollectGift, opts ...grpc.CallOption) (*Empty, error) {
|
||||
client := pb.NewGiftClient(m.cli.Conn())
|
||||
return client.CollectGift(ctx, in, opts...)
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type (
|
||||
Kafka struct {
|
||||
Addr []string
|
||||
Topic string
|
||||
ConsumerGroupId string
|
||||
}
|
||||
Config struct {
|
||||
zrpc.RpcServerConf
|
||||
Log logx.LogConf
|
||||
DB struct {
|
||||
DataSource string
|
||||
}
|
||||
|
||||
Gift struct {
|
||||
Kafka Kafka
|
||||
}
|
||||
}
|
||||
)
|
@ -1,53 +0,0 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.noahlan.cn/northlan/ntools-go/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"live-service/app/gift/model"
|
||||
"strconv"
|
||||
|
||||
"live-service/app/gift/rpc/internal/svc"
|
||||
"live-service/app/gift/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CollectGiftLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewCollectGiftLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectGiftLogic {
|
||||
return &CollectGiftLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// CollectGift 收集礼物,仅作为收集使用
|
||||
func (l *CollectGiftLogic) CollectGift(in *pb.CollectGift) (*pb.Empty, error) {
|
||||
giftIdStr := strconv.FormatInt(in.GiftId, 10)
|
||||
dbGift, err := l.svcCtx.GiftModel.FindOneByGiftId(l.ctx, giftIdStr)
|
||||
if err == nil {
|
||||
return &pb.Empty{}, nil
|
||||
}
|
||||
if err != nil {
|
||||
if !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrap(err, "数据库错误")
|
||||
}
|
||||
}
|
||||
dbGift = &model.Gift{
|
||||
Id: uuid.NextId(),
|
||||
GiftId: giftIdStr,
|
||||
GiftName: in.GiftName,
|
||||
Platform: in.Platform,
|
||||
PPrice: in.TotalCoin,
|
||||
}
|
||||
if _, err := l.svcCtx.GiftModel.Insert(l.ctx, dbGift); err != nil {
|
||||
return nil, errors.Wrap(err, "数据库插入错误")
|
||||
}
|
||||
return &pb.Empty{}, nil
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: gift.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"live-service/app/gift/rpc/internal/logic"
|
||||
"live-service/app/gift/rpc/internal/svc"
|
||||
"live-service/app/gift/rpc/pb"
|
||||
)
|
||||
|
||||
type GiftServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
pb.UnimplementedGiftServer
|
||||
}
|
||||
|
||||
func NewGiftServer(svcCtx *svc.ServiceContext) *GiftServer {
|
||||
return &GiftServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// collectGift 收集礼物,仅作为收集使用
|
||||
func (s *GiftServer) CollectGift(ctx context.Context, in *pb.CollectGift) (*pb.Empty, error) {
|
||||
l := logic.NewCollectGiftLogic(ctx, s.svcCtx)
|
||||
return l.CollectGift(in)
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"live-service/app/gift/model"
|
||||
"live-service/app/gift/rpc/internal/config"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
|
||||
GiftModel model.GiftModel
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
// model
|
||||
GiftModel: model.NewGiftModel(sqlx.NewMysql(c.DB.DataSource)),
|
||||
}
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
package gift
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.noahlan.cn/northlan/ntools-go/kafka"
|
||||
"github.com/Shopify/sarama"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"live-service/app/gift/rpc/internal/server"
|
||||
"live-service/app/gift/rpc/internal/svc"
|
||||
"live-service/app/gift/rpc/pb"
|
||||
pbMq "live-service/app/pb/mq"
|
||||
kfk "live-service/common/kafka"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type msgHandlerFunc func(data []byte, msgKey string)
|
||||
|
||||
type MsgToDBHandler struct {
|
||||
context context.Context
|
||||
ctx *svc.ServiceContext
|
||||
svr *server.GiftServer
|
||||
|
||||
msgHandle map[string]msgHandlerFunc
|
||||
|
||||
ConsumerGroup *kafka.ConsumerGroup
|
||||
|
||||
mutex sync.Mutex
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func (h *MsgToDBHandler) Init(context context.Context, ctx *svc.ServiceContext, svr *server.GiftServer) {
|
||||
h.context = context
|
||||
h.ctx = ctx
|
||||
h.svr = svr
|
||||
h.Logger = logx.WithContext(h.context)
|
||||
|
||||
cfg := ctx.Config.Gift.Kafka
|
||||
h.msgHandle = make(map[string]msgHandlerFunc)
|
||||
h.msgHandle["gift"] = h.handleGift
|
||||
|
||||
var err error
|
||||
h.ConsumerGroup, err = kafka.NewConsumerGroup(&kafka.ConsumerGroupConfig{
|
||||
KafkaVersion: sarama.V3_1_0_0,
|
||||
OffsetsInitial: sarama.OffsetNewest,
|
||||
IsReturnErr: false,
|
||||
UnMarshaler: kfk.ProtobufMarshaler,
|
||||
}, cfg.Addr, []string{cfg.Topic}, cfg.ConsumerGroupId)
|
||||
|
||||
if err != nil {
|
||||
h.Logger.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MsgToDBHandler) handleGift(data []byte, msgKey string) {
|
||||
// msg proto
|
||||
var msgFromMq pbMq.MqGift
|
||||
if err := proto.Unmarshal(data, &msgFromMq); err != nil {
|
||||
h.Logger.Error("unmarshal msg err", err)
|
||||
return
|
||||
}
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
if _, err := h.svr.CollectGift(context.Background(), &pb.CollectGift{
|
||||
Platform: msgFromMq.Platform,
|
||||
GiftId: msgFromMq.GiftId,
|
||||
GiftName: msgFromMq.GiftName,
|
||||
TotalCoin: msgFromMq.TotalCoin,
|
||||
}); err != nil {
|
||||
h.Logger.Error("收集礼物错误 ", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (*MsgToDBHandler) Setup(_ sarama.ConsumerGroupSession) error { return nil }
|
||||
func (*MsgToDBHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
|
||||
func (h *MsgToDBHandler) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
|
||||
for msg := range claim.Messages() {
|
||||
if hFunc, ok := h.msgHandle[msg.Topic]; ok {
|
||||
hFunc(msg.Value, string(msg.Key))
|
||||
}
|
||||
sess.MarkMessage(msg, "")
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package msg_transfer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"live-service/app/gift/rpc/internal/server"
|
||||
"live-service/app/gift/rpc/internal/svc"
|
||||
"live-service/app/gift/rpc/msg_transfer/gift"
|
||||
)
|
||||
|
||||
var (
|
||||
giftMsgToDb gift.MsgToDBHandler
|
||||
)
|
||||
|
||||
func Init(context context.Context, ctx *svc.ServiceContext, svr *server.GiftServer) {
|
||||
giftMsgToDb.Init(context, ctx, svr)
|
||||
}
|
||||
|
||||
func Run() {
|
||||
go giftMsgToDb.ConsumerGroup.RegisterHandlerAndConsumer(&giftMsgToDb)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package pb
|
||||
|
||||
//go:generate goctl rpc protoc gift.proto --style=go_zero --go_out=../ --go-grpc_out=../ --zrpc_out=../
|
@ -1,228 +0,0 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.27.1
|
||||
// protoc v3.19.4
|
||||
// source: gift.proto
|
||||
|
||||
package pb
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
// req
|
||||
type CollectGift struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Platform string `protobuf:"bytes,1,opt,name=platform,proto3" json:"platform,omitempty"`
|
||||
GiftId int64 `protobuf:"varint,2,opt,name=giftId,proto3" json:"giftId,omitempty"`
|
||||
GiftName string `protobuf:"bytes,3,opt,name=giftName,proto3" json:"giftName,omitempty"`
|
||||
TotalCoin int64 `protobuf:"varint,4,opt,name=totalCoin,proto3" json:"totalCoin,omitempty"`
|
||||
}
|
||||
|
||||
func (x *CollectGift) Reset() {
|
||||
*x = CollectGift{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_gift_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *CollectGift) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*CollectGift) ProtoMessage() {}
|
||||
|
||||
func (x *CollectGift) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gift_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use CollectGift.ProtoReflect.Descriptor instead.
|
||||
func (*CollectGift) Descriptor() ([]byte, []int) {
|
||||
return file_gift_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *CollectGift) GetPlatform() string {
|
||||
if x != nil {
|
||||
return x.Platform
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CollectGift) GetGiftId() int64 {
|
||||
if x != nil {
|
||||
return x.GiftId
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *CollectGift) GetGiftName() string {
|
||||
if x != nil {
|
||||
return x.GiftName
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CollectGift) GetTotalCoin() int64 {
|
||||
if x != nil {
|
||||
return x.TotalCoin
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type Empty struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
}
|
||||
|
||||
func (x *Empty) Reset() {
|
||||
*x = Empty{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_gift_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Empty) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Empty) ProtoMessage() {}
|
||||
|
||||
func (x *Empty) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gift_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Empty.ProtoReflect.Descriptor instead.
|
||||
func (*Empty) Descriptor() ([]byte, []int) {
|
||||
return file_gift_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
var File_gift_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_gift_proto_rawDesc = []byte{
|
||||
0x0a, 0x0a, 0x67, 0x69, 0x66, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x02, 0x70, 0x62,
|
||||
0x22, 0x7b, 0x0a, 0x0b, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x47, 0x69, 0x66, 0x74, 0x12,
|
||||
0x1a, 0x0a, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28,
|
||||
0x09, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x67,
|
||||
0x69, 0x66, 0x74, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x67, 0x69, 0x66,
|
||||
0x74, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x67, 0x69, 0x66, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x18,
|
||||
0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x67, 0x69, 0x66, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x12,
|
||||
0x1c, 0x0a, 0x09, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x43, 0x6f, 0x69, 0x6e, 0x18, 0x04, 0x20, 0x01,
|
||||
0x28, 0x03, 0x52, 0x09, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x43, 0x6f, 0x69, 0x6e, 0x22, 0x07, 0x0a,
|
||||
0x05, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x32, 0x31, 0x0a, 0x04, 0x67, 0x69, 0x66, 0x74, 0x12, 0x29,
|
||||
0x0a, 0x0b, 0x63, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x47, 0x69, 0x66, 0x74, 0x12, 0x0f, 0x2e,
|
||||
0x70, 0x62, 0x2e, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x47, 0x69, 0x66, 0x74, 0x1a, 0x09,
|
||||
0x2e, 0x70, 0x62, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70,
|
||||
0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_gift_proto_rawDescOnce sync.Once
|
||||
file_gift_proto_rawDescData = file_gift_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_gift_proto_rawDescGZIP() []byte {
|
||||
file_gift_proto_rawDescOnce.Do(func() {
|
||||
file_gift_proto_rawDescData = protoimpl.X.CompressGZIP(file_gift_proto_rawDescData)
|
||||
})
|
||||
return file_gift_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_gift_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||
var file_gift_proto_goTypes = []interface{}{
|
||||
(*CollectGift)(nil), // 0: pb.CollectGift
|
||||
(*Empty)(nil), // 1: pb.Empty
|
||||
}
|
||||
var file_gift_proto_depIdxs = []int32{
|
||||
0, // 0: pb.gift.collectGift:input_type -> pb.CollectGift
|
||||
1, // 1: pb.gift.collectGift:output_type -> pb.Empty
|
||||
1, // [1:2] is the sub-list for method output_type
|
||||
0, // [0:1] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_gift_proto_init() }
|
||||
func file_gift_proto_init() {
|
||||
if File_gift_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_gift_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*CollectGift); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_gift_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Empty); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_gift_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 2,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_gift_proto_goTypes,
|
||||
DependencyIndexes: file_gift_proto_depIdxs,
|
||||
MessageInfos: file_gift_proto_msgTypes,
|
||||
}.Build()
|
||||
File_gift_proto = out.File
|
||||
file_gift_proto_rawDesc = nil
|
||||
file_gift_proto_goTypes = nil
|
||||
file_gift_proto_depIdxs = nil
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package pb;
|
||||
|
||||
option go_package = "./pb";
|
||||
|
||||
// req
|
||||
message CollectGift {
|
||||
string platform = 1;
|
||||
int64 giftId = 2;
|
||||
string giftName = 3;
|
||||
int64 totalCoin = 4;
|
||||
}
|
||||
|
||||
message Empty {}
|
||||
|
||||
service gift {
|
||||
// collectGift 收集礼物,仅作为收集使用
|
||||
rpc collectGift(CollectGift) returns (Empty);
|
||||
}
|
@ -1,107 +0,0 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.2.0
|
||||
// - protoc v3.19.4
|
||||
// source: gift.proto
|
||||
|
||||
package pb
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.32.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
// GiftClient is the client API for Gift service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type GiftClient interface {
|
||||
// collectGift 收集礼物,仅作为收集使用
|
||||
CollectGift(ctx context.Context, in *CollectGift, opts ...grpc.CallOption) (*Empty, error)
|
||||
}
|
||||
|
||||
type giftClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewGiftClient(cc grpc.ClientConnInterface) GiftClient {
|
||||
return &giftClient{cc}
|
||||
}
|
||||
|
||||
func (c *giftClient) CollectGift(ctx context.Context, in *CollectGift, opts ...grpc.CallOption) (*Empty, error) {
|
||||
out := new(Empty)
|
||||
err := c.cc.Invoke(ctx, "/pb.gift/collectGift", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// GiftServer is the server API for Gift service.
|
||||
// All implementations must embed UnimplementedGiftServer
|
||||
// for forward compatibility
|
||||
type GiftServer interface {
|
||||
// collectGift 收集礼物,仅作为收集使用
|
||||
CollectGift(context.Context, *CollectGift) (*Empty, error)
|
||||
mustEmbedUnimplementedGiftServer()
|
||||
}
|
||||
|
||||
// UnimplementedGiftServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedGiftServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedGiftServer) CollectGift(context.Context, *CollectGift) (*Empty, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method CollectGift not implemented")
|
||||
}
|
||||
func (UnimplementedGiftServer) mustEmbedUnimplementedGiftServer() {}
|
||||
|
||||
// UnsafeGiftServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to GiftServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeGiftServer interface {
|
||||
mustEmbedUnimplementedGiftServer()
|
||||
}
|
||||
|
||||
func RegisterGiftServer(s grpc.ServiceRegistrar, srv GiftServer) {
|
||||
s.RegisterService(&Gift_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _Gift_CollectGift_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CollectGift)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(GiftServer).CollectGift(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/pb.gift/collectGift",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(GiftServer).CollectGift(ctx, req.(*CollectGift))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// Gift_ServiceDesc is the grpc.ServiceDesc for Gift service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var Gift_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "pb.gift",
|
||||
HandlerType: (*GiftServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "collectGift",
|
||||
Handler: _Gift_CollectGift_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "gift.proto",
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
import "gorm.io/gorm"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
var ErrNotFound = gorm.ErrRecordNotFound
|
||||
|
@ -0,0 +1,45 @@
|
||||
package platform_user
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRetrieveBilibili(t *testing.T) {
|
||||
str := `{
|
||||
"error": "",
|
||||
"message": [
|
||||
{
|
||||
"id": 48,
|
||||
"ip": "120.42.46.226",
|
||||
"port": "6666",
|
||||
"scheme_type": 0,
|
||||
"content": "120.42.46.226:6666",
|
||||
"assess_times": 16,
|
||||
"success_times": 14,
|
||||
"avg_response_time": 2.063625,
|
||||
"continuous_failed_times": 0,
|
||||
"score": 3.7924709776481986,
|
||||
"insert_time": 1650852112,
|
||||
"update_time": 1650853064
|
||||
}
|
||||
]
|
||||
}`
|
||||
var respStruct struct {
|
||||
Error string `json:"error"`
|
||||
Message []MyProxy `json:"message"`
|
||||
}
|
||||
err := json.Unmarshal([]byte(str), &respStruct)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Printf("%+v\n", respStruct)
|
||||
|
||||
bilibili, err := RetrieveBilibili("2")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("%+v\n", bilibili)
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package danmaku
|
||||
|
||||
import pbMq "live-service/app/pb/mq"
|
||||
|
||||
type HandlerFunc func(roomId int64, cmd string, dm *pbMq.MqDanmaku)
|
||||
|
||||
type Manager struct {
|
||||
handlers map[string]HandlerFunc
|
||||
}
|
||||
|
||||
func NewManager() *Manager {
|
||||
return &Manager{
|
||||
handlers: make(map[string]HandlerFunc),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) Register(h HandlerFunc, cmd string, alias ...string) {
|
||||
if _, ok := m.handlers[cmd]; ok {
|
||||
return
|
||||
}
|
||||
m.handlers[cmd] = h
|
||||
// alias
|
||||
for _, s := range alias {
|
||||
if _, ok := m.handlers[cmd]; ok {
|
||||
continue
|
||||
}
|
||||
m.handlers[s] = h
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) Handle(roomId int64, cmd string, user *pbMq.MqDanmaku) {
|
||||
if h, ok := m.handlers[cmd]; ok {
|
||||
h(roomId, cmd, user)
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
package danmaku
|
||||
|
||||
import (
|
||||
"git.noahlan.cn/northlan/ntools-go/stringn/ac"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
CMD struct {
|
||||
IsCMD bool // 是否CMD
|
||||
Arr []string // 具体CMD []string
|
||||
}
|
||||
Parser struct {
|
||||
trie *ac.Trie
|
||||
allKeyArr []string
|
||||
keywordMap map[string]struct{}
|
||||
}
|
||||
ParserBuilder struct {
|
||||
}
|
||||
)
|
||||
|
||||
func NewCMDParser(keys []string) *Parser {
|
||||
p := &Parser{
|
||||
keywordMap: make(map[string]struct{}),
|
||||
allKeyArr: make([]string, len(keys)),
|
||||
}
|
||||
for _, keyword := range keys {
|
||||
p.keywordMap[keyword] = struct{}{}
|
||||
p.allKeyArr = append(p.allKeyArr, keyword)
|
||||
}
|
||||
p.trie = ac.NewTrieBuilder().AddStrings(p.allKeyArr).Build()
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Parser) ParseTest(content string) {
|
||||
p.trie.MatchString(content)
|
||||
}
|
||||
|
||||
func (p *Parser) Parse(content string) *CMD {
|
||||
// 移除多余空格,小写
|
||||
tmpContent := strings.ToLower(strings.TrimSpace(content))
|
||||
matches := p.trie.MatchString(tmpContent)
|
||||
|
||||
allKeyLen := 0
|
||||
matchedKeyMap := make(map[string]struct{})
|
||||
for _, match := range matches {
|
||||
tmp := p.allKeyArr[match.Pattern()]
|
||||
matchedKeyMap[tmp] = struct{}{}
|
||||
allKeyLen += len(tmp)
|
||||
}
|
||||
isCMD := len(tmpContent) <= allKeyLen
|
||||
|
||||
// 避免同类型指令重复
|
||||
arrMap := make(map[rune]struct{})
|
||||
var matchedCmdArr []string
|
||||
if isCMD {
|
||||
matchedCmdArr = make([]string, 0, len(matchedKeyMap))
|
||||
for s := range matchedKeyMap {
|
||||
sRune := []rune(s)
|
||||
if _, ok := arrMap[sRune[0]]; !ok {
|
||||
arrMap[sRune[0]] = struct{}{}
|
||||
matchedCmdArr = append(matchedCmdArr, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
return &CMD{
|
||||
IsCMD: len(tmpContent) <= allKeyLen,
|
||||
Arr: matchedCmdArr,
|
||||
}
|
||||
}
|
@ -1,103 +0,0 @@
|
||||
package danmaku
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.noahlan.cn/northlan/ntools-go/kafka"
|
||||
"github.com/Shopify/sarama"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/protobuf/proto"
|
||||
pbMq "live-service/app/pb/mq"
|
||||
"live-service/app/user_center/rpc/internal/server"
|
||||
"live-service/app/user_center/rpc/internal/svc"
|
||||
"live-service/app/user_center/rpc/pb"
|
||||
kfk "live-service/common/kafka"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type msgHandlerFunc func(data []byte, msgKey string)
|
||||
|
||||
type MsgToDBHandler struct {
|
||||
context context.Context
|
||||
ctx *svc.ServiceContext
|
||||
svr *server.UserCenterServer
|
||||
|
||||
msgHandle map[string]msgHandlerFunc
|
||||
|
||||
cmdParser *Parser
|
||||
commandManager *Manager
|
||||
|
||||
ConsumerGroup *kafka.ConsumerGroup
|
||||
|
||||
mutex sync.Mutex
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func (h *MsgToDBHandler) Init(context context.Context, ctx *svc.ServiceContext, svr *server.UserCenterServer) {
|
||||
h.context = context
|
||||
h.ctx = ctx
|
||||
h.svr = svr
|
||||
h.Logger = logx.WithContext(h.context)
|
||||
|
||||
cfg := ctx.Config.Danmaku.Kafka
|
||||
h.msgHandle = make(map[string]msgHandlerFunc)
|
||||
h.msgHandle["danmaku"] = h.handleDanmaku
|
||||
|
||||
h.cmdParser = NewCMDParser(ctx.Config.Danmaku.Command.Keys)
|
||||
//
|
||||
h.commandManager = NewManager()
|
||||
h.commandManager.Register(h.handleJoinGame, "j", "J", "加入", "加入游戏")
|
||||
|
||||
var err error
|
||||
h.ConsumerGroup, err = kafka.NewConsumerGroup(&kafka.ConsumerGroupConfig{
|
||||
KafkaVersion: sarama.V3_1_0_0,
|
||||
OffsetsInitial: sarama.OffsetNewest,
|
||||
IsReturnErr: false,
|
||||
UnMarshaler: kfk.ProtobufMarshaler,
|
||||
}, cfg.Addr, []string{cfg.Topic}, cfg.ConsumerGroupId)
|
||||
|
||||
if err != nil {
|
||||
h.Logger.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MsgToDBHandler) handleJoinGame(_ int64, _ string, danmaku *pbMq.MqDanmaku) {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
_, err := h.svr.RetrievePlatformUser(h.context, &pb.PlatformUserReq{
|
||||
Platform: danmaku.Platform,
|
||||
PUid: strconv.FormatInt(danmaku.Uid, 10),
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MsgToDBHandler) handleDanmaku(data []byte, msgKey string) {
|
||||
// msg proto
|
||||
var msgFromMq pbMq.MqDanmaku
|
||||
if err := proto.Unmarshal(data, &msgFromMq); err != nil {
|
||||
h.Logger.Error("unmarshal msg err", err)
|
||||
return
|
||||
}
|
||||
|
||||
cmdStruct := h.cmdParser.Parse(msgFromMq.Content)
|
||||
// 只处理命令
|
||||
if cmdStruct.IsCMD {
|
||||
for _, cmd := range cmdStruct.Arr {
|
||||
h.commandManager.Handle(msgFromMq.LiveRoomId, cmd, &msgFromMq)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (*MsgToDBHandler) Setup(_ sarama.ConsumerGroupSession) error { return nil }
|
||||
func (*MsgToDBHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
|
||||
func (h *MsgToDBHandler) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
|
||||
for msg := range claim.Messages() {
|
||||
if hFunc, ok := h.msgHandle[msg.Topic]; ok {
|
||||
hFunc(msg.Value, string(msg.Key))
|
||||
}
|
||||
sess.MarkMessage(msg, "")
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package msg_transfer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"live-service/app/user_center/rpc/internal/server"
|
||||
"live-service/app/user_center/rpc/internal/svc"
|
||||
"live-service/app/user_center/rpc/msg_transfer/danmaku"
|
||||
)
|
||||
|
||||
var (
|
||||
danmakuMsgToDb danmaku.MsgToDBHandler
|
||||
)
|
||||
|
||||
func Init(context context.Context, ctx *svc.ServiceContext, svr *server.UserCenterServer) {
|
||||
danmakuMsgToDb.Init(context, ctx, svr)
|
||||
}
|
||||
|
||||
func Run() {
|
||||
go danmakuMsgToDb.ConsumerGroup.RegisterHandlerAndConsumer(&danmakuMsgToDb)
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
package {{.pkg}}
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
import "gorm.io/gorm"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
var ErrNotFound = gorm.ErrRecordNotFound
|
||||
|
@ -1,12 +1,10 @@
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc"
|
||||
"strings"
|
||||
{{if .time}}"time"{{end}}
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
@ -1,13 +1,12 @@
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
{{if .time}}"time"{{end}}
|
||||
|
||||
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc"
|
||||
"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"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
@ -1,10 +1,8 @@
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error) {
|
||||
func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, data *{{.upperStartCamelObject}}) error {
|
||||
{{if .withCache}}{{.keys}}
|
||||
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 ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, {{.expressionValues}})
|
||||
}, {{.keyValues}}){{else}}query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
|
||||
ret,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}}
|
||||
return ret,err
|
||||
err := m.ExecCtx(ctx, func(conn *gorm.DB) *gorm.DB {
|
||||
return conn.Save(&data)
|
||||
}, {{.keyValues}}){{else}}err:=m.conn.WithContext(ctx).Save(&data).Error{{end}}
|
||||
return err
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error)
|
||||
Insert(ctx context.Context, data *{{.upperStartCamelObject}}) error
|
@ -1,7 +1,7 @@
|
||||
|
||||
func new{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c cache.CacheConf{{end}}) *default{{.upperStartCamelObject}}Model {
|
||||
func new{{.upperStartCamelObject}}Model(conn *gorm.DB{{if .withCache}}, c cache.CacheConf{{end}}) *default{{.upperStartCamelObject}}Model {
|
||||
return &default{{.upperStartCamelObject}}Model{
|
||||
{{if .withCache}}CachedConn: sqlc.NewConn(conn, c){{else}}conn:conn{{end}},
|
||||
table: {{.table}},
|
||||
{{if .withCache}}CachedConn: gormc.NewConn(conn, c){{else}}conn:conn{{end}},
|
||||
table: {{.table}},
|
||||
}
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
`db:"{{.field}}"`
|
||||
`gorm:"column:{{.field}}"`
|
@ -1,10 +1,8 @@
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, data *{{.upperStartCamelObject}}) error {
|
||||
{{if .withCache}}{{.keys}}
|
||||
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder)
|
||||
return conn.ExecCtx(ctx, query, {{.expressionValues}})
|
||||
}, {{.keyValues}}){{else}}query := fmt.Sprintf("update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder)
|
||||
_,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}}
|
||||
err := m.ExecCtx(ctx, func(conn *gorm.DB) *gorm.DB {
|
||||
return conn.Save(data)
|
||||
}, {{.keyValues}}){{else}}err:=m.conn.WithContext(ctx).Save(data).Error{{end}}
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue