package com.yem;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yem.wm.utils.RequestCmmp;
import com.yem.wm.utils.RequestEAS;
import com.yem.wm.utils.YunzhijiaUtils;
import kd.bos.algo.DataSet;
import kd.bos.algo.Row;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.exception.KDBizException;
import kd.bos.imageplatform.axis.EASLoginProxyServiceStub;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.DispatchServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.workflow.engine.msg.util.yzj.YunzhijiaCommonUtil;
import org.apache.axiom.mime.MultipartBody;
import org.apache.flink.runtime.rest.messages.RequestBody;


import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import java.io.*;
import java.rmi.RemoteException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.*;

public class Test extends AbstractOperationServicePlugIn {
    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        try {
            String openId = YunzhijiaUtils.getOpenId("13012959851");
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchAlgorithmException ex) {
            throw new RuntimeException(ex);
        } catch (KeyManagementException ex) {
            throw new RuntimeException(ex);
        }
        /**
         * CMMP测试
         */

//        String sql = "select mater_code from VIEW_MATER_PRO_WM where mater_code = '60122A00000100'";
//            long l = 1736174630718146560l;
//        List<Map> fromCmmp =  RequestCmmp.getFromCmmp(l, sql);
        //获取调拨价格
//        String cnNumber = "eas";
//        String materialNum = "05020236";
//        String inNum = "01016000";
//        String outNum = "01010104";
//        java.sql.Date date = new java.sql.Date(new Date().getTime());
////        String sql = "SELECT FID, FNUMBER, FNAME, FCREATETIME FROM T_ISC_DEMO_BASEDATA_1@ISCB WHERE FNUMBER = ? AND FNAME = ?";
//        String sql = "select h.fnumber,e.FPRICE,o.FNUMBER inorg, o1.fnumber outorg,m.FNUMBER mnumber,h.FPRICECOMPOSITIONPRIORITY from T_SCM_PricePolicyEntry e \n" +
//                "LEFT OUTER JOIN T_BD_MATERIAL m on e.FMATERIALID  = m.fid \n" +
//                "LEFT OUTER JOIN T_BD_CUSTOMER c ON E.FCUSTOMERID = C.FID \n" +
//                "left join t_org_baseunit o on c.FINTERNALCOMPANYID = o.fid \n" +
//                "LEFT JOIN T_SCM_PricePolicy h ON E.FPARENTID = H.FID\n" +
//                "left join t_org_baseunit o1 on h.FSALEORGUNITID =o1.fid\n" +
//                "where  h.FBLOCKEDSTATUS = 1 and h.FCHECKEDSTATUS = 2 \n" +
//                "and m.FNUMBER  = ? and o.fnumber = ? and o1.fnumber = ?\n" +
////                "and e.FEFFECTIVEDATE <=to_date(?,'yyyy-MM-dd hh24:mi:ss') and e.FEXPIREDATE>=to_date(?,'yyyy-MM-dd hh24:mi:ss') \n" +
//                "and e.FEFFECTIVEDATE <=? and e.FEXPIREDATE>=? \n" +
//                "order by h.FPRICECOMPOSITIONPRIORITY,e.FEFFECTIVEDATE";
//        List<Object> values = Arrays.asList(materialNum,inNum,outNum,date,date);
//        List<Integer> types = Arrays.asList(Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.DATE, Types.DATE);
//        List<Map<String, Object>> list = DispatchServiceHelper.invokeBizService(
//                "isc", "iscb", "ISCDataCopyService", "executeQuerySQL", new Object[] { cnNumber, sql, values, types, 100});
//        System.out.println();
        /**
         * 云之家测试
         */
        /*
        String s = "";
        try {
            s = YunzhijiaUtils.doPost(YunzhijiaUtils.yzjurl+"/cmmpapi/cmmp/viewFormDef.do?tokenId=926F10AE05A411EEA331BBC15146DBF5",
                    "{\"formCodeId\":\"1c9ca25060f94e9c998e78f8efe129f5\"}");
        } catch (Exception ex) {
            throw new KDBizException("接口调用异常");
        }
        JSONObject json = JSONObject.parseObject(s);
        Boolean success = json.getBoolean("success");
        if (success) {
            JSONObject data = json.getJSONObject("data");
            if (data != null) {
                JSONObject formInfo = data.getJSONObject("formInfo");
                if (formInfo != null) {
                    JSONObject widgetMap = formInfo.getJSONObject("widgetMap");//表头字段
                    for (String headKey : widgetMap.keySet()) {//字段标识
                        JSONObject fieldJson = widgetMap.getJSONObject(headKey);//字段对象
                        String title = fieldJson.getString("title");//标题
                        String type = fieldJson.getString("type");//类型
                        System.out.println(headKey + " " + title + " " + type);
                    }
                    JSONObject detailMap = formInfo.getJSONObject("detailMap");//分录字段
                    for (String detailKey : detailMap.keySet()) {//detailKey分录标识
                        JSONObject detailJson = detailMap.getJSONObject(detailKey);
                        String detailTitle = detailJson.getString("title");//分录名称
                        JSONObject widgetVosJson = detailJson.getJSONObject("widgetVos");//分录字段Json
                        for (String eField : widgetVosJson.keySet()) {//eField分录字段标识
                            JSONObject eFieldJson = widgetVosJson.getJSONObject(eField);
                            String etitle = eFieldJson.getString("title");//标题
                            String etype = eFieldJson.getString("type");//类型
                            System.out.println(detailKey + " " + detailTitle + " " + eField + " " + etitle + " " + etype);
                        }

                    }
                } else {
                    throw new KDBizException("接口返回数据为空！");
                }
            } else {
                throw new KDBizException("接口返回数据为空！");
            }

        } else {
            throw new KDBizException(json.getString("error"));
        }
        */
        //createInst
//        Date date = new Date();
//        long time = date.getTime();
//        String url = YunzhijiaUtils.yzjurl+"/cmmpapi/cmmp/flowCreate.do?tokenId=0C89F5DBEB704C1F8671721B148E8224";
//        DynamicObject dynamicObject = QueryServiceHelper.queryOne("yem_ia_intebilllogon", "yem_generatejson", new QFilter[]{new QFilter("billno", QCP.equals, "11111")});
//        String yemGeneratejson = dynamicObject.getString("yem_generatejson");
//        JSONObject jsonObject = JSON.parseObject(yemGeneratejson);
//        jsonObject.put("creator","61d3d46fe4b067193172af61");
//        String s = "";
//        try {
//            s = YunzhijiaUtils.doPost(url, jsonObject.toString());
//        } catch (Exception ex) {
//            throw new KDBizException(ex.toString());
//        }
//        System.out.println();
        //获取文件上传token
//        String url = YunzhijiaUtils.yzjurl+"/cmmpapi/cmmp/fileToken.do?tokenId=D4EB05B9FE9C41D0868D33EC92AAEDB4";

//        List<Map<String, Object>> attachments = AttachmentServiceHelper.getAttachments("yem_im_inquirylist", obj.get("id"), "attachmentpanel");
//        System.out.println("");
//        String s ="";
//        try {
//             s = YunzhijiaUtils.doPost(url, "");
//        } catch (Exception ex) {
//            throw new KDBizException(ex.toString());
//        }
//        JSONObject json = JSONObject.parseObject(s);
//        Boolean success = json.getBoolean("success");
//        if(success){
//            JSONObject data = json.getJSONObject("data");
//            String accessToken = data.getString("accessToken");
//            //实际情况
//            File  file = new File("E:\\Downloads\\eas基础资料导入导出接口.docx");
//            String filename = "eas基础资料导入导出接口.docx";
//            InputStream input = null;
//            try {
//                input = new FileInputStream(file);
//                byte[] byt = new byte[input.available()];
//                String s1 = YunzhijiaUtils.doPost("https://yunzhijia.com/docrest/doc/file/uploadfile", byt, accessToken,filename);
//                System.out.println(s1);
//            }  catch (Exception ex) {
//                throw new KDBizException(ex.toString());
//            }
//        }
//        JSONObject json = new JSONObject();
//        json.put("start", 0);//起始数量--必填
//        json.put("len", 1000);//查询长度--必填
//        JSONArray storageOrgs = new JSONArray();
//        storageOrgs.add("010101");
//        json.put("storageOrgs", storageOrgs);//库存组织--必填数组
//        JSONArray warehouses = new JSONArray();
//        warehouses.add("B061");
//        json.put("warehouses", warehouses);//仓库--必填数组
//        json.put("locations","");//库位数组
//        json.put("storeTypeFrom","");//库存类型起
//        json.put("StoreTypeTo","");//库存类型止
//        json.put("materialNumFrom", "YX635NJA1B1X1A311");//物料起--必填
//        json.put("materialNumTo", "YX635NJA1B1X1A311");//物料止--必填
//        json.put("groupStandard", "BaseGroupStandard");//物料分类标准--必填
//        json.put("materialTypeFrom","");//物料分类起
//        json.put("materialTypeTo","");//物料分类止
//        json.put("assisAttrFrom","");//辅助属性起
//        json.put("assisAttrTo","");//辅助属性止
//        json.put("batchFrom","");//批次起
//        json.put("batchTo","");//批次止
//        json.put("projectNumFrom","");//项目号起
//        json.put("projectNumTo","");//项目号止
//        json.put("trackNumFrom","");//跟踪号起
//        json.put("trackNumTo","");//跟踪号止
//        json.put("storeType","");//存货类型
//        String s = RequestEAS.InventoryListServiceFacadeGetData(json.toString());
//        JSONObject result = JSONObject.parseObject(s);
//        JSONArray list = result.getJSONArray("list");
//        System.out.println(s);
    }
}
