慢吞云雾缓吐愁 发表于 昨天 04:43

泛微e9 OA中通过Java SDK获取金蝶云星空中的表单数据

泛微e9 OA中通过Java SDK获取金蝶云星空中的表单数据

一、开发前准备


[*]必要先在金蝶中配置金蝶体系中的第三方体系登录授权
[*]下载1.8的JavaSDK
[*]找到对应表单的接口API文档中心
二、运行情况配置

你最好是先直接跑一下金蝶sdk中的那个demo,再去集成到OA中
首先必要编辑一下配置文件,将金蝶的第三方授权登陆的信息填写过来
https://i-blog.csdnimg.cn/direct/f5cd6679d9204931be15625bd0c642da.png
上面的这些配置信息中的第一个,AcctID可以这样获取:
https://i-blog.csdnimg.cn/direct/72f5781d77514f5e869d24d9e2be025f.png
然后就是填写应用ID、密钥、用户名。这个LCID不用变,末了就把地点改成你公司的金蝶地点就可以了。必要留意的是这个配置文件必要放到项目标根目次下,在OA中则是将该文件放到ecology/classbean下才会见效。
https://i-blog.csdnimg.cn/direct/f74a0513780241a09ea9b2e774e4bf4b.png
在本地情况中必要把金蝶sdk加到lib中,在服务器上则是把这个金蝶的包放到ecology/WEB-INF/lib目次下
https://i-blog.csdnimg.cn/direct/62ebc7e188054c4ebcb3b665baa102ec.png
三、使用金蝶的SDK获取表单数据

https://i-blog.csdnimg.cn/direct/c6c6cf071c2045eb98d64448dd8923c9.png
3.1 票据查询案例解析

先看官网API使用案例:
https://i-blog.csdnimg.cn/direct/433ea6fae30b424c999042f6b9114c4b.png
使用这个api返回的值其实是数据而不是json,想要json的数据格式就用executeBillQueryJson这个方法,不过我感觉没必要,直接我下面的方式解析就可以:
    @Test
    public void getPAYORG() throws Exception {
      HashMap<Integer, String> orgNameMap = new HashMap<>();
      // 定义每页获取的数据条数,这里可根据接口最大允许值设置(例如10000)
      int limit = 10000;
      // 从第0条数据开始读取
      int startRow = 0;

      while (true) {
            JsonObject params = new JsonObject();
            params.addProperty("FormId", "ORG_Organizations");
            params.addProperty("FieldKeys", "FOrgID,FName");
            params.addProperty("StartRow", startRow);
            params.addProperty("Limit", limit);
            params.addProperty("IsSortBySeq", false);
            String jsonData = GSON.toJson(params);

            // 调用接口获取一页数据
            List<List<Object>> result = client.executeBillQuery(jsonData);
            if (result == null || result.isEmpty()) {
                // 当返回结果为空时,说明所有数据已经读取完毕,退出循环
                break;
            }
            for (List<Object> obj : result) {
                orgNameMap.put((Integer) obj.get(0), (String) obj.get(1));
            }
            startRow += result.size();
      }
      System.out.println(orgNameMap);
    }
假如你想查别的表就直接换一下params.addProperty(“FormId”, “ORG_Organizations”);当中表名就可以了,表名在api接口中都有。
https://i-blog.csdnimg.cn/direct/421929ef35274e22aa7af38a5be5ac5b.png
这个比较麻烦的问题是你不知道你要的字段的字段名到底叫做什么?
解决的办法是去检察保存的api接口
https://i-blog.csdnimg.cn/direct/6b9a2146488a4b71bc116393754754bf.png
大概是这个地方:
https://i-blog.csdnimg.cn/direct/d1b0998417d647c283b583f2ac07f3fd.png
https://i-blog.csdnimg.cn/direct/145e4ca4f7b0405083b75be4f9889617.png
你就能获得大部分的字段名了,假如涉及到多张表的那种表单,有些字段你还是不知道,还可以这样:先去登陆体系,然后找到这张表单,再按照模板引出,这样表单的字段名也会出来。
https://i-blog.csdnimg.cn/direct/da0b2e649e454c4dab5c37af28c9a6b3.png
https://i-blog.csdnimg.cn/direct/21d6c50fc35e43829a7093139daec411.png
但是还是有些字段你还是发现查不出来,你还可以通过view来先查询一条数据,这样它会返回给你该数据的所有字段的属性名和值,然后去确认这个字段叫做什么。
但是还是有些字段你还是查不出来,好比这种的情况:
https://i-blog.csdnimg.cn/direct/8f12996f8d754c02a8f7bfd6d26a6953.png
像这种就去金蝶社区的帮助中心查询相干的情况。这样你就应该可以查询金蝶api中所有表单的值了。
3.2 view接口解析

先看官网案例:
https://i-blog.csdnimg.cn/direct/2ff98c3a7972478f8393ac29b94939b1.png
只要在json中传入编码大概是id就可以返回对应数据了,你只要能区分什么是编码什么是Id就可以了。https://i-blog.csdnimg.cn/direct/3cbda76164c447128ae5f66bae32ee61.png
API中的Number就是上图的编码,而Id是指这条数据的数据库id
四、获取金蝶应付单的现实案例

金蝶表单的数据同步可以写到OA的计划任务当中,至于计划任务具体的编写方式可以参考这个:
泛微二开文档
package com.customization.suppy.cron;

import com.customization.suppy.pojo.Appayable;
import com.google.gson.*;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import org.junit.Test;
import weaver.conn.RecordSet;
import weaver.formmode.data.ModeDataIdUpdate;
import weaver.formmode.setup.ModeRightInfo;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



public class SyncAPPayableCron extends BaseCronJob {
    K3CloudApi client = new K3CloudApi();
    private static final Gson GSON = new Gson();
    HashMap<Integer, String> supplierMap = new HashMap<>();
    HashMap<Integer, String> unitNameMap = new HashMap<>();
    HashMap<Integer, String> materialMap = new HashMap<>();
    HashMap<Integer, String> orderMap = new HashMap<>();

    /**
   * 定时任务
   */
    @Override
    public void execute() {
      // SYN_PERSON_TABLE_NAME 建模表名
      String SYN_PERSON_TABLE_NAME = "uf_AP_Payable";
      // SYN_PERSON_FLOW_ID 模块id
      int SYN_PERSON_FLOW_ID = 14;

      RecordSet recordSet = new RecordSet();
      try {
            // 首先获取所有的数据
            List<Appayable> apPayableList = getAPPayableList();
            // 遍历list,通过FBillNo来判断
            for (Appayable appayable : apPayableList) {
                if (appayable.getFBillNo().equals(" "))continue;
                String sql = "select id from uf_AP_Payable where FBillNo= '"+appayable.getFBillNo()+"';";
                recordSet.executeQuery(sql);
                if (recordSet.next()) {
                  String mainid = recordSet.getString("id");
                  // 代表是重复的数据,再查询明细表,将数据插入到明细表
                  String sql_detail = "select id from uf_AP_Payable_dt1 where FMATERIALID='"+appayable.getFMATERIALID()+"'";
                  recordSet.executeQuery(sql_detail);
                  if (recordSet.next()) {
                        // 代表此物料编码已经存在了,直接跳过该条数据
                  } else {
                        // 代表需要插入明细表
                        String insertSql = "INSERT INTO uf_AP_Payable_dt1 (mainid, FBillNo, FMATERIALID, FMaterialDesc, FModel, FPRICEUNIT,FPriceQty,FTaxPrice,FALLAMOUNTFOR_D) " +
                              "VALUES (" + mainid + ", '" + appayable.getFOrderNumber() + "', '" + appayable.getFMATERIALID() + "','" + appayable.getFMaterialDesc() + "','" + appayable.getFModel() + "','" + appayable.getFPRICEUNIT() + "'," + appayable.getFPriceQty() + "," + appayable.getFTaxPrice() + "," + appayable.getFALLAMOUNTFOR_D() + ")";
                        recordSet.executeUpdate(insertSql);
                  }
                } else {
                  // 新增建模表,然后直接将所有数据更新到建模表当中。
                  // 获取现在的时间
                  LocalDate currentDate = LocalDate.now();
                  LocalTime currentTime = LocalTime.now();
                  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH-mm-ss");
                  String _currentTime = currentTime.format(formatter);
                  // 通过代码创建一条建模数据
                  int id = ModeDataIdUpdate.getInstance().getModeDataNewIdByUUID(SYN_PERSON_TABLE_NAME, SYN_PERSON_FLOW_ID, 1, 1, currentDate.toString(), _currentTime);
                  ModeRightInfo ModeRightInfo = new ModeRightInfo();
                  ModeRightInfo.setNewRight(true);
                  // 新建的时候添加共享
                  ModeRightInfo.editModeDataShare(1, SYN_PERSON_FLOW_ID, id);
                  // 新建的时候添加文档共享
                  ModeRightInfo.addDocShare(1, SYN_PERSON_FLOW_ID, id);
                  // 调整主表数据
                  String updateSql = "UPDATE uf_AP_Payable " +
                            " SET FBillNo = '" + appayable.getFBillNo() + "', " +
                            " FSUPPLIER = '" + appayable.getFSUPPLIER() + "', " +
                            " FENDDATE_H = '" + appayable.getFENDDATE_H() + "', " +
                            " FDATE = '" + appayable.getFDATE() + "', " +
                            " FEntryTaxRate = " + appayable.getFEntryTaxRate() +
                            " WHERE id = " + id;
                  recordSet.executeUpdate(updateSql);
                  // 调整明细表数据
                  String insertSql = "INSERT INTO uf_AP_Payable_dt1 (mainid, FBillNo, FMATERIALID, FMaterialDesc, FModel, FPRICEUNIT,FPriceQty,FTaxPrice,FALLAMOUNTFOR_D) " +
                            "VALUES (" + id + ", '" + appayable.getFOrderNumber() + "', '" + appayable.getFMATERIALID() + "','" + appayable.getFMaterialDesc() + "','" + appayable.getFModel() + "','" + appayable.getFPRICEUNIT() + "'," + appayable.getFPriceQty() + "," + appayable.getFTaxPrice() + "," + appayable.getFALLAMOUNTFOR_D() + ")";
                  recordSet.executeUpdate(insertSql);
                }
            }
      } catch (Exception e) {
            throw new RuntimeException(e);
      }
    }

    /**
   * 获取所有的供应商名称并且将其传入 supplierMap 当中
   */
    public void getAllSupplier() throws Exception {
      // 定义每页获取的数据条数,这里可根据接口最大允许值设置(例如10000)
      int limit = 10000;
      // 从第0条数据开始读取
      int startRow = 0;

      while (true) {
            JsonObject params = new JsonObject();
            params.addProperty("FormId", "BD_Supplier");
            params.addProperty("FieldKeys", "FSUPPLIERID,FName");
            params.addProperty("StartRow", startRow);
            params.addProperty("Limit", limit);
            params.addProperty("IsSortBySeq", false);
            String jsonData = GSON.toJson(params);

            // 调用接口获取一页数据
            List<List<Object>> result = client.executeBillQuery(jsonData);
            if (result == null || result.isEmpty()) {
                // 当返回结果为空时,说明所有数据已经读取完毕,退出循环
                break;
            }
            for (List<Object> obj : result) {
                supplierMap.put((Integer) obj.get(0), (String) obj.get(1));
            }
            startRow += result.size();
      }
    }

    /**
   * 获取所有的计量单位并且存在 unitNameMap 当中
   */
    public void getAllUnitName() throws Exception {

      // 定义每页获取的数据条数,这里可根据接口最大允许值设置(例如10000)
      int limit = 10000;
      // 从第0条数据开始读取
      int startRow = 0;

      while (true) {
            JsonObject params = new JsonObject();
            params.addProperty("FormId", "BD_UNIT");
            params.addProperty("FieldKeys", "FUNITID,FName");
            params.addProperty("StartRow", startRow);
            params.addProperty("Limit", limit);
            params.addProperty("IsSortBySeq", false);
            String jsonData = GSON.toJson(params);

            // 调用接口获取一页数据
            List<List<Object>> result = client.executeBillQuery(jsonData);
            if (result == null || result.isEmpty()) {
                // 当返回结果为空时,说明所有数据已经读取完毕,退出循环
                break;
            }
            for (List<Object> obj : result) {
                unitNameMap.put((Integer) obj.get(0), (String) obj.get(1));
            }
            startRow += result.size();
      }
    }

    /**
   * 获取所有的物料信息,然后将物料的id转成物料的编码
   */
    public void getAllMaterial() throws Exception {
      // 定义每页获取的数据条数,这里可根据接口最大允许值设置(例如10000)
      int limit = 10000;
      // 从第0条数据开始读取
      int startRow = 0;

      while (true) {
            JsonObject params = new JsonObject();
            params.addProperty("FormId", "BD_MATERIAL");
            params.addProperty("FieldKeys", "FMATERIALID,FNumber");
            params.addProperty("StartRow", startRow);
            params.addProperty("Limit", limit);
            params.addProperty("IsSortBySeq", false);
            String jsonData = GSON.toJson(params);

            // 调用接口获取一页数据
            List<List<Object>> result = client.executeBillQuery(jsonData);
            if (result == null || result.isEmpty()) {
                // 当返回结果为空时,说明所有数据已经读取完毕,退出循环
                break;
            }
            for (List<Object> obj : result) {
                materialMap.put((Integer) obj.get(0), (String) obj.get(1));
            }
            startRow += result.size();
      }
    }

    /**
   * 存采购明细内码和采购订单号
   */
    public void getAllOrderNumber() throws Exception {
      // 定义每页获取的数据条数,这里可根据接口最大允许值设置(例如10000)
      int limit = 10000;
      // 从第0条数据开始读取
      int startRow = 0;

      while (true) {
            JsonObject params = new JsonObject();
            params.addProperty("FormId", "PUR_PurchaseOrder");
            params.addProperty("FieldKeys", "FPOOrderEntry_FEntryID,FPURCHASEORDERNO");
            params.addProperty("StartRow", startRow);
            params.addProperty("Limit", limit);
            params.addProperty("IsSortBySeq", false);
            String jsonData = GSON.toJson(params);

            // 调用接口获取一页数据
            List<List<Object>> result = client.executeBillQuery(jsonData);
            if (result == null || result.isEmpty()) {
                // 当返回结果为空时,说明所有数据已经读取完毕,退出循环
                break;
            }
            for (List<Object> obj : result) {
                orderMap.put((Integer) obj.get(0), (String) obj.get(1));
            }
            startRow += result.size();
      }
    }

    /**
   * 获取金蝶中应付单的完整数据
   */
    public List<Appayable> getAPPayableList() throws Exception {
      getAllSupplier();
      getAllUnitName();
      getAllMaterial();
      getAllOrderNumber();

      K3CloudApi client = new K3CloudApi();
      Gson gson = new Gson();
      ArrayList<Appayable> appayables = new ArrayList<>();

      // 定义每页获取的数据条数,这里可根据接口最大允许值设置(例如10000)

      int limit = 10000;
      // 从第0条数据开始读取
      int startRow = 0;

      while (true) {
            // 构建请求的 JSON 对象
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("FormId", "AP_Payable");
            jsonObject.addProperty("FieldKeys", "FBillNo,FSUPPLIERID,FENDDATE_H,FDATE,FEntryTaxRate,FOrderEntryID,FMATERIALID,FMaterialDesc,FModel,FPRICEUNITID,FPriceQty,FTaxPrice,FALLAMOUNTFOR_D");
            jsonObject.addProperty("FilterString", "");
            jsonObject.addProperty("OrderString", "");
            jsonObject.addProperty("TopRowCount", 0);
            jsonObject.addProperty("StartRow", startRow);
            jsonObject.addProperty("Limit", limit);
            jsonObject.addProperty("SubSystemId", "");

            String jsonData = gson.toJson(jsonObject);

            // 调用接口获取一页数据
            List<List<Object>> result = client.executeBillQuery(jsonData);
            if (result == null || result.isEmpty()) {
                // 当返回结果为空时,说明所有数据已经读取完毕,退出循环
                break;
            }

            // 处理当前页数据
            for (List<Object> obj : result) {
                Appayable appayable = new Appayable();
                // 注意:此处的类型转换需要与实际返回的数据类型相符
                appayable.setFBillNo((String) obj.get(0));
                appayable.setFSUPPLIER(supplierMap.get((Integer) obj.get(1)));
                appayable.setFENDDATE_H(((String) obj.get(2)).substring(0,10));
                appayable.setFDATE(((String) obj.get(3)).substring(0,10));
                appayable.setFEntryTaxRate((Double) obj.get(4));
                appayable.setFOrderNumber(orderMap.get((Integer)obj.get(5)));
                appayable.setFMATERIALID(materialMap.get((Integer) obj.get(6)));
                appayable.setFMaterialDesc((String) obj.get(7));
                appayable.setFModel((String) obj.get(8));
                appayable.setFPRICEUNIT(unitNameMap.get((Integer) obj.get(9)));
                appayable.setFPriceQty((Double) obj.get(10));
                appayable.setFTaxPrice((Double) obj.get(11));
                appayable.setFALLAMOUNTFOR_D((Double) obj.get(12));
                appayables.add(appayable);
            }

            // 累加已读取的行数,准备读取下一页
            startRow += result.size();
      }
      return appayables;
    }

/*    @Test
    public void test() throws Exception {
      List<Appayable> apPayableList = getAPPayableList();
      for (Appayable appayable : apPayableList) {
            System.out.println(appayable);
      }
    }*/
}


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 泛微e9 OA中通过Java SDK获取金蝶云星空中的表单数据