This commit is contained in:
2025-12-09 18:55:28 +08:00
parent 8d00d67540
commit c23ab8338b
209 changed files with 5445 additions and 3963 deletions

View File

@@ -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 (
agentInviteCodeFieldNames = builder.RawFieldNames(&AgentInviteCode{})
agentInviteCodeRows = strings.Join(agentInviteCodeFieldNames, ",")
agentInviteCodeRowsExpectAutoSet = strings.Join(stringx.Remove(agentInviteCodeFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
agentInviteCodeRowsExpectAutoSet = strings.Join(stringx.Remove(agentInviteCodeFieldNames, "`create_time`", "`update_time`"), ",")
agentInviteCodeRowsWithPlaceHolder = strings.Join(stringx.Remove(agentInviteCodeFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
cacheYccAgentInviteCodeIdPrefix = "cache:ycc:agentInviteCode:id:"
@@ -33,7 +35,7 @@ var (
type (
agentInviteCodeModel interface {
Insert(ctx context.Context, session sqlx.Session, data *AgentInviteCode) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*AgentInviteCode, error)
FindOne(ctx context.Context, id string) (*AgentInviteCode, error)
FindOneByCode(ctx context.Context, code string) (*AgentInviteCode, error)
Update(ctx context.Context, session sqlx.Session, data *AgentInviteCode) (sql.Result, error)
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *AgentInviteCode) error
@@ -47,7 +49,7 @@ type (
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*AgentInviteCode, int64, error)
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*AgentInviteCode, error)
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*AgentInviteCode, error)
Delete(ctx context.Context, session sqlx.Session, id int64) error
Delete(ctx context.Context, session sqlx.Session, id string) error
}
defaultAgentInviteCodeModel struct {
@@ -56,21 +58,21 @@ type (
}
AgentInviteCode struct {
Id int64 `db:"id"` // 主键ID
Code string `db:"code"` // 邀请码(唯一)
AgentId sql.NullInt64 `db:"agent_id"` // 发放代理IDNULL表示平台发放的钻石邀请码
TargetLevel int64 `db:"target_level"` // 目标等级1=普通2=黄金3=钻石
Status int64 `db:"status"` // 状态0=未使用1=已使用2=已失效(所有邀请码只能使用一次,使用后立即失效)
UsedUserId sql.NullInt64 `db:"used_user_id"` // 使用用户ID
UsedAgentId sql.NullInt64 `db:"used_agent_id"` // 使用代理ID
UsedTime sql.NullTime `db:"used_time"` // 使用时间
ExpireTime sql.NullTime `db:"expire_time"` // 过期时间(可选)
Remark sql.NullString `db:"remark"` // 备注
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"`
Code string `db:"code"` // 邀请码(唯一)
AgentId sql.NullString `db:"agent_id"`
TargetLevel int64 `db:"target_level"` // 目标等级1=普通2=黄金3=钻石
Status int64 `db:"status"` // 状态0=未使用1=已使用2=已失效(所有邀请码只能使用一次,使用后立即失效)
UsedUserId sql.NullString `db:"used_user_id"`
UsedAgentId sql.NullString `db:"used_agent_id"`
UsedTime sql.NullTime `db:"used_time"` // 使用时间
ExpireTime sql.NullTime `db:"expire_time"` // 过期时间(可选)
Remark sql.NullString `db:"remark"` // 备注
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"` // 版本号(乐观锁)
}
)
@@ -83,18 +85,35 @@ func newAgentInviteCodeModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultAgent
func (m *defaultAgentInviteCodeModel) Insert(ctx context.Context, session sqlx.Session, data *AgentInviteCode) (sql.Result, error) {
data.DelState = globalkey.DelStateNo
m.insertUUID(data)
yccAgentInviteCodeCodeKey := fmt.Sprintf("%s%v", cacheYccAgentInviteCodeCodePrefix, data.Code)
yccAgentInviteCodeIdKey := fmt.Sprintf("%s%v", cacheYccAgentInviteCodeIdPrefix, 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, agentInviteCodeRowsExpectAutoSet)
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, agentInviteCodeRowsExpectAutoSet)
if session != nil {
return session.ExecCtx(ctx, query, data.Code, data.AgentId, data.TargetLevel, data.Status, data.UsedUserId, data.UsedAgentId, data.UsedTime, data.ExpireTime, data.Remark, data.DeleteTime, data.DelState, data.Version)
return session.ExecCtx(ctx, query, data.Id, data.Code, data.AgentId, data.TargetLevel, data.Status, data.UsedUserId, data.UsedAgentId, data.UsedTime, data.ExpireTime, data.Remark, data.DeleteTime, data.DelState, data.Version)
}
return conn.ExecCtx(ctx, query, data.Code, data.AgentId, data.TargetLevel, data.Status, data.UsedUserId, data.UsedAgentId, data.UsedTime, data.ExpireTime, data.Remark, data.DeleteTime, data.DelState, data.Version)
return conn.ExecCtx(ctx, query, data.Id, data.Code, data.AgentId, data.TargetLevel, data.Status, data.UsedUserId, data.UsedAgentId, data.UsedTime, data.ExpireTime, data.Remark, data.DeleteTime, data.DelState, data.Version)
}, yccAgentInviteCodeCodeKey, yccAgentInviteCodeIdKey)
}
func (m *defaultAgentInviteCodeModel) insertUUID(data *AgentInviteCode) {
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 *defaultAgentInviteCodeModel) FindOne(ctx context.Context, id int64) (*AgentInviteCode, error) {
func (m *defaultAgentInviteCodeModel) FindOne(ctx context.Context, id string) (*AgentInviteCode, error) {
yccAgentInviteCodeIdKey := fmt.Sprintf("%s%v", cacheYccAgentInviteCodeIdPrefix, id)
var resp AgentInviteCode
err := m.QueryRowCtx(ctx, &resp, yccAgentInviteCodeIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
@@ -384,7 +403,7 @@ func (m *defaultAgentInviteCodeModel) Trans(ctx context.Context, fn func(ctx con
func (m *defaultAgentInviteCodeModel) SelectBuilder() squirrel.SelectBuilder {
return squirrel.Select().From(m.table)
}
func (m *defaultAgentInviteCodeModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
func (m *defaultAgentInviteCodeModel) Delete(ctx context.Context, session sqlx.Session, id string) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err