yaoyu пре 1 година
родитељ
комит
2639c3ed6d

+ 29 - 14
pu/pu/src/client/nc/pub/filesystem/newui/ThOCRAction.java

@@ -15,6 +15,7 @@ import org.apache.commons.httpclient.HttpException;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 
+import nc.bs.framework.common.InvocationInfoProxy;
 import nc.bs.framework.common.NCLocator;
 import nc.bs.framework.common.RuntimeEnv;
 import nc.funcnode.ui.action.AbstractNCAction;
@@ -53,7 +54,7 @@ public class ThOCRAction extends AbstractNCAction{
 		String vdef17 =(String) parentpanel.editor_.getBillCardPanel().getHeadItem("vdef17").getValueObject();//附件id
 		String pk_org = (String) parentpanel.editor_.getBillCardPanel().getHeadItem("pk_org").getValueObject();
 		String pk_group = (String) parentpanel.editor_.getBillCardPanel().getHeadItem("pk_group").getValueObject();
-		
+		String billmaker = InvocationInfoProxy.getInstance().getUserId();
 		IThOcrService  ocrservice=(IThOcrService) NCLocator.getInstance().lookup(IThOcrService.class.getName());
 		
 //		IInvoiceOCRMaintain iInvoiceOCRMaintain = (IInvoiceOCRMaintain) NCLocator.getInstance().lookup(IInvoiceOCRMaintain.class.getName());
@@ -74,20 +75,20 @@ public class ThOCRAction extends AbstractNCAction{
 		//获取token
 		String token="";
 		try {
-			 token=ocrservice.getToken();
-		} catch (HttpException e) {
+			 token=ocrservice.getToken(billmaker);
+		} catch (Exception e) {
 	//		Logger.error(e.getMessage(), e);
 			MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/
 					,"获取token:\n"+e.getMessage());
 			return;
-		} catch (IOException e) {
-			MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/
-					,"获取token:\n"+e.getMessage());
-			return;
-		} catch (BusinessException e) {
-			MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/
-					,"获取token:\n"+e.getMessage());
-			return;
+//		} catch (IOException e) {
+//			MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/
+//					,"获取token:\n"+e.getMessage());
+//			return;
+//		} catch (BusinessException e) {
+//			MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/
+//					,"获取token:\n"+e.getMessage());
+//			return;
 		}
 		
 //		MessageDialog.showHintDlg(parentpanel, "提示","token:"+token);
@@ -124,6 +125,7 @@ public class ThOCRAction extends AbstractNCAction{
 							,"ocr返回数据校验:\n"+checkresult);
 					return;
 				}
+				
 			} catch (Exception e) {
 				// TODO 自动生成的 catch 块
 				e.printStackTrace();
@@ -132,7 +134,15 @@ public class ThOCRAction extends AbstractNCAction{
 			JSONArray infos = data.getJSONArray("infos");
 			for(int j = 0;j < infos.size();j++){
 				JSONObject info = infos.getJSONObject(j).getJSONObject("info");
-//				String code = info.getString("code");//发票代码
+				String ret = infos.getJSONObject(j).getString("ret");
+				String memo = info.getString("number");//发票号
+				String code = info.getString("number");//发票代码
+				if(!"0".equals(ret)) {
+					String message = infos.getJSONObject(j).getString("message");
+					MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/
+							,"ocr返回数据校验:\n"+"发票:"+code+":"+message);
+					//return;
+				}
 				String fid = info.getString("fid");
 				String ext = info.getString("ext");
 				JSONObject extobj = JSONObject.parseObject(ext);
@@ -140,14 +150,19 @@ public class ThOCRAction extends AbstractNCAction{
 				
 				JSONArray flist = info.getJSONArray("flist");
 				String name = filewjlis.get(i);
-				String memo = info.getString("number");//发票号
-				String code = info.getString("number");//发票代码
 				JSONObject comm_info = info.getJSONObject("comm_info");
 				JSONObject pro = comm_info.getJSONObject("pro");
 				String date = pro.getString("date");//发票日期
 				String type = pro.getString("type");//发票类型
 				String cname = pro.getString("cname");//发票类型名称
 				JSONObject price = comm_info.getJSONObject("price");
+				//发票识别之后进行发票查验
+				try {
+					ocrservice.InvoiceCheck(token,fid);
+				} catch (Exception e) {
+					MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/
+							,"发票查验:\n"+"发票:"+code+":"+e.getMessage());
+				}
 				String amount = "";
 				String total = "";
 				String dtax = "";

+ 78 - 47
pu/pu/src/private/nc/impl/th/ThOcrServiceImpl.java

@@ -13,7 +13,9 @@ import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -26,7 +28,6 @@ import nc.bs.framework.common.RuntimeEnv;
 import nc.itf.uap.IUAPQueryBS;
 import nc.jdbc.framework.processor.ArrayListProcessor;
 import nc.jdbc.framework.processor.BeanListProcessor;
-import nc.jdbc.framework.processor.BeanProcessor;
 import nc.jdbc.framework.processor.MapProcessor;
 import nc.log.NcLog;
 import nc.vo.bd.defdoc.DefdocVO;
@@ -38,14 +39,12 @@ import nc.itf.uap.IVOPersistence;
 
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.RequestEntity;
 import org.apache.commons.httpclient.methods.StringRequestEntity;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import com.gbase.jdbc.log.Log;
 
 import nc.itf.bd.defdoc.IDefdocService;
 import nc.itf.th.IThOcrService;
@@ -63,23 +62,13 @@ public class ThOcrServiceImpl implements IThOcrService{
 	
 	private IVOPersistence IVOPersistence = (IVOPersistence) NCLocator.getInstance().lookup(IVOPersistence.class);
 	private IUAPQueryBS iuap=(IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
-	//锁定发票使用{
 	private String aesKey = "w34xx413y2x23w4y";
-	private String userName = "1438490100786012161";
-	private String password = "80c8664aa7a444049b8ed49ed5095561";
-	private String cid_ = "766941083040768000";
-	private String userid_="1";
-	//}
-	
-	//token使用{
-	
-	private String appId="773676128773832704";
-	private String secret="6726dea9883f4f6bb2725ac8750d01de";
-	private Long cid=773676128725336064l;
-	private String userid="2108";
-	//}
+	private static String appId="1438490100786012161";
+	private static String secret="80c8664aa7a444049b8ed49ed5095561";
+	private static String cid="1438490101547343873";
 	private String upurl="https://up.mypiaojia.com/api/invoiceApi/file/uploadFileSpilit";
 	private String inverurl="https://api.mypiaojia.com/api/invoiceApi/invoice/ocr";
+	private String person_local_valid = "https://api.mypiaojia.com/api/invoiceApi/invoice/person_local_valid";
 	//发票锁定地址
 	private String ec_comm = "https://api.mypiaojia.com/api/invoiceApi/invoice/ec_comm";
 	private String buildToken="https://api.mypiaojia.com/api/invoiceApi/token/buildToken";
@@ -92,16 +81,17 @@ public class ThOcrServiceImpl implements IThOcrService{
 	/**
 	 * 获取token
 	 */
-	public String getToken() throws HttpException, IOException, BusinessException {
+	public String getToken(String billmaker) throws Exception {
 		NcLog.info("=======获取token开始=======");
 		//redis中获取用户token
-		String userToken = RedisTokenManager.getToken(userid);
+		String userToken = RedisTokenManager.getToken(billmaker);
 		//token为空
 		if(Toolkits.isEmpty(userToken)){
+			
 			//调用OCR接口
-			userToken=getOcrToken(appId,cid,secret,userid);
+			userToken=getOcrToken(appId,cid,secret,billmaker);
 			//保存到redis,设置失效时间36000秒
-			RedisTokenManager.setTokenTTL(userid,36000, userToken);
+			RedisTokenManager.setTokenTTL(billmaker,36000, userToken);
 		}
 		NcLog.info("=======token======="+userToken);
 		NcLog.info("=======获取token结束=======");
@@ -126,8 +116,7 @@ public class ThOcrServiceImpl implements IThOcrService{
 			 if(null!=data){
 				 id=data.getString("id");
 			 }else{
-				 String msg=json.getJSONObject("res_base").getString("message");
-				 throw new BusinessException(msg);
+				 throw new Exception(json.toString());
 			 }
 			
 		}
@@ -144,16 +133,41 @@ public class ThOcrServiceImpl implements IThOcrService{
 	public String ocrIdentification(String token,String data) {
 		// TODO 自动生成的方法存根
 		 NcLog.info("=======发票OCR识别开始=======");
-		 JSONObject json = JSONObject.parseObject(data);
+		 JSONObject jsondata = JSONObject.parseObject(data);
 		 JSONObject jsonobject = new JSONObject();
-         jsonobject.put("image_index", json.getJSONObject("data").getString("id"));
-         jsonobject.put("fileTypeStr", json.getJSONObject("data").getString("fileType"));
+         jsonobject.put("image_index", jsondata.getJSONObject("data").getString("id"));
+         jsonobject.put("fileTypeStr", jsondata.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);
-		 return doPost(inverurl,token,jsonobject.toString());
+         String json = doPost(inverurl,token,jsonobject.toString());
+         NcLog.info("发票OCR识别返回json"+json);
+		 return json;
+	}
+	
+	@Override
+	public void InvoiceCheck(String token,String fid)
+			throws Exception {
+		 NcLog.info("=======发票查验识别开始=======");
+		 JSONObject jsonobject = new JSONObject();
+		 jsonobject.put("id", fid);
+         jsonobject.put("fid", fid);
+         jsonobject.put("ic_cut", "0");
+         jsonobject.put("operate_type","0");
+         JSONObject req_base = new JSONObject();
+         req_base.put("trace", fid);
+         req_base.put("dev", "1");
+         jsonobject.put("req_base",req_base);
+         JSONObject json = JSONObject.parseObject(doPost(person_local_valid,token,jsonobject.toString()));
+         
+         NcLog.info("发票查验返回json"+json);
+         JSONObject data=json.getJSONObject("data");
+         if(data == null) {
+        	 String msg=json.getJSONObject("actionMsg").getString("message");
+			 throw new Exception(msg);
+         }
 	}
 	
 	
@@ -167,19 +181,19 @@ public class ThOcrServiceImpl implements IThOcrService{
 	
 	
 
-	public String getOcrToken(String appId,Long cid,String secret,String userid)  throws HttpException, IOException, BusinessException{
+	public String getOcrToken(String appId,String cid,String secret,String userid)  throws Exception{
     	HttpClient httpClient = new HttpClient();
     	httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
         httpClient.getHttpConnectionManager().getParams().setSoTimeout(5000);
         PostMethod postMethod = new PostMethod(buildToken);
         postMethod.setRequestHeader("content-type", "application/json");
         postMethod.setRequestHeader("appkey", appId);
+        Map<String,String> oamap = QueryOAUserid(userid);
         long l = System.currentTimeMillis();
         Map<String, Object> map = new HashMap<String, Object>();
         map.put("role", 1);
-        map.put("name", "dmy06");
-        map.put("name", "dmy06");
-        map.put("userId", userid);//OA用户体系的用户id 2108
+        map.put("name", oamap.get("lastname"));
+        map.put("userId", oamap.get("id"));//OA用户体系的用户id 2108
         map.put("cid", cid);
         String s1 = JSONObject.toJSONString(map);
         String s2 = s1 + "&time=" + l + "&secret="+secret;
@@ -357,10 +371,7 @@ public class ThOcrServiceImpl implements IThOcrService{
 	            } catch (IOException e) {
 	                e.printStackTrace();
 	            }
-	        }
-	        
-	        NcLog.info("发票OCR识别返回json:"+result);
-	        
+	        } 
 			return result;
 		 }
 		 
@@ -615,7 +626,6 @@ public class ThOcrServiceImpl implements IThOcrService{
 		    			+ "(select pk_defdoclist from bd_defdoclist where code='OCRTHNC' and nvl(dr,0)=0) and memo='" + name + "' ";
 		    	ArrayList<Object> arrayvos=null;
 		    	try {
-		    		IUAPQueryBS iuap= (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
 					arrayvos=(ArrayList<Object>) iuap.executeQuery(sql, new BeanListProcessor(DefdocVO.class));
 				} catch (BusinessException e) {
 					// TODO 自动生成的 catch 块
@@ -679,7 +689,9 @@ public class ThOcrServiceImpl implements IThOcrService{
 
 		@Override
 		public void UpdateInvoiceStatus(String status, InvoiceVO[] vos) throws Exception {
+			NcLog.info("=======锁定发票、修改开始=======");
 			for (InvoiceVO invoiceVO : vos) {
+				Map<String,String> oamap = QueryOAUserid(invoiceVO.getParent().getAttributeValue("billmaker").toString());
 				//审批结果Y2代表通过 N0代表不通过 R0代表驳回T1报销中
 				InvoiceItemVO[] arrInvoiceItemVO = (InvoiceItemVO[]) invoiceVO.getChildren(InvoiceItemVO.class);
 				List<Map<Object, Object>> listMapVbdef20 = new ArrayList<Map<Object, Object>>();
@@ -705,14 +717,14 @@ public class ThOcrServiceImpl implements IThOcrService{
 				if(listMapVbdef20.size() > 0) {
 					JSONObject bodyJson = new JSONObject();
 					//发票云团队id
-					bodyJson.put("cid", cid_);
+					bodyJson.put("cid", cid);
 					//userId报销人id(OA)
-					bodyJson.put("userId", userid);
+					bodyJson.put("userId", oamap.get("id"));
 					//userId报销人id(OA)
 					bodyJson.put("flag", "3");
 					//发票云发票ID数组
 					bodyJson.put("fids", listVbdef20.toArray());
-					UpdateInvoiceOCR(bodyJson.toString());
+					NcLog.info("发票锁定返回json:"+UpdateInvoiceOCR(bodyJson.toString()));
 					//0 未报销 1 报销中 2已报销
 					String  sreim = "0";
 					if("Y".equals(status)) {
@@ -721,8 +733,8 @@ public class ThOcrServiceImpl implements IThOcrService{
 						sreim = "1";
 					}
 					JSONObject bodyJson1 = new JSONObject();
-					bodyJson1.put("cid", cid_);
-					bodyJson1.put("userId", userid);
+					bodyJson1.put("cid", cid);
+					bodyJson1.put("userId", oamap.get("id"));
 					bodyJson1.put("flag", "2");
 					bodyJson1.put("sreim",sreim);
 					JSONArray arrinfos = new JSONArray();
@@ -732,15 +744,15 @@ public class ThOcrServiceImpl implements IThOcrService{
 						bodyJson2.put("dataid", invoiceVO.getParent().getAttributeValue("pk_invoice"));
 						bodyJson2.put("number", invoiceVO.getParent().getAttributeValue("pk_invoice"));
 						bodyJson2.put("amount", "0".equals(sreim) ? "0.00" : map.get("shortname2"));
-						bodyJson2.put("uid", userid);
+						bodyJson2.put("uid", oamap.get("id"));
 						bodyJson2.put("date",Long.toString(date));
 						bodyJson2.put("name", invoiceVO.getParent().getAttributeValue("pk_invoice"));
 						bodyJson2.put("fid", map.get("name5"));
-						bodyJson2.put("cid", cid_);
+						bodyJson2.put("cid", cid);
 						arrinfos.add(bodyJson2);
 					}
 					bodyJson1.put("infos", arrinfos);
-					UpdateInvoiceOCR(bodyJson1.toString());
+					NcLog.info("发票锁定修改返回json:"+UpdateInvoiceOCR(bodyJson1.toString()));
 				}
 			}
 		}
@@ -750,12 +762,12 @@ public class ThOcrServiceImpl implements IThOcrService{
 		 * @param content
 		 * @throws Exception
 		 */
-		private void UpdateInvoiceOCR(String bodyJson) throws Exception {
-			String content =  InvoiceCloudAESUtil.startAssemble(aesKey, password, bodyJson);
+		private String UpdateInvoiceOCR(String bodyJson) throws Exception {
+			String content =  InvoiceCloudAESUtil.startAssemble(aesKey, secret, bodyJson);
 			HttpClient httpClient = new HttpClient();
 	        PostMethod postMethod = new PostMethod(ec_comm);
 	        postMethod.setRequestHeader("content-type", "application/json");
-	        postMethod.setRequestHeader("appkey", userName);
+	        postMethod.setRequestHeader("appkey", appId);
 	        RequestEntity entity = new StringRequestEntity (content ,"application/json" ,"UTF-8");
 	        postMethod.setRequestEntity(entity);
 	        httpClient.executeMethod(postMethod);
@@ -765,5 +777,24 @@ public class ThOcrServiceImpl implements IThOcrService{
 			if(data == null) {
 				 throw new BusinessException(responseMsg);
 			}
+			return json.toJSONString();
+		}
+		
+		
+		private Map<String,String> QueryOAUserid(String cuserid) throws Exception {
+			StringBuffer sql = new StringBuffer();
+			sql.append("SELECT");
+			sql.append(" oa_user.id,");
+			sql.append(" oa_user.lastname");
+			sql.append(" FROM");
+			sql.append(" sm_user sm_user");
+			sql.append(" left join oa_user oa_user on sm_user.pk_psndoc = TRIM(oa_user.outkey)");
+			sql.append(" WHERE");
+			sql.append(" sm_user.cuserid = '"+cuserid+"'");
+			Map<String,String> map = (Map<String, String>) iuap.executeQuery(sql.toString(), new MapProcessor());
+			if(map == null) {
+				throw new Exception("获取oaid失败,请检查用户和人员是否关联!");
+			}
+			return map;
 		}
 }

+ 4 - 1
pu/pu/src/public/nc/itf/th/IThOcrService.java

@@ -26,7 +26,7 @@ public interface IThOcrService {
 	 * 
 	 * »ñÈ¡token
 	 */
-	public String getToken() throws HttpException, IOException , BusinessException;
+	public String getToken(String billmaker) throws Exception;
 	
 	/**
 	 * ÉÏ´«·¢Æ±
@@ -85,4 +85,7 @@ public interface IThOcrService {
 	 */
 	public void UpdateInvoiceStatus(String status,InvoiceVO[] vos) throws Exception;
 	
+	
+	public void InvoiceCheck(String token,String fid) throws Exception;
+	
 }