当前位置: 首页 > 工具软件 > go-xorm > 使用案例 >

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

谭鹏云
2023-12-01

关键代码:

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
}
 类似资料: