package com.engine.htxc.receipt.web;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ParamUtil;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

public class ReceiptWebForwardAction {

    @POST
    @Path("/createFromReceipt")
    @Produces({MediaType.TEXT_PLAIN})
    public String forwardCreateFromReceipt(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bs = new BaseBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        bs.writeLog("----ReceiptWebForwardAction-----");
        try{
            String requestid = request.getParameter("requestid");

            Map<String,Object> requestMap = new HashMap<>();
            requestMap.put("requestid",requestid);
            PropBean pb = new PropBean();
            //触发协作任务地址
            String receiptCreateTaskUrl = Util.null2String(pb.getPropName("receipt.creatTask.url"))+"?requestid="+requestid;
            bs.writeLog("doCreatTask----->" + receiptCreateTaskUrl);
            HTUtil htUtil = new HTUtil();
            return htUtil.httpPostRaw(receiptCreateTaskUrl, JSON.toJSON(requestMap).toString(), null, null);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/getTaskBySubRequestid")
    @Produces({MediaType.TEXT_PLAIN})
    public String forwardGetTaskBySubRequestid(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean bs = new BaseBean();
        Map<String, Object> apidatas = new HashMap();
        bs.writeLog("----ReceiptWebForwardAction-----");
        try {
            String requestid = request.getParameter("requestid");
            Map<String, Object> requestMap = new HashMap();
            requestMap.put("requestid", requestid);
            PropBean pb = new PropBean();
            String receiptGetTaskUrl = Util.null2String(pb.getPropName("receipt.getTask.url")) + "?requestid=" + requestid;
            bs.writeLog("doGetTask----->" + receiptGetTaskUrl);
            HTUtil htUtil = new HTUtil();
            return htUtil.httpPostRaw(receiptGetTaskUrl, JSON.toJSON(requestMap).toString(), null, null);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
            return JSON.toJSONString(apidatas);
        }
    }

}
