纷享销客线索回收批量更换负责人

需求:跟进中的线索,超过创建时间的72小时还在跟进中,并且跟进次
 数小于6次,更换负责人为某某。

/**
 * @author QiaoChu
 * @codeName 线索回收
 * @description 跟进中的线索,超过创建时间的72小时还在跟进中,并且跟进次
 数小于6次,更换负责人为某某
 */
//当前时间-72小时
def diffTime = DateTime.of(DateTime.now().toTimestamp() - 3 * 24 * 60 * 60 * 1000);
//2024年6月1号之后的数据
DateTime startTime = '2024-06-01 00:00';
//进行中
def status = 'processed';
//小于6次
def num = 6;
//某某
def owner1 = "1234";
List ownerList = [];
ownerList.add(owner1);
def searchCondition = QueryTemplate.AND(
        ["biz_status": QueryOperator.EQ(status)],
        ["field_AT6Xp__c": QueryOperator.LT(num)],
        ["owner": QueryOperator.NLIKE(owner1)],
        ["create_time": QueryOperator.LT(diffTime)],
        ["create_time": QueryOperator.GT(startTime)]
)

Fx.log.info("查询条件为 :" + searchCondition)
//查询条件 要以id 进行排序,find默认的排序是最后修改时间,如果排序字段存在重复,数据库是不能保证每次查询出来顺序是一致的
//分页的时候可能会出现重复数据,导致执行遗漏。
Map orderBy = ["_id": -1]

boolean hasData = true
Integer limit = 100
//循环聚合所有数据
Map updateData = [:]
Range range = Ranges.of(0, 200)
range.each { i ->
    if (hasData == false) {
        return
    }

    Integer skip = i * limit
    def (Boolean error, QueryResult result, String errorMessage) = Fx.object.find("LeadsObj",
            FQLAttribute.builder()
                    .orderBy(orderBy)
                    .limit(limit)
                    .skip(skip)
                    .columns(["_id", "name", "create_time", "owner", "remark", "tel"])
                    .queryTemplate(searchCondition)
                    .build(),
            SelectAttribute.builder().build())

    if (error) {
        hasData = false
        Fx.log.info("查询失败" + "当前跳过的数量为 " + skip + "错误原因为" + errorMessage)
        return
    }
    log.info(result)
    //获取所有更新的数据,如果是预设对象,就在循环里面直接更新。
    List dataList = result.dataList
    if (dataList.size() < limit) {
        hasData = false
    }

    //如果不需要合并数据,在这里就可以100条
    dataList.each { item ->
        Map objectData = item as Map
        log.info(objectData)
        updateData.put(objectData._id, ["objectId": objectData._id, "ownerId": ownerList])
    }
}

//极端情况,所有查询都失败了
if (!updateData) {
    Fx.log.info("循环查询出来数据为空")
    return
}

//如果是自定义对象,直接调用批量更新即可,更新单次500条限制,所以我们拆分数据
List idList = updateData.keys() as List
//log.info(updateData)
//log.info(idList)
List partitionList = Fx.utils.listPartition(idList, 500)
partitionList.each { item ->
    Map currentUpdateMap = [:]
    //根据id分批组装数据
    List ids = item as List
    ids.each { id ->
        String stringId = id as String
        Map totalMap = updateData
        Map updateMap = totalMap[stringId] as Map
        currentUpdateMap.put(id, updateMap)
    }
    //指定更新的字段
    List fields = Lists.newArrayList("owner");
    //log.info(fields);
    //获取这一批的数据,然后调用批量更新接口
    def updateResult = Fx.object.batchUpdate("LeadsObj", currentUpdateMap, fields)

    if (updateResult[0]) {
        Fx.log.info("更新失败 原因为 " + updateResult[2])
    } else {
        Fx.log.info("更新成功")
    }
}
Fx.log.info("实际更新的数据为" + idList.size())

备注:Fx.object.batchUpdate函数,不会自动创建系统日志,不添加修改记录。  

参考:
1、Fx.object.find

Fx.object | 纷享销客 | 帮助中心
2、Fx.object.batchUpdate
Fx.object | 纷享销客 | 帮助中心

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值