tp的复合查询可以用_complex来进行动态组合,而xorm没有这个字段,除了用sql凭接的方式,还可以用切片来
func (m *LogModel) BuildRemovalSql(ctx context.Context, model *xorm.Session, actInfo acts.Act, req *req_ent.GetRemovalByActRequest) error {
var (
tagIds = make([]int64, 0)
tagUser = make([]string, 0)
tagUserMod = tag_user.GetInstance()
ActModel = acts.GetInstance()
statusArr = make([]string, 0)
tagActArr = make([]string, 0)
actIds = make([]int64, 0)
err error
)
//时间排重
date := time.Now().AddDate(0, 0, 0-actInfo.RemovalInfo.Days).Format("2006-01-02")
model.Where("created_at >= ?", date)
//类型排重
for _, act_type := range actInfo.RemovalInfo.ActType {
if act_type == acts.RemovalActTypeSms {
statusArr = append(statusArr, "sms_status = 1")
} else if act_type == acts.RemovalActTypePush {
statusArr = append(statusArr, "push_status = 1")
} else if act_type == acts.RemovalActTypeAppLetter {
statusArr = append(statusArr, "app_letter_status = 1")
} else if act_type == acts.RemovalActTypeCoupon {
statusArr = append(statusArr, "coupon_status = 1")
} else if act_type == acts.RemovalActTypeCoin {
statusArr = append(statusArr, "coin_status = 1")
}
}
if len(statusArr) > 0 {
if len(req.Operate) == 0 || req.Operate == "OR" {
model.Where(strings.Join(statusArr, " OR "))
} else {
model.Where(strings.Join(statusArr, " AND "))
}
}
//标签排重
if len(actInfo.RemovalInfo.TagIds) > 0 {
for _, tag := range actInfo.RemovalInfo.TagIds {
tagIds = append(tagIds, tag.Id)
}
if len(tagIds) > 0 {
tagUser, err = tagUserMod.GetUserIdByTagId(ctx, tagIds)
if err != nil {
logger.Error(ctx, "GetUserIdByTagId", err.Error(), logger.NewWithField("tagIds", tagIds))
return err
}
if len(tagUser) > 0 {
tagActArr = append(tagActArr, fmt.Sprintf("user_id in (%s)", strings.Join(gconv.Strings(tagUser), ",")))
}
}
}
//本身排重
if actInfo.RemovalInfo.Type == acts.RemovalTypeSelf {
actInfo.RemovalInfo.CycleIds = append(actInfo.RemovalInfo.CycleIds, actInfo.RelatedId)
}
//活动排重,计划排重
if len(actInfo.RemovalInfo.CycleIds) > 0 {
actIds, err = ActModel.GetActIdsByCycleIds(ctx, actInfo.RemovalInfo.CycleIds)
if err != nil {
logger.Error(ctx, "GetActIdsByCycleIds", err.Error(), logger.NewWithField("CycleIds", actInfo.RemovalInfo.CycleIds))
return err
}
if len(actIds) > 0 {
actInfo.RemovalInfo.ActIds = append(actInfo.RemovalInfo.ActIds, actIds...)
}
}
if len(actInfo.RemovalInfo.ActIds) > 0 {
tagActArr = append(tagActArr, fmt.Sprintf("act_id in (%s)", strings.Join(gconv.Strings(actInfo.RemovalInfo.ActIds), ",")))
}
//活动和标签是或者关系
if len(tagActArr) > 0 {
model.Where(strings.Join(tagActArr, " OR "))
}
//UniqueId排重
if len(req.UniqueIds) > 0 {
model.In("unique_id", req.UniqueIds)
}
//userId排重
if len(req.UserIds) > 0 {
model.In("user_id", req.UserIds)
}
return err
}
利用数组来把需要的条件放到tagActArr
数组中,再用 strings.Join(tagActArr, " OR ")
,然后再where查询
model.Where(strings.Join(tagActArr, " OR "))