package nc.impl.pu.service;

import java.io.File;


import org.jeecg.common.util.HttpHelper;

import com.alibaba.fastjson.JSONObject;

import nc.bs.framework.common.NCLocator;
import nc.itf.pu.service.IInvoiceOCRMaintain;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.log.NcLog;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class InvoiceOCRMaintainImpl implements IInvoiceOCRMaintain{
	
	
	private String url = "https://api.mypiaojia.com/api/invoiceApi/";
	private String url2 = "https://up.mypiaojia.com/api/invoiceApi/";

	
	@Override
	public JSONObject startInvoiceOCR(File file) {
		try {
			NcLog.info("流程开始");
			//获取发票云token
			JSONObject tokenjb = getOCRToken();
			NcLog.info("获取发票云token:"+tokenjb.toString());
			String token = tokenjb.getString("data");
			//上传发票到发票云系统
			JSONObject uploadjb = uploadInvoice(token, file);
			NcLog.info("上传发票到发票云:"+uploadjb.toString());
			//进行orc识别
			JSONObject scanjb = scanInvoice(token,uploadjb);
			NcLog.info("发票云OCR识别:"+scanjb.toString());
			
			return scanjb;
			
		} catch (Exception e) {
			ExceptionUtils.wrappBusinessException(e.getMessage());
			NcLog.info("流程失败:"+e.getMessage());
		}
		return null;
	}
	
	
	/**
	 * 获取发票云token
	 * @return token
	 * @throws Exception
	 */
	private JSONObject getOCRToken() throws Exception{
		String sql = "select defaultvalue from pub_sysinittemp where  initcode ='fptoken'";
		IUAPQueryBS iUAPQueryBS= (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
		JSONObject jsonobject = JSONObject.parseObject(iUAPQueryBS.executeQuery(sql, new ColumnProcessor()).toString());
		JSONObject responseMsg = HttpHelper.httpTokenPost(url+"token/buildToken", jsonobject.getString("appId"), jsonobject.getString("cid"), jsonobject.getString("secret"));
		if(responseMsg.get("data") != null) {
			return responseMsg;		
		}else {
			throw new Exception("获取发票云token失败:"+responseMsg.getJSONObject("res_base").getString("message"));
		}

	}
	
	/**
	 * 上传发票到发票云系统
	 * @param token
	 * @param file
	 * @return
	 * @throws Exception
	 */
	private JSONObject uploadInvoice(String token, File file) throws Exception {
		NcLog.info("上传发票到发票云系统开始");
		JSONObject responseMsg = HttpHelper.httpFilePost(url2+"file/uploadFileSpilit",token, file);
		if(responseMsg.get("data") != null) {
			return responseMsg;
		}else {
			throw new Exception("上传发票到发票云系统失败:"+responseMsg.getJSONObject("res_base").getString("message"));
		}
	}
	
	
	/**
	 * 进行orc识别
	 * @param token
	 * @param uploadjb   上传发票到发票云系统时返回的数据
	 * @return
	 * @throws Exception
	 */
	private JSONObject scanInvoice(String token,JSONObject uploadjb) throws Exception {
		 JSONObject jsonobject = new JSONObject();
         jsonobject.put("image_index", uploadjb.getJSONObject("data").getString("id"));
         jsonobject.put("fileTypeStr", uploadjb.getJSONObject("data").getString("fileType"));
         jsonobject.put("flag", "13");
         jsonobject.put("is_save", "0");
         jsonobject.put("is_sync", "1");
         jsonobject.put("ocrMap","");
         jsonobject.put("operate_type",0);
         JSONObject responseMsg = HttpHelper.httpPost(url+"invoice/ocr", token, jsonobject);
         if(responseMsg.get("data") != null) {
 			return responseMsg;
 		}else {
 			throw new Exception("发票orc识别失败:"+responseMsg.getJSONObject("actionMsg").getString("message"));
 		}
	}
	
	
	//删除发票云系统发票

	
	//修改发票云发票状态
}
