Java接口说明

package fx.custom.apl.example.import_;

import com.fxiaoke.functions.FunctionContext;
import com.fxiaoke.functions.Fx;
import com.fxiaoke.functions.client.DebugHelper;
import com.fxiaoke.functions.interfaces.Cache;
import com.fxiaoke.functions.model.ValidateResult;
import com.fxiaoke.functions.template.ImportAction;
import com.fxiaoke.functions.utils.Lists;
import com.fxiaoke.functions.utils.Maps;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 导入前验证APL代码使用样例
 * 导入前验证APL代码使用ValidateResult进行验证,并对字段进行赋值
 */
public class ImportExample implements ImportAction {
    /**
     * 导入函数的运行方法
     */
    @Override
    public ValidateResult execute(FunctionContext context, Map<String, Object> args) {
        // 1、导入前验证函数和新建编辑的前验证函数类似,可以提示验证信息,同时支持阻断;
        // 2、若数据导入失败,或校验出现错误,错误信息会回填到Excel失败列表中
        // 3、返回值类型为validateResult

        String taskId = String.valueOf(context.getTask().get("taskId"));
        Fx.log.info(taskId);// 获取导入的任务id
        Fx.log.info(context.getTask().get("lastBatch")); //是否为最后一批数据

        // 导入预处理时数据是分批执行的,一批20条,放在context.dataList中
        List<Map> dataList = context.getDataList();
        // 在 cache 中缓存信息,供前验证函数使用
        Cache cache = Fx.getCache().getDefaultCache();
        dataList.forEach(data -> {
            String rowNo = (String) data.get("_RowNo");
            String type = (String) data.get("field_7xTmu__c");
            String key = taskId + "_" + rowNo;
            Fx.log.info(key);
            String value = "" + type;
            cache.put(key, value, 30);
        });

        ValidateResult validate = ValidateResult.builder()
                .success(true) //校验是否成功
//                .errorMessage("错误信息") //校验失败时的错误提示
                .build();
        return validate;
    }


    public static void main(String[] args) {
        //调试器
        DebugHelper helper = new DebugHelper();
        helper.init();
        Map<String, Object> param = new HashMap<>();

        Map task = Maps.of("taskId", "63feefd6b605270001f00d8a",
                "lastBatch", true,
                "unionImport", false,
                "fileCode", Maps.of("md5", "5590dcf932975951bcaf672012f351b9",
                        "sha256", "20+1cPpYRmdReP5loKz2VOZ3nI37m6N4aeNWP85rDHk=")
        );

        List dataList = Lists.newArrayList();
        Map data = new HashMap();
        data.put("name", "2");
        data.put("field_7xTmu__c", "1");
        data.put("tenant_id", "83740");
        data.put("data_own_department", Lists.newArrayList("1000"));
        data.put("object_describe_api_name", "object_25w17__c");
        data.put("_RowNo", "2");
        data.put("_TaskId", "63fef209b605270001f00dda");
        data.put("_id", "63feeeea12a03e00012d1d55");
        dataList.add(data);

        FunctionContext context = helper.importContext(dataList, task);
        ValidateResult execute = new ImportExample().execute(context, param);
        Fx.log.info(execute);
    }
}





2024-10-22
0 0