
package com.srm.bpm.facde.oa;

/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

import com.alibaba.fastjson.JSONObject;
import com.srm.bpm.facde.oa.common.HttpClient;


import javax.json.JsonObject;
import javax.xml.namespace.QName;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * This class was generated by Apache CXF 3.3.5
 * 2020-11-09T08:21:10.187+08:00
 * Generated source version: 3.3.5
 *
 */
public final class OfsTodoDataWebServicePortType_OfsTodoDataWebServiceHttpPort_Client {

    private static final QName SERVICE_NAME = new QName("webservices.ofs.weaver.com.cn", "OfsTodoDataWebService");

    public OfsTodoDataWebServicePortType_OfsTodoDataWebServiceHttpPort_Client() {
    }

    public static void main(String args[]) throws Exception {
//        URL wsdlURL = OfsTodoDataWebService.WSDL_LOCATION;
//        if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
//            File wsdlFile = new File(args[0]);
//            try {
//                if (wsdlFile.exists()) {
//                    wsdlURL = wsdlFile.toURI().toURL();
//                } else {
//                    wsdlURL = new URL(args[0]);
//                }
//            } catch (MalformedURLException e) {
//                e.printStackTrace();
//            }
//        }
//
//        OfsTodoDataWebService ss = new OfsTodoDataWebService(wsdlURL, SERVICE_NAME);
//        OfsTodoDataWebServicePortType port = ss.getOfsTodoDataWebServiceHttpPort();
//        {
//        System.out.println("Invoking getOfsTodoDataWebService...");
        String requestData = "{\"requestname\":\"系列报价审批流程-新-钟洁丹-2024-02-05\",\"creator\":\"1309251\",\"receiver\":\"2204653\",\"workflowname\":\"E10本地测试\",\"pcurl\":\"/interOAingleLogin?accountName=0510000&bill_id=1754358190743212033&is_oa=0\",\"workflowcode\":\"20240206001\",\"viewtype\":\"0\",\"appurl\":\"/interOAingleLogin?accountName=0510000&bill_id=1754358190743212033&is_oa=0\",\"syscode\":\"SRM1\",\"nodename\":\"河源磁性-资材课长\",\"receivets\":1707106562940,\"receivedatetime\":\"2024-02-05 00:00:00\",\"isremark\":\"0\",\"createdatetime\":\"2024-02-05 00:00:00\",\"flowid\":\"1754358190743212032\"}";
//        String _getOfsTodoDataWebService__return = OfsTodoDataWebService(requestData);
//        //System.out.println("getOfsTodoDataWebService.result=" + _getOfsTodoDataWebService__return);
//        }
//        System.exit(0);
        OfsTodoDataWebServicePortType_OfsTodoDataWebServiceHttpPort_Client s1 = new OfsTodoDataWebServicePortType_OfsTodoDataWebServiceHttpPort_Client();
        String result= s1.ofsTodoDatabyEten(requestData,"207c4f57fe094c1c283f6705d88febcf");
        System.out.println("result="+result);

    }
    //E8统一待办
    public  String OfsTodoDataWebService(String requestData) {
        String _OfsTodoDataWebService__return ="";
        try{
            URL wsdlURL = OfsTodoDataWebService.WSDL_LOCATION;
            OfsTodoDataWebService ss = new OfsTodoDataWebService(wsdlURL, SERVICE_NAME);
            OfsTodoDataWebServicePortType port = ss.getOfsTodoDataWebServiceHttpPort();
            _OfsTodoDataWebService__return =port.receiveRequestInfoByJson(requestData);
            System.out.println("getOfsTodoDataWebService.result=" + _OfsTodoDataWebService__return);
        }catch (Exception e){
            e.printStackTrace();
        }
        return _OfsTodoDataWebService__return;

    }
    //E10统一待办
    public String ofsTodoDatabyEten(String EtenRequestData,String token){
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("content-Type", "application/json");
        String result="";
        try {
            result=HttpClient.post(WebServiceRequestInfo.oaEtenAddress+"/papi/openapi/sapi/open_intunifytodo/server_engine/receiveRequestInfoByJson?access_token="+token,EtenRequestData,headMap);
            System.out.println("receivets="+System.currentTimeMillis());
            System.out.println("result="+result);
            JSONObject jsonObject = JSONObject.parseObject(result);
            String message = jsonObject.getString("message");
            if (message !=null){
                JSONObject s1 = JSONObject.parseObject(message);
                String errcode = s1.getString("errcode");
                String errmsg = s1.getString("errmsg");
                if (!"200".equals(errcode)){
                    result=jsonObject.toJSONString();
                }else{
                    result=errmsg;
                }
            }else{
                token =getAccessToken();
                result=HttpClient.post(WebServiceRequestInfo.oaEtenAddress+"/papi/openapi/sapi/open_intunifytodo/server_engine/receiveRequestInfoByJson?access_token="+token,EtenRequestData,headMap);
                System.out.println("result="+result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public  String getRequestData (ReceiveRequestInfoByJsonDto receiveRequestInfoByJsonDto,String isremark){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        JSONObject json = new JSONObject();
        json.put("syscode", receiveRequestInfoByJsonDto.getSyscode());
        json.put("flowid", receiveRequestInfoByJsonDto.getFlowid());
        json.put("requestname", receiveRequestInfoByJsonDto.getRequestname());
        json.put("workflowcode",receiveRequestInfoByJsonDto.getWorkflowcode());
        json.put("workflowname", receiveRequestInfoByJsonDto.getWorkflowname());
        json.put("nodename", receiveRequestInfoByJsonDto.getNodename());
        json.put("pcurl", "/interOAingleLogin?accountName="+receiveRequestInfoByJsonDto.getReceiver()+"&bill_id="+receiveRequestInfoByJsonDto.getFlowid()+"&is_oa="+isremark);
        json.put("appurl", "/interOAingleLogin?accountName="+receiveRequestInfoByJsonDto.getReceiver()+"&bill_id="+receiveRequestInfoByJsonDto.getFlowid()+"&is_oa="+isremark);
        json.put("isremark", isremark);
        if (isremark.equals("0")){
            json.put("viewtype", "0");
        }else{
            json.put("viewtype", "1");
        }
        json.put("creator", receiveRequestInfoByJsonDto.getCreator());
        json.put("createdatetime", receiveRequestInfoByJsonDto.getCreatedatetime()+" 00:00:00");
        json.put("receiver", receiveRequestInfoByJsonDto.getReceiver());
        json.put("receivedatetime", receiveRequestInfoByJsonDto.getReceivedatetime()+" 00:00:00");
        json.put("receivets",System.currentTimeMillis());
        System.out.println("RequestData="+json.toJSONString());
        return json.toJSONString();
    }

    /*获取accessToken信息
     *access_token有效时间为2小时
     * */
    public String getAccessToken() {
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("content-Type", "application/json");
        String result = "";
        String code = getCode();
        HashMap<String,Object> data =new HashMap<>();
        data.put("app_key","dd0ed7d1a7a961aea404a492f24b6c2e");
        data.put("app_secret","ea892ee6b236978e01fb6daa3a850ad");
        data.put("grant_type","authorization_code");
        data.put("code",code);
        String json = new JSONObject(data).toString();
        try {
            result=HttpClient.post(WebServiceRequestInfo.oaEtenAddress + "/papi/openapi/oauth2/access_token",json,headMap);
            //     System.out.println("token=" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        //{"errcode":"0","errmsg":"success","accessToken":"979e8c0411c991fdf1fcfecc2097e5f4","acessToken":"979e8c0411c991fdf1fcfecc2097e5f4","refreshToken":"cfd577924705eb6586e91804e6e35b60","expires_in":7200}
        String errmsg = jsonObject.getString("errmsg");
        String accessToken = jsonObject.getString("accessToken");
        if ("success".equals(errmsg)) {
            result = accessToken;
        } else{
        //    ZcLogs.WriteLog(mc, "对接Eten获取accessToken异常："+jsonObject);
        }
        return result;
    }

    /*获取code信息
     * code有效时间为10分钟，且只能使用一次；`
     * */
    public String getCode() {
        String result = "";
        try {
            Map<String, String> headMap = new HashMap<String, String>();
            headMap.put("content-Type", "application/x-www-form-urlencoded");
            result = HttpClient.get(WebServiceRequestInfo.oaEtenAddress + "/papi/openapi/oauth2/authorize?corpid=faececddfd1ce313176b88b485cae0f0&response_type=code&state=", headMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        //{"errcode":"0","code":"52eae26f965a39491791a6696204df8e","errmsg":"success"}
        String errmsg = jsonObject.getString("errmsg");
        String code = jsonObject.getString("code");
        if ("success".equals(errmsg)) {
            result = code;
        }else{
        //    ZcLogs.WriteLog(mc, "对接Eten获取Code异常："+jsonObject);
        }

        return result;
    }
}
