new
This commit is contained in:
@@ -12,12 +12,18 @@ import (
|
||||
var _ ProductsModel = (*customProductsModel)(nil)
|
||||
var productsRowsWithoutContent = strings.Join(stringx.Remove(productsFieldNames, "`ProductContent`"), ",")
|
||||
|
||||
type ProductFilter struct {
|
||||
ProductName string // 产品名称,模糊匹配
|
||||
ProductGroup string // 产品分类,模糊匹配
|
||||
IsEnabled *int64 // 是否启用,1-启用,0-禁用,使用指针类型来区分是否需要筛选
|
||||
IsVisible *int64 // 是否展示,1-展示,0-隐藏,使用指针类型来区分是否需要筛选
|
||||
}
|
||||
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)
|
||||
FindProductsList(ctx context.Context, page, pageSize int64, filter *ProductFilter) ([]*Products, int64, error)
|
||||
}
|
||||
|
||||
customProductsModel struct {
|
||||
@@ -31,20 +37,53 @@ func NewProductsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option
|
||||
defaultProductsModel: newProductsModel(conn, c, opts...),
|
||||
}
|
||||
}
|
||||
func (m *defaultProductsModel) FindProductsList(ctx context.Context, page, pageSize int64) ([]*Products, int64, error) {
|
||||
func (m *defaultProductsModel) FindProductsList(ctx context.Context, page, pageSize int64, filter *ProductFilter) ([]*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)
|
||||
// 构建筛选条件
|
||||
var conditions []string
|
||||
var args []interface{}
|
||||
|
||||
if filter.ProductName != "" {
|
||||
conditions = append(conditions, "product_name LIKE ?")
|
||||
args = append(args, "%"+filter.ProductName+"%")
|
||||
}
|
||||
|
||||
if filter.ProductGroup != "" {
|
||||
conditions = append(conditions, "product_group LIKE ?")
|
||||
args = append(args, "%"+filter.ProductGroup+"%")
|
||||
}
|
||||
|
||||
// 如果明确传入启用状态为 0 或 1 则筛选,否则跳过筛选
|
||||
if filter.IsEnabled != nil {
|
||||
conditions = append(conditions, "is_enabled = ?")
|
||||
args = append(args, *filter.IsEnabled)
|
||||
}
|
||||
|
||||
// 如果明确传入展示状态为 0 或 1 则筛选,否则跳过筛选
|
||||
if filter.IsVisible != nil {
|
||||
conditions = append(conditions, "is_visible = ?")
|
||||
args = append(args, *filter.IsVisible)
|
||||
}
|
||||
|
||||
whereClause := ""
|
||||
if len(conditions) > 0 {
|
||||
whereClause = "WHERE " + strings.Join(conditions, " AND ")
|
||||
}
|
||||
|
||||
// 查询产品列表
|
||||
query := fmt.Sprintf("SELECT %s FROM %s %s ORDER BY created_at DESC LIMIT ?,?", productsRows, m.table, whereClause)
|
||||
args = append(args, offset, pageSize)
|
||||
err := m.QueryRowsNoCacheCtx(ctx, &products, query, args...)
|
||||
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)
|
||||
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s %s", m.table, whereClause)
|
||||
err = m.QueryRowNoCacheCtx(ctx, &total, countQuery, args[:len(args)-2]...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
@@ -50,6 +50,8 @@ type (
|
||||
ProductContent sql.NullString `db:"product_content"` // 产品内容
|
||||
ProductGroup string `db:"product_group"` // 产品分类
|
||||
ProductPrice float64 `db:"product_price"` // 产品价格
|
||||
IsEnabled int64 `db:"is_enabled"` // 是否启用,1-启用,0-禁用
|
||||
IsVisible int64 `db:"is_visible"` // 是否展示,1-展示,0-隐藏
|
||||
CreatedAt time.Time `db:"created_at"` // 创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 更新时间
|
||||
}
|
||||
@@ -118,8 +120,8 @@ func (m *defaultProductsModel) Insert(ctx context.Context, data *Products) (sql.
|
||||
productsIdKey := fmt.Sprintf("%s%v", cacheProductsIdPrefix, data.Id)
|
||||
productsProductCodeKey := fmt.Sprintf("%s%v", cacheProductsProductCodePrefix, data.ProductCode)
|
||||
ret, err := 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, productsRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.ProductName, data.ProductCode, data.ProductDescription, data.ProductContent, data.ProductGroup, data.ProductPrice)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, productsRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.ProductName, data.ProductCode, data.ProductDescription, data.ProductContent, data.ProductGroup, data.ProductPrice, data.IsEnabled, data.IsVisible)
|
||||
}, productsIdKey, productsProductCodeKey)
|
||||
return ret, err
|
||||
}
|
||||
@@ -134,7 +136,7 @@ func (m *defaultProductsModel) Update(ctx context.Context, newData *Products) er
|
||||
productsProductCodeKey := fmt.Sprintf("%s%v", cacheProductsProductCodePrefix, data.ProductCode)
|
||||
_, 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` = ?", m.table, productsRowsWithPlaceHolder)
|
||||
return conn.ExecCtx(ctx, query, newData.ProductName, newData.ProductCode, newData.ProductDescription, newData.ProductContent, newData.ProductGroup, newData.ProductPrice, newData.Id)
|
||||
return conn.ExecCtx(ctx, query, newData.ProductName, newData.ProductCode, newData.ProductDescription, newData.ProductContent, newData.ProductGroup, newData.ProductPrice, newData.IsEnabled, newData.IsVisible, newData.Id)
|
||||
}, productsIdKey, productsProductCodeKey)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user