v1.1
This commit is contained in:
@@ -8,36 +8,37 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"ycc-server/common/globalkey"
|
||||
|
||||
"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"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"ycc-server/common/globalkey"
|
||||
)
|
||||
|
||||
var (
|
||||
exampleFieldNames = builder.RawFieldNames(&Example{})
|
||||
exampleRows = strings.Join(exampleFieldNames, ",")
|
||||
exampleRowsExpectAutoSet = strings.Join(stringx.Remove(exampleFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
exampleRowsExpectAutoSet = strings.Join(stringx.Remove(exampleFieldNames, "`create_time`", "`update_time`"), ",")
|
||||
exampleRowsWithPlaceHolder = strings.Join(stringx.Remove(exampleFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
cacheHmExampleIdPrefix = "cache:ycc:example:id:"
|
||||
cacheHmExampleApiIdPrefix = "cache:ycc:example:apiId:"
|
||||
cacheHmExampleFeatureIdPrefix = "cache:ycc:example:featureId:"
|
||||
cacheYccExampleIdPrefix = "cache:ycc:example:id:"
|
||||
cacheYccExampleApiIdPrefix = "cache:ycc:example:apiId:"
|
||||
cacheYccExampleFeatureIdPrefix = "cache:ycc:example:featureId:"
|
||||
)
|
||||
|
||||
type (
|
||||
exampleModel interface {
|
||||
Insert(ctx context.Context, session sqlx.Session, data *Example) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*Example, error)
|
||||
FindOne(ctx context.Context, id string) (*Example, error)
|
||||
FindOneByApiId(ctx context.Context, apiId string) (*Example, error)
|
||||
FindOneByFeatureId(ctx context.Context, featureId int64) (*Example, error)
|
||||
FindOneByFeatureId(ctx context.Context, featureId string) (*Example, error)
|
||||
Update(ctx context.Context, session sqlx.Session, data *Example) (sql.Result, error)
|
||||
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *Example) error
|
||||
Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
|
||||
@@ -50,7 +51,7 @@ type (
|
||||
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*Example, int64, error)
|
||||
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*Example, error)
|
||||
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*Example, error)
|
||||
Delete(ctx context.Context, session sqlx.Session, id int64) error
|
||||
Delete(ctx context.Context, session sqlx.Session, id string) error
|
||||
}
|
||||
|
||||
defaultExampleModel struct {
|
||||
@@ -59,15 +60,17 @@ type (
|
||||
}
|
||||
|
||||
Example struct {
|
||||
Id int64 `db:"id"` // 主键ID
|
||||
CreateTime time.Time `db:"create_time"` // 创建时间
|
||||
UpdateTime time.Time `db:"update_time"` // 更新时间
|
||||
DeleteTime sql.NullTime `db:"delete_time"` // 删除时间
|
||||
DelState int64 `db:"del_state"` // 删除状态
|
||||
Version int64 `db:"version"` // 版本号
|
||||
ApiId string `db:"api_id"` // API标识
|
||||
FeatureId int64 `db:"feature_id"` // 关联feature表的ID
|
||||
Content string `db:"content"` // 内容
|
||||
Id string `db:"id"`
|
||||
CreateTime time.Time `db:"create_time"` // 创建时间
|
||||
UpdateTime time.Time `db:"update_time"` // 更新时间
|
||||
DeleteTime sql.NullTime `db:"delete_time"` // 删除时间
|
||||
DelState int64 `db:"del_state"` // 删除状态
|
||||
Version int64 `db:"version"` // 版本号
|
||||
ApiId string `db:"api_id"` // API标识
|
||||
FeatureId string `db:"feature_id"`
|
||||
Content string `db:"content"` // 内容
|
||||
ProductIdUuid sql.NullString `db:"product_id_uuid"`
|
||||
FeatureIdUuid sql.NullString `db:"feature_id_uuid"`
|
||||
}
|
||||
)
|
||||
|
||||
@@ -80,22 +83,39 @@ func newExampleModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultExampleModel
|
||||
|
||||
func (m *defaultExampleModel) Insert(ctx context.Context, session sqlx.Session, data *Example) (sql.Result, error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
hmExampleApiIdKey := fmt.Sprintf("%s%v", cacheHmExampleApiIdPrefix, data.ApiId)
|
||||
hmExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheHmExampleFeatureIdPrefix, data.FeatureId)
|
||||
hmExampleIdKey := fmt.Sprintf("%s%v", cacheHmExampleIdPrefix, data.Id)
|
||||
m.insertUUID(data)
|
||||
yccExampleApiIdKey := fmt.Sprintf("%s%v", cacheYccExampleApiIdPrefix, data.ApiId)
|
||||
yccExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheYccExampleFeatureIdPrefix, data.FeatureId)
|
||||
yccExampleIdKey := fmt.Sprintf("%s%v", cacheYccExampleIdPrefix, 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, exampleRowsExpectAutoSet)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, exampleRowsExpectAutoSet)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.ApiId, data.FeatureId, data.Content)
|
||||
return session.ExecCtx(ctx, query, data.Id, data.DeleteTime, data.DelState, data.Version, data.ApiId, data.FeatureId, data.Content, data.ProductIdUuid, data.FeatureIdUuid)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.ApiId, data.FeatureId, data.Content)
|
||||
}, hmExampleApiIdKey, hmExampleFeatureIdKey, hmExampleIdKey)
|
||||
return conn.ExecCtx(ctx, query, data.Id, data.DeleteTime, data.DelState, data.Version, data.ApiId, data.FeatureId, data.Content, data.ProductIdUuid, data.FeatureIdUuid)
|
||||
}, yccExampleApiIdKey, yccExampleFeatureIdKey, yccExampleIdKey)
|
||||
}
|
||||
func (m *defaultExampleModel) insertUUID(data *Example) {
|
||||
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 *defaultExampleModel) FindOne(ctx context.Context, id int64) (*Example, error) {
|
||||
hmExampleIdKey := fmt.Sprintf("%s%v", cacheHmExampleIdPrefix, id)
|
||||
func (m *defaultExampleModel) FindOne(ctx context.Context, id string) (*Example, error) {
|
||||
yccExampleIdKey := fmt.Sprintf("%s%v", cacheYccExampleIdPrefix, id)
|
||||
var resp Example
|
||||
err := m.QueryRowCtx(ctx, &resp, hmExampleIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
err := m.QueryRowCtx(ctx, &resp, yccExampleIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", exampleRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
|
||||
})
|
||||
@@ -110,9 +130,9 @@ func (m *defaultExampleModel) FindOne(ctx context.Context, id int64) (*Example,
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindOneByApiId(ctx context.Context, apiId string) (*Example, error) {
|
||||
hmExampleApiIdKey := fmt.Sprintf("%s%v", cacheHmExampleApiIdPrefix, apiId)
|
||||
yccExampleApiIdKey := fmt.Sprintf("%s%v", cacheYccExampleApiIdPrefix, apiId)
|
||||
var resp Example
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, hmExampleApiIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, yccExampleApiIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `api_id` = ? and del_state = ? limit 1", exampleRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, apiId, globalkey.DelStateNo); err != nil {
|
||||
return nil, err
|
||||
@@ -129,10 +149,10 @@ func (m *defaultExampleModel) FindOneByApiId(ctx context.Context, apiId string)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindOneByFeatureId(ctx context.Context, featureId int64) (*Example, error) {
|
||||
hmExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheHmExampleFeatureIdPrefix, featureId)
|
||||
func (m *defaultExampleModel) FindOneByFeatureId(ctx context.Context, featureId string) (*Example, error) {
|
||||
yccExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheYccExampleFeatureIdPrefix, featureId)
|
||||
var resp Example
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, hmExampleFeatureIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, yccExampleFeatureIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `feature_id` = ? and del_state = ? limit 1", exampleRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, featureId, globalkey.DelStateNo); err != nil {
|
||||
return nil, err
|
||||
@@ -154,16 +174,16 @@ func (m *defaultExampleModel) Update(ctx context.Context, session sqlx.Session,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hmExampleApiIdKey := fmt.Sprintf("%s%v", cacheHmExampleApiIdPrefix, data.ApiId)
|
||||
hmExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheHmExampleFeatureIdPrefix, data.FeatureId)
|
||||
hmExampleIdKey := fmt.Sprintf("%s%v", cacheHmExampleIdPrefix, data.Id)
|
||||
yccExampleApiIdKey := fmt.Sprintf("%s%v", cacheYccExampleApiIdPrefix, data.ApiId)
|
||||
yccExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheYccExampleFeatureIdPrefix, data.FeatureId)
|
||||
yccExampleIdKey := fmt.Sprintf("%s%v", cacheYccExampleIdPrefix, data.Id)
|
||||
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, exampleRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.Id)
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.ProductIdUuid, newData.FeatureIdUuid, newData.Id)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.Id)
|
||||
}, hmExampleApiIdKey, hmExampleFeatureIdKey, hmExampleIdKey)
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.ProductIdUuid, newData.FeatureIdUuid, newData.Id)
|
||||
}, yccExampleApiIdKey, yccExampleFeatureIdKey, yccExampleIdKey)
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *Example) error {
|
||||
@@ -178,16 +198,16 @@ func (m *defaultExampleModel) UpdateWithVersion(ctx context.Context, session sql
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hmExampleApiIdKey := fmt.Sprintf("%s%v", cacheHmExampleApiIdPrefix, data.ApiId)
|
||||
hmExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheHmExampleFeatureIdPrefix, data.FeatureId)
|
||||
hmExampleIdKey := fmt.Sprintf("%s%v", cacheHmExampleIdPrefix, data.Id)
|
||||
yccExampleApiIdKey := fmt.Sprintf("%s%v", cacheYccExampleApiIdPrefix, data.ApiId)
|
||||
yccExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheYccExampleFeatureIdPrefix, data.FeatureId)
|
||||
yccExampleIdKey := fmt.Sprintf("%s%v", cacheYccExampleIdPrefix, data.Id)
|
||||
sqlResult, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, exampleRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.Id, oldVersion)
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.ProductIdUuid, newData.FeatureIdUuid, newData.Id, oldVersion)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.Id, oldVersion)
|
||||
}, hmExampleApiIdKey, hmExampleFeatureIdKey, hmExampleIdKey)
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.ProductIdUuid, newData.FeatureIdUuid, newData.Id, oldVersion)
|
||||
}, yccExampleApiIdKey, yccExampleFeatureIdKey, yccExampleIdKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -404,26 +424,26 @@ func (m *defaultExampleModel) Trans(ctx context.Context, fn func(ctx context.Con
|
||||
func (m *defaultExampleModel) SelectBuilder() squirrel.SelectBuilder {
|
||||
return squirrel.Select().From(m.table)
|
||||
}
|
||||
func (m *defaultExampleModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
|
||||
func (m *defaultExampleModel) Delete(ctx context.Context, session sqlx.Session, id string) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hmExampleApiIdKey := fmt.Sprintf("%s%v", cacheHmExampleApiIdPrefix, data.ApiId)
|
||||
hmExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheHmExampleFeatureIdPrefix, data.FeatureId)
|
||||
hmExampleIdKey := fmt.Sprintf("%s%v", cacheHmExampleIdPrefix, id)
|
||||
yccExampleApiIdKey := fmt.Sprintf("%s%v", cacheYccExampleApiIdPrefix, data.ApiId)
|
||||
yccExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheYccExampleFeatureIdPrefix, data.FeatureId)
|
||||
yccExampleIdKey := fmt.Sprintf("%s%v", cacheYccExampleIdPrefix, id)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, id)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, id)
|
||||
}, hmExampleApiIdKey, hmExampleFeatureIdKey, hmExampleIdKey)
|
||||
}, yccExampleApiIdKey, yccExampleFeatureIdKey, yccExampleIdKey)
|
||||
return err
|
||||
}
|
||||
func (m *defaultExampleModel) formatPrimary(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheHmExampleIdPrefix, primary)
|
||||
return fmt.Sprintf("%s%v", cacheYccExampleIdPrefix, primary)
|
||||
}
|
||||
func (m *defaultExampleModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", exampleRows, m.table)
|
||||
|
||||
Reference in New Issue
Block a user