金蝶Kis云代码实现

1、金蝶认证(获取到token)也就是code,获取业务接口网关和auth_data,请求头
RequestHeader类

package com.api.xic.jd.tool;
import net.sf.json.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class RequestHeader {

    //通过header头传递access_token获取数据
    public static JSONObject httpDoPost(String serverURL, JSONObject parm, Map<String, String> header) throws IOException {
        StringBuffer sbf = new StringBuffer();
        String strRead = null;
        URL url = new URL(serverURL);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");//请求post方式
        connection.setDoInput(true);
        connection.setDoOutput(true);
        //header内的的参数在这里set。||connection.setRequestProperty("健, "值");
        connection.setRequestProperty("Content-Type", "application/json");
        if (header != null) {
            Iterator it = header.entrySet().iterator();
            while(it.hasNext()) {
                Map.Entry<String, String> entry = (Map.Entry)it.next();
                connection.setRequestProperty((String)entry.getKey(), (String)entry.getValue());
            }
        }
        connection.connect();
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
        writer.write(parm.toString());
        writer.flush();
        InputStream is = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        while ((strRead = reader.readLine()) != null) {
            sbf.append(strRead);
        }
        reader.close();
        connection.disconnect();
        String results = sbf.toString();
        JSONObject jsonObject = JSONObject.fromObject(results);
        return jsonObject;
    }


    public static JSONObject getCoke(JSONObject parm) throws Exception {
        long timestamp =InterfaceToolAPI.shijianchuo();
        String urll = InvokeAPI.Sxscks("RZcode") + InvokeAPI.client_id + "&client_secret=" + InvokeAPI.client_secret;
        //通过code获取到token和session
        JSONObject jsonObject = null;
        try {
            jsonObject = httpPosyauthdata(urll,String.valueOf(timestamp), parm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject data = jsonObject.getJSONObject("data");
        return data;
    }


    //获取到token和sessiontoken和auth_data和gw_router_addr
    public static JSONObject gettoken(String acctnumber,String session_id,String access_token) throws Exception {
//        JSONObject coke = getCoke();
//        String access_token = coke.getString("access_token");//全局都要用
//        String session_id = coke.getString("session_id");
        String urls =InvokeAPI.Sxscks("YWwg") + access_token;
        JSONObject json2 = new JSONObject();
        json2.put("session_id", session_id);
        json2.put("pid", InvokeAPI.pid);
        json2.put("acctnumber", acctnumber);
        json2.put("icrmid", InvokeAPI.icrmid);
        long timestamp =InterfaceToolAPI.shijianchuo();
        JSONObject de = httpPosyauthdata(urls, String.valueOf(timestamp), json2);
        JSONObject authData = de.getJSONObject("data");
        String auth_data = authData.getString("auth_data");
        String gw_router_addr = authData.getString("gw_router_addr");
        JSONObject extend_data = authData.getJSONObject("extend_data");
        String refresh_auth_data_token = extend_data.getString("refresh_auth_data_token");
        JSONObject json= new JSONObject();
        json.put("access_token",access_token);
        json.put("auth_data",auth_data);
        json.put("gw_router_addr",gw_router_addr);
        json.put("refresh_auth_data_token",refresh_auth_data_token);
        return json;
    }

    //通用认证调用接口工具类
    public static JSONObject httpPosyauthdata(String serverURL, String shijiancuo, JSONObject parm) throws Exception {
        //headers
        Map<String, String> map = new HashMap<>();
        map.put("KIS-State", "1");
        map.put("KIS-Timestamp", shijiancuo);
        map.put("KIS-TraceID", "270962");
        JSONObject jsonObject =httpDoPost(serverURL, parm, map);
        return jsonObject;
    }

    //实际获取接口工具类
    public static JSONObject httpPosysjhq(JSONObject data,String serverURL, String shijiancuo, JSONObject parm) throws Exception {
        String auth_data = data.getString("auth_data");
        String gw_router_addr = data.getString("gw_router_addr");
        //headers
        Map<String, String> map = new HashMap<>();
        map.put("KIS-State", "1");//请求随机数
        map.put("KIS-Timestamp", shijiancuo);//时间戳
        map.put("KIS-TraceID", "270962");//客户终端请求轨迹id
        map.put("KIS-AuthData", auth_data);//
        map.put("KIS-Ver", "1.0");//接口版本号
        map.put("X-GW-Router-Addr", gw_router_addr);//
        JSONObject jsonObject = httpDoPost(serverURL, parm, map);
        return jsonObject;
    }

}

存放金蝶kis的接口

package com.api.xic.jd.tool;
import java.util.HashMap;
import java.util.Map;
//金蝶kis的接口
public class InvokeAPI {
    private static Map<String, String> map = new HashMap<String, String>();
    public final static String client_id = "270962";
    public final static String client_secret = "f4950271dc9e061b33de5d4f2ca851e7";
    public final static String pid = "16010091586cab90f3eeb764ceab5eef";//固定
    public final static String icrmid = "2c9223b083cc0f130183e4c32be01544";//固定

    static {
        //金蝶认证(获取到token)code
        map.put("RZcode","https://api.kingdee.com/koas/oauth2/access_token?client_id=");
        //获取业务接口网关和auth_data
        map.put("YWwg","https://api.kingdee.com/koas/user/get_service_gateway?access_token=");
        //销售出库单
        //批量查询销售出库单
        map.put("Sxscks","https://api.kingdee.com/koas/app007104/api/salesdelivery/list?access_token=");
        //查询销售出库单详情()
        map.put("Sxsck","https://api.kingdee.com/koas/app007104/api/salesdelivery/getdetail?access_token=");
        //新增销售出库单
        map.put("Axsck", "https://api.kingdee.com/koas/app007104/api/salesdelivery/create?access_token=");
        //修改销售出库单
        map.put("Uxsck","https://api.kingdee.com/koas/app007104/api/salesdelivery/update?access_token=");
        //删除销售出库单
        map.put("Dxsck","https://api.kingdee.com/koas/app007104/api/salesdelivery/delete?access_token=");
        //批量查询即时库存
        map.put("Sjskcs","https://api.kingdee.com/koas/APP007104/api/ICInventory/List?access_token=");
        //查询物料基础资料详情
        map.put("Swlxx","https://api.kingdee.com/koas/APP006992/api/Material/GetDetail?access_token=");
        //批量查询物料基础资料
        map.put("Swlxxs","https://api.kingdee.com/koas/APP006992/api/Material/List?access_token=");
        //获取仓库列表信息
        map.put("CKlbxx","https://api.kingdee.com/koas/APP006992/api/Stock/List?access_token=");
        //查询金蝶人员信息(工号)
        map.put("CKlbxx","https://api.kingdee.com/koas/APP006992/api/Employee/GetDetail?access_token=");
        //查询金蝶客户名称(客户id)
        map.put("KHMCXX","https://api.kingdee.com/koas/APP006992/api/Customer/GetDetail?access_token=");

    }
    public static String Sxscks(String key) throws Exception {
        String sUrl = map.get(key);
        return sUrl;
    }
}

日期时间相关类

package com.api.xic.jd.tool;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DataTool {
     //获取当天时间
     public String getNowTime(String dateformat){
         Date now = new Date();
         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
         String hehe = dateFormat.format(now);
         return hehe;
     }
     //获取两个日期之间的所有日期
     public List<LocalDate> getDatesBetween(LocalDate startDate, LocalDate endDate) {
         List<LocalDate> dates = new ArrayList<>();
         long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
         for (int i = 0; i <= numOfDaysBetween; i++) {
             LocalDate date = startDate.plusDays(i);
             dates.add(date);
         }
         return dates;
     }
}

封装金蝶相关操作类型(减少重复代码)

package com.api.xic.jd.tool;
import net.sf.json.JSONObject;
//接口查询
public class InterfaceToolAPI {
    //时间戳
    public static long shijianchuo(){
        long djcData3 = System.currentTimeMillis() / 1000;//循环时间戳
        return djcData3;
    }
    public final static long interval = 1000;
    //获取批量查询的总页数
    public  int Selectpages(int page, JSONObject data, String url) throws Exception {
        JSONObject json2 = new JSONObject();
        json2.put("CurrentPage", String.valueOf(page));//当前页
        json2.put("ItemsOfPage", "100");//一页大小
        Thread.sleep(interval);
        long shijianchuo = shijianchuo();
        JSONObject jsonObject = RequestHeader.httpPosysjhq(data,url, String.valueOf(shijianchuo), json2);
        int pages=jsonObject.getJSONObject("data").getInt("TotalPages");
        return pages;
    }
    //获取批量查询数据
    public  JSONObject SelectData(int ii,JSONObject data,String url) throws Exception {
        JSONObject json2 = new JSONObject();
        json2.put("CurrentPage",ii);//当前页
        json2.put("ItemsOfPage", "100");//一页大小
        Thread.sleep(interval);
        long shijianchuo = shijianchuo();
        JSONObject jsonObject = RequestHeader.httpPosysjhq(data,url, String.valueOf(shijianchuo), json2);
        return jsonObject;
    }
    //根据主键查询数据
    public JSONObject selectConditionId(JSONObject data,String url,int zid) throws Exception {
        JSONObject json = new JSONObject();
        json.put("ItemId",zid);//金蝶产品id
        Thread.sleep(interval);
        long shijianchuo = shijianchuo();
        JSONObject jsonObject = RequestHeader.httpPosysjhq(data,url, String.valueOf(shijianchuo), json);
        return jsonObject;
    }

}

销售出库代码

package com.api.xic.jd.jdapi;
import com.api.xic.jd.tool.DataTool;
import com.api.xic.jd.tool.InterfaceToolAPI;
import com.api.xic.jd.tool.InvokeAPI;
import com.weaver.general.TimeUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import weaver.conn.RecordSet;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
//同步金蝶销售出库的数据
public class HttpBatchQuery {
    //批量查询销售出库
    public static int wlf(JSONObject data, int page, int ztid, LocalDate startDate, LocalDate endDate) throws Exception {
        DataTool tool = new DataTool();
        List<LocalDate> datesBetween = tool.getDatesBetween(startDate, endDate);
        int jdid = 0, FItemID, Fauxqty, ii, count, j, anInt, FSupplyID, FInterID, ryid = 0, bmid=0, i, x, size, size1,departmentid = 0,FPosterID;
        String mxbsql, insertsql, formmodeid = "241", nowDate = TimeUtil.getCurrentDateString(), nowTime = TimeUtil.getOnlyCurrentTimeString(), sql, url2;
        String sub2, rq, ny, fSettleDate, localTime, sqlString, url3, rydm4, FNumber, wldm, xhmc,rydm;
        JSONObject da2, de4, json,de3;
        JSONArray jsonObj, entryjson;
        double FConsignPrice, FConsignAmount;
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        InterfaceToolAPI interfaceTool = new InterfaceToolAPI();
        String access_token = data.getString("access_token");
        String url = InvokeAPI.Sxscks("Sxscks") + access_token;
        int pages = interfaceTool.Selectpages(page, data, url);
        RecordSet rs = new RecordSet(), recordSet = new RecordSet();
        outer:
        for (i = 0; i <= pages; i++) {//根据总页数循环
            ii = i + 1;
            url2 = InvokeAPI.Sxscks("Sxscks") + access_token;
            da2 = interfaceTool.SelectData(ii, data, url2);
            jsonObj = da2.getJSONObject("data").getJSONArray("List");
            // 计数
            count = 0;
            size = jsonObj.size();
            for (j = 0; j < size; j++) {//根据循环的当前页去循环里面的页数
                UUID uuid = UUID.randomUUID();//主键
                count++;
                if (count < 100) {
                    fSettleDate = jsonObj.getJSONObject(j).getJSONObject("Head").getString("Fdate");
                    FPosterID = jsonObj.getJSONObject(j).getJSONObject("Head").getInt("FPosterID");
                    if (fSettleDate != null) {
                        sub2 = fSettleDate.substring(0, 4); // 截取
                        rq = fSettleDate.substring(0, 10); // 截取
                        ny = fSettleDate.substring(0, 7); // 截取
                        for (LocalDate localDate : datesBetween) {
                            localTime = df.format(localDate);
                            if (rq.equals(localTime)) {
                                anInt = jsonObj.getJSONObject(j).getJSONObject("Head").getInt("FEmpID");//业务员
                                FSupplyID = jsonObj.getJSONObject(j).getJSONObject("Head").getInt("FSupplyID");//购货单位
                                FInterID = jsonObj.getJSONObject(j).getJSONObject("Head").getInt("FInterID");//出庫ID
                                sqlString = "select jdid from uf_xsckbdsj where jdid=" + FInterID;
                                recordSet.execute(sqlString);
                                if (recordSet.next()) {
                                    jdid = recordSet.getInt("jdid");
                                }
                                if (jdid != FInterID) {
                                    //根据人员编号查询金蝶人员
                                    url3 = InvokeAPI.Sxscks("Swlxx") + access_token;
                                    de3 = interfaceTool.selectConditionId(data, url3, anInt);
                                    rydm = de3.getJSONObject("data").getString("FNumber");
                                    if (rydm != null) {
                                        //根据人员编号查询在oa查询人员id
                                        RecordSet res = new RecordSet();
                                        String sqlry = "select id,departmentid from HrmResource where workcode='" + rydm + "'";
                                        res.execute(sqlry);
                                        if (res.next()) {
                                            ryid = res.getInt("id");//人员id
                                            departmentid = res.getInt("departmentid");//部门id
                                        }
                                    }
                                    //根据客户id查询金蝶客户名称
                                    url3 = InvokeAPI.Sxscks("KHMCXX") + access_token;
                                    de4 = interfaceTool.selectConditionId(data, url3, FSupplyID);
                                    rydm4 = de4.getJSONObject("data").getString("FName");//客户名称
                                    FNumber = de4.getJSONObject("data").getString("FNumber");//客户代码
                                    if (FNumber.equals("XIC0001") || FNumber.equals("XIC0002") || FNumber.equals("XIC0003")) {
                                        continue;
                                    } else if (!FNumber.equals("XIC0001") || !FNumber.equals("XIC0002") || !FNumber.equals("XIC0003")) {    //客户不等于芯车和新富强
                                        try {
                                            insertsql = String.format("insert into uf_xsckbdsj" +
                                                    "(ztmc,xsry,xsbm,khmc,khdm,xsckrq,jdid,xsny,formmodeid,modedatacreater,modedatacreatertype," +
                                                    "modedatacreatedate,modedatacreatetime,MODEUUID)" +
                                                    "VALUES" +
                                                    "('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", ztid, ryid, departmentid, rydm4, FNumber, rq, FInterID,ny,formmodeid, 1, 0, nowDate, nowTime, uuid);
                                            rs.execute(insertsql);
                                           // System.out.println(insertsql);
                                        } catch (Exception e) {
                                            //System.out.println("主表插入失败");
                                        }
                                        //产品循环
                                        entryjson = jsonObj.getJSONObject(j).getJSONArray("Entry");
                                        size1 = entryjson.size();
                                        for (x = 0; x < size1; x++) {
                                            FItemID = entryjson.getJSONObject(x).getInt("FItemID");//产品代码
                                            //根据产品id查询产品名称
                                            url3 = InvokeAPI.Sxscks("Swlxx") + access_token;
                                            json = interfaceTool.selectConditionId(data, url3, FItemID);
                                            wldm = json.getJSONObject("data").getString("FNumber");//物料代码
                                            xhmc = json.getJSONObject("data").getString("FName");//型号名称
                                            FConsignPrice = entryjson.getJSONObject(x).getDouble("FConsignPrice");//销售单价
                                            FConsignAmount = entryjson.getJSONObject(x).getDouble("FConsignAmount");//销售金额
                                            Fauxqty = entryjson.getJSONObject(x).getInt("Fauxqty");//实发数量
                                            if(FConsignAmount<0){
                                                FConsignAmount=0.00;
                                            }
                                            //插入明细表
                                            try {
                                                //插入完成查询最大id
                                                sql = "select max(id) as id from uf_xsckbdsj";
                                                rs.execute(sql);
                                                int zuihouId = 0;
                                                if (rs.next()) {
                                                    zuihouId = rs.getInt("id");
                                                }
                                                mxbsql = String.format("insert into uf_xsckbdsj_dt1" +
                                                        "(xhmc,wldm,xssl,xsdj,xsjefds,mainid)" +
                                                        "VALUES" +
                                                        "('%s','%s','%s','%s','%s','%s')", xhmc, wldm, Fauxqty, FConsignPrice, FConsignAmount, zuihouId);
                                                rs.execute(mxbsql);
                                               // System.out.println(mxbsql);

                                            } catch (Exception e) {
                                               // System.out.println("插入明细表出错");
                                            }
                                        }

                                    }
                                }
                            }
                        }
                    }
                } else {
                    continue outer;
                }
            }
            continue;
        }
        return pages;
    }
}

同步金蝶库存的数据

package com.api.xic.jd.jdapi;

import com.api.xic.jd.tool.InterfaceToolAPI;
import com.api.xic.jd.tool.InvokeAPI;
import com.weaver.general.TimeUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import weaver.conn.RecordSet;
import java.util.*;
//同步金蝶库存的数据
public class HttpHDInventory {

    //同步金蝶库存的数据
    public int wlf(JSONObject data, int page, int ztmczs) throws Exception {

        String access_token = data.getString("access_token");//全局都要用
        InterfaceToolAPI interfaceTool = new InterfaceToolAPI();
        RecordSet rs = new RecordSet(),rs2 = new RecordSet(),res = new RecordSet();
        String url = InvokeAPI.Sxscks("Sjskcs") + access_token;
        int pages = interfaceTool.Selectpages(page, data, url);
        int ckid = 0, ii, i, count = 0, j, FMaterialID, FBUQty, FStockID, size;
        String formmodeid = "238", url2, sql2, url3, wldm, xhmc, sqlry, wudm = null, mxbsql;
        JSONObject da2, jsonObject;
        JSONArray jsonObj;
        String nowDate = TimeUtil.getCurrentDateString(),nowTime = TimeUtil.getOnlyCurrentTimeString();
        List<Map<String, Object>> list = new ArrayList<>(),list2 = new ArrayList<>();
        Map<String, Object> map,map2;
        outer:
        for (i = 0; i <= pages; i++) {//根据总页数循环
            ii = i + 1;
            url2 = InvokeAPI.Sxscks("Sjskcs") + access_token;
            da2 = interfaceTool.SelectData(ii, data, url2);
            jsonObj = da2.getJSONObject("data").getJSONArray("List");
            size = jsonObj.size();
            for (j = 0; j < size; j++) {//根据循环的当前页去循环里面的页数
                count++;
                if (count < 100) {
                    map2=new HashMap<>();
                    FMaterialID = jsonObj.getJSONObject(j).getInt("FMaterialID");//物料id
                    FBUQty = jsonObj.getJSONObject(j).getInt("FBUQty");//基本单位数量
                    FStockID = jsonObj.getJSONObject(j).getInt("FStockID");//仓库内码
                    map2.put("FMaterialID",FMaterialID);
                    map2.put("FBUQty",FBUQty);
                    map2.put("FStockID",FStockID);
                    list2.add(map2);
                }else {
                    continue outer;
                }
            }
            continue;
        }
        for (Map<String, Object> bean : list2) {
            FMaterialID =(int) bean.get("FMaterialID");
            FStockID =(int) bean.get("FStockID");
            FBUQty =(int) bean.get("FBUQty");
            //根据产品id查询产品名称
            url3 = InvokeAPI.Sxscks("Swlxx") + access_token;
            jsonObject = interfaceTool.selectConditionId(data, url3, FMaterialID);
            wldm = jsonObject.getJSONObject("data").getString("FNumber");//物料代码
            xhmc = jsonObject.getJSONObject("data").getString("FName");//型号名称
            //根据仓库内码id查询仓库
            sql2 = "select id from uf_ck where cknmjdid=" + FStockID;
            rs2.execute(sql2);
            if (rs2.next()) {
                ckid = rs2.getInt("id");
            }
            if (!wldm.equals("") || wldm != "") {
                //根据物料代码去查询oa的物料数据
                sqlry = "select wldm,id from uf_wlkcsj where wldm='" + wldm + "'";
                res.execute(sqlry);
                if (res.next()) {
                    wudm = res.getString("wldm");//物料代码
                    //wlid=res.getInt("id");//物料代码
                }
                if (!wldm.equals(wudm)) {
                    //插入明细表
                    try {
                        map = new HashMap<>();
                        map.put("wldm", wldm);
                        map.put("FBUQty", FBUQty);
                        map.put("xhmc", xhmc);
                        map.put("ckid", ckid);
                        map.put("nowDate", nowDate);
                        map.put("nowTime", nowTime);
                        list.add(map);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }


        }
        for (Map<String, Object> bean : list) {
            FBUQty = (int) bean.get("FBUQty");
            wldm = (String) bean.get("wldm");
            xhmc = (String) bean.get("xhmc");
            ckid = (int) bean.get("ckid");
            nowDate = (String) bean.get("nowDate");
            nowTime = (String) bean.get("nowTime");
            UUID uuid = UUID.randomUUID();//主键

            mxbsql = String.format("insert into uf_wlkcsj" +
                    "(ztmc,wldm,kcsl,cpxh,ck,formmodeid,modedatacreater,modedatacreatertype," +
                    "modedatacreatedate,modedatacreatetime,MODEUUID)" +
                    "VALUES" +
                    "('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", ztmczs, wldm, FBUQty, xhmc, ckid, formmodeid, 1, 0, nowDate, nowTime, uuid);
            rs.execute(mxbsql);
            //System.out.println(mxbsql);
        }
        return pages;
    }
}

同步更新金蝶物料的数据

package com.api.xic.jd.jdapi;

import com.api.xic.jd.tool.InterfaceToolAPI;
import com.api.xic.jd.tool.InvokeAPI;
import com.weaver.general.TimeUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import weaver.conn.RecordSet;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import java.text.NumberFormat;
import java.util.*;
//同步更新金蝶物料的数据
public class HttpJDmaterial {
    private Logger log = LoggerFactory.getLogger(getClass());
    //同步金蝶物料的数据
    public int wlf(JSONObject data, int page, int ztmczs) throws Exception {
        String access_token = data.getString("access_token");//全局都要用
        InterfaceToolAPI interfaceTool = new InterfaceToolAPI();
        RecordSet rs = new RecordSet(), res = new RecordSet();
        String url = InvokeAPI.Sxscks("Swlxxs") + access_token;
        int pages = interfaceTool.Selectpages(page, data, url);
        String str = null, xjjs, yj30s, yj90s, yj60s, nowTime = TimeUtil.getOnlyCurrentTimeString(), nowDate = TimeUtil.getCurrentDateString(), formmodeid = "54", url2, sqlry;
        String FNumber, wudm = null, fName, FFullName, url3, FOrderPrice, sqlupdate, mxbsql;
        int i, ii, j, count, FItemID;
        JSONObject da2, jsonObject;
        JSONArray data1;
        double FOrderPrice2, xjj = 0.0000, yj30 = 0.0000, yj60 = 0.0000, yj90 = 0.0000;
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(4); // 设置最大小数位数为4
        outer:
        for (i = 0; i < pages; i++) {//根据总页数循环
            ii = i + 1;
            url2 = InvokeAPI.Sxscks("Swlxxs") + access_token;
            da2 = interfaceTool.SelectData(ii, data, url2);
            data1 = da2.getJSONObject("data").getJSONArray("List");
            // 计数
            count = 0;
            for (j = 0; j < data1.size(); j++) {//根据循环的当前页去循环里面的页数
                UUID uuid = UUID.randomUUID();//主键
                count++;
                if (count < 100) {
                    FItemID = data1.getJSONObject(j).getInt("FItemID");//物料id
                    FNumber = data1.getJSONObject(j).getString("FNumber");//物料id
                    //根据产品id查询产品名称
                    if (!FNumber.equals("") || FNumber != "") {
                        //根据物料代码去查询oa的物料数据
                        sqlry = "select wldm from uf_xhcjbxx where wldm='" + FNumber + "'";
                        res.execute(sqlry);
                        if (res.next()) {
                            wudm = res.getString("wldm");//物料代码
                        }
                        url3 = InvokeAPI.Sxscks("Swlxx") + access_token;
                        jsonObject = interfaceTool.selectConditionId(data, url3, FItemID);
                        fName = jsonObject.getJSONObject("data").getString("FName");//产品型号名称
                        FFullName = jsonObject.getJSONObject("data").getString("FFullName");//物料全名
                        FOrderPrice = jsonObject.getJSONObject("data").getString("FOrderPrice");//采购单价
                        boolean contains = FFullName.contains("_");
                        if (contains) {
                            str = FFullName.substring(0, FFullName.lastIndexOf("_"));
                        } else if (contains == false) {
                            str = FFullName;
                        }
                        if (FOrderPrice.equals("null")) {
                            if (!FNumber.equals(wudm)) {
                                mxbsql = String.format("insert into uf_xhcjbxx" +
                                                "(sccj,ppmc,cpxh,cgj,wldm,xjj,yj30t,yj60t,yj90t,ztmc,formmodeid,modedatacreater,modedatacreatertype," +
                                                "modedatacreatedate,modedatacreatetime,MODEUUID)" +
                                                "VALUES" +
                                                "('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
                                        str, str, fName, FOrderPrice, FNumber, xjj, yj30, yj60, yj90, ztmczs, formmodeid, 1, 0, nowDate, nowTime, uuid);
                                rs.execute(mxbsql);
                                log.info(mxbsql);
                            }
                        } else if (!FOrderPrice.equals("null")) {
                            FOrderPrice2 = Double.valueOf(FOrderPrice);
                            xjj = FOrderPrice2 / 0.95;//销售单价
                            yj30 = FOrderPrice2 / 0.92;
                            yj60 = FOrderPrice2 / 0.90;
                            yj90 = FOrderPrice2 / 0.85;
                            // 格式化并输出
                            xjjs = nf.format(xjj);
                            yj30s = nf.format(yj30);
                            yj60s = nf.format(yj60);
                            yj90s = nf.format(yj90);
                            if (FNumber.equals(wudm)) {//相等修改采购价
                                sqlupdate = "update uf_xhcjbxx set cgj=" + FOrderPrice2 + ",xjj=" + xjjs + ",yj30t=" + yj30s + ",yj60t=" + yj60s + ",yj90t=" + yj90s + ",ztmc=0 where wldm='" + wudm + "'";
                                rs.execute(sqlupdate);
                                log.info(sqlupdate);
                                // System.out.println(sqlupdate);
                            } else if (!FNumber.equals(wudm)) {
                                mxbsql = String.format("insert into uf_xhcjbxx" +
                                                "(sccj,ppmc,cpxh,cgj,wldm,xjj,yj30t,yj60t,yj90t,ztmc,formmodeid,modedatacreater,modedatacreatertype," +
                                                "modedatacreatedate,modedatacreatetime,MODEUUID)" +
                                                "VALUES" +
                                                "('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
                                        str, str, fName, FOrderPrice2, FNumber, xjjs, yj30s, yj60s, yj90s, ztmczs, formmodeid, 1, 0, nowDate, nowTime, uuid);
                                rs.execute(mxbsql);
                                log.info(mxbsql);
                            }
                        }
                    }
                } else {
                    continue outer;
                }
            }
            continue;
        }
        return pages;
    }
}

仓库数据同步

package com.api.xic.jd.jdapi;

import com.api.xic.jd.tool.InterfaceToolAPI;
import com.api.xic.jd.tool.InvokeAPI;
import com.engine.board.web.DashboardAction;
import com.weaver.general.TimeUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.opensaml.xml.signature.J;
import weaver.conn.RecordSet;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.util.Iterator;
import java.util.UUID;

//仓库数据同步
public class WarehouseApi {

    private Logger log = LoggerFactory.getLogger(getClass());

    //同步金蝶仓库的数据
    public int wlf(JSONObject data, int page, int ztmczs) throws Exception {

        String access_token = data.getString("access_token");//全局都要用
        InterfaceToolAPI interfaceTool = new InterfaceToolAPI();
        RecordSet rs = new RecordSet(), res = new RecordSet();
        String url = InvokeAPI.Sxscks("CKlbxx") + access_token;
        JSONObject da2;
        JSONArray jsonObj;
        int i = 0, ii, count, j, FItemID, cknmjdid = 0;
        int pages = interfaceTool.Selectpages(page, data, url);
        //泛微建模标准字段
        String formmodeid = "263", url2, FName, FNumber, sqlry, mxbsql;//模块id
        String nowDate = TimeUtil.getCurrentDateString();
        String nowTime = TimeUtil.getOnlyCurrentTimeString();
        outer:
        for (i = 0; i <= pages; i++) {//根据总页数循环
            ii = i + 1;
            url2 = InvokeAPI.Sxscks("CKlbxx") + access_token;
            da2 = interfaceTool.SelectData(ii, data, url2);
            jsonObj = da2.getJSONObject("data").getJSONArray("List");
            count = 0;
            UUID uuid = UUID.randomUUID();//主键
            for (j = 0; j < jsonObj.size(); j++) {//根据循环的当前页去循环里面的页数
                count++;
                if (count < 100) {
                    FName = jsonObj.getJSONObject(j).getString("FName");//仓库名称
                    FNumber = jsonObj.getJSONObject(j).getString("FNumber");//	仓库代码
                    FItemID = jsonObj.getJSONObject(j).getInt("FItemID");//仓库内码
                    if (FItemID != 0) {
                        //根据物料代码去查询oa的物料数据
                        sqlry = "select cknmjdid from uf_ck where cknmjdid=" + FItemID;
                        res.execute(sqlry);
                        if (res.next()) {
                            cknmjdid = res.getInt("cknmjdid");//仓库内码
                        }
                        if (FItemID != cknmjdid) {
                            try {
                                mxbsql = String.format("insert into uf_ck" +
                                        "(ckmc,ckdm,cknmjdid,formmodeid,modedatacreater,modedatacreatertype," +
                                        "modedatacreatedate,modedatacreatetime,MODEUUID)" +
                                        "VALUES" +
                                        "('%s','%s','%s','%s','%s','%s','%s','%s','%s')", FName, FNumber, FItemID, formmodeid, 1, 0, nowDate, nowTime, uuid);
                                rs.execute(mxbsql);
                                log.info(mxbsql);
                            } catch (Exception e) {
                                log.info("插入失败");
                            }
                        }
                    }
                } else {
                    continue outer;
                }
            }
            continue;
        }
        return pages;
    }

}

执行流程

package com.api.xic.jd.action;

import com.api.xic.jd.jdapi.*;

import com.api.xic.jd.tool.RequestHeader;
import com.oneWorld.ecology.workflow.utils.ActionUtils;
import lombok.SneakyThrows;
import net.sf.json.JSONObject;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

//同步数据流程
public class SalesoutboundAction2 implements Action {

    @SneakyThrows
    @Override
    public String execute(RequestInfo requestInfo) {
        String retVal = SUCCESS;//返回流程打印
        //主表字段
        ActionUtils actionUtils = new ActionUtils();
        Map<String, String> mapMain = actionUtils.getMainData(requestInfo);
        String czlx = actionUtils.getMapValue(mapMain, "czsjlx");//操作类型
        String code = actionUtils.getMapValue(mapMain, "code");//code
        String zt = actionUtils.getMapValue(mapMain, "zt");//code
        String access_token ,ztid,session_id,startDate,endDate;
        startDate = actionUtils.getMapValue(mapMain, "ksrq");//开始日期
        endDate = actionUtils.getMapValue(mapMain, "jsrq");//结束日期
        RecordSet recordSet = new RecordSet();
        String sql = "select * from uf_ztxx where id in (" + zt + ")";
        recordSet.execute(sql);
        int ztmczs = 0,lx,wlf,i;
        JSONObject json = new JSONObject(),data,jsonObject5;
        json.put("code", code);
        JSONObject coke = RequestHeader.getCoke(json);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate endDate2,startDate2;
        WarehouseApi warehouseApi = new WarehouseApi();
        HttpHDInventory2 httpHDInventory = new HttpHDInventory2();
        HttpJDmaterial httpJDmaterial = new HttpJDmaterial();
        String[] czlxs=czlx.split(",");
        while (recordSet.next()) {
            ztid = recordSet.getString("ztid");
            ztmczs = recordSet.getInt("ztmczs");
            if (coke != null && !coke.isNullObject()) {
                access_token = coke.getString("access_token");//全局都要用
                session_id = coke.getString("session_id");
                for(i=0;i<czlxs.length;i++){
                    lx = Integer.valueOf(czlxs[i]);
                    switch(lx){
                        case 0:
                            endDate2 = LocalDate.parse(endDate, df);
                            startDate2 = LocalDate.parse(startDate, df);
                            data = RequestHeader.gettoken(ztid, session_id, access_token);//获取到token和sessiontoken和auth_data
                            // System.out.println("數量" + i);
                            //HttpBatchQuery httpBatchQuery=new HttpBatchQuery();
                            HttpBatchQuery.wlf(data, 1, ztmczs, startDate2, endDate2);//调用批查询销售出库的接口
                            //System.out.println("销售出库" + jsonObject5);
                            break;
                        case 1:
                            //同步更新金蝶物料的数据
                            data = RequestHeader.gettoken(ztid, session_id, access_token);//获取到token和sessiontoken和auth_data
                            httpJDmaterial.wlf(data, 1,ztmczs);
                            break;
                        case 2:
                            //同步金蝶库存的数据
                            data = RequestHeader.gettoken(ztid, session_id, access_token);//获取到token和sessiontoken和auth_data
                            httpHDInventory.wlf(data, 1,ztmczs);
                            break;
                        case 3:
                            //同步金蝶仓库的数据
                            data = RequestHeader.gettoken(ztid, session_id, access_token);//获取到token和sessiontoken和auth_data
                            warehouseApi.wlf(data, 1,ztmczs);
                            break;
                    }
                }

            }
        }
        return retVal;
    }
}

执行建模

package com.api.xic.jd.action;

import com.api.xic.jd.jdapi.HttpBatchQuery;
import com.api.xic.jd.jdapi.HttpHDInventory2;
import com.api.xic.jd.jdapi.HttpJDmaterial;
import com.api.xic.jd.jdapi.WarehouseApi;
import com.api.xic.jd.tool.RequestHeader;
import com.oneWorld.ecology.workflow.utils.ActionUtils;
import net.sf.json.JSONObject;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCode;
import weaver.soa.workflow.request.RequestInfo;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class SalesoutboundExpand extends AbstractModeExpandJavaCode {
    @Override
    public void doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                //获取到台账里面的数据
                if (requestInfo != null) {
                    //主表字段
                    ActionUtils actionUtils = new ActionUtils();
                    Map<String, String> mapMain = actionUtils.getMainData(requestInfo);
                    String czlx = actionUtils.getMapValue(mapMain, "czsjlx");//操作类型
                    String code = actionUtils.getMapValue(mapMain, "code");//code
                    String zt = actionUtils.getMapValue(mapMain, "zt");//code
                    String access_token, ztid, session_id, startDate, endDate;
                    startDate = actionUtils.getMapValue(mapMain, "ksrq");//开始日期
                    endDate = actionUtils.getMapValue(mapMain, "jsrq");//结束日期
                    RecordSet recordSet = new RecordSet();
                    String sql = "select * from uf_ztxx where id in (" + zt + ")";
                    recordSet.execute(sql);
                    int ztmczs, lx,i;
                    JSONObject json = new JSONObject(), data, jsonObject5;
                    json.put("code", code);
                    JSONObject coke = RequestHeader.getCoke(json);
                    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    LocalDate endDate2, startDate2;
                    long timestamp;
                    WarehouseApi warehouseApi = new WarehouseApi();
                    HttpHDInventory2 httpHDInventory = new HttpHDInventory2();
                    HttpJDmaterial httpJDmaterial = new HttpJDmaterial();
                    String[] czlxs = czlx.split(",");
                    while (recordSet.next()) {
                        ztid = recordSet.getString("ztid");
                        ztmczs = recordSet.getInt("ztmczs");
                        if (coke != null && !coke.isNullObject()) {
                            access_token = coke.getString("access_token");//全局都要用
                            session_id = coke.getString("session_id");
                            for (i = 0; i < czlxs.length; i++) {
                                lx = Integer.valueOf(czlxs[i]);
                                switch (lx) {
                                    case 0:
                                        endDate2 = LocalDate.parse(endDate, df);
                                        startDate2 = LocalDate.parse(startDate, df);
                                        data = RequestHeader.gettoken(ztid, session_id, access_token);//获取到token和sessiontoken和auth_data
                                        // System.out.println("數量" + i);
                                        HttpBatchQuery.wlf(data, 1, ztmczs, startDate2, endDate2);//调用批查询销售出库的接口
                                        //System.out.println("销售出库" + jsonObject5);
                                        break;
                                    case 1:
                                        //同步更新金蝶物料的数据
                                        data = RequestHeader.gettoken(ztid, session_id, access_token);//获取到token和sessiontoken和auth_data
                                        httpJDmaterial.wlf(data, 1, ztmczs);
                                        break;
                                    case 2:
                                        //同步金蝶库存的数据
                                        data = RequestHeader.gettoken(ztid, session_id, access_token);//获取到token和sessiontoken和auth_data
                                        httpHDInventory.wlf(data, 1, ztmczs);
                                        break;
                                    case 3:
                                        //同步金蝶仓库的数据
                                        data = RequestHeader.gettoken(ztid, session_id, access_token);//获取到token和sessiontoken和auth_data
                                        warehouseApi.wlf(data, 1, ztmczs);
                                        break;
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }

    }
}


posted @ 2024-05-10 19:55  LL爱吃香菜  阅读(38)  评论(0编辑  收藏  举报