批量选择数据,点击UI按钮,匹配到客户,通知当前客户负责人和客户销售订单的所有负责人;匹配不到客户,根据角色查询所有员工ID并发送CRM消息通知

/**
 * @codeName 批量选择数据,点击UI按钮,根据角色查询所有员工ID并发送CRM消息通知
 */
log.info(context)
List dataList = context.dataList as List;
Map updateData = [:]
dataList.each {
    item ->
        String id = item["_id"] as String;
        String status = item["status"] as String;
        String accountId = item["account_id"] != null ? item["account_id"] : "";
        String amountCompany = item["field_zWjN4__c"] as String;
        String amountReceived = item["amount_received"] as String;
        updateData.put(id, ["status": status, "account_id": accountId, "amount_company": amountCompany, "amount_received": amountReceived])
}
List idList = dataList.collect {
    x -> x["_id"]
}
log.info(idList)
/***********************IF-识别到客户-IF****************************/
//查询六个月以内的销售订单
//开始时间
DateTime dateTime = DateTime.now();
//距当前时间6个月
DateTime startTime = dateTime - 6.months;
//结束时间
DateTime endTime = dateTime;
/***********************IF-识别到客户-IF****************************/

/***********************IF-识别不到客户-IF****************************/
/**
 * @codeName 根据角色查询所有员工ID并发送CRM消息通知
 */
List roleCodes = ["00000000000000000000000000000015"]
// 调用方法获取用户信息
def (boolean errorR, List dataR, String messageR) = Fx.auth.user.getUsersByRoleCodes(roleCodes)
if (errorR) {
    // 如果有错误信息,记录日志
    log.info("error: " + messageR)
} else {
    // 记录成功获取的数据
    log.info(dataR)
}
boolean isBj = false
// 定义正则表达式模式 需要过滤的 userId 列表
def bJPattern = ~/^(1186|1187|1188|1189|1050|1075)$/
List userIdList = [];
dataR.each { itemR ->
    String userId = itemR['userId'] as String;
    isBj = bJPattern.matcher(userId).matches();
    if (!isBj) {
        userIdList.add(userId);
    }
}
log.info(userIdList)
/***********************IF-识别不到客户-IF***********************/

List partitionList = Fx.utils.listPartition(idList, 100)
partitionList.each { item ->
    //根据id分批组装数据
    List ids = item as List
    ids.each { id ->
        String stringId = id as String
        log.info(updateData[stringId]['status'])
        //认领状态:1-待认领
        if (updateData[stringId]['status'] == '1') {
            //识别不到客户-全员发送
            if (updateData[stringId]['account_id'] == "") {
                List partitionListR = Fx.utils.listPartition(userIdList, 50)
                partitionListR.each { itemR2 ->
                    //根据id分批组装数据
                    List userIds = itemR2 as List
                    Fx.log.info("当前id分批组装数据条数:" + userIds.size())
                    log.info(userIds)
                    //Fx.log.info("实际发送的数据条数:" + userIdList.size())
                    Notice objectNotify = Notice.objectNotice("ReceivedPaymentObj", stringId)
                    def (Boolean dataErrorR, String dataNoticeR, String errorMessageR) = Fx.message.sendNotice("到款认领", "打款公司:[" + updateData[stringId]['amount_company'] + "],到款金额:[" + updateData[stringId]['amount_received'] + "]", userIds, objectNotify)
                    if (dataErrorR) {
                        log.info(errorMessageR)
                    } else {
                        log.info(dataNoticeR)
                    }
                }
            } else {
                /***********************IF-识别到客户-IF***********************/
                // 获取客户负责
                def retExist = Fx.object.findById("AccountObj", updateData[stringId]['account_id'] as String,
                        FQLAttribute.builder().columns(["_id", "name", "owner"]).build(),
                        SelectAttribute.builder().build()).result() as Map
                // 查询销售订单
                def searchConditionExist = QueryTemplate.AND(
                        ["account_id": QueryOperator.EQ(updateData[stringId]['account_id'])],
                        ["create_time": QueryOperator.GTE(startTime)],
                        ["create_time": QueryOperator.LTE(endTime)]
                )
                Fx.log.info("查询条件为: $searchConditionExist")
                // 分页查询销售订单并去重创建人
                List dataAllListExist = []
                Range rangeExist = Ranges.of(0, 200)
                boolean hasDataExist = true
                Integer limitExist = 100
                rangeExist.each { i ->
                    if (!hasDataExist) return
                    Integer skipExist = i * limitExist
                    def (Boolean errorExist, QueryResult resultExist, String errorMessageExist) = Fx.object.find(
                            "SalesOrderObj",
                            FQLAttribute.builder()
                                    .orderBy(_id: -1)
                                    .limit(limitExist)
                                    .skip(skipExist)
                                    .columns(["_id", "owner", "created_by"])
                                    .queryTemplate(searchConditionExist)
                                    .build(),
                            SelectAttribute.builder().build()
                    )
                    if (errorExist) {
                        Fx.log.info("查询失败,当前跳过的数量为 {$skipExist},错误原因为 {$errorMessageExist}")
                        hasDataExist = false
                        return
                    }
                    dataAllListExist.addAll(resultExist.dataList)
                    if (resultExist.dataList.size() < limitExist) hasDataExist = false
                }
                if (dataAllListExist.isEmpty()) {
                    Fx.log.info("查询出来的数据为空")
                    return
                }
                // 获取所有创建人并去重
                List memberList = (dataAllListExist.collect { it["owner"] }.flatten() + retExist['owner']).unique()
                /***********************IF-识别到客户-IF***********************/
                Notice objectNotify = Notice.objectNotice("ReceivedPaymentObj", stringId)
                def (Boolean error, String data, String errorMessage) = Fx.message.sendNotice("到款认领", "打款公司:[" + updateData[stringId]['amount_company'] + "],到款金额:[" + updateData[stringId]['amount_received'] + "]", memberList, objectNotify)
                if (error) {
                    log.info(errorMessage)
                } else {
                    log.info(data)
                }
            }
        } else {
            log.info(dataList[stringId]['name'] + ":已认领")
        }
    }
}
return ''

 参考:

 1、context.dataList
context | 纷享销客 | 帮助中心
2、Fx.auth.user.getRolesByUsers
Fx.auth | 纷享销客 | 帮助中心
3、Fx.object.findById
Fx.object | 纷享销客 | 帮助中心
4、Fx.message.sendNotice
Fx.message | 纷享销客 | 帮助中心
注意:
返回值:字符串,空则表示当前页面刷新,不进行跳转;URL或者其他信息,会在新选项卡或新页面进行跳转。

return ' '

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值