package com.engine.web.tjbk;


import com.alibaba.druid.util.StringUtils;
import com.engine.common.util.ParamUtil;

import com.engine.service.TJBKWorkFlowService;
import com.engine.service.impl.TJBKWorkFlowServiceImpl;
import com.engine.util.XMLUtils;
import weaver.security.rsa.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import java.io.*;

import java.util.Map;

public class TJBKWorkFlow {

    private TJBKWorkFlowService tjbkWorkFlowService = new TJBKWorkFlowServiceImpl();

    @Path("/workflow/getToDoCount")
    @POST
    public String getToDoCount(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> stringObjectMap = ParamUtil.request2Map(request);
        String result = "";
        String requestBody = "";
        try {
            requestBody = IOUtils.toString(request.getReader());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // new BaseBean().writeLog("请求参数为" + requestBody);
//        if (StringUtil.isEmpty(requestBody)) {
//            result =XMLUtils.count2XML(paramMap,null,null);
//            return "参数为空";
//        }
        Map<String, String> paramMap = null;
        try {
            paramMap = XMLUtils.parseXMLToMap(requestBody);
            String path = paramMap.get("path");
            String method = paramMap.get("method");
            //待办
            if (StringUtils.equals("todo", path) && StringUtils.equals("getCount", method)) {
                Integer toDoCount = tjbkWorkFlowService.getToDoCount(paramMap);
                result = XMLUtils.count2XML(paramMap, toDoCount, null);
            } else if (StringUtils.equals("todo", path) && StringUtils.equals("getCount2", method)) {
                //代办和代阅
                Integer toDoCount = tjbkWorkFlowService.getToDoCount(paramMap);
                Integer toReadCount = tjbkWorkFlowService.getToReadCount(paramMap);
                result = XMLUtils.count2XML(paramMap, toDoCount, toReadCount);
            } else {
                paramMap.put("status", "FAIL");
                paramMap.put("code", "ESB-E-000002");
                paramMap.put("desc", "参数解析异常");
                return XMLUtils.count2XML(paramMap, null, null);
            }
            return result;
        }catch ( Exception E){
            paramMap.put("status", "FAIL");
            paramMap.put("code", "ESB-E-000002");
            paramMap.put("desc", "未知错误");
            return XMLUtils.count2XML(paramMap, null, null);
        }
    }

    public String getToDoCount(Map<String, String> param) {

        // new BaseBean().writeLog("请求参数为" + param);
//        if (StringUtil.isEmpty(requestBody)) {
//            result =XMLUtils.count2XML(paramMap,null,null);
//            return "参数为空";
//        }
        Map<String, String> paramMap = null;
        String result = "";
        try {
            // String substring = param.substring(8, param.length());
            // new BaseBean().writeLog("xml报文"+substring);
            // paramMap = XMLUtils.parseXMLToMap(param);
            paramMap = param;

            String path = paramMap.get("path");
            String method = paramMap.get("method");
            //待办
            if (StringUtils.equals("todo", path) && StringUtils.equals("getCount", method)) {
                Integer toDoCount = tjbkWorkFlowService.getToDoCount(paramMap);
                result = XMLUtils.count2XML(paramMap, toDoCount, null);
            } else if (StringUtils.equals("todo", path) && StringUtils.equals("getCount2", method)) {
                //代办和代阅
                Integer toDoCount = tjbkWorkFlowService.getToDoCount(paramMap);
                Integer toReadCount = tjbkWorkFlowService.getToReadCount(paramMap);
                result = XMLUtils.count2XML(paramMap, toDoCount, toReadCount);
            } else {
                paramMap.put("status", "FAIL");
                paramMap.put("code", "ESB-E-000002");
                paramMap.put("desc", "参数解析异常");
                String xmlStr = XMLUtils.count2XML(paramMap, null, null);
                int length = xmlStr.length();
                String format = String.format("%08d", length);
                // new BaseBean().writeLog("响应报文"+format+result);
                return format+xmlStr;
            }
            int length = result.length();
            String format = String.format("%08d", length);
            // new BaseBean().writeLog("响应报文"+format+result);
            return format+result;
        }catch ( Exception E){
            // new BaseBean().writeLog("未知错误"+E.getMessage());
            paramMap.put("status", "FAIL");
            paramMap.put("code", "ESB-E-000002");
            paramMap.put("desc", "未知错误");
            String xmlStr = XMLUtils.count2XML(paramMap, null, null);
            int length = xmlStr.length();
            String format = String.format("%08d", length);
            return format+xmlStr;
        }
    }
}