temp
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"qnc-server/deploy/script/model"
|
||||
"strings"
|
||||
|
||||
"time"
|
||||
@@ -18,7 +17,7 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"qnc-server/common/globalkey"
|
||||
"tyc-server/common/globalkey"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -27,8 +26,8 @@ var (
|
||||
featureRowsExpectAutoSet = strings.Join(stringx.Remove(featureFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
featureRowsWithPlaceHolder = strings.Join(stringx.Remove(featureFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
cacheQncFeatureIdPrefix = "cache:qnc:feature:id:"
|
||||
cacheQncFeatureApiIdPrefix = "cache:qnc:feature:apiId:"
|
||||
cacheTycFeatureIdPrefix = "cache:tyc:feature:id:"
|
||||
cacheTycFeatureApiIdPrefix = "cache:tyc:feature:apiId:"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -77,39 +76,39 @@ func newFeatureModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultFeatureModel
|
||||
|
||||
func (m *defaultFeatureModel) Insert(ctx context.Context, session sqlx.Session, data *Feature) (sql.Result, error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
qncFeatureApiIdKey := fmt.Sprintf("%s%v", cacheQncFeatureApiIdPrefix, data.ApiId)
|
||||
qncFeatureIdKey := fmt.Sprintf("%s%v", cacheQncFeatureIdPrefix, data.Id)
|
||||
tycFeatureApiIdKey := fmt.Sprintf("%s%v", cacheTycFeatureApiIdPrefix, data.ApiId)
|
||||
tycFeatureIdKey := fmt.Sprintf("%s%v", cacheTycFeatureIdPrefix, 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, featureRowsExpectAutoSet)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?)", m.table, featureRowsExpectAutoSet)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.ApiId, data.Name)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.ApiId, data.Name)
|
||||
}, qncFeatureApiIdKey, qncFeatureIdKey)
|
||||
}, tycFeatureApiIdKey, tycFeatureIdKey)
|
||||
}
|
||||
|
||||
func (m *defaultFeatureModel) FindOne(ctx context.Context, id int64) (*Feature, error) {
|
||||
qncFeatureIdKey := fmt.Sprintf("%s%v", cacheQncFeatureIdPrefix, id)
|
||||
tycFeatureIdKey := fmt.Sprintf("%s%v", cacheTycFeatureIdPrefix, id)
|
||||
var resp Feature
|
||||
err := m.QueryRowCtx(ctx, &resp, qncFeatureIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? AND del_state = ? limit 1", featureRows, m.table)
|
||||
err := m.QueryRowCtx(ctx, &resp, tycFeatureIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", featureRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, model.ErrNotFound
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultFeatureModel) FindOneByApiId(ctx context.Context, apiId string) (*Feature, error) {
|
||||
qncFeatureApiIdKey := fmt.Sprintf("%s%v", cacheQncFeatureApiIdPrefix, apiId)
|
||||
tycFeatureApiIdKey := fmt.Sprintf("%s%v", cacheTycFeatureApiIdPrefix, apiId)
|
||||
var resp Feature
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, qncFeatureApiIdKey, 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", featureRows, m.table)
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, tycFeatureApiIdKey, 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", featureRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, apiId, globalkey.DelStateNo); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -119,7 +118,7 @@ func (m *defaultFeatureModel) FindOneByApiId(ctx context.Context, apiId string)
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, model.ErrNotFound
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
@@ -130,15 +129,15 @@ func (m *defaultFeatureModel) Update(ctx context.Context, session sqlx.Session,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
qncFeatureApiIdKey := fmt.Sprintf("%s%v", cacheQncFeatureApiIdPrefix, data.ApiId)
|
||||
qncFeatureIdKey := fmt.Sprintf("%s%v", cacheQncFeatureIdPrefix, data.Id)
|
||||
tycFeatureApiIdKey := fmt.Sprintf("%s%v", cacheTycFeatureApiIdPrefix, data.ApiId)
|
||||
tycFeatureIdKey := fmt.Sprintf("%s%v", cacheTycFeatureIdPrefix, 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, featureRowsWithPlaceHolder)
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, featureRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.Name, newData.Id)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.Name, newData.Id)
|
||||
}, qncFeatureApiIdKey, qncFeatureIdKey)
|
||||
}, tycFeatureApiIdKey, tycFeatureIdKey)
|
||||
}
|
||||
|
||||
func (m *defaultFeatureModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *Feature) error {
|
||||
@@ -153,15 +152,15 @@ func (m *defaultFeatureModel) UpdateWithVersion(ctx context.Context, session sql
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
qncFeatureApiIdKey := fmt.Sprintf("%s%v", cacheQncFeatureApiIdPrefix, data.ApiId)
|
||||
qncFeatureIdKey := fmt.Sprintf("%s%v", cacheQncFeatureIdPrefix, data.Id)
|
||||
tycFeatureApiIdKey := fmt.Sprintf("%s%v", cacheTycFeatureApiIdPrefix, data.ApiId)
|
||||
tycFeatureIdKey := fmt.Sprintf("%s%v", cacheTycFeatureIdPrefix, 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, featureRowsWithPlaceHolder)
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, featureRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.Name, newData.Id, oldVersion)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.Name, newData.Id, oldVersion)
|
||||
}, qncFeatureApiIdKey, qncFeatureIdKey)
|
||||
}, tycFeatureApiIdKey, tycFeatureIdKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -170,7 +169,7 @@ func (m *defaultFeatureModel) UpdateWithVersion(ctx context.Context, session sql
|
||||
return err
|
||||
}
|
||||
if updateCount == 0 {
|
||||
return model.ErrNoRowsUpdate
|
||||
return ErrNoRowsUpdate
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -384,22 +383,22 @@ func (m *defaultFeatureModel) Delete(ctx context.Context, session sqlx.Session,
|
||||
return err
|
||||
}
|
||||
|
||||
qncFeatureApiIdKey := fmt.Sprintf("%s%v", cacheQncFeatureApiIdPrefix, data.ApiId)
|
||||
qncFeatureIdKey := fmt.Sprintf("%s%v", cacheQncFeatureIdPrefix, id)
|
||||
tycFeatureApiIdKey := fmt.Sprintf("%s%v", cacheTycFeatureApiIdPrefix, data.ApiId)
|
||||
tycFeatureIdKey := fmt.Sprintf("%s%v", cacheTycFeatureIdPrefix, 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)
|
||||
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)
|
||||
}, qncFeatureApiIdKey, qncFeatureIdKey)
|
||||
}, tycFeatureApiIdKey, tycFeatureIdKey)
|
||||
return err
|
||||
}
|
||||
func (m *defaultFeatureModel) formatPrimary(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheQncFeatureIdPrefix, primary)
|
||||
return fmt.Sprintf("%s%v", cacheTycFeatureIdPrefix, primary)
|
||||
}
|
||||
func (m *defaultFeatureModel) 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", featureRows, m.table)
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", featureRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary, globalkey.DelStateNo)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user