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 (
|
||||
agentLinkFieldNames = builder.RawFieldNames(&AgentLink{})
|
||||
agentLinkRows = strings.Join(agentLinkFieldNames, ",")
|
||||
agentLinkRowsExpectAutoSet = strings.Join(stringx.Remove(agentLinkFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
agentLinkRowsExpectAutoSet = strings.Join(stringx.Remove(agentLinkFieldNames, "`create_time`", "`update_time`"), ",")
|
||||
agentLinkRowsWithPlaceHolder = strings.Join(stringx.Remove(agentLinkFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
cacheYccAgentLinkIdPrefix = "cache:ycc:agentLink:id:"
|
||||
@@ -34,8 +36,8 @@ var (
|
||||
type (
|
||||
agentLinkModel interface {
|
||||
Insert(ctx context.Context, session sqlx.Session, data *AgentLink) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*AgentLink, error)
|
||||
FindOneByAgentIdProductIdSetPriceDelState(ctx context.Context, agentId int64, productId int64, setPrice float64, delState int64) (*AgentLink, error)
|
||||
FindOne(ctx context.Context, id string) (*AgentLink, error)
|
||||
FindOneByAgentIdProductIdSetPriceDelState(ctx context.Context, agentId string, productId string, setPrice float64, delState int64) (*AgentLink, error)
|
||||
FindOneByLinkIdentifier(ctx context.Context, linkIdentifier string) (*AgentLink, error)
|
||||
Update(ctx context.Context, session sqlx.Session, data *AgentLink) (sql.Result, error)
|
||||
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *AgentLink) error
|
||||
@@ -49,7 +51,7 @@ type (
|
||||
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*AgentLink, int64, error)
|
||||
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*AgentLink, error)
|
||||
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*AgentLink, error)
|
||||
Delete(ctx context.Context, session sqlx.Session, id int64) error
|
||||
Delete(ctx context.Context, session sqlx.Session, id string) error
|
||||
}
|
||||
|
||||
defaultAgentLinkModel struct {
|
||||
@@ -58,10 +60,10 @@ type (
|
||||
}
|
||||
|
||||
AgentLink struct {
|
||||
Id int64 `db:"id"` // 主键ID
|
||||
AgentId int64 `db:"agent_id"` // 代理ID
|
||||
UserId int64 `db:"user_id"` // 用户ID
|
||||
ProductId int64 `db:"product_id"` // 产品ID
|
||||
Id string `db:"id"`
|
||||
AgentId string `db:"agent_id"`
|
||||
UserId string `db:"user_id"`
|
||||
ProductId string `db:"product_id"`
|
||||
LinkIdentifier string `db:"link_identifier"` // 推广链接标识(加密)
|
||||
SetPrice float64 `db:"set_price"` // 代理设定价格
|
||||
ActualBasePrice float64 `db:"actual_base_price"` // 实际底价(基础底价+等级加成)
|
||||
@@ -82,19 +84,36 @@ func newAgentLinkModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultAgentLinkMo
|
||||
|
||||
func (m *defaultAgentLinkModel) Insert(ctx context.Context, session sqlx.Session, data *AgentLink) (sql.Result, error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
m.insertUUID(data)
|
||||
yccAgentLinkAgentIdProductIdSetPriceDelStateKey := fmt.Sprintf("%s%v:%v:%v:%v", cacheYccAgentLinkAgentIdProductIdSetPriceDelStatePrefix, data.AgentId, data.ProductId, data.SetPrice, data.DelState)
|
||||
yccAgentLinkIdKey := fmt.Sprintf("%s%v", cacheYccAgentLinkIdPrefix, data.Id)
|
||||
yccAgentLinkLinkIdentifierKey := fmt.Sprintf("%s%v", cacheYccAgentLinkLinkIdentifierPrefix, data.LinkIdentifier)
|
||||
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, agentLinkRowsExpectAutoSet)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, agentLinkRowsExpectAutoSet)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.AgentId, data.UserId, data.ProductId, data.LinkIdentifier, data.SetPrice, data.ActualBasePrice, data.DeleteTime, data.DelState, data.Version)
|
||||
return session.ExecCtx(ctx, query, data.Id, data.AgentId, data.UserId, data.ProductId, data.LinkIdentifier, data.SetPrice, data.ActualBasePrice, data.DeleteTime, data.DelState, data.Version)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.AgentId, data.UserId, data.ProductId, data.LinkIdentifier, data.SetPrice, data.ActualBasePrice, data.DeleteTime, data.DelState, data.Version)
|
||||
return conn.ExecCtx(ctx, query, data.Id, data.AgentId, data.UserId, data.ProductId, data.LinkIdentifier, data.SetPrice, data.ActualBasePrice, data.DeleteTime, data.DelState, data.Version)
|
||||
}, yccAgentLinkAgentIdProductIdSetPriceDelStateKey, yccAgentLinkIdKey, yccAgentLinkLinkIdentifierKey)
|
||||
}
|
||||
func (m *defaultAgentLinkModel) insertUUID(data *AgentLink) {
|
||||
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 *defaultAgentLinkModel) FindOne(ctx context.Context, id int64) (*AgentLink, error) {
|
||||
func (m *defaultAgentLinkModel) FindOne(ctx context.Context, id string) (*AgentLink, error) {
|
||||
yccAgentLinkIdKey := fmt.Sprintf("%s%v", cacheYccAgentLinkIdPrefix, id)
|
||||
var resp AgentLink
|
||||
err := m.QueryRowCtx(ctx, &resp, yccAgentLinkIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
@@ -111,7 +130,7 @@ func (m *defaultAgentLinkModel) FindOne(ctx context.Context, id int64) (*AgentLi
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultAgentLinkModel) FindOneByAgentIdProductIdSetPriceDelState(ctx context.Context, agentId int64, productId int64, setPrice float64, delState int64) (*AgentLink, error) {
|
||||
func (m *defaultAgentLinkModel) FindOneByAgentIdProductIdSetPriceDelState(ctx context.Context, agentId string, productId string, setPrice float64, delState int64) (*AgentLink, error) {
|
||||
yccAgentLinkAgentIdProductIdSetPriceDelStateKey := fmt.Sprintf("%s%v:%v:%v:%v", cacheYccAgentLinkAgentIdProductIdSetPriceDelStatePrefix, agentId, productId, setPrice, delState)
|
||||
var resp AgentLink
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, yccAgentLinkAgentIdProductIdSetPriceDelStateKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
@@ -406,7 +425,7 @@ func (m *defaultAgentLinkModel) Trans(ctx context.Context, fn func(ctx context.C
|
||||
func (m *defaultAgentLinkModel) SelectBuilder() squirrel.SelectBuilder {
|
||||
return squirrel.Select().From(m.table)
|
||||
}
|
||||
func (m *defaultAgentLinkModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
|
||||
func (m *defaultAgentLinkModel) 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