package model import ( "app-cloudep-member-server/gen_result/pb/member" "context" "database/sql" "fmt" "strings" "time" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" ) var _ UserTableModel = (*customUserTableModel)(nil) type ( // UserTableModel is an interface to be customized, add more methods here, // and implement the added methods in customUserTableModel. UserTableModel interface { userTableModel FindOneByNickName(ctx context.Context, uid string) (*UserTable, error) ListMembers(ctx context.Context, params *UserQueryParams) ([]*UserTable, error) Count(ctx context.Context) (int64, error) UpdateStatus(ctx context.Context, uid string, status int32) error UpdateSome(ctx context.Context, newData *member.UpdateUserInfoReq) error } customUserTableModel struct { *defaultUserTableModel } UserQueryParams struct { RoleId *string VerifyType *int32 AlarmType *int32 Status *int32 CreateStartTime *int64 CreateEndTime *int64 PageSize int64 PageIndex int64 } ) // NewUserTableModel returns a model for the database table. func NewUserTableModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) UserTableModel { return &customUserTableModel{ defaultUserTableModel: newUserTableModel(conn, c, opts...), } } func (m *defaultUserTableModel) FindOneByNickName(ctx context.Context, nickName string) (*UserTable, error) { userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, nickName) var resp UserTable err := m.QueryRowIndexCtx(ctx, &resp, userTableUidKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { query := fmt.Sprintf("select %s from %s where `nick_name` = ? limit 1", userTableRows, m.table) if err := conn.QueryRowCtx(ctx, &resp, query, nickName); err != nil { return nil, err } return resp.Id, nil }, m.queryPrimary) switch err { case nil: return &resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultUserTableModel) ListMembers(ctx context.Context, params *UserQueryParams) ([]*UserTable, error) { query := fmt.Sprintf("select %s from %s", userTableRows, m.table) var args = make([]any, 0, 10) var conditions []string if params.RoleId != nil { conditions = append(conditions, "role_id = ?") args = append(args, *params.RoleId) } if params.VerifyType != nil { conditions = append(conditions, "verify_type = ?") args = append(args, *params.VerifyType) } if params.AlarmType != nil { conditions = append(conditions, "alarm_type = ?") args = append(args, *params.AlarmType) } if params.Status != nil { conditions = append(conditions, "status = ?") args = append(args, *params.Status) } if params.CreateStartTime != nil { conditions = append(conditions, "create_time >= ?") args = append(args, *params.CreateStartTime) } if params.CreateEndTime != nil { conditions = append(conditions, "create_time <= ?") args = append(args, *params.CreateEndTime) } // 加入條件到查詢語句中 if len(conditions) > 0 { query += " WHERE " + strings.Join(conditions, " AND ") } // 分頁處理 offset := (params.PageIndex - 1) * params.PageSize query += " LIMIT ? OFFSET ?" args = append(args, params.PageSize, offset) var users []*UserTable err := m.QueryRowsNoCacheCtx(ctx, &users, query, args...) if err != nil { return nil, err } return users, nil } func (m *defaultUserTableModel) Count(ctx context.Context) (int64, error) { var count int64 query := fmt.Sprintf("select count(*) from %s", m.table) err := m.QueryRowNoCacheCtx(ctx, &count, query) if err != nil { return 0, err } return count, nil } func (m *defaultUserTableModel) UpdateStatus(ctx context.Context, uid string, status int32) error { data, err := m.FindOneByUid(ctx, uid) if err != nil { return err } userTableIdKey := fmt.Sprintf("%s%v", cacheUserTableIdPrefix, data.Id) userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, data.Uid) _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("update %s set `status` = ? where `id` = ?", m.table) return conn.ExecCtx(ctx, query, status, data.Id) }, userTableIdKey, userTableUidKey) return err } func (m *defaultUserTableModel) UpdateSome(ctx context.Context, newData *member.UpdateUserInfoReq) error { data, err := m.FindOneByUid(ctx, newData.Uid) if err != nil { return err } // 初始化缓存键 userTableIdKey := fmt.Sprintf("%s%v", cacheUserTableIdPrefix, data.Id) userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, data.Uid) query := fmt.Sprintf("update %s set ", m.table) var args []interface{} var updates []string if newData.VerifyType != nil { updates = append(updates, "verify_type = ?") args = append(args, *newData.VerifyType) } if newData.AlarmType != nil { updates = append(updates, "alarm_type = ?") args = append(args, *newData.AlarmType) } if newData.Status != nil { updates = append(updates, "status = ?") args = append(args, *newData.Status) } if newData.Language != nil { updates = append(updates, "language = ?") args = append(args, *newData.Language) } if newData.Currency != nil { updates = append(updates, "currency = ?") args = append(args, *newData.Currency) } if newData.NickName != nil { updates = append(updates, "nick_name = ?") args = append(args, *newData.NickName) } if newData.Avatar != nil { updates = append(updates, "avatar = ?") args = append(args, *newData.Avatar) } if len(updates) == 0 { return nil } update := time.Now().UTC().Unix() updates = append(updates, "update_time = ?") args = append(args, &update) query += strings.Join(updates, ", ") + " where `id` = ?" args = append(args, data.Id) _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { return conn.ExecCtx(ctx, query, args...) }, userTableIdKey, userTableUidKey) return err }