package model import ( "context" "fmt" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stringx" "strings" ) var _ ProductsModel = (*customProductsModel)(nil) var productsRowsWithoutContent = strings.Join(stringx.Remove(productsFieldNames, "`ProductContent`"), ",") type ( // ProductsModel is an interface to be customized, add more methods here, // and implement the added methods in customProductsModel. ProductsModel interface { productsModel FindProductsList(ctx context.Context, page, pageSize int64) ([]*Products, int64, error) } customProductsModel struct { *defaultProductsModel } ) // NewProductsModel returns a model for the database table. func NewProductsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) ProductsModel { return &customProductsModel{ defaultProductsModel: newProductsModel(conn, c, opts...), } } func (m *defaultProductsModel) FindProductsList(ctx context.Context, page, pageSize int64) ([]*Products, int64, error) { offset := (page - 1) * pageSize var products []*Products query := fmt.Sprintf("SELECT %s FROM %s ORDER BY created_at DESC LIMIT ?,?", productsRowsWithoutContent, m.table) err := m.QueryRowsNoCacheCtx(ctx, &products, query, offset, pageSize) if err != nil { return nil, 0, err } // 查询总数量 var total int64 countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s", m.table) err = m.QueryRowNoCacheCtx(ctx, &total, countQuery) if err != nil { return nil, 0, err } return products, total, nil }