v1.1
This commit is contained in:
@@ -8,9 +8,11 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
@@ -23,7 +25,7 @@ import (
|
||||
var (
|
||||
agentInviteCodeUsageFieldNames = builder.RawFieldNames(&AgentInviteCodeUsage{})
|
||||
agentInviteCodeUsageRows = strings.Join(agentInviteCodeUsageFieldNames, ",")
|
||||
agentInviteCodeUsageRowsExpectAutoSet = strings.Join(stringx.Remove(agentInviteCodeUsageFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
agentInviteCodeUsageRowsExpectAutoSet = strings.Join(stringx.Remove(agentInviteCodeUsageFieldNames, "`create_time`", "`update_time`"), ",")
|
||||
agentInviteCodeUsageRowsWithPlaceHolder = strings.Join(stringx.Remove(agentInviteCodeUsageFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
cacheYccAgentInviteCodeUsageIdPrefix = "cache:ycc:agentInviteCodeUsage:id:"
|
||||
@@ -32,7 +34,7 @@ var (
|
||||
type (
|
||||
agentInviteCodeUsageModel interface {
|
||||
Insert(ctx context.Context, session sqlx.Session, data *AgentInviteCodeUsage) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*AgentInviteCodeUsage, error)
|
||||
FindOne(ctx context.Context, id string) (*AgentInviteCodeUsage, error)
|
||||
Update(ctx context.Context, session sqlx.Session, data *AgentInviteCodeUsage) (sql.Result, error)
|
||||
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *AgentInviteCodeUsage) error
|
||||
Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
|
||||
@@ -45,7 +47,7 @@ type (
|
||||
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*AgentInviteCodeUsage, int64, error)
|
||||
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*AgentInviteCodeUsage, error)
|
||||
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*AgentInviteCodeUsage, error)
|
||||
Delete(ctx context.Context, session sqlx.Session, id int64) error
|
||||
Delete(ctx context.Context, session sqlx.Session, id string) error
|
||||
}
|
||||
|
||||
defaultAgentInviteCodeUsageModel struct {
|
||||
@@ -54,18 +56,18 @@ type (
|
||||
}
|
||||
|
||||
AgentInviteCodeUsage struct {
|
||||
Id int64 `db:"id"` // 主键ID
|
||||
InviteCodeId int64 `db:"invite_code_id"` // 邀请码ID(关联agent_invite_code表)
|
||||
Code string `db:"code"` // 邀请码(冗余字段,便于查询)
|
||||
UserId int64 `db:"user_id"` // 使用用户ID
|
||||
AgentId int64 `db:"agent_id"` // 成为的代理ID
|
||||
AgentLevel int64 `db:"agent_level"` // 代理等级:1=普通,2=黄金,3=钻石
|
||||
UsedTime time.Time `db:"used_time"` // 使用时间
|
||||
CreateTime time.Time `db:"create_time"` // 创建时间
|
||||
UpdateTime time.Time `db:"update_time"` // 更新时间
|
||||
DeleteTime sql.NullTime `db:"delete_time"` // 删除时间
|
||||
DelState int64 `db:"del_state"` // 删除状态:0=未删除,1=已删除
|
||||
Version int64 `db:"version"` // 版本号(乐观锁)
|
||||
Id string `db:"id"`
|
||||
InviteCodeId string `db:"invite_code_id"`
|
||||
Code string `db:"code"` // 邀请码(冗余字段,便于查询)
|
||||
UserId string `db:"user_id"`
|
||||
AgentId string `db:"agent_id"`
|
||||
AgentLevel int64 `db:"agent_level"` // 代理等级:1=普通,2=黄金,3=钻石
|
||||
UsedTime time.Time `db:"used_time"` // 使用时间
|
||||
CreateTime time.Time `db:"create_time"` // 创建时间
|
||||
UpdateTime time.Time `db:"update_time"` // 更新时间
|
||||
DeleteTime sql.NullTime `db:"delete_time"` // 删除时间
|
||||
DelState int64 `db:"del_state"` // 删除状态:0=未删除,1=已删除
|
||||
Version int64 `db:"version"` // 版本号(乐观锁)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -78,17 +80,34 @@ func newAgentInviteCodeUsageModel(conn sqlx.SqlConn, c cache.CacheConf) *default
|
||||
|
||||
func (m *defaultAgentInviteCodeUsageModel) Insert(ctx context.Context, session sqlx.Session, data *AgentInviteCodeUsage) (sql.Result, error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
m.insertUUID(data)
|
||||
yccAgentInviteCodeUsageIdKey := fmt.Sprintf("%s%v", cacheYccAgentInviteCodeUsageIdPrefix, data.Id)
|
||||
return 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, agentInviteCodeUsageRowsExpectAutoSet)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, agentInviteCodeUsageRowsExpectAutoSet)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.InviteCodeId, data.Code, data.UserId, data.AgentId, data.AgentLevel, data.UsedTime, data.DeleteTime, data.DelState, data.Version)
|
||||
return session.ExecCtx(ctx, query, data.Id, data.InviteCodeId, data.Code, data.UserId, data.AgentId, data.AgentLevel, data.UsedTime, data.DeleteTime, data.DelState, data.Version)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.InviteCodeId, data.Code, data.UserId, data.AgentId, data.AgentLevel, data.UsedTime, data.DeleteTime, data.DelState, data.Version)
|
||||
return conn.ExecCtx(ctx, query, data.Id, data.InviteCodeId, data.Code, data.UserId, data.AgentId, data.AgentLevel, data.UsedTime, data.DeleteTime, data.DelState, data.Version)
|
||||
}, yccAgentInviteCodeUsageIdKey)
|
||||
}
|
||||
func (m *defaultAgentInviteCodeUsageModel) insertUUID(data *AgentInviteCodeUsage) {
|
||||
t := reflect.TypeOf(data).Elem()
|
||||
v := reflect.ValueOf(data).Elem()
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
sf := t.Field(i)
|
||||
if sf.Tag.Get("db") == "id" {
|
||||
f := v.Field(i)
|
||||
if f.IsValid() && f.CanSet() && f.Kind() == reflect.String {
|
||||
if f.String() == "" {
|
||||
f.SetString(uuid.NewString())
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultAgentInviteCodeUsageModel) FindOne(ctx context.Context, id int64) (*AgentInviteCodeUsage, error) {
|
||||
func (m *defaultAgentInviteCodeUsageModel) FindOne(ctx context.Context, id string) (*AgentInviteCodeUsage, error) {
|
||||
yccAgentInviteCodeUsageIdKey := fmt.Sprintf("%s%v", cacheYccAgentInviteCodeUsageIdPrefix, id)
|
||||
var resp AgentInviteCodeUsage
|
||||
err := m.QueryRowCtx(ctx, &resp, yccAgentInviteCodeUsageIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
@@ -348,7 +367,7 @@ func (m *defaultAgentInviteCodeUsageModel) Trans(ctx context.Context, fn func(ct
|
||||
func (m *defaultAgentInviteCodeUsageModel) SelectBuilder() squirrel.SelectBuilder {
|
||||
return squirrel.Select().From(m.table)
|
||||
}
|
||||
func (m *defaultAgentInviteCodeUsageModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
|
||||
func (m *defaultAgentInviteCodeUsageModel) Delete(ctx context.Context, session sqlx.Session, id string) error {
|
||||
yccAgentInviteCodeUsageIdKey := fmt.Sprintf("%s%v", cacheYccAgentInviteCodeUsageIdPrefix, 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)
|
||||
|
||||
Reference in New Issue
Block a user