package weaver.fna.invoice.utils; import com.engine.personalIncomeTax.biz.RecordsBiz; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NTCredentials; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.lang.StringEscapeUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.params.ConnRouteParams; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import weaver.conn.RecordSet; import weaver.fna.general.FnaCommon; import weaver.fna.general.FnaLogSqlUtil; import weaver.fna.invoice.Constants; import weaver.fna.invoice.common.FnaAbstractInterface; import weaver.fna.invoice.common.FnaInvoiceCommon; import weaver.fna.invoice.sdk.SdkInvoiceUtil; import weaver.general.BaseBean; import weaver.general.GCONST; import weaver.general.MD5; import weaver.general.Util; import weaver.hrm.HrmUserVarify; import weaver.hrm.User; import weaver.systeminfo.SystemEnv; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.math.BigInteger; import java.net.Proxy; import java.net.URLEncoder; import java.nio.charset.Charset; import java.security.MessageDigest; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.*; /** * @Author: lb2018 * @Description:发票云工具类 * @Date: 2020/6/9 10:45 */ public class InvoiceCloudUtil { /** * 判断是否有生效的ocr接口、查验接口、发票云接口,并返回相关接口信息 * @param user * @param queryOcr * @param queryCheck * @param queryCloud * @return */ public static JSONObject getInterfaceInfo(User user,boolean queryOcr,boolean queryCheck,boolean queryCloud){ JSONObject resultJson = new JSONObject(); RecordSet rs = new RecordSet(); if(queryOcr){ boolean existEffectOcr=false; int imageCatalog = 0; String interfaceurl = ""; String appKey = ""; String secret = ""; //如果有生效的未开启分部的ocr,直接使用 rs.executeQuery("select * from fnainvoiceinterface where interfaceType=0 and status=1 and subIdStatus=0 "); if(rs.next()){ imageCatalog = Util.getIntValue(rs.getString("imageCatalog"),0); interfaceurl = Util.null2String(rs.getString("interfaceurl")); appKey = Util.null2String(rs.getString("username")); secret = Util.null2String(rs.getString("password")); existEffectOcr = true; } if(!existEffectOcr){ //查询当前人员所在分部的ocr接口 rs.executeQuery("select * from fnainvoiceinterface where interfaceType=0 and status=1 and subIdStatus=1 "); while (rs.next()){ String subIds = Util.null2String(rs.getString("subIds")); String[] subIds_split = subIds.split(","); for(int i=0;i map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid,Util.date(2),"OA发送请求","获取token",tokenurl,userName,password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content",content, "uuid", uuid, "string"); String token = postToUrl(tokenurl, content, map, "",uuid); //将token存入表中 boolean existHistory=false; rs.executeQuery("select * from fnaInvoiceCloudToken where userId=?",user.getUID()); if(rs.next()){ existHistory = true; } String time = Util.null2String(System.currentTimeMillis()); String updateTime = Util.date(2); if(existHistory){//update rs.executeUpdate("update fnaInvoiceCloudToken set cid=?,token=?,updateTime=?,currentTimeMillis=? where userId=?", cid,token,updateTime,time,user.getUID()); }else{//insert rs.executeUpdate("insert into fnaInvoiceCloudToken(cid,token,updateTime,currentTimeMillis,userId) values(?,?,?,?,?)", cid,token,updateTime,time,user.getUID()); } jsonObject.put("flag",true); jsonObject.put("token",token); }catch (Exception e){ jsonObject.put("flag",false); jsonObject.put("msg",e.getMessage()); } return jsonObject; } /** * 获取cid * @return */ public static JSONObject getCloudCid(String interfaceurl,String account,String corpName,String userName,String password){ JSONObject jsonObject = new JSONObject(); try{ JSONObject param = new JSONObject(); param.put("account",account); param.put("corpName",corpName); String content = invoiceCloudEncode(param.toString(), password); Map map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid,Util.date(2),"OA发送请求","获取CID",interfaceurl,userName,password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content",content, "uuid", uuid, "string"); String cid = postToUrl(interfaceurl, content, map, "",uuid); jsonObject.put("flag",true); jsonObject.put("cid",cid); }catch (Exception e){ jsonObject.put("flag",false); jsonObject.put("msg",e.getMessage()); } return jsonObject; } /** * 更新应用数据 * @return */ public static JSONObject updateAppInfo(String updateAppInfoUrl,String account,String oaUrl,String userName,String password){ JSONObject jsonObject = new JSONObject(); try{ JSONObject param = new JSONObject(); param.put("appName",account); param.put("thirdAddr",oaUrl); String content = invoiceCloudEncode(param.toString(), password); Map map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid,Util.date(2),"OA发送请求","更新应用数据",updateAppInfoUrl,userName,password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content",content, "uuid", uuid, "string"); postToUrl(updateAppInfoUrl, content, map, "",uuid); jsonObject.put("flag",true); }catch (Exception e){ jsonObject.put("flag",false); jsonObject.put("msg",e.getMessage()); } return jsonObject; } /** * 创建应用,获取AppId 和 secret * @param interfaceurl * @param appName * @return */ public static JSONObject CreateApp(String interfaceurl,String appName,String thirdAppId,String thirdAddr){ JSONObject jsonObject = new JSONObject(); try{ JSONObject param = new JSONObject(); param.put("appName",appName); param.put("appType",2); param.put("thirdAppId",thirdAppId); param.put("thirdAddr",thirdAddr); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl) values(?,?,?,?,?)", uuid,Util.date(2),"OA发送请求","创建应用",interfaceurl); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); String data = postToUrl(interfaceurl, param.toString(), null, "",uuid); jsonObject.put("flag",true); jsonObject.put("data",data); }catch (Exception e){ jsonObject.put("flag",false); jsonObject.put("msg",e.getMessage()); } return jsonObject; } /** * 同步开票信息、同步发票信息 * @param interfaceurl * @param param * @param userName * @param password * @return */ public static JSONObject synBillingInfo(String interfaceurl,JSONObject param,String userName,String password){ JSONObject jsonObject = new JSONObject(); try{ String content = invoiceCloudEncode(param.toString(), password); Map map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid,Util.date(2),"OA发送请求","实时同步开票、发票信息",interfaceurl,userName,password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content",content, "uuid", uuid, "string"); String data = postToUrl(interfaceurl, content, map, "",uuid); jsonObject.put("flag",true); jsonObject.put("data",data); }catch (Exception e){ jsonObject.put("flag",false); jsonObject.put("msg",e.getMessage()); } return jsonObject; } /** * 推送目录id * @param synDocUrl * @param cid * @param secid * @param userName * @param password * @return */ public static JSONObject pushSecid(String synDocUrl,String cid,String secid,String userName,String password,int userId){ JSONObject jsonObject = new JSONObject(); try{ JSONObject param = new JSONObject(); param.put("cid",cid); param.put("secid",secid); param.put("userId",userId); String content = invoiceCloudEncode(param.toString(), password); Map map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid,Util.date(2),"OA发送请求","同步目录",synDocUrl,userName,password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content",content, "uuid", uuid, "string"); postToUrl(synDocUrl, content, map, "",uuid); jsonObject.put("flag",true); }catch (Exception e){ jsonObject.put("flag",false); jsonObject.put("msg",e.getMessage()); } return jsonObject; } /** * 获取json中的值 * @param json * @param key * @return */ public static String getString(JSONObject json, String key){ if(json.containsKey(key)){ return json.getString(key); }else{ return ""; } } public static String postToUrl(String url, String content,Map headers,String xmlEncoding,String uuid,String aesKey) throws Exception { return postToUrl(url,content,headers,xmlEncoding,uuid,"",null,aesKey); } public static String postToUrl(String url, String content,Map headers,String xmlEncoding,String uuid) throws Exception { return postToUrl(url,content,headers,xmlEncoding,uuid,"",null,""); } /** * * @param url * @param content * @param headers * @param xmlEncoding * @return * @throws IOException */ public static String postToUrl(String url, String content,Map headers,String xmlEncoding,String uuid,String from,User user,String aesKey) throws Exception { String errorMsg=""; String data=""; if("".equals(xmlEncoding)){ xmlEncoding = "UTF-8"; } DefaultHttpClient httpclient = null; HttpPost httpPost = null; try { httpclient = new DefaultHttpClient(); if(ProxyUtil.ifExistsProxy()){ Map proxyProperty = ProxyUtil.getProxyProperty(); String proxyUrl = proxyProperty.getOrDefault("proxyUrl",""); int proxyPort = Util.getIntValue(proxyProperty.getOrDefault("proxyPort",""),8080); String proxyUser = proxyProperty.getOrDefault("proxyUser",""); String proxyPwd = proxyProperty.getOrDefault("proxyPwd",""); if(!"".equals(proxyUser)&&!"".equals(proxyPwd)) { httpclient.getCredentialsProvider().setCredentials( new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPwd) ); } HttpHost proxy = new HttpHost(proxyUrl,proxyPort); httpclient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,proxy); } httpPost = new HttpPost(url); JSONObject response = null; StringEntity s = new StringEntity(content, Charset.forName("UTF-8")); httpPost.addHeader("Content-type", "application/json; charset=utf-8"); httpPost.addHeader("Accept", "application/json"); //设置超时时间 RequestConfig config = RequestConfig.custom().setConnectTimeout(130000) //连接超时时间 .setConnectionRequestTimeout(130000) .setSocketTimeout(130000) //数据传输的超时时间 .setStaleConnectionCheckEnabled(true) //提交请求前测试连接是否可用 .build(); httpPost.setConfig(config); httpPost.setEntity(s); if (null != headers) { for (Map.Entry entry : headers.entrySet()) { httpPost.addHeader(entry.getKey(), entry.getValue()); } } HttpResponse res = httpclient.execute(httpPost); if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = res.getEntity(); String result = Util.null2String(EntityUtils.toString(res.getEntity())); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "returnResult", result, "uuid", uuid, "string"); if ("".equals(result)) { throw new Exception("发票云接口返回空!"); } try { response = JSONObject.fromObject(result); } catch (Exception e) { throw new Exception("发票云接口返参数json格式化异常!"); } JSONObject actionMsg = response.getJSONObject("actionMsg"); String code = actionMsg.getString("code"); if ("0".equals(code)) {//创建成功 if (response.containsKey("data")) { data = response.getString("data"); //新接口,需要解密 if (!"".equals(aesKey) && !"".equals(data)) { data = InvoiceCloudAESUtil.decrypt(aesKey, data); response.put("data", data); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "returnResult", response.toString(), "uuid", uuid, "string"); } } } else { errorMsg = actionMsg.getString("message"); if ("check".equals(from)) { new BaseBean().writeLog("checkException", errorMsg); FnaInvoiceCommon.saveFailLog(user.getUID(), 1, errorMsg); } throw new Exception(errorMsg); } } else { FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "returnResult", "请求失败HttpStatus=" + res.getStatusLine().getStatusCode(), "uuid", uuid, "string"); throw new Exception("请求失败!"); } }catch (Exception e){ errorMsg = e.getMessage(); }finally { if(httpclient!=null){ httpclient.close(); } } if(!"".equals(errorMsg)){ throw new Exception(errorMsg); } return data; } /** * 发票云加密 * @param content * @param secret * @return */ public static String invoiceCloudEncode(String content,String secret){ long time = System.currentTimeMillis(); content = content+"&time="+time; String md5Content = Md5Encode(content + "&secret=" + secret); content = content+"&md5="+md5Content; return content; } /** * 发票云加密 * @param content * @param secret * @return */ public static String invoiceCloudEncode(String content,String secret,long time){ content = content+"&time="+time; String md5Content = Md5Encode(content + "&secret=" + secret); return md5Content; } /** * md5加密 * @param content * @return */ public static String Md5Encode(String content){ char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; try { byte[] btInput = content.getBytes("UTF-8"); // 获得MD5摘要算法的 MessageDigest 对象 MessageDigest mdInst = MessageDigest.getInstance("MD5"); // 使用指定的字节更新摘要 mdInst.update(btInput); // 获得密文 byte[] md = mdInst.digest(); // 把密文转换成十六进制的字符串形式 int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 根据sql获取发票信息 * @return */ public static JSONArray getInvoiceInfos(String sql,int start_pos,int page_size){ DecimalFormat df = new DecimalFormat("#############################0.00"); JSONArray invoiceArray = new JSONArray(); RecordSet rs = new RecordSet(); RecordSet rs2 = new RecordSet(); RecordSet rs3 = new RecordSet(); RecordSet rs4 = new RecordSet(); String fnainvoiceledger[] = {"id","billingDate","invoiceCode","invoiceNumber","invoiceType","seller","purchaser","invoiceServiceYype", "priceWithoutTax","taxRate","tax","taxIncludedPrice","authenticity","reimbursementDate","reimbursePerson","requestId","userid_new", "invoiceSource_new","checkcode","status","card_id_new","encrypt_code_new","openid_new","wechatstatus","imageID","purchaserTaxNo","salesTaxNo", "entryTime","company_seal","form_type","form_name","kind","category","imageDocId","checkStatus","updateOperate","cloudId","codeConfirm","numberConfirm","requestName", "receiptor","reviewer","issuer","province","city","travel_tax"}; String fnainvoiceledgerdetail[]={"invoiceserviceyype","specification","unit","unitNumber2","originalUnitPrice","priceWithoutTax","taxRate","tax","startDate","endDate","numberPlate","type"}; String fnaInvoiceLedgerDtl[]={"remark","checkCodeSix","sellerAddress","purchaserAddress","salesBank","purchaserBank"}; String trainInvoice[]={"time","name","station_geton","station_getoff","train_number","seat"}; String taxiInvoice[]={"time_geton","time_getoff","mileage","place"}; String machineInvoice[]={"time","category","seller_tax_id","buyer_tax_id"}; String tollInvoice[]={"time","entrance","export"}; String carInvoice[]={"time","station_geton","station_getoff","name"}; String second_carInvoice[]={"seller_id","buyer_id","company_name","company_tax_id","license_plate","registration_number","car_code","car_model"}; String motor_VehicleInvoice[]={"machine_code","machine_number","pretax_amount","seller_tax_id","buyer_id","tax_authorities","tax_authorities_code", "car_code","car_engine_code","car_model","certificate_number"}; String airInvoice[]={"user_name","user_id","agentcode","issue_by","fare","fuel_surcharge","caac_development_fund","insurance"}; String airDtlInvoice[]={"from_city","to_city","flight_number","airdate","airtime","seat","carrier"}; String smallInvoice[]={"storename","time","tips","currency_code","type","discount"}; String fnaDidiInvoice[]={"startDate","endDate","phone"}; String fnaDidiDtlInvoice[]={"carType","time_geton","city","station_geton","station_getoff","mileage","total"}; String fnaBoatInvoice[]={"time","station_geton","station_getoff","name","currency_code"}; //报销状况 String fnaInvoiceReimbursement[]={"requestid","reimbursementAmt","userid","reimbursementDate","requestName"}; int start=0; rs.executeQuery(sql); while (rs.next()){ if(start>=start_pos && page_size>0 ) { JSONObject fnainvoiceledgerJo = new JSONObject(); int id = Util.getIntValue(rs.getString("id"), 0); int invoiceType = Util.getIntValue(rs.getString("invoiceType"), 0); int requestId = Util.getIntValue(rs.getString("requestId"), 0); for (int i = 0; i < fnainvoiceledger.length; i++) { String key = fnainvoiceledger[i]; if("requestName".equals(key)){ String requestName=""; if(requestId>0){ rs2.executeQuery("select requestName from workflow_requestBase where requestId=?",requestId); if(rs2.next()){ requestName = Util.null2String(rs2.getString("requestName")); } } fnainvoiceledgerJo.put("requestName",requestName); }else{ fnainvoiceledgerJo.put(key, Util.null2String(rs.getString(key))); } } JSONArray fnainvoiceledgerdetailJa = new JSONArray(); rs2.executeQuery("select * from fnainvoiceledgerdetail where mainid=?", id); while (rs2.next()) { JSONObject fnainvoiceledgerdetailJo = new JSONObject(); for (int i = 0; i < fnainvoiceledgerdetail.length; i++) { String key = fnainvoiceledgerdetail[i]; fnainvoiceledgerdetailJo.put(key, Util.null2String(rs2.getString(key))); } fnainvoiceledgerdetailJa.add(fnainvoiceledgerdetailJo); } fnainvoiceledgerJo.put("fnainvoiceledgerdetail", fnainvoiceledgerdetailJa); if (invoiceType == 1 || invoiceType == 2 || invoiceType == 15 || invoiceType == 16 || invoiceType == 12 || invoiceType == 3 || invoiceType == 17 || invoiceType == 19 || invoiceType == 20 || invoiceType == 21) { rs3.executeQuery("select * from FnaInvoiceLedgerDtl where mainid=?", id); if (rs3.next()) { JSONObject FnaInvoiceLedgerDtlJo = new JSONObject(); for (int i = 0; i < fnaInvoiceLedgerDtl.length; i++) { String key = fnaInvoiceLedgerDtl[i]; FnaInvoiceLedgerDtlJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("fnaInvoiceLedgerDtl", FnaInvoiceLedgerDtlJo); } } if (invoiceType == 8) {//火车票 rs3.executeQuery("select * from trainInvoice where mainid=?", id); if (rs3.next()) { JSONObject trainInvoiceJo = new JSONObject(); for (int i = 0; i < trainInvoice.length; i++) { String key = trainInvoice[i]; trainInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("trainInvoice", trainInvoiceJo); } } else if (invoiceType == 7) {//出租车发票 rs3.executeQuery("select * from taxiInvoice where mainid=?", id); if (rs3.next()) { JSONObject taxiInvoiceJo = new JSONObject(); for (int i = 0; i < taxiInvoice.length; i++) { String key = taxiInvoice[i]; taxiInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("taxiInvoice", taxiInvoiceJo); } } else if (invoiceType == 3) {//通用机打发票 rs3.executeQuery("select * from machineInvoice where mainid=?", id); if (rs3.next()) { JSONObject machineInvoiceJo = new JSONObject(); for (int i = 0; i < machineInvoice.length; i++) { String key = machineInvoice[i]; machineInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("machineInvoice", machineInvoiceJo); } } else if (invoiceType == 9) {//过路费发票表 rs3.executeQuery("select * from tollInvoice where mainid=?", id); if (rs3.next()) { JSONObject tollInvoiceJo = new JSONObject(); for (int i = 0; i < tollInvoice.length; i++) { String key = tollInvoice[i]; tollInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("tollInvoice", tollInvoiceJo); } } else if (invoiceType == 10) {//客运汽车发票 rs3.executeQuery("select * from carInvoice where mainid=?", id); if (rs3.next()) { JSONObject carInvoiceJo = new JSONObject(); for (int i = 0; i < carInvoice.length; i++) { String key = carInvoice[i]; carInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("carInvoice", carInvoiceJo); } } else if (invoiceType == 11) {//二手车销售统一发票表 rs3.executeQuery("select * from second_carInvoice where mainid=?", id); if (rs3.next()) { JSONObject second_carInvoiceJo = new JSONObject(); for (int i = 0; i < second_carInvoice.length; i++) { String key = second_carInvoice[i]; second_carInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("second_carInvoice", second_carInvoiceJo); } } else if (invoiceType == 12) {//机动车销售统一发票 rs3.executeQuery("select * from motor_VehicleInvoice where mainid=?", id); if (rs3.next()) { JSONObject motor_VehicleInvoiceJo = new JSONObject(); for (int i = 0; i < motor_VehicleInvoice.length; i++) { String key = motor_VehicleInvoice[i]; motor_VehicleInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("motor_VehicleInvoice", motor_VehicleInvoiceJo); } } else if (invoiceType == 14) {//航空运输电子客票行程单 rs3.executeQuery("select * from airInvoice where mainid=?", id); if (rs3.next()) { JSONObject airInvoiceJo = new JSONObject(); for (int i = 0; i < airInvoice.length; i++) { String key = airInvoice[i]; airInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("airInvoice", airInvoiceJo); } rs3.executeQuery("select * from airDtlInvoice where mainid=?", id); JSONArray airDtlInvoiceJa = new JSONArray(); while (rs3.next()) { JSONObject airDtlInvoiceJo = new JSONObject(); for (int i = 0; i < airDtlInvoice.length; i++) { String key = airDtlInvoice[i]; airDtlInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } airDtlInvoiceJa.add(airDtlInvoiceJo); } fnainvoiceledgerJo.put("airDtlInvoice", airDtlInvoiceJa); } else if (invoiceType == 13) {//国际小票 rs3.executeQuery("select * from smallInvoice where mainid=?", id); if (rs3.next()) { JSONObject smallInvoiceJo = new JSONObject(); for (int i = 0; i < smallInvoice.length; i++) { String key = smallInvoice[i]; smallInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("smallInvoice", smallInvoiceJo); } } else if (invoiceType == 22) {//滴滴出行行程单 rs3.executeQuery("select * from fnaDidiInvoice where mainid=?", id); if (rs3.next()) { JSONObject fnaDidiInvoiceJo = new JSONObject(); for (int i = 0; i < fnaDidiInvoice.length; i++) { String key = fnaDidiInvoice[i]; fnaDidiInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("fnaDidiInvoice", fnaDidiInvoice); } rs3.executeQuery("select * from fnaDidiDtlInvoice where mainid=?", id); JSONArray fnaDidiDtlInvoiceJa = new JSONArray(); while (rs3.next()) { JSONObject fnaDidiDtlInvoiceJo = new JSONObject(); for (int i = 0; i < fnaDidiDtlInvoice.length; i++) { String key = fnaDidiDtlInvoice[i]; fnaDidiDtlInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnaDidiDtlInvoiceJa.add(fnaDidiDtlInvoiceJo); } fnainvoiceledgerJo.put("fnaDidiDtlInvoice", fnaDidiDtlInvoiceJa); } else if (invoiceType == 23) {//船票 rs3.executeQuery("select * from fnaBoatInvoice where mainid=?", id); if (rs3.next()) { JSONObject fnaBoatInvoiceJo = new JSONObject(); for (int i = 0; i < fnaBoatInvoice.length; i++) { String key = fnaBoatInvoice[i]; fnaBoatInvoiceJo.put(key, Util.null2String(rs3.getString(key))); } fnainvoiceledgerJo.put("fnaBoatInvoice", fnaBoatInvoiceJo); } } JSONArray fnaInvoiceReimbursementJa = new JSONArray(); rs3.executeQuery("select * from fnaInvoiceReimbursement where mainid=?", id); while (rs3.next()) { int requestid3 = Util.getIntValue(rs3.getString("requestId"),0); JSONObject fnaInvoiceReimbursementJo = new JSONObject(); for (int i = 0; i < fnaInvoiceReimbursement.length; i++) { String key = fnaInvoiceReimbursement[i]; if("requestName".equals(key)){ String requestName=""; if(requestid3>0){ rs4.executeQuery("select requestName from workflow_requestBase where requestId=?",requestid3); if(rs4.next()){ requestName = Util.null2String(rs4.getString("requestName")); } } fnaInvoiceReimbursementJo.put("requestName",requestName); }else{ fnaInvoiceReimbursementJo.put(key, Util.null2String(rs3.getString(key))); } } fnaInvoiceReimbursementJa.add(fnaInvoiceReimbursementJo); } fnainvoiceledgerJo.put("fnaInvoiceReimbursement", fnaInvoiceReimbursementJa); invoiceArray.add(fnainvoiceledgerJo); page_size--; } start++; } return invoiceArray; } /** * 实时同步更新发票信息 * @param infoJson * @return cloudId和id */ public static JSONObject updateInvoiceInfo(JSONObject infoJson,String ipAdress) throws Exception { JSONObject jsonObject = new JSONObject(); String fnainvoiceledger[] = {"billingDate","invoiceCode","invoiceNumber","invoiceType","seller","purchaser","invoiceServiceYype", "priceWithoutTax","taxRate","tax","taxIncludedPrice","authenticity","userid_new",//"reimbursementDate","reimbursePerson","requestId" 报销信息无需从发票云获取 "invoiceSource_new","checkcode","status","card_id_new","encrypt_code_new","openid_new","wechatstatus","imageID","purchaserTaxNo","salesTaxNo", "entryTime","company_seal","form_type","form_name","kind","category","imageDocId","checkStatus","updateOperate","cloudId","codeConfirm","numberConfirm", "receiptor","reviewer","issuer","province","city","travel_tax"}; String fnainvoiceledgerdetail[]={"invoiceserviceyype","specification","unit","unitNumber2","originalUnitPrice","priceWithoutTax","taxRate","tax","startDate","endDate","numberPlate","type"}; String fnaInvoiceLedgerDtl[]={"remark","checkCodeSix","sellerAddress","purchaserAddress","salesBank","purchaserBank"}; String trainInvoice[]={"time","name","station_geton","station_getoff","train_number","seat"}; String taxiInvoice[]={"time_geton","time_getoff","mileage","place"}; String machineInvoice[]={"time","category","seller_tax_id","buyer_tax_id"}; String tollInvoice[]={"time","entrance","export"}; String carInvoice[]={"time","station_geton","station_getoff","name"}; String second_carInvoice[]={"seller_id","buyer_id","company_name","company_tax_id","license_plate","registration_number","car_code","car_model"}; String motor_VehicleInvoice[]={"machine_code","machine_number","pretax_amount","seller_tax_id","buyer_id","tax_authorities","tax_authorities_code", "car_code","car_engine_code","car_model","certificate_number"}; String airInvoice[]={"user_name","user_id","agentcode","issue_by","fare","fuel_surcharge","caac_development_fund","insurance"}; String airDtlInvoice[]={"from_city","to_city","flight_number","airdate","airtime","seat","carrier"}; String smallInvoice[]={"storename","time","tips","currency_code","type","discount"}; String fnaDidiInvoice[]={"startDate","endDate","phone"}; String fnaDidiDtlInvoice[]={"carType","time_geton","city","station_geton","station_getoff","mileage","total"}; String fnaBoatInvoice[]={"time","station_geton","station_getoff","name","currency_code"}; String formatDoubleColumn="priceWithoutTax,taxRate,tax,taxIncludedPrice,mileage,pretax_amount,travel_tax,total";//需要格式化金额的列,用逗号隔开 List formatDoubleList = Arrays.asList(formatDoubleColumn.split(",")); String formatIntColumn="invoiceType,authenticity,reimbursePerson,requestId,userid_new,invoiceSource_new," + "status,imageID,company_seal,imageDocId,checkStatus,updateOperate"; List formatIntList = Arrays.asList(formatIntColumn.split(",")); int id = Util.getIntValue(infoJson.getString("id"),0); String cloudId= Util.null2String(infoJson.getString("cloudId")); int invoiceType = Util.getIntValue(infoJson.getString("invoiceType"),0); int userid_new = Util.getIntValue(infoJson.getString("userid_new"),0); String invoiceNumber= Util.null2String(infoJson.getString("invoiceNumber")); String invoiceCode= Util.null2String(infoJson.getString("invoiceCode")); RecordSet rs = new RecordSet(); ListvalueList =new ArrayList(); //脏数据优化 if(id==0){ rs.executeQuery("select id from fnainvoiceLedger where invoicenumber=? and invoiceCode=?",invoiceNumber,invoiceCode); if(rs.next()){ id = Util.getIntValue(rs.getString("id"),0); } }else{ //判断该id在台账中是否存在 int cnt = 0; rs.executeQuery("select count(id) cnt from fnainvoiceLedger where id=?",id); if(rs.next()){ cnt= Util.getIntValue(rs.getString("cnt"),0); } if(cnt==0){ id=0; } } synchronized(InvoiceCloudUtil.class) { //插入日志1 FnaLogSqlUtil fnaLogSqlUtil = new FnaLogSqlUtil(); Map labelIdMap = new HashMap(); Map beforeMap = new HashMap(); String opType="0"; if(id>0){ opType="2"; beforeMap = fnaLogSqlUtil.getInvoiceMap(String.valueOf(id),labelIdMap); } //修改主表数据 fnainvoiceledger String sql = getsqlByColumn(id, fnainvoiceledger, infoJson, valueList, formatDoubleList,formatIntList,"fnainvoiceledger"); rs.executeUpdate(sql,valueList); if(id<=0){ rs.executeQuery("select id from fnainvoiceledger where cloudId=?",cloudId); if(rs.next()){ id = Util.getIntValue(rs.getString("id")); } } //修改明细表数据 fnainvoiceledgerdetail rs.executeUpdate("delete from fnainvoiceledgerdetail where mainid=?", id); if (infoJson.containsKey("fnainvoiceledgerdetail")) { String fnainvoiceledgerdetailStr = infoJson.getString("fnainvoiceledgerdetail"); if (fnainvoiceledgerdetailStr != null && !"".equals(fnainvoiceledgerdetailStr)) { JSONArray infoJsonArray = infoJson.getJSONArray("fnainvoiceledgerdetail"); for (int i = 0; i < infoJsonArray.size(); i++) { JSONObject infoJsonDtl = infoJsonArray.getJSONObject(i); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, fnainvoiceledgerdetail, infoJsonDtl, valueList, formatDoubleList, formatIntList, "fnainvoiceledgerdetail"); rs.executeUpdate(sql, valueList); } } } } if (invoiceType == 1 || invoiceType == 2 || invoiceType == 15 || invoiceType == 16 || invoiceType == 12 || invoiceType == 3 || invoiceType == 17 || invoiceType == 19 || invoiceType == 20 || invoiceType == 21) { //修改明细表数据 fnaInvoiceLedgerDtl rs.executeUpdate("delete from fnaInvoiceLedgerDtl where mainid=?", id); if (infoJson.containsKey("fnaInvoiceLedgerDtl")) { JSONObject infoJsonDtl = infoJson.getJSONObject("fnaInvoiceLedgerDtl"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, fnaInvoiceLedgerDtl, infoJsonDtl, valueList, formatDoubleList, formatIntList, "fnaInvoiceLedgerDtl"); rs.executeUpdate(sql, valueList); } } } if (invoiceType == 8) {//火车票 //修改明细表数据 trainInvoice rs.executeUpdate("delete from trainInvoice where mainid=?", id); if (infoJson.containsKey("trainInvoice")) { JSONObject infoJsonDtl = infoJson.getJSONObject("trainInvoice"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, trainInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "trainInvoice"); rs.executeUpdate(sql, valueList); } } } else if (invoiceType == 7) {//出租车发票 //修改明细表数据 taxiInvoice rs.executeUpdate("delete from taxiInvoice where mainid=?", id); if (infoJson.containsKey("taxiInvoice")) { JSONObject infoJsonDtl = infoJson.getJSONObject("taxiInvoice"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, taxiInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "taxiInvoice"); rs.executeUpdate(sql, valueList); } } } else if (invoiceType == 3) {//通用机打发票 //修改明细表数据 machineInvoice rs.executeUpdate("delete from machineInvoice where mainid=?", id); if (infoJson.containsKey("machineInvoice")) { JSONObject infoJsonDtl = infoJson.getJSONObject("machineInvoice"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, machineInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "machineInvoice"); rs.executeUpdate(sql, valueList); } } } else if (invoiceType == 9) {//过路费发票表 //修改明细表数据 tollInvoice rs.executeUpdate("delete from tollInvoice where mainid=?", id); if (infoJson.containsKey("tollInvoice")) { JSONObject infoJsonDtl = infoJson.getJSONObject("tollInvoice"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, tollInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "tollInvoice"); rs.executeUpdate(sql, valueList); } } } else if (invoiceType == 10) {//客运汽车发票 //修改明细表数据 carInvoice rs.executeUpdate("delete from carInvoice where mainid=?", id); if (infoJson.containsKey("carInvoice")) { JSONObject infoJsonDtl = infoJson.getJSONObject("carInvoice"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, carInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "carInvoice"); rs.executeUpdate(sql, valueList); } } } else if (invoiceType == 11) {//二手车销售统一发票表 //修改明细表数据 second_carInvoice rs.executeUpdate("delete from second_carInvoice where mainid=?", id); if (infoJson.containsKey("second_carInvoice")) { JSONObject infoJsonDtl = infoJson.getJSONObject("second_carInvoice"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, second_carInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "second_carInvoice"); rs.executeUpdate(sql, valueList); } } } else if (invoiceType == 12) {//机动车销售统一发票 //修改明细表数据 motor_VehicleInvoice rs.executeUpdate("delete from motor_VehicleInvoice where mainid=?", id); if (infoJson.containsKey("motor_VehicleInvoice")) { JSONObject infoJsonDtl = infoJson.getJSONObject("motor_VehicleInvoice"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, motor_VehicleInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "motor_VehicleInvoice"); rs.executeUpdate(sql, valueList); } } } else if (invoiceType == 14) {//航空运输电子客票行程单 //修改明细表数据 airInvoice rs.executeUpdate("delete from airInvoice where mainid=?", id); if (infoJson.containsKey("airInvoice")) { JSONObject infoJsonDtl = infoJson.getJSONObject("airInvoice"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, airInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "airInvoice"); rs.executeUpdate(sql, valueList); } } rs.executeUpdate("delete from airDtlInvoice where mainid=?", id); if (infoJson.containsKey("airDtlInvoice")) { String airDtlInvoiceStr = infoJson.getString("airDtlInvoice"); if (airDtlInvoiceStr != null && !"".equals(airDtlInvoiceStr)) { JSONArray infoJsonArray = infoJson.getJSONArray("airDtlInvoice"); for (int i = 0; i < infoJsonArray.size(); i++) { JSONObject infoJsonDtl = infoJsonArray.getJSONObject(i); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, airDtlInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "airDtlInvoice"); rs.executeUpdate(sql, valueList); } } } } } else if (invoiceType == 13) {//国际小票 //修改明细表数据 smallInvoice rs.executeUpdate("delete from smallInvoice where mainid=?", id); if (infoJson.containsKey("smallInvoice")) { JSONObject infoJsonDtl = infoJson.getJSONObject("smallInvoice"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, smallInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "smallInvoice"); rs.executeUpdate(sql, valueList); } } } else if (invoiceType == 22) {//船票 //修改明细表数据 fnaBoatInvoice rs.executeUpdate("delete from fnaBoatInvoice where mainid=?", id); if (infoJson.containsKey("fnaBoatInvoice")) { JSONObject infoJsonDtl = infoJson.getJSONObject("fnaBoatInvoice"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, fnaBoatInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "fnaBoatInvoice"); rs.executeUpdate(sql, valueList); } } } else if (invoiceType == 23) {//滴滴出行行程单 //修改明细表数据 fnaDidiInvoice rs.executeUpdate("delete from fnaDidiInvoice where mainid=?", id); if (infoJson.containsKey("fnaDidiInvoice")) { JSONObject infoJsonDtl = infoJson.getJSONObject("fnaDidiInvoice"); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, fnaDidiInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "fnaDidiInvoice"); rs.executeUpdate(sql, valueList); } } rs.executeUpdate("delete from fnaDidiDtlInvoice where mainid=?", id); if (infoJson.containsKey("fnaDidiDtlInvoice")) { String fnaDidiDtlInvoiceStr = infoJson.getString("fnaDidiDtlInvoice"); if (fnaDidiDtlInvoiceStr != null && !"".equals(fnaDidiDtlInvoiceStr)) { JSONArray infoJsonArray = infoJson.getJSONArray("fnaDidiDtlInvoice"); for (int i = 0; i < infoJsonArray.size(); i++) { JSONObject infoJsonDtl = infoJsonArray.getJSONObject(i); if (infoJsonDtl != null && infoJsonDtl.size() > 0) { sql = getsqlByColumn(id, fnaDidiDtlInvoice, infoJsonDtl, valueList, formatDoubleList, formatIntList, "fnaDidiDtlInvoice"); rs.executeUpdate(sql, valueList); } } } } } //插入日志2 Map afterMap = fnaLogSqlUtil.getInvoiceMap(String.valueOf(id), labelIdMap); fnaLogSqlUtil.invoiceLedgerAddlog(new User(1), ipAdress, opType, String.valueOf(id), "2", beforeMap, afterMap, labelIdMap); } jsonObject.put("id",String.valueOf(id)); jsonObject.put("cloudId",cloudId); return jsonObject; } /** * 通过数据库字段获取sql * @return */ public static String getsqlByColumn(int id,String tableNameArray[] ,JSONObject infoJson,ListvalueList, List formatDoubleList,List formatIntList,String tableName){ DecimalFormat df = new DecimalFormat("###############################0.00"); StringBuffer sql=new StringBuffer(); StringBuffer sqlPlaceHolder=new StringBuffer(); valueList.clear(); if(id<=0 || !"fnainvoiceledger".equals(tableName)){ sql.append("insert into ").append(tableName).append("("); sqlPlaceHolder.append(" values("); if(!"fnainvoiceledger".equals(tableName)){//明细表,插入mainid sql.append("mainid,"); sqlPlaceHolder.append("?,"); valueList.add(String.valueOf(id)); } for(int i=0;i map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid, Util.date(2), "OA发送请求", "发票识别-发票云", ocrUrl, userName, password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content", content, "uuid", uuid, "string"); String data = postToUrl(ocrUrl, content, map, "", uuid, "check", user,""); new BaseBean().writeLog("--------fnaLog--------发票云返回data:" + data); JSONObject dataJson = JSONObject.fromObject(data); //解析报文开始 JSONObject dataJsonObj = new JSONObject(); //封装的接口返回标准信息 JSONArray identify_results = new JSONArray(); //识别结果数据 JSONArray infoArrayJson = dataJson.getJSONArray("infos"); int infoArrayJson_len = infoArrayJson.size(); for (int i = 0; i < infoArrayJson_len; i++) { JSONObject infoArrayJsonElt = infoArrayJson.getJSONObject(i); String ret = infoArrayJsonElt.getString("ret"); if ("0".equals(ret)) { //获取发票云相关信息对象 JSONObject infoJson = infoArrayJsonElt.getJSONObject("info"); JSONObject modify_infoJson = infoJson.getJSONObject("modify_info"); JSONObject comm_infoJson = infoJson.getJSONObject("comm_info"); JSONObject proJson = comm_infoJson.getJSONObject("pro"); JSONObject priceJson = comm_infoJson.getJSONObject("price"); JSONObject buyerJson = comm_infoJson.getJSONObject("buyer"); JSONObject payerJson = comm_infoJson.getJSONObject("payer"); //不常用信息 String ext = infoJson.getString("ext"); JSONObject extJson = JSONObject.fromObject(ext); //转化为标准的结构 JSONObject identifyObj = new JSONObject(); JSONObject detailsObj = new JSONObject(); String type = proJson.getString("type"); String standardType =Constants.STANDARDTYPE.get(type); if ("".equals(standardType)) { //识别失败 resultJsonObj.put("result", 0); resultJsonObj.put("message", "发票识别失败!"); return resultJsonObj; } identifyObj.put("type", standardType); identifyObj.put("orientation", ""); identifyObj.put("region", infoJson.getString("region")); if ("1".equals(type) || "2".equals(type) || "3".equals(type) || "4".equals(type) || "5".equals(type) || "22".equals(type) || "28".equals(type) ) { //增值税 //if ("1".equals(type) || "2".equals(type) || "3".equals(type) || "5".equals(type)) { //增值税 detailsObj.put("code", getString(infoJson, "code")); detailsObj.put("number", getString(infoJson, "number")); detailsObj.put("date", getString(proJson, "date")); detailsObj.put("pretax_amount", getString(priceJson, "amount")); detailsObj.put("total", getString(priceJson, "total")); detailsObj.put("tax", getString(extJson, "ttax")); detailsObj.put("check_code", getString(proJson, "chcode")); detailsObj.put("seller", getString(payerJson, "company")); detailsObj.put("seller_tax_id", getString(payerJson, "tcode")); detailsObj.put("buyer", getString(buyerJson, "company")); detailsObj.put("buyer_tax_id", getString(buyerJson, "tcode")); detailsObj.put("company_seal", getString(extJson, "corp_seal")); detailsObj.put("form_type", getString(extJson, "form_type")); detailsObj.put("form_name", getString(extJson, "form_name")); detailsObj.put("kind", getString(proJson, "kind")); detailsObj.put("ciphertext", getString(extJson, "ciphertext")); detailsObj.put("transit_mark", getString(extJson, "transit")); detailsObj.put("oil_mark", getString(extJson, "oil_mark")); detailsObj.put("machine_code", getString(extJson, "machine_code")); detailsObj.put("travel_tax", getString(extJson, "travel_tax")); detailsObj.put("receiptor", getString(extJson, "receiptor")); detailsObj.put("reviewer", getString(extJson, "reviewer")); detailsObj.put("issuer", getString(extJson, "issuer")); detailsObj.put("province", getString(extJson, "province")); detailsObj.put("city", getString(extJson, "city")); detailsObj.put("service_name", getString(extJson, "service_name")); detailsObj.put("remark", getString(extJson, "comment")); detailsObj.put("code_confirm", getString(extJson, "code_confirm")); detailsObj.put("number_confirm", getString(extJson, "number_confirm")); String item_names = ""; if(extJson.containsKey("products")) { JSONArray productsArrayJson = extJson.getJSONArray("products"); for (int j = 0; j < productsArrayJson.size(); j++) { if (!"".equals(item_names)) { item_names += ","; } item_names += productsArrayJson.getJSONObject(j).get("name"); } } detailsObj.put("item_names", item_names); detailsObj.put("agent_mark", getString(extJson, "agent_mark")); detailsObj.put("acquisition_mark", getString(extJson, "acquisition")); detailsObj.put("block_chain", getString(extJson, "block_chain")); if("22".equals(type)){//区块链发票 detailsObj.put("block_chain", "1"); } if("5".equals(type)){//通行费 detailsObj.put("transit_mark", "1"); } detailsObj.put("code_confirm", getString(extJson, "code_confirm")); detailsObj.put("number_confirm", getString(extJson, "number_confirm")); JSONArray items = new JSONArray();//发票明细 if(extJson.containsKey("products")){ JSONArray productsArray = extJson.getJSONArray("products"); for(int j=0;j map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid, Util.date(2),"OA发送请求","发票查验",checkUrl,userName,password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content",content, "uuid", uuid, "string"); String data = postToUrl(checkUrl, content, map, "", uuid,"check",user,""); JSONObject dataJson = JSONObject.fromObject(data); //解析报文开始 JSONObject infoJson = dataJson.getJSONObject("info"); JSONObject comm_infoJson = infoJson.getJSONObject("comm_info"); JSONObject proJson = comm_infoJson.getJSONObject("pro"); JSONObject priceJson = comm_infoJson.getJSONObject("price"); JSONObject buyerJson = comm_infoJson.getJSONObject("buyer"); JSONObject payerJson = comm_infoJson.getJSONObject("payer"); String ext = infoJson.getString("ext"); JSONObject extJson = JSONObject.fromObject(ext); String status = getString(proJson,"status");//状态 if("2".equals(status)){ //记录错误信息 FnaInvoiceCommon.saveFailLog(user.getUID(), 1, SystemEnv.getHtmlLabelName(509480, user.getLanguage())); throw new Exception(SystemEnv.getHtmlLabelName(509480, user.getLanguage())); } String type = getString(proJson,"type");//标题 String title = getString(proJson,"cname");//标题 String _taxIncludedPrice = getString(priceJson,"total");//价税合计 String _priceWithoutTax = getString(priceJson,"amount");//不含税金额 String _tax = getString(extJson,"ttax"); String _invoiceNumber = getString(infoJson,"number");//发票号码 String _invoicecode = getString(infoJson,"code");//代码 String _checkCode = getString(proJson,"chcode");//校验码 String _purchaser = getString(buyerJson,"company");//购买方 String _seller = getString(payerJson,"company");//销售方 String _purchaserTaxNo = getString(buyerJson,"tcode");//购买方纳税人识别号 String _salesTaxNo = getString(payerJson,"tcode");//销售方纳税人识别号 String _billingDate = getString(proJson,"date");//开票日期 String _remark = getString(extJson,"comment");//备注信息 String _sellerAddress = getString(extJson,"pcontact");//销售方电话、地址 String _purchaserAddress = getString(extJson,"bcontact");//购买方电话、地址 String _salesBank = getString(extJson,"pbank");//销售方开户行及账号 String _purchaserBank = getString(extJson,"bbank");//购买方开户行及账号 String _taxRate = getString(extJson,"trate");//税率 String _invoiceServiceYype = "";//货物或应税服务类型 JSONArray items = new JSONArray();//发票明细 if(extJson.containsKey("products")){ JSONArray productsArray = extJson.getJSONArray("products"); for(int i=0;i map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid, Util.date(2), "OA发送请求", "同步转移归属人/共享人信息到发票云", interfaceurl, userName, password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content", content, "uuid", uuid, "string"); String data = postToUrl(interfaceurl, content, map, "", uuid); jsonObject.put("flag", true); jsonObject.put("data", data); } catch (Exception e) { jsonObject.put("flag", false); jsonObject.put("msg", e.getMessage()); } return jsonObject; } /** * @Description: 同步共享人信息到发票云 * @param interfaceurl * @param param * @param userName * @param password * @return: net.sf.json.JSONObject * @Author: konghang * @Date: 10:04 2020/10/14 */ public static JSONObject sysSharerInfo(String interfaceurl, JSONObject param, String userName, String password) { JSONObject jsonObject = new JSONObject(); try { String content = invoiceCloudEncode(param.toString(), password); Map map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid, Util.date(2), "OA发送请求", "同步共享人信息到发票云", interfaceurl, userName, password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content", content, "uuid", uuid, "string"); String data = postToUrl(interfaceurl, content, map, "", uuid); jsonObject.put("flag", true); jsonObject.put("data", data); } catch (Exception e) { jsonObject.put("flag", false); jsonObject.put("msg", e.getMessage()); } return jsonObject; } /** * 获取发票使用次数接口 * @param cid * @param userId * @return */ public static JSONObject getInvoiceUsedNumberInfo(String cid,int userId){ JSONObject invoiceCloudUrl = InvoiceCloudUtil.getInvoiceCloudUrl(); RecordSet rs = new RecordSet(); String userName=""; String password=""; String javaUrl=""; rs.executeQuery("select * from fnaInvoiceInterface where cid=?",cid); if(rs.next()){ userName = Util.null2String(rs.getString("userName")); password = FnaInvoiceCommon.fnaDecrypt(Util.null2String(rs.getString("password"))); javaUrl = Util.null2String(rs.getString("javaUrl")); } return getInvoiceUsedNumberInfo(javaUrl+"/"+invoiceCloudUrl.getString("invoiceUsedNumberInfo"),cid,userName,password,userId); } /** * 获取发票使用次数接口 * @param invoiceUsedNumberInfoUrl * @param cid * @param userName * @param password * @param userId * @return */ public static JSONObject getInvoiceUsedNumberInfo(String invoiceUsedNumberInfoUrl,String cid,String userName,String password,int userId){ JSONObject jsonObject = new JSONObject(); try{ JSONObject param = new JSONObject(); param.put("cid",cid); param.put("userId",userId); String content = invoiceCloudEncode(param.toString(), password); Map map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid,Util.date(2),"OA发送请求","获取发票使用次数",invoiceUsedNumberInfoUrl,userName,password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content",content, "uuid", uuid, "string"); String data = postToUrl(invoiceUsedNumberInfoUrl, content, map, "",uuid); jsonObject.put("flag",true); jsonObject.put("data",data); }catch (Exception e){ jsonObject.put("flag",false); jsonObject.put("msg",e.getMessage()); } return jsonObject; } /** * 获取AES加密的key * @param invoiceUsedNumberInfoUrl * @param cid * @param userName * @param password * @param userId * @return */ public static JSONObject createAesKey(String createAesKeyUrl,String password,String userName){ JSONObject jsonObject = new JSONObject(); try{ String content = invoiceCloudEncode("", password); Map map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid,Util.date(2),"OA发送请求","获取AES加密的key",createAesKeyUrl,userName,password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content",content, "uuid", uuid, "string"); String data = postToUrl(createAesKeyUrl, content, map, "",uuid); jsonObject.put("flag",true); jsonObject.put("data",data); }catch (Exception e){ jsonObject.put("flag",false); jsonObject.put("msg",e.getMessage()); } return jsonObject; } /** * 报销校验 * @param reimburseUrl * @param aesKey * @param bodyJson * @param userName * @param password * @param description * @return */ public static JSONObject reimburseInvoice(String reimburseUrl,String aesKey ,JSONObject bodyJson ,String userName,String password,String description){ JSONObject jsonObject = new JSONObject(); try{ String bodyStr = InvoiceCloudAESUtil.encrypt(aesKey,bodyJson.toString()); String content = invoiceCloudEncode(bodyStr.toString(), password); Map map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid,Util.date(2),"OA发送请求",description,reimburseUrl,userName,password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", bodyJson.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content",content, "uuid", uuid, "string"); String data = postToUrl(reimburseUrl, content, map, "",uuid,aesKey); jsonObject.put("flag",true); jsonObject.put("data",data); }catch (Exception e){ jsonObject.put("flag",false); jsonObject.put("msg",e.getMessage()); } return jsonObject; } /** * 发票云附件上传 * @param imageBytes * @param cid * @param ocrUrl * @param tokenUrl * @param uploadUrl * @param user * @param userName * @param password * @return * @throws Exception */ public static JSONArray uploadFileInvoice(byte[] imageBytes, String cid, String ocrUrl,String localCheckUrl,String tokenUrl,String uploadUrl, User user, String userName, String password) throws Exception { JSONArray uoloadFileArray = new JSONArray(); //1.先上传图片获取图片id String imageId = getImageId(imageBytes,cid,tokenUrl,uploadUrl,user, userName, password); //2.识别 JSONObject param = new JSONObject(); param.put("cid", cid); param.put("userId", user.getUID()); param.put("image_index", imageId); param.put("operate_type", "0"); param.put("is_save", "0"); String content = invoiceCloudEncode(param.toString(), password); Map map = new HashMap(); map.put("appkey", userName); //插入日志表fnaInvoiceCloudLog RecordSet rs = new RecordSet(); String uuid = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid, Util.date(2), "OA发送请求", "发票识别新-发票云", ocrUrl, userName, password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param.toString(), "uuid", uuid, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content", content, "uuid", uuid, "string"); String data = postToUrl(ocrUrl, content, map, "", uuid, "check", user,""); JSONObject dataJson = JSONObject.fromObject(data); //插入调用记录日志 JSONObject jo = new JSONObject(); jo.put("status","0"); jo.put("returnInfo", dataJson); FnaInvoiceCommon.addFnainvoiceLog("1",imageBytes,"","","","","", jo, user, "OCR",""); JSONArray infoArrayJson = dataJson.getJSONArray("infos"); String open_valid = Util.null2String(getString(dataJson,"open_valid")); int infoArrayJson_len = infoArrayJson.size(); if(infoArrayJson_len==0){ FnaInvoiceCommon.saveFailLog(user.getUID(), 0, SystemEnv.getHtmlLabelName(509482, user.getLanguage()));//识别结果为空! throw new Exception(SystemEnv.getHtmlLabelName( 509587,user.getLanguage())); } for (int i = 0; i < infoArrayJson_len; i++) { JSONObject uploadFileJson = new JSONObject(); JSONObject infoArrayJsonElt = infoArrayJson.getJSONObject(i); String ret = Util.null2String(getString(infoArrayJsonElt,"ret")); JSONObject infoJson = infoArrayJsonElt.getJSONObject("info"); JSONObject comm_info = infoJson.getJSONObject("comm_info"); JSONObject pro = comm_info.getJSONObject("pro"); String type = Util.null2String(getString(infoJson,"type")); String cloudId = Util.null2String(getString(infoJson,"fid")); String invoiceNumber = Util.null2String(getString(infoJson,"number")); if ("0".equals(ret)) { if("1".equals(open_valid) && ("1".equals(type) || "2".equals(type) || "3".equals(type) || "4".equals(type) || "5".equals(type) || "6".equals(type) || "7".equals(type))){//需要查验 JSONObject param1 = new JSONObject(); param1.put("fid", cloudId); String content1 = invoiceCloudEncode(param1.toString(), password); Map map1 = new HashMap(); map1.put("appkey", userName); String uuid1 = FnaCommon.getPrimaryKeyGuid1(); rs.executeUpdate("insert into fnaInvoiceCloudLog(uuid,enterTime,requestType,description,interfaceurl,userName,password) values(?,?,?,?,?,?,?)", uuid1, Util.date(2), "OA发送请求", "发票查验新-发票云", localCheckUrl, userName, password); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "param", param1.toString(), "uuid", uuid1, "string"); FnaCommon.updateDbClobOrTextFieldValue("fnaInvoiceCloudLog", "md5Content", content1, "uuid", uuid1, "string"); try{ String data1 = postToUrl(localCheckUrl, content1, map1, "", uuid1,"check",user,""); JSONObject dataJson1 = JSONObject.fromObject(data1); JSONObject infoJson1 = dataJson1.getJSONObject("info"); JSONObject comm_infoJson = infoJson1.getJSONObject("comm_info"); JSONObject proJson = comm_infoJson.getJSONObject("pro"); String status = getString(proJson,"status");//状态 if("2".equals(status)){ //记录错误信息 FnaInvoiceCommon.saveFailLog(user.getUID(), 1, SystemEnv.getHtmlLabelName(509480, user.getLanguage())); uploadFileJson.put("msg",invoiceNumber+":"+SystemEnv.getHtmlLabelName(509480, user.getLanguage())); uploadFileJson.put("flag",false); }else{ uploadFileJson.put("flag",true); } }catch (Exception e){ uploadFileJson.put("flag",false); uploadFileJson.put("msg",invoiceNumber+":"+e.getMessage()); } }else{ uploadFileJson.put("flag",true); } uploadFileJson.put("cloudId",cloudId); }else{ uploadFileJson.put("cloudId",cloudId); uploadFileJson.put("msg",Util.null2String(getString(infoArrayJsonElt,"message"))); uploadFileJson.put("flag",true); } uoloadFileArray.add(uploadFileJson); } return uoloadFileArray; } }