package nc.util.uap.wgpt;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import nc.bs.framework.common.NCLocator;
import nc.itf.uap.pf.IPfExchangeService;
import nc.itf.wgpt.log.IWuYeWgptLogService;
import nc.itf.wgpt.process.IWuYeWgptBusiService;
import nc.net.servlet.IReturnStatus;
import nc.vo.esb.EsbInfoVO;
import nc.vo.esb.QueryInfoVO;
import nc.vo.esb.ResultEsbVO;
import nc.vo.pu.m21.entity.OrderItemVO;
import nc.vo.pu.m21.entity.OrderVO;
import nc.vo.pu.m21.enumeration.EnumActive;
import uap.json.JSONObject;
/**
 * @Description: 
 * 
 * @author 谢文如 【碧桂园-物业板块-网购平台接口】
 * @date 2019-3-6
 * @version with NC V6.3
 */
public class WuYeWgptUtil {

	public static IWuYeWgptLogService getIWuYeWgptLogService() {
		return NCLocator.getInstance().lookup(IWuYeWgptLogService.class);
	}

	public static IWuYeWgptBusiService getIWuYeWgptBusiService() {
		return NCLocator.getInstance().lookup(IWuYeWgptBusiService.class);
	}

	public static IPfExchangeService getExchangeService() {
		return  NCLocator.getInstance().lookup(IPfExchangeService.class);
	}

	/**
	 * 生成ESB响应对象
	 * @param esb
	 */
	public static void createResultEsbVO(ResultEsbVO esb){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		EsbInfoVO esbInfo=new EsbInfoVO();
		QueryInfoVO queryInfo=new QueryInfoVO();
		Date date=Calendar.getInstance().getTime();//获取当前时间
		//设置响应时间
		esbInfo.setInstId(UUID.randomUUID().toString());//使用UUID作为流水号
		esbInfo.setRequestTime(dateFormat.format(Calendar.getInstance().getTime()));//设置请求开始时间
		esbInfo.setResponseTime(dateFormat.format(date));//设置响应时间
		esbInfo.setReturnStatus(IReturnStatus.ESB_SUCCEED);//默认响应成功
		esbInfo.setReturnCode(IReturnStatus.ESB_SUCCEED_CODE);//默认成功code
		esbInfo.setReturnMsg("成功");//默认响应成功提示
		esb.setEsbInfo(esbInfo);
		esb.setQueryInfo(queryInfo);
	}
	
	/**
	 * @param e	异常
	 * return 异常详细信息
	 */
	public static String getExceptionDetail(Exception e) {  
		if(e==null)
			return null;
        StringBuffer stringBuffer = new StringBuffer(e.toString() + "\n");  
        if(e.getStackTrace()==null || e.getStackTrace().length==0)
        	return stringBuffer.toString();  
        StackTraceElement[] messages = e.getStackTrace();  
        int length = messages.length;  
        for (int i = 0; i < length; i++) {
            stringBuffer.append("\t"+messages[i].toString()+"\n");  
        }  
        return stringBuffer.toString();  
    }
	
	/**
	 * 加载Post提交上来的Json参数
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static JSONObject findPostJsonValue(HttpServletRequest request) throws Exception {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8"));
			StringBuffer sb = new StringBuffer();
			String temp = null;
			while ((temp = br.readLine()) != null) {
				sb.append(temp);
			}
			return new JSONObject(sb.toString());
		} catch (Exception e) {
			throw e;
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**判断采购订单是否整单关闭
	 * @author 符耀铭
	 * @date 2020年10月14日
	 * @param vo
	 * @return
	 */
	public static boolean isFinalClosable(OrderVO vo) {
		OrderItemVO[] itemVOs=vo.getBVO();
		
		for(int i=0;i<itemVOs.length;i++) {
			OrderItemVO item = itemVOs[i];
			if (!item.getFisactive().equals(EnumActive.CLOSE.value())) {
				return false;
			}
		}
		return true;
	}
	
}
