package nc.bs.ic.m45.wgpt.rule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.bs.dao.BaseDAO;
import nc.bs.logging.Logger;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.ic.m45.entity.PurchaseInBodyVO;
import nc.vo.ic.m45.entity.PurchaseInHeadVO;
import nc.vo.ic.m45.entity.PurchaseInVO;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import ncc.pub.wywgpt.utils.WyWGPTUtils;

/**
 * @Description: 签字时，回写入库信息给网购平台
 * 
 * @author 谢文如 【碧桂园-物业板块-网购平台接口】
 * @date 2019-3-6
 * @version with NC V6.3
 */


public class WuYeWgptWriteBackBS {

	public void process(PurchaseInVO[] vos) {
		if(null ==vos){
			return;
		}
		String sendJsonData = null;
		String esbResultinfoStr = null;
		try {
			PurchaseInHeadVO hVO;
			PurchaseInBodyVO[] bodys;
			for(PurchaseInVO vo:vos){
				hVO = vo.getHead();
				if("Y".equals(hVO.getVdef14())){
					//Vdef26 网购平台发送
					if(hVO.getVdef16()==null){
						ExceptionUtils.wrappBusinessException("网购平台发货单号为空！");
					}
					bodys = vo.getBodys();
					
					BaseDAO dao  = new BaseDAO();
					StringBuffer sqlstr = new StringBuffer("");
					sqlstr.append("select vdef15 from po_order where pk_order = '"+bodys[0].getCfirstbillhid()+"'"); 
					String orderNo = (String)dao.executeQuery(sqlstr.toString(), new ColumnProcessor());
					if(orderNo==null){
						ExceptionUtils.wrappBusinessException("网购平台订单号为空！");
					}
					
					JSONObject requestInfo = new JSONObject();
					JSONArray details = new JSONArray();
					JSONObject detail;
					for(PurchaseInBodyVO body:bodys){
						detail = new JSONObject();
						//网购平台商品编码
						detail.put("sku", body.getVfirstrowno());
						//实收主数量
						detail.put("num", body.getNnum().abs());
						details.add(detail);
					}
					//method 接口方法名
					requestInfo.put("method", "orderDetail");
					//dealTime 收货时间
					requestInfo.put("dealTime",new UFDateTime().toString());
					//dealUser 收货人姓名
					requestInfo.put("dealUser","");
					//dealUserId 收货人标识
					requestInfo.put("dealUserId","");
					//interfaceType 接口类型 1:验证 2：收货 3：退货
					requestInfo.put("interfaceType", hVO.getFreplenishflag().booleanValue()?"3":"2");
					//orderNo 网购平台订单号
					requestInfo.put("orderNo",orderNo);
					//reason 具体描述
					requestInfo.put("reason", "");
					//sendOrderNo 发货单号
					//Vdef25：网购平台 发货单号
					requestInfo.put("sendOrderNo",hVO.getVdef16());//Vdef25
					//sku 商品 sku
					//num 商品数量
					requestInfo.put("details",details);
					
					String instId = "D91S008-WYOSP-1903-011-"+System.currentTimeMillis();
					sendJsonData = WyWGPTUtils.getSendToEsbData(instId, requestInfo);
					//发送请求
					JSONObject esbResultinfo = WyWGPTUtils.sendToESB(sendJsonData,"NC-WGPT-PU-001");
					if(esbResultinfo!=null){
						esbResultinfoStr = esbResultinfo.toString();
						Logger.error("【保存时验证入库信息是否与网购平台一致】请求参数："+sendJsonData+"；\r\n返回参数："+esbResultinfoStr);
						JSONObject esbInfo = (JSONObject)esbResultinfo.get("esbInfo");
						if(esbInfo!=null){
							//接口返回状态：S（成功）/W（警告）/E（错误）
							String returnStatus = esbInfo.get("returnStatus")+"";
							returnStatus = "E".equals(returnStatus)?"否":"是";
							if("否".equals(returnStatus)){
								String returnMsg = esbInfo.get("returnMsg")+"";
								ExceptionUtils.wrappBusinessException("接口错误："+returnMsg);
							}
						}
						JSONObject resultInfo = esbResultinfo.getJSONObject("resultInfo");
						Object success = resultInfo.get("success");
						if(!"true".equals(success+"")){
							Object result = resultInfo.get("result");
							ExceptionUtils.wrappBusinessException("网购平台提示:"+result);
						}
					}
				}
			}
		} catch (Exception e) {
			Logger.error(e);
			e.printStackTrace();
			ExceptionUtils.wrappBusinessException("异常：\n"+e.getMessage()+"\n请求报文：\n"+sendJsonData+"\n返回报文：\n"+esbResultinfoStr);
		}
	}
	
}
