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 (
|
||||
agentShortLinkFieldNames = builder.RawFieldNames(&AgentShortLink{})
|
||||
agentShortLinkRows = strings.Join(agentShortLinkFieldNames, ",")
|
||||
agentShortLinkRowsExpectAutoSet = strings.Join(stringx.Remove(agentShortLinkFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
agentShortLinkRowsExpectAutoSet = strings.Join(stringx.Remove(agentShortLinkFieldNames, "`create_time`", "`update_time`"), ",")
|
||||
agentShortLinkRowsWithPlaceHolder = strings.Join(stringx.Remove(agentShortLinkFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
cacheYccAgentShortLinkIdPrefix = "cache:ycc:agentShortLink:id:"
|
||||
@@ -35,9 +37,9 @@ var (
|
||||
type (
|
||||
agentShortLinkModel interface {
|
||||
Insert(ctx context.Context, session sqlx.Session, data *AgentShortLink) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*AgentShortLink, error)
|
||||
FindOneByInviteCodeIdTypeDelState(ctx context.Context, inviteCodeId sql.NullInt64, tp int64, delState int64) (*AgentShortLink, error)
|
||||
FindOneByLinkIdTypeDelState(ctx context.Context, linkId sql.NullInt64, tp int64, delState int64) (*AgentShortLink, error)
|
||||
FindOne(ctx context.Context, id string) (*AgentShortLink, error)
|
||||
FindOneByInviteCodeIdTypeDelState(ctx context.Context, inviteCodeId sql.NullString, tp int64, delState int64) (*AgentShortLink, error)
|
||||
FindOneByLinkIdTypeDelState(ctx context.Context, linkId sql.NullString, tp int64, delState int64) (*AgentShortLink, error)
|
||||
FindOneByShortCodeDelState(ctx context.Context, shortCode string, delState int64) (*AgentShortLink, error)
|
||||
Update(ctx context.Context, session sqlx.Session, data *AgentShortLink) (sql.Result, error)
|
||||
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *AgentShortLink) error
|
||||
@@ -51,7 +53,7 @@ type (
|
||||
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*AgentShortLink, int64, error)
|
||||
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*AgentShortLink, error)
|
||||
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*AgentShortLink, error)
|
||||
Delete(ctx context.Context, session sqlx.Session, id int64) error
|
||||
Delete(ctx context.Context, session sqlx.Session, id string) error
|
||||
}
|
||||
|
||||
defaultAgentShortLinkModel struct {
|
||||
@@ -60,10 +62,10 @@ type (
|
||||
}
|
||||
|
||||
AgentShortLink struct {
|
||||
Id int64 `db:"id"` // 主键ID
|
||||
Type int64 `db:"type"` // 类型:1=推广报告(promotion),2=邀请好友(invite)
|
||||
LinkId sql.NullInt64 `db:"link_id"` // 推广链接ID(关联agent_link表,仅推广报告类型使用)
|
||||
InviteCodeId sql.NullInt64 `db:"invite_code_id"` // 邀请码ID(关联agent_invite_code表,仅邀请好友类型使用)
|
||||
Id string `db:"id"`
|
||||
Type int64 `db:"type"` // 类型:1=推广报告(promotion),2=邀请好友(invite)
|
||||
LinkId sql.NullString `db:"link_id"`
|
||||
InviteCodeId sql.NullString `db:"invite_code_id"`
|
||||
LinkIdentifier sql.NullString `db:"link_identifier"` // 推广链接标识(加密,仅推广报告类型使用)
|
||||
InviteCode sql.NullString `db:"invite_code"` // 邀请码(仅邀请好友类型使用)
|
||||
ShortCode string `db:"short_code"` // 短链标识(6位随机字符串)
|
||||
@@ -86,20 +88,37 @@ func newAgentShortLinkModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultAgentS
|
||||
|
||||
func (m *defaultAgentShortLinkModel) Insert(ctx context.Context, session sqlx.Session, data *AgentShortLink) (sql.Result, error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
m.insertUUID(data)
|
||||
yccAgentShortLinkIdKey := fmt.Sprintf("%s%v", cacheYccAgentShortLinkIdPrefix, data.Id)
|
||||
yccAgentShortLinkInviteCodeIdTypeDelStateKey := fmt.Sprintf("%s%v:%v:%v", cacheYccAgentShortLinkInviteCodeIdTypeDelStatePrefix, data.InviteCodeId, data.Type, data.DelState)
|
||||
yccAgentShortLinkLinkIdTypeDelStateKey := fmt.Sprintf("%s%v:%v:%v", cacheYccAgentShortLinkLinkIdTypeDelStatePrefix, data.LinkId, data.Type, data.DelState)
|
||||
yccAgentShortLinkShortCodeDelStateKey := fmt.Sprintf("%s%v:%v", cacheYccAgentShortLinkShortCodeDelStatePrefix, data.ShortCode, data.DelState)
|
||||
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, agentShortLinkRowsExpectAutoSet)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, agentShortLinkRowsExpectAutoSet)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.Type, data.LinkId, data.InviteCodeId, data.LinkIdentifier, data.InviteCode, data.ShortCode, data.TargetPath, data.PromotionDomain, data.DeleteTime, data.DelState, data.Version)
|
||||
return session.ExecCtx(ctx, query, data.Id, data.Type, data.LinkId, data.InviteCodeId, data.LinkIdentifier, data.InviteCode, data.ShortCode, data.TargetPath, data.PromotionDomain, data.DeleteTime, data.DelState, data.Version)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.Type, data.LinkId, data.InviteCodeId, data.LinkIdentifier, data.InviteCode, data.ShortCode, data.TargetPath, data.PromotionDomain, data.DeleteTime, data.DelState, data.Version)
|
||||
return conn.ExecCtx(ctx, query, data.Id, data.Type, data.LinkId, data.InviteCodeId, data.LinkIdentifier, data.InviteCode, data.ShortCode, data.TargetPath, data.PromotionDomain, data.DeleteTime, data.DelState, data.Version)
|
||||
}, yccAgentShortLinkIdKey, yccAgentShortLinkInviteCodeIdTypeDelStateKey, yccAgentShortLinkLinkIdTypeDelStateKey, yccAgentShortLinkShortCodeDelStateKey)
|
||||
}
|
||||
func (m *defaultAgentShortLinkModel) insertUUID(data *AgentShortLink) {
|
||||
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 *defaultAgentShortLinkModel) FindOne(ctx context.Context, id int64) (*AgentShortLink, error) {
|
||||
func (m *defaultAgentShortLinkModel) FindOne(ctx context.Context, id string) (*AgentShortLink, error) {
|
||||
yccAgentShortLinkIdKey := fmt.Sprintf("%s%v", cacheYccAgentShortLinkIdPrefix, id)
|
||||
var resp AgentShortLink
|
||||
err := m.QueryRowCtx(ctx, &resp, yccAgentShortLinkIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
@@ -116,7 +135,7 @@ func (m *defaultAgentShortLinkModel) FindOne(ctx context.Context, id int64) (*Ag
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultAgentShortLinkModel) FindOneByInviteCodeIdTypeDelState(ctx context.Context, inviteCodeId sql.NullInt64, tp int64, delState int64) (*AgentShortLink, error) {
|
||||
func (m *defaultAgentShortLinkModel) FindOneByInviteCodeIdTypeDelState(ctx context.Context, inviteCodeId sql.NullString, tp int64, delState int64) (*AgentShortLink, error) {
|
||||
yccAgentShortLinkInviteCodeIdTypeDelStateKey := fmt.Sprintf("%s%v:%v:%v", cacheYccAgentShortLinkInviteCodeIdTypeDelStatePrefix, inviteCodeId, tp, delState)
|
||||
var resp AgentShortLink
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, yccAgentShortLinkInviteCodeIdTypeDelStateKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
@@ -136,7 +155,7 @@ func (m *defaultAgentShortLinkModel) FindOneByInviteCodeIdTypeDelState(ctx conte
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultAgentShortLinkModel) FindOneByLinkIdTypeDelState(ctx context.Context, linkId sql.NullInt64, tp int64, delState int64) (*AgentShortLink, error) {
|
||||
func (m *defaultAgentShortLinkModel) FindOneByLinkIdTypeDelState(ctx context.Context, linkId sql.NullString, tp int64, delState int64) (*AgentShortLink, error) {
|
||||
yccAgentShortLinkLinkIdTypeDelStateKey := fmt.Sprintf("%s%v:%v:%v", cacheYccAgentShortLinkLinkIdTypeDelStatePrefix, linkId, tp, delState)
|
||||
var resp AgentShortLink
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, yccAgentShortLinkLinkIdTypeDelStateKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
@@ -433,7 +452,7 @@ func (m *defaultAgentShortLinkModel) Trans(ctx context.Context, fn func(ctx cont
|
||||
func (m *defaultAgentShortLinkModel) SelectBuilder() squirrel.SelectBuilder {
|
||||
return squirrel.Select().From(m.table)
|
||||
}
|
||||
func (m *defaultAgentShortLinkModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
|
||||
func (m *defaultAgentShortLinkModel) Delete(ctx context.Context, session sqlx.Session, id string) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user