自定义Groovy脚本开发范例

说明:在平台配置开发中,如果平台原提供的脚本无法满足业务需要,可根据具体业务情况自定义开发符合自身业务需要的脚本。比如:流程分支网关中需要根据扩展属性值配置走不同分支,这时需要自己开发一个获取扩展属性值的脚本方法!

温馨提示:规范开发

1、后端java方法编写

/ibps-excessive-platform/src/main/java/com/lc/ibps/platform/script/script下新增一个jave类,比如:MyScript.java
需要注意的是,该类需要继承BaseScriptIScript

详细代码如下:

package com.lc.ibps.platform.script.script;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.lc.ibps.api.org.constant.PartyType;
import com.lc.ibps.base.core.util.BeanUtils;
import com.lc.ibps.base.core.util.string.StringUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.org.api.IPartyAttrService;
import com.lc.ibps.org.party.persistence.entity.PartyAttrPo;
import com.lc.ibps.org.party.persistence.entity.PartyAttrValuePo;

/**
 * 自定义开发脚本,可直接在Groovy脚本中调用。
 *
 */
 @Service
public class MyScript extends BaseScript {

    @Resource
    private  IPartyAttrService partyAttrService ;

    /**
     * 获取当前用户员工扩展属性值
     * @param attrKey 具体的扩展属性key
     * <pre>
     *    脚本中使用方法: myScript.getCurrentEmployeeAttrVal("securityLevel");
     * </pre>
     * @return
     */
    public String getCurrentEmployeeAttrVal(String attrKey) {
        if (BeanUtils.isEmpty(currentContext.getCurrentUserId())){
            return "";
        }
        return getAttrVal(currentContext.getCurrentUserId(),PartyType.EMPLOYEE.getValue(),attrKey);
    }

    /**
     * 获取员工扩展属性值
     * @param id
     * @param attrKey
     * <pre>
     *    脚本中使用方法: myScript.getEmployeeAttrVal(userId,"securityLevel");
     * </pre>
     * @return
     */
    public String getEmployeeAttrVal(String id,String attrKey) {
        if (BeanUtils.isEmpty(currentContext.getCurrentUserId())){
            return "";
        }
        return getAttrVal(id,PartyType.EMPLOYEE.getValue(),attrKey);
    }

    /**
     * 获取扩展属性值
     * 
     * @param id 
     * @param partyType  employee-员工,org-机构,position-岗位,role-角色
     * @param attrKey 具体的扩展属性key
     * <pre>
     *    脚本中使用方法: cscript.getCurrentAttrVal(userId,"employee","securityLevel");
     * </pre>
     * 
     * @return
     */
    public String getAttrVal(String id,String partyType,String attrKey) {
        if(StringUtil.isBlank(id) ||StringUtil.isBlank(partyType) || StringUtil.isBlank(attrKey)) {
            return "";
        }

        //根据用户ID查询扩展属性
        APIResult<List<PartyAttrPo>> apiResult = partyAttrService.findByPartyTypeUserId4Get(partyType, id);
        if (apiResult.isFailed() || BeanUtils.isEmpty(apiResult.getData())) {
            return "";
        }

        List<PartyAttrPo> partyAttrPoList = apiResult.getData();
        for (PartyAttrPo partyAttrPo : partyAttrPoList) {
            if (!attrKey.equals(partyAttrPo.getKey())) {
                continue;
            }
            List<PartyAttrValuePo> values = partyAttrPo.getValues();
            if (BeanUtils.isEmpty(values)) {
                return "";
            }
            return values.get(0).getValue();
        }
        return "";
    }
}

2、在平台配置中使用该脚本

例如在流程配置分支网关中使用该脚本:

myScript.getEmployeeAttrVal(startUser,"securityLevel").equals("三级")

文档更新时间: 2023-06-13 08:35   作者:admin