This commit is contained in:
2024-12-28 17:13:18 +08:00
parent a70f5f0122
commit 96de8e978a
3 changed files with 32 additions and 21 deletions

View File

@@ -6,7 +6,7 @@ import (
"context"
"database/sql"
"fmt"
"qnc-server/deploy/script/model"
model2 "qnc-server/deploy/script/model"
"strings"
"time"
@@ -67,6 +67,7 @@ type (
Password sql.NullString `db:"password"`
Nickname string `db:"nickname"`
Info string `db:"info"`
Inside int64 `db:"inside"`
}
)
@@ -82,11 +83,11 @@ func (m *defaultUserModel) Insert(ctx context.Context, session sqlx.Session, dat
qncUserIdKey := fmt.Sprintf("%s%v", cacheQncUserIdPrefix, data.Id)
qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, data.Mobile)
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, userRowsExpectAutoSet)
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, userRowsExpectAutoSet)
if session != nil {
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info)
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside)
}
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info)
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside)
}, qncUserIdKey, qncUserMobileKey)
}
@@ -94,14 +95,14 @@ func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error)
qncUserIdKey := fmt.Sprintf("%s%v", cacheQncUserIdPrefix, id)
var resp User
err := m.QueryRowCtx(ctx, &resp, qncUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? AND del_state = ? limit 1", userRows, m.table)
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", userRows, 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, model2.ErrNotFound
default:
return nil, err
}
@@ -111,7 +112,7 @@ func (m *defaultUserModel) FindOneByMobile(ctx context.Context, mobile string) (
qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, mobile)
var resp User
err := m.QueryRowIndexCtx(ctx, &resp, qncUserMobileKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
query := fmt.Sprintf("SELECT %s FROM %s WHERE `mobile` = ? AND del_state = ? limit 1", userRows, m.table)
query := fmt.Sprintf("select %s from %s where `mobile` = ? and del_state = ? limit 1", userRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, mobile, globalkey.DelStateNo); err != nil {
return nil, err
}
@@ -121,7 +122,7 @@ func (m *defaultUserModel) FindOneByMobile(ctx context.Context, mobile string) (
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, model.ErrNotFound
return nil, model2.ErrNotFound
default:
return nil, err
}
@@ -135,11 +136,11 @@ func (m *defaultUserModel) Update(ctx context.Context, session sqlx.Session, new
qncUserIdKey := fmt.Sprintf("%s%v", cacheQncUserIdPrefix, data.Id)
qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, data.Mobile)
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, userRowsWithPlaceHolder)
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, userRowsWithPlaceHolder)
if session != nil {
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Id)
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id)
}
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Id)
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id)
}, qncUserIdKey, qncUserMobileKey)
}
@@ -158,11 +159,11 @@ func (m *defaultUserModel) UpdateWithVersion(ctx context.Context, session sqlx.S
qncUserIdKey := fmt.Sprintf("%s%v", cacheQncUserIdPrefix, data.Id)
qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, data.Mobile)
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, userRowsWithPlaceHolder)
query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, userRowsWithPlaceHolder)
if session != nil {
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Id, oldVersion)
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id, oldVersion)
}
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Id, oldVersion)
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id, oldVersion)
}, qncUserIdKey, qncUserMobileKey)
if err != nil {
return err
@@ -172,7 +173,7 @@ func (m *defaultUserModel) UpdateWithVersion(ctx context.Context, session sqlx.S
return err
}
if updateCount == 0 {
return model.ErrNoRowsUpdate
return model2.ErrNoRowsUpdate
}
return nil
@@ -389,7 +390,7 @@ func (m *defaultUserModel) Delete(ctx context.Context, session sqlx.Session, id
qncUserIdKey := fmt.Sprintf("%s%v", cacheQncUserIdPrefix, id)
qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, data.Mobile)
_, 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)
}
@@ -401,7 +402,7 @@ func (m *defaultUserModel) formatPrimary(primary interface{}) string {
return fmt.Sprintf("%s%v", cacheQncUserIdPrefix, primary)
}
func (m *defaultUserModel) 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", userRows, m.table)
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", userRows, m.table)
return conn.QueryRowCtx(ctx, v, query, primary, globalkey.DelStateNo)
}