golang xorm 复合查询

xorm 复合查询

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 "))

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值