package com.customization.htsc.integration;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.util.HttpUtil;
import com.engine.htsc.workflow.util.WorkflowUtil;
import weaver.conn.RecordSet;
import weaver.filter.WeaverRequest;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.util.HttpRequestUtil;
import weaver.workflow.workflow.WorkflowComInfo;

import javax.ws.rs.HEAD;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PushDataThread implements Runnable{

    private WeaverRequest request;

    public PushDataThread(WeaverRequest request) {
        this.request = request;
    }


    /**
     * "title": "标题333444",
     * "todoid": "345678",
     * "userid": "zhangsan,suhf",
     * "qcuserid": "wangwu",
     * "qcusername": "王五",
     * "tododescribe": "用印申请123",
     * "createdate": "2016-05-26 12:23:23",
     * "receivedate": "2018-05-26 12:23:23",
     * "contenttype": "1",
     * "todostate": "0",
     * "flowstate": "已办结",
     * "links": "http://xxx.xxx.com.cn/xxxxxx",
     * "mlinks": "http://yyy.yyyy.com.cn/yyyyyy",
     * "qcuserdeptid": "445566",
     * "qcuserdeptname": "测试部门111",
     * "todocode": "XYZ111333",
     * "todotype": "我处理的"
     */

    @Override
    public void run() {
        try {

            PropBean pb = new PropBean();
            BaseBean bb = new BaseBean();
            WorkflowUtil workflowUtil = new WorkflowUtil();
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            String nodetype = this.request.getParameter("nodetype");
            String nodeid = this.request.getParameter("nodeid");
            String src = this.request.getParameter("src");
            String isremark = this.request.getParameter("isremark");

            String title = this.request.getParameter("requestname");
            String todoid = this.request.getParameter("requestid");

            String workflowid = this.request.getParameter("workflowid");
            WorkflowComInfo workflowComInfo = new WorkflowComInfo();
            String tododescribe = workflowComInfo.getWorkflowname(workflowid);

            String userid = this.request.getParameter("f_weaver_belongto_userid");
            String usercode = resourceComInfo.getWorkcode(userid);

            //给代理人创建已办信息
            bb.writeLog("PushDataThread getAgentInfo====="+todoid+"  "+userid+"   "+nodeid);
            String bagentCodes = workflowUtil.getAgentInfo(todoid,userid,nodeid);
            if(!"".equals(bagentCodes)){
                usercode += ","+bagentCodes;
            }


            RecordSet rs = new RecordSet();
            rs.executeQuery(" select currentnodetype,creater,createdate,createtime,lastoperatedate,lastoperatetime,requestmark " +
                    "from workflow_requestbase where requestid=? ",new Object[]{todoid});
            rs.next();
            String creater = Util.null2String(rs.getString("creater"));
            String createdate = Util.null2String(rs.getString("createdate"))+" "+Util.null2String(rs.getString("createtime"));;
            String receivedate = "";
            String contenttype = "1";
            String todostate = "0";

            String qcuserid = Util.null2String(this.request.getParameter("creater"));
            if("".equals(qcuserid)){
                qcuserid = creater;
            }
            String qcusercode = resourceComInfo.getWorkcode(qcuserid);
            String qcusername = resourceComInfo.getLastname(qcuserid);


            String flowstate = "";
            String currentnodetype = Util.null2String(rs.getString("currentnodetype"));
            if("3".equals(currentnodetype)){
                flowstate = "已办结";
                receivedate = Util.null2String(rs.getString("lastoperatedate"))+" "+Util.null2String(rs.getString("lastoperatetime"));

            }else {
                flowstate = "进行中";
            }

            String oahost = pb.getPropName("OAHOST");
            String links = oahost+"workflow/request/ViewRequestForwardSPA.jsp?requestid="+todoid+"&_workflowid="+workflowid+"&f_weaver_belongto_userid="+userid;

            String mobileHost = pb.getPropName("mobileHost");
            String mobileJumpUrl = pb.getPropName("mobileJumpUrl");
            String mlinks = mobileHost+mobileJumpUrl+"?requestid="+todoid+"&f_weaver_belongto_userid="+userid;

            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            String qcuserdeptid = resourceComInfo.getDepartmentID(qcuserid);
            String qcuserdeptcode = departmentComInfo.getDepartmentCode(qcuserdeptid);
            String qcuserdeptname = departmentComInfo.getDepartmentName(qcuserdeptid);

            String todocode = Util.null2String(rs.getString("requestmark"));
            String todotype = "";
            if("submit".equals(src) || "reject".equals(src)){
                if("0".equals(nodetype)){
                    todotype = "我发起的";
                }else{
                    todotype = "我处理的";
                }

            }else{
                if("1".equals(isremark)){
                    todotype = "他人移交";
                }else if("8".equals(isremark) || "9".equals(isremark)){
                    todotype = "通知阅读";
                }
            }
            String lstusername = workflowUtil.getUnoperators(todoid,userid);

            Map<String,String> dataMap = new HashMap<>();
            dataMap.put("title",title);
            dataMap.put("todoid",todoid);
            dataMap.put("userid",usercode);
            dataMap.put("qcuserid",qcusercode);
            dataMap.put("qcusername",qcusername);
            dataMap.put("lstusername",lstusername);
            dataMap.put("tododescribe",tododescribe);
            dataMap.put("createdate",createdate);
            dataMap.put("receivedate",receivedate);
            dataMap.put("contenttype",contenttype);
            dataMap.put("todostate",todostate);
            dataMap.put("flowstate",flowstate);
            dataMap.put("links",links);
            dataMap.put("mlinks",mlinks);
            dataMap.put("qcuserdeptid",qcuserdeptcode);
            dataMap.put("qcuserdeptname",qcuserdeptname);
            dataMap.put("todocode",todocode);
            dataMap.put("todotype",todotype);

            List<Map<String,String>> dataList = new ArrayList<>();
            dataList.add(dataMap);

            Map<String,String> paramsMap = new HashMap<>();
            paramsMap.put("systemtype","HTOA");
            paramsMap.put("todojson", JSON.toJSONString(dataList));

            String addUrl = Util.null2String(pb.getPropName("PortalAddDoneDataUrl"),"http://168.61.9.228:8080/wps/TodoRest/todo/addtodo");

            bb.writeLog("PushDataThread  addparams===="+JSON.toJSONString(paramsMap));
            bb.writeLog("PushDataThread  addurl===="+addUrl);

            String addResult = HttpUtil.postFormUrlEncode(addUrl,paramsMap);
            bb.writeLog("PushDataThread  addResult===="+addResult);

            //更新已办数据，
            dataList.clear();
            dataMap.remove("todotype");
            dataMap.remove("todostate");
            dataMap.remove("links");
            dataMap.remove("mlinks");
            dataMap.remove("receivedate");
            dataMap.remove("createdate");
            dataMap.remove("tododescribe");
            dataMap.put("userid","ALL");
            dataList.add(dataMap);
            paramsMap.put("todojson",JSON.toJSONString(dataList));
            bb.writeLog("PushDataThread  updateparams===="+JSON.toJSONString(paramsMap));
            String updateUrl = Util.null2String(pb.getPropName("PortalUpdateDoneDataUrl"),"http://168.61.9.228:8080/wps/TodoRest/todo/updatetodo");
            String updateResult = HttpUtil.postFormUrlEncode(updateUrl,paramsMap);
            bb.writeLog("PushDataThread  updateResult===="+updateResult);




        } catch (Exception e) {
            e.printStackTrace();
        }



    }





    public static void main(String[] args) throws Exception {
        HttpRequestUtil httpRequestUtil = new HttpRequestUtil();
        String url = "http://168.61.9.228:8080/wps/TodoRest/todo/addtodo";
        String params = "{\"todojson\":\"[{\\\"qcuserid\\\":\\\"011413\\\",\\\"qcusername\\\":\\\"蔡洋\\\",\\\"todostate\\\":\\\"0\\\",\\\"tododescribe\\\":\\\"总部重要事项\\\",\\\"createdate\\\":\\\"2021-03-18 17:34:24\\\",\\\"title\\\":\\\"测试门户已办\\\",\\\"userid\\\":\\\"011413\\\",\\\"contenttype\\\":\\\"1\\\",\\\"mlinks\\\":\\\"http://linkxt-test.htsc.com.cn:38080/htmoa/wxapi/wxclientmenu/7bfc8bd866a74b0788f6c205a8668877?requestid=4906903&f_weaver_belongto_userid=28302\\\",\\\"flowstate\\\":\\\"进行中\\\",\\\"qcuserdeptid\\\":\\\"ZZ001014\\\",\\\"todotype\\\":\\\"我发起的\\\",\\\"receivedate\\\":\\\"2021-03-18 17:34:59\\\",\\\"todocode\\\":\\\"申报单信20210318104848\\\",\\\"links\\\":\\\"http://168.61.8.169:8080/htoa/workflow/request/ViewRequestForwardSPA.jsp?requestid=4906903&_workflowid=24&f_weaver_belongto_userid=28302\\\",\\\"qcuserdeptname\\\":\\\"信息技术部\\\"}]\",\"systemtype\":\"oa\"}\n";
        JSONObject json = JSON.parseObject(params);
        String systemtype = json.getString("systemtype");
        String todojson = json.getString("todojson");
        Map<String,String> paramsMap = new HashMap<>();
        paramsMap.put("systemtype",systemtype);
        paramsMap.put("todojson", todojson);

        Map<String,String> header = new HashMap<>();
        header.put("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
        System.out.println(todojson);
        String result = HttpUtil.postFormUrlEncode(url,paramsMap,header);
        System.out.println(result);
    }


}
