func SearchVP(req *dto.VoicePackageReq) ([]*models.VoicePackage, int, error) {
query := []baseDto.Query{
{Field: "name", Value: req.Name, Type: db.QUERY_LIKE},
{Field: "product_code", Value: req.ProductCode, Type: db.QUERY_EQUAL}}
order := []string{"product_code desc", "create_time"}
var objs []*models.VoicePackage
total, err := db.DbQuery("smart_home", "voice_package", query, req.Pagination, order, &objs)
if err != nil {
return nil, total, err
}
return objs, total, nil
}
func DbQuery(service string, table string, query []baseDto.Query, pagination *baseDto.Pagination, order []string, objs interface{}, ) (int, error) {
db := cache.MapDb[service]
db = db.Table(table)
total := 0
for _, v := range query {
if v.Value != "" && v.Type == QUERY_LIKE {
db = db.Where(v.Field+" LIKE ?", "%"+v.Value+"%")
}
if v.Value != "" && v.Type == QUERY_EQUAL {
db = db.Where(v.Field+" = ?", v.Value)
}
}
for _, v := range order {
db = db.Order(v)
}
if err := db.Count(&total).Error; err != nil {
return 0, err
}
if pagination != nil {
db = db.Offset(pagination.Offset()).Limit(pagination.PageSize)
}
if err := db.Find(objs).Error; err != nil {
return total, err
}
return total, nil
}