package com.engine.htsc.payment.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.engine.htsc.payment.util.HttpUtil;
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.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 总部付款restful接口
 */
public class ITPaymentAction {

	/**
	 * 查询it付款项目详细信息
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@GET
	@Path("/projectDetailInfo")
	@Produces({MediaType.TEXT_PLAIN})
	public String getWfSigns(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		JSONObject resultJson = new JSONObject();
		try {
			String[] itprojects = Util.null2String(request.getParameter("itprojects")).split(",");
			List paramsList = new ArrayList();
			for (int i = 0; i < itprojects.length; i++) {
				String[] itproject = itprojects[i].split("#");
				Map<String, String> itprjMap = new HashMap<String, String>();
				itprjMap.put("id", itproject[0]);
				itprjMap.put("type", itproject[1]);
				paramsList.add(itprjMap);

			}

			String requestUrl = new PropBean().getPropName("PaymentDetailsUrl");
			HTUtil htUtil = new HTUtil();
			Map<String, String> appHeader = htUtil.getAppHeader();
			String data = HttpUtil.postApplicationJson(requestUrl, JSON.toJSONString(paramsList), appHeader);
			JSONArray dataJson = JSONObject.parseArray(data);
			resultJson.put("data", dataJson);
			resultJson.put("success", true);

		} catch (Exception e) {
			e.printStackTrace();
			resultJson.put("success", false);
		}

		return JSON.toJSONString(resultJson, SerializerFeature.WriteMapNullValue);
	}


	public static void main(String[] args) throws Exception {
		JSONObject resultJson = new JSONObject();
		String[] itprojects = "29#1,50#1,54#1".split(",");
		List paramsList = new ArrayList();
		for (int i = 0; i < itprojects.length; i++) {
			String[] itproject = itprojects[i].split("#");
			Map<String, String> itprjMap = new HashMap<String, String>();
			itprjMap.put("id", itproject[0]);
			itprjMap.put("type", itproject[1]);
			paramsList.add(itprjMap);

		}

		HTUtil htUtil = new HTUtil();
		Map<String, String> appHeader = htUtil.getAppHeader();
		String requestUrl = "http://168.61.69.225:8080/jtxt/weaver/itmanage/getPaymentDetails";
		String data = HttpUtil.postApplicationJson(requestUrl, JSON.toJSONString(paramsList), appHeader);
		System.out.println(data);
		JSONArray dataJson = JSONObject.parseArray(data);
		resultJson.put("data", dataJson);
		resultJson.put("success", true);
		System.out.println(JSON.toJSONString(resultJson, SerializerFeature.WriteMapNullValue));
	}


}
