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 (
agentRelationFieldNames = builder.RawFieldNames(&AgentRelation{})
agentRelationRows = strings.Join(agentRelationFieldNames, ",")
agentRelationRowsExpectAutoSet = strings.Join(stringx.Remove(agentRelationFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
agentRelationRowsExpectAutoSet = strings.Join(stringx.Remove(agentRelationFieldNames, "`create_time`", "`update_time`"), ",")
agentRelationRowsWithPlaceHolder = strings.Join(stringx.Remove(agentRelationFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
cacheYccAgentRelationIdPrefix = "cache:ycc:agentRelation:id:"
@@ -33,8 +35,8 @@ var (
type (
agentRelationModel interface {
Insert(ctx context.Context, session sqlx.Session, data *AgentRelation) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*AgentRelation, error)
FindOneByParentIdChildIdRelationType(ctx context.Context, parentId int64, childId int64, relationType int64) (*AgentRelation, error)
FindOne(ctx context.Context, id string) (*AgentRelation, error)
FindOneByParentIdChildIdRelationType(ctx context.Context, parentId string, childId string, relationType int64) (*AgentRelation, error)
Update(ctx context.Context, session sqlx.Session, data *AgentRelation) (sql.Result, error)
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *AgentRelation) error
Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
@@ -47,7 +49,7 @@ type (
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*AgentRelation, int64, error)
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*AgentRelation, error)
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*AgentRelation, error)
Delete(ctx context.Context, session sqlx.Session, id int64) error
Delete(ctx context.Context, session sqlx.Session, id string) error
}
defaultAgentRelationModel struct {
@@ -56,9 +58,9 @@ type (
}
AgentRelation struct {
Id int64 `db:"id"` // 主键ID
ParentId int64 `db:"parent_id"` // 上级代理ID
ChildId int64 `db:"child_id"` // 下级代理ID
Id string `db:"id"`
ParentId string `db:"parent_id"`
ChildId string `db:"child_id"`
RelationType int64 `db:"relation_type"` // 关系类型1=直接关系2=已脱离(历史记录)
DetachReason sql.NullString `db:"detach_reason"` // 脱离原因upgrade=升级脱离
DetachTime sql.NullTime `db:"detach_time"` // 脱离时间
@@ -79,18 +81,35 @@ func newAgentRelationModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultAgentRe
func (m *defaultAgentRelationModel) Insert(ctx context.Context, session sqlx.Session, data *AgentRelation) (sql.Result, error) {
data.DelState = globalkey.DelStateNo
m.insertUUID(data)
yccAgentRelationIdKey := fmt.Sprintf("%s%v", cacheYccAgentRelationIdPrefix, data.Id)
yccAgentRelationParentIdChildIdRelationTypeKey := fmt.Sprintf("%s%v:%v:%v", cacheYccAgentRelationParentIdChildIdRelationTypePrefix, data.ParentId, data.ChildId, data.RelationType)
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, agentRelationRowsExpectAutoSet)
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, agentRelationRowsExpectAutoSet)
if session != nil {
return session.ExecCtx(ctx, query, data.ParentId, data.ChildId, data.RelationType, data.DetachReason, data.DetachTime, data.DeleteTime, data.DelState, data.Version)
return session.ExecCtx(ctx, query, data.Id, data.ParentId, data.ChildId, data.RelationType, data.DetachReason, data.DetachTime, data.DeleteTime, data.DelState, data.Version)
}
return conn.ExecCtx(ctx, query, data.ParentId, data.ChildId, data.RelationType, data.DetachReason, data.DetachTime, data.DeleteTime, data.DelState, data.Version)
return conn.ExecCtx(ctx, query, data.Id, data.ParentId, data.ChildId, data.RelationType, data.DetachReason, data.DetachTime, data.DeleteTime, data.DelState, data.Version)
}, yccAgentRelationIdKey, yccAgentRelationParentIdChildIdRelationTypeKey)
}
func (m *defaultAgentRelationModel) insertUUID(data *AgentRelation) {
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 *defaultAgentRelationModel) FindOne(ctx context.Context, id int64) (*AgentRelation, error) {
func (m *defaultAgentRelationModel) FindOne(ctx context.Context, id string) (*AgentRelation, error) {
yccAgentRelationIdKey := fmt.Sprintf("%s%v", cacheYccAgentRelationIdPrefix, id)
var resp AgentRelation
err := m.QueryRowCtx(ctx, &resp, yccAgentRelationIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
@@ -107,7 +126,7 @@ func (m *defaultAgentRelationModel) FindOne(ctx context.Context, id int64) (*Age
}
}
func (m *defaultAgentRelationModel) FindOneByParentIdChildIdRelationType(ctx context.Context, parentId int64, childId int64, relationType int64) (*AgentRelation, error) {
func (m *defaultAgentRelationModel) FindOneByParentIdChildIdRelationType(ctx context.Context, parentId string, childId string, relationType int64) (*AgentRelation, error) {
yccAgentRelationParentIdChildIdRelationTypeKey := fmt.Sprintf("%s%v:%v:%v", cacheYccAgentRelationParentIdChildIdRelationTypePrefix, parentId, childId, relationType)
var resp AgentRelation
err := m.QueryRowIndexCtx(ctx, &resp, yccAgentRelationParentIdChildIdRelationTypeKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
@@ -380,7 +399,7 @@ func (m *defaultAgentRelationModel) Trans(ctx context.Context, fn func(ctx conte
func (m *defaultAgentRelationModel) SelectBuilder() squirrel.SelectBuilder {
return squirrel.Select().From(m.table)
}
func (m *defaultAgentRelationModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
func (m *defaultAgentRelationModel) Delete(ctx context.Context, session sqlx.Session, id string) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err