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 (
|
||||
queryRowsExpectAutoSet = strings.Join(stringx.Remove(queryFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
queryRowsWithPlaceHolder = strings.Join(stringx.Remove(queryFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
cacheQncQueryIdPrefix = "cache:qnc:query:id:"
|
||||
cacheQncQueryOrderIdPrefix = "cache:qnc:query:orderId:"
|
||||
cacheTycQueryIdPrefix = "cache:tyc:query:id:"
|
||||
cacheTycQueryOrderIdPrefix = "cache:tyc:query:orderId:"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -81,39 +80,39 @@ func newQueryModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultQueryModel {
|
||||
|
||||
func (m *defaultQueryModel) Insert(ctx context.Context, session sqlx.Session, data *Query) (sql.Result, error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
qncQueryIdKey := fmt.Sprintf("%s%v", cacheQncQueryIdPrefix, data.Id)
|
||||
qncQueryOrderIdKey := fmt.Sprintf("%s%v", cacheQncQueryOrderIdPrefix, data.OrderId)
|
||||
tycQueryIdKey := fmt.Sprintf("%s%v", cacheTycQueryIdPrefix, data.Id)
|
||||
tycQueryOrderIdKey := fmt.Sprintf("%s%v", cacheTycQueryOrderIdPrefix, data.OrderId)
|
||||
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, queryRowsExpectAutoSet)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, queryRowsExpectAutoSet)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.OrderId, data.UserId, data.ProductId, data.QueryParams, data.QueryData, data.QueryState, data.DelState, data.Version, data.DeleteTime)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.OrderId, data.UserId, data.ProductId, data.QueryParams, data.QueryData, data.QueryState, data.DelState, data.Version, data.DeleteTime)
|
||||
}, qncQueryIdKey, qncQueryOrderIdKey)
|
||||
}, tycQueryIdKey, tycQueryOrderIdKey)
|
||||
}
|
||||
|
||||
func (m *defaultQueryModel) FindOne(ctx context.Context, id int64) (*Query, error) {
|
||||
qncQueryIdKey := fmt.Sprintf("%s%v", cacheQncQueryIdPrefix, id)
|
||||
tycQueryIdKey := fmt.Sprintf("%s%v", cacheTycQueryIdPrefix, id)
|
||||
var resp Query
|
||||
err := m.QueryRowCtx(ctx, &resp, qncQueryIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? AND del_state = ? limit 1", queryRows, m.table)
|
||||
err := m.QueryRowCtx(ctx, &resp, tycQueryIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", queryRows, 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 *defaultQueryModel) FindOneByOrderId(ctx context.Context, orderId int64) (*Query, error) {
|
||||
qncQueryOrderIdKey := fmt.Sprintf("%s%v", cacheQncQueryOrderIdPrefix, orderId)
|
||||
tycQueryOrderIdKey := fmt.Sprintf("%s%v", cacheTycQueryOrderIdPrefix, orderId)
|
||||
var resp Query
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, qncQueryOrderIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := fmt.Sprintf("SELECT %s FROM %s WHERE `order_id` = ? AND del_state = ? limit 1", queryRows, m.table)
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, tycQueryOrderIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `order_id` = ? and del_state = ? limit 1", queryRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, orderId, globalkey.DelStateNo); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -123,7 +122,7 @@ func (m *defaultQueryModel) FindOneByOrderId(ctx context.Context, orderId int64)
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, model.ErrNotFound
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
@@ -134,15 +133,15 @@ func (m *defaultQueryModel) Update(ctx context.Context, session sqlx.Session, ne
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
qncQueryIdKey := fmt.Sprintf("%s%v", cacheQncQueryIdPrefix, data.Id)
|
||||
qncQueryOrderIdKey := fmt.Sprintf("%s%v", cacheQncQueryOrderIdPrefix, data.OrderId)
|
||||
tycQueryIdKey := fmt.Sprintf("%s%v", cacheTycQueryIdPrefix, data.Id)
|
||||
tycQueryOrderIdKey := fmt.Sprintf("%s%v", cacheTycQueryOrderIdPrefix, data.OrderId)
|
||||
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, queryRowsWithPlaceHolder)
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, queryRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, newData.OrderId, newData.UserId, newData.ProductId, newData.QueryParams, newData.QueryData, newData.QueryState, newData.DelState, newData.Version, newData.DeleteTime, newData.Id)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, newData.OrderId, newData.UserId, newData.ProductId, newData.QueryParams, newData.QueryData, newData.QueryState, newData.DelState, newData.Version, newData.DeleteTime, newData.Id)
|
||||
}, qncQueryIdKey, qncQueryOrderIdKey)
|
||||
}, tycQueryIdKey, tycQueryOrderIdKey)
|
||||
}
|
||||
|
||||
func (m *defaultQueryModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *Query) error {
|
||||
@@ -157,15 +156,15 @@ func (m *defaultQueryModel) UpdateWithVersion(ctx context.Context, session sqlx.
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
qncQueryIdKey := fmt.Sprintf("%s%v", cacheQncQueryIdPrefix, data.Id)
|
||||
qncQueryOrderIdKey := fmt.Sprintf("%s%v", cacheQncQueryOrderIdPrefix, data.OrderId)
|
||||
tycQueryIdKey := fmt.Sprintf("%s%v", cacheTycQueryIdPrefix, data.Id)
|
||||
tycQueryOrderIdKey := fmt.Sprintf("%s%v", cacheTycQueryOrderIdPrefix, data.OrderId)
|
||||
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, queryRowsWithPlaceHolder)
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, queryRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, newData.OrderId, newData.UserId, newData.ProductId, newData.QueryParams, newData.QueryData, newData.QueryState, newData.DelState, newData.Version, newData.DeleteTime, newData.Id, oldVersion)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, newData.OrderId, newData.UserId, newData.ProductId, newData.QueryParams, newData.QueryData, newData.QueryState, newData.DelState, newData.Version, newData.DeleteTime, newData.Id, oldVersion)
|
||||
}, qncQueryIdKey, qncQueryOrderIdKey)
|
||||
}, tycQueryIdKey, tycQueryOrderIdKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -174,7 +173,7 @@ func (m *defaultQueryModel) UpdateWithVersion(ctx context.Context, session sqlx.
|
||||
return err
|
||||
}
|
||||
if updateCount == 0 {
|
||||
return model.ErrNoRowsUpdate
|
||||
return ErrNoRowsUpdate
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -388,22 +387,22 @@ func (m *defaultQueryModel) Delete(ctx context.Context, session sqlx.Session, id
|
||||
return err
|
||||
}
|
||||
|
||||
qncQueryIdKey := fmt.Sprintf("%s%v", cacheQncQueryIdPrefix, id)
|
||||
qncQueryOrderIdKey := fmt.Sprintf("%s%v", cacheQncQueryOrderIdPrefix, data.OrderId)
|
||||
tycQueryIdKey := fmt.Sprintf("%s%v", cacheTycQueryIdPrefix, id)
|
||||
tycQueryOrderIdKey := fmt.Sprintf("%s%v", cacheTycQueryOrderIdPrefix, data.OrderId)
|
||||
_, 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)
|
||||
}, qncQueryIdKey, qncQueryOrderIdKey)
|
||||
}, tycQueryIdKey, tycQueryOrderIdKey)
|
||||
return err
|
||||
}
|
||||
func (m *defaultQueryModel) formatPrimary(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheQncQueryIdPrefix, primary)
|
||||
return fmt.Sprintf("%s%v", cacheTycQueryIdPrefix, primary)
|
||||
}
|
||||
func (m *defaultQueryModel) 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", queryRows, m.table)
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", queryRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary, globalkey.DelStateNo)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user