v1.1
This commit is contained in:
@@ -8,32 +8,33 @@ 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 (
|
||||
globalNotificationsFieldNames = builder.RawFieldNames(&GlobalNotifications{})
|
||||
globalNotificationsRows = strings.Join(globalNotificationsFieldNames, ",")
|
||||
globalNotificationsRowsExpectAutoSet = strings.Join(stringx.Remove(globalNotificationsFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
globalNotificationsRowsExpectAutoSet = strings.Join(stringx.Remove(globalNotificationsFieldNames, "`create_time`", "`update_time`"), ",")
|
||||
globalNotificationsRowsWithPlaceHolder = strings.Join(stringx.Remove(globalNotificationsFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
cacheHmGlobalNotificationsIdPrefix = "cache:ycc:globalNotifications:id:"
|
||||
cacheYccGlobalNotificationsIdPrefix = "cache:ycc:globalNotifications:id:"
|
||||
)
|
||||
|
||||
type (
|
||||
globalNotificationsModel interface {
|
||||
Insert(ctx context.Context, session sqlx.Session, data *GlobalNotifications) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*GlobalNotifications, error)
|
||||
FindOne(ctx context.Context, id string) (*GlobalNotifications, error)
|
||||
Update(ctx context.Context, session sqlx.Session, data *GlobalNotifications) (sql.Result, error)
|
||||
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *GlobalNotifications) error
|
||||
Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
|
||||
@@ -46,7 +47,7 @@ type (
|
||||
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*GlobalNotifications, int64, error)
|
||||
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*GlobalNotifications, error)
|
||||
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*GlobalNotifications, error)
|
||||
Delete(ctx context.Context, session sqlx.Session, id int64) error
|
||||
Delete(ctx context.Context, session sqlx.Session, id string) error
|
||||
}
|
||||
|
||||
defaultGlobalNotificationsModel struct {
|
||||
@@ -55,7 +56,7 @@ type (
|
||||
}
|
||||
|
||||
GlobalNotifications struct {
|
||||
Id int64 `db:"id"`
|
||||
Id string `db:"id"`
|
||||
Title string `db:"title"`
|
||||
Content string `db:"content"`
|
||||
NotificationPage string `db:"notification_page"`
|
||||
@@ -81,20 +82,37 @@ func newGlobalNotificationsModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultG
|
||||
|
||||
func (m *defaultGlobalNotificationsModel) Insert(ctx context.Context, session sqlx.Session, data *GlobalNotifications) (sql.Result, error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
hmGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheHmGlobalNotificationsIdPrefix, data.Id)
|
||||
m.insertUUID(data)
|
||||
yccGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheYccGlobalNotificationsIdPrefix, 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, globalNotificationsRowsExpectAutoSet)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, globalNotificationsRowsExpectAutoSet)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime)
|
||||
return session.ExecCtx(ctx, query, data.Id, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime)
|
||||
}, hmGlobalNotificationsIdKey)
|
||||
return conn.ExecCtx(ctx, query, data.Id, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime)
|
||||
}, yccGlobalNotificationsIdKey)
|
||||
}
|
||||
func (m *defaultGlobalNotificationsModel) insertUUID(data *GlobalNotifications) {
|
||||
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 *defaultGlobalNotificationsModel) FindOne(ctx context.Context, id int64) (*GlobalNotifications, error) {
|
||||
hmGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheHmGlobalNotificationsIdPrefix, id)
|
||||
func (m *defaultGlobalNotificationsModel) FindOne(ctx context.Context, id string) (*GlobalNotifications, error) {
|
||||
yccGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheYccGlobalNotificationsIdPrefix, id)
|
||||
var resp GlobalNotifications
|
||||
err := m.QueryRowCtx(ctx, &resp, hmGlobalNotificationsIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
err := m.QueryRowCtx(ctx, &resp, yccGlobalNotificationsIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", globalNotificationsRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
|
||||
})
|
||||
@@ -109,14 +127,14 @@ func (m *defaultGlobalNotificationsModel) FindOne(ctx context.Context, id int64)
|
||||
}
|
||||
|
||||
func (m *defaultGlobalNotificationsModel) Update(ctx context.Context, session sqlx.Session, data *GlobalNotifications) (sql.Result, error) {
|
||||
hmGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheHmGlobalNotificationsIdPrefix, data.Id)
|
||||
yccGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheYccGlobalNotificationsIdPrefix, 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, globalNotificationsRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime, data.Id)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime, data.Id)
|
||||
}, hmGlobalNotificationsIdKey)
|
||||
}, yccGlobalNotificationsIdKey)
|
||||
}
|
||||
|
||||
func (m *defaultGlobalNotificationsModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, data *GlobalNotifications) error {
|
||||
@@ -127,14 +145,14 @@ func (m *defaultGlobalNotificationsModel) UpdateWithVersion(ctx context.Context,
|
||||
var sqlResult sql.Result
|
||||
var err error
|
||||
|
||||
hmGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheHmGlobalNotificationsIdPrefix, data.Id)
|
||||
yccGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheYccGlobalNotificationsIdPrefix, 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, globalNotificationsRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime, data.Id, oldVersion)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime, data.Id, oldVersion)
|
||||
}, hmGlobalNotificationsIdKey)
|
||||
}, yccGlobalNotificationsIdKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -351,19 +369,19 @@ func (m *defaultGlobalNotificationsModel) Trans(ctx context.Context, fn func(ctx
|
||||
func (m *defaultGlobalNotificationsModel) SelectBuilder() squirrel.SelectBuilder {
|
||||
return squirrel.Select().From(m.table)
|
||||
}
|
||||
func (m *defaultGlobalNotificationsModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
|
||||
hmGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheHmGlobalNotificationsIdPrefix, id)
|
||||
func (m *defaultGlobalNotificationsModel) Delete(ctx context.Context, session sqlx.Session, id string) error {
|
||||
yccGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheYccGlobalNotificationsIdPrefix, 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)
|
||||
}, hmGlobalNotificationsIdKey)
|
||||
}, yccGlobalNotificationsIdKey)
|
||||
return err
|
||||
}
|
||||
func (m *defaultGlobalNotificationsModel) formatPrimary(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheHmGlobalNotificationsIdPrefix, primary)
|
||||
return fmt.Sprintf("%s%v", cacheYccGlobalNotificationsIdPrefix, primary)
|
||||
}
|
||||
func (m *defaultGlobalNotificationsModel) 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", globalNotificationsRows, m.table)
|
||||
|
||||
Reference in New Issue
Block a user