package nc.net.uap.servlet;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nc.bs.framework.common.NCLocator;
import nc.itf.bgy.ws.my2nc.write.INCTallybillWsFromMy;
import nc.net.servlet.EsbJsonServlet;
import nc.util.uap.twxt.WYTwxtUtil;
import nc.vo.arap.payable.PayableBillVO;
import nc.vo.esb.ResultEsbVO;
import nc.vo.esb.ResultEsbVO_Arap;
import nc.vo.esb.ResultGuidVO;
import nc.vo.esb.ResultMultEsbVO;
import nc.vo.pub.BusinessException;
import nc.vo.sap.log.SapPurchaseLogVO;
import uap.json.JSONArray;
import uap.json.JSONObject;
 
/**
 * @author zhengyao3/郑瑶
 * @date 2018-9-13 下午3:47:04
 * @Description:接收明源传入应付计提单
 * 
 */
public class NCTallybillWsFromMyServlet extends EsbJsonServlet {

	@SuppressWarnings("unchecked")
	@Override
	public void execFunction(HttpServletRequest request,
			HttpServletResponse response) {
		JSONObject jsonobj = getPostJsonValue().getJSONObject("requestInfo");// 获取请求值
		JSONObject esbInfo =  getPostJsonValue().getJSONObject("esbInfo");
		
		JSONArray billdata = jsonobj.getJSONArray("billdata"); 
		ResultEsbVO resultVO = new ResultEsbVO();
		ResultMultEsbVO  multi = new ResultMultEsbVO();
		List<ResultGuidVO> results = new LinkedList<ResultGuidVO>();
		List<PayableBillVO> lispay = new ArrayList<PayableBillVO>();
		INCTallybillWsFromMy billServlice = NCLocator.getInstance().lookup(
				INCTallybillWsFromMy.class);
		for(int i = 0 ; i < billdata.length(); i++){
			JSONObject payablebillData = billdata.getJSONObject(i);
			payablebillData.put("esbInfo", esbInfo);
			//修改返回类型
			ResultEsbVO_Arap newresultVO = billServlice.addTallybill_RequiresNew(payablebillData,getPostJsonValue().toString());
			resultVO.setEsbInfo(newresultVO.getEsbInfo());
			resultVO.setQueryInfo(newresultVO.getQueryInfo());
			results.add((ResultGuidVO) newresultVO.getResultInfo());
			//返回值构造
			if(newresultVO.getPayablebillvo() != null){
				lispay.add(newresultVO.getPayablebillvo());
			}
		}
		multi.setResultDatas(results);
		resultVO.setResultInfo(multi);
		sendResponse(response, resultVO);
		//保存所有单据之后核销
		for (PayableBillVO hvo :lispay) {
			 //核销---
		    if(hvo.getDef71() != null && hvo.getDef71().length() > 0){
				try {
					billServlice.hxTallybill(hvo,hvo.getDef71());
				} catch (BusinessException e) {
					SapPurchaseLogVO log = new SapPurchaseLogVO();
					log.setPk_org("NC-MY-PM-001-核销");
			    	log.setBillid(hvo.getPk_payablebill());
			    	log.setBillno(hvo.getBillno());
			    	log.setPk_billtypecode("F1");
			    	log.setPk_transtypecode(hvo.getPk_tradetype());
			    	log.setMsg_sn(e.getMessage());
			    	log.setIssuccess_sn("N");
			    	log.setDef1(this.getClass().getName());
					WYTwxtUtil.getIWuYeTwxtLogService().insertLog_RequiresNew(log);
				}
		    }
		}
	}

	/**
	 * @param response  
	 * @param esb
	 */
	public void sendResponse(HttpServletResponse response, ResultEsbVO esb) {
		String jsondata = com.alibaba.fastjson.JSONObject.toJSONString(esb);
		PrintWriter out = null;
		try {
			response.setHeader("Content-type", "application/json;charset=UTF-8");
			out = new PrintWriter(new OutputStreamWriter(
					response.getOutputStream(), "UTF-8"));
			out.write(jsondata);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
}
