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 (
|
||||
agentConfigFieldNames = builder.RawFieldNames(&AgentConfig{})
|
||||
agentConfigRows = strings.Join(agentConfigFieldNames, ",")
|
||||
agentConfigRowsExpectAutoSet = strings.Join(stringx.Remove(agentConfigFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
agentConfigRowsExpectAutoSet = strings.Join(stringx.Remove(agentConfigFieldNames, "`create_time`", "`update_time`"), ",")
|
||||
agentConfigRowsWithPlaceHolder = strings.Join(stringx.Remove(agentConfigFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
cacheYccAgentConfigIdPrefix = "cache:ycc:agentConfig:id:"
|
||||
@@ -33,7 +35,7 @@ var (
|
||||
type (
|
||||
agentConfigModel interface {
|
||||
Insert(ctx context.Context, session sqlx.Session, data *AgentConfig) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*AgentConfig, error)
|
||||
FindOne(ctx context.Context, id string) (*AgentConfig, error)
|
||||
FindOneByConfigKey(ctx context.Context, configKey string) (*AgentConfig, error)
|
||||
Update(ctx context.Context, session sqlx.Session, data *AgentConfig) (sql.Result, error)
|
||||
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *AgentConfig) error
|
||||
@@ -47,7 +49,7 @@ type (
|
||||
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*AgentConfig, int64, error)
|
||||
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*AgentConfig, error)
|
||||
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*AgentConfig, error)
|
||||
Delete(ctx context.Context, session sqlx.Session, id int64) error
|
||||
Delete(ctx context.Context, session sqlx.Session, id string) error
|
||||
}
|
||||
|
||||
defaultAgentConfigModel struct {
|
||||
@@ -56,7 +58,7 @@ type (
|
||||
}
|
||||
|
||||
AgentConfig struct {
|
||||
Id int64 `db:"id"` // 主键ID
|
||||
Id string `db:"id"`
|
||||
ConfigKey string `db:"config_key"` // 配置键
|
||||
ConfigValue string `db:"config_value"` // 配置值
|
||||
ConfigType string `db:"config_type"` // 配置类型:price=价格,bonus=等级加成,upgrade=升级费用,rebate=返佣,tax=税费
|
||||
@@ -78,18 +80,35 @@ func newAgentConfigModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultAgentConf
|
||||
|
||||
func (m *defaultAgentConfigModel) Insert(ctx context.Context, session sqlx.Session, data *AgentConfig) (sql.Result, error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
m.insertUUID(data)
|
||||
yccAgentConfigConfigKeyKey := fmt.Sprintf("%s%v", cacheYccAgentConfigConfigKeyPrefix, data.ConfigKey)
|
||||
yccAgentConfigIdKey := fmt.Sprintf("%s%v", cacheYccAgentConfigIdPrefix, 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, agentConfigRowsExpectAutoSet)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, agentConfigRowsExpectAutoSet)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.ConfigKey, data.ConfigValue, data.ConfigType, data.Description, data.DeleteTime, data.DelState, data.Version)
|
||||
return session.ExecCtx(ctx, query, data.Id, data.ConfigKey, data.ConfigValue, data.ConfigType, data.Description, data.DeleteTime, data.DelState, data.Version)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.ConfigKey, data.ConfigValue, data.ConfigType, data.Description, data.DeleteTime, data.DelState, data.Version)
|
||||
return conn.ExecCtx(ctx, query, data.Id, data.ConfigKey, data.ConfigValue, data.ConfigType, data.Description, data.DeleteTime, data.DelState, data.Version)
|
||||
}, yccAgentConfigConfigKeyKey, yccAgentConfigIdKey)
|
||||
}
|
||||
func (m *defaultAgentConfigModel) insertUUID(data *AgentConfig) {
|
||||
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 *defaultAgentConfigModel) FindOne(ctx context.Context, id int64) (*AgentConfig, error) {
|
||||
func (m *defaultAgentConfigModel) FindOne(ctx context.Context, id string) (*AgentConfig, error) {
|
||||
yccAgentConfigIdKey := fmt.Sprintf("%s%v", cacheYccAgentConfigIdPrefix, id)
|
||||
var resp AgentConfig
|
||||
err := m.QueryRowCtx(ctx, &resp, yccAgentConfigIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
@@ -379,7 +398,7 @@ func (m *defaultAgentConfigModel) Trans(ctx context.Context, fn func(ctx context
|
||||
func (m *defaultAgentConfigModel) SelectBuilder() squirrel.SelectBuilder {
|
||||
return squirrel.Select().From(m.table)
|
||||
}
|
||||
func (m *defaultAgentConfigModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
|
||||
func (m *defaultAgentConfigModel) 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