go-xorm 多条件组合搜索,分页,排序

关键代码:

func ADGetComm(Db *xorm.Engine, model1 *model.IncentiveAd, limit, start int) (*[]model.IncentiveAd, error) {
   var ads []model.IncentiveAd
   //多条件动态查询
   session := Db.Where("")
   if model1.AdName != "" {
      name := "%" + model1.AdName + "%"
      session = session.And("ad_name LIKE ?", name)
   }
   if model1.SdkType != "" {
      name := "%" + model1.SdkType + "%"
      session = session.And("sdk_type LIKE ?", name)
   }
   if model1.IosMediaId != "" {
      name := "%" + model1.IosMediaId + "%"
      session = session.And("ios_media_id LIKE ?", name)
   }
   if model1.IosAdId != "" {
      name := "%" + model1.IosAdId + "%"
      session = session.And("ios_ad_id LIKE ?", name)
   }
   if model1.AndroidMediaId != "" {
      name := "%" + model1.AndroidMediaId + "%"
      session = session.And("android_media_id LIKE ?", name)
   }
   if model1.AndroidAdId != "" {
      name := "%" + model1.AndroidAdId + "%"
      session = session.And("android_ad_id LIKE ?", name)
   }
   //
   err := session.Decr("id").Limit(limit, (start-1)*limit).Find(&ads)
   if err != nil {
      _ = logx.Warn(err)
      return nil, err
   }
   return &ads, nil
}

你可能感兴趣的:(go-xorm 多条件组合搜索,分页,排序)