diff --git a/uapbd/src/private/nc/bs/uapbd/bip/workplugin/ErpSaleOrderToBIPBackgroupWorkPlugin.java b/uapbd/src/private/nc/bs/uapbd/bip/workplugin/ErpSaleOrderToBIPBackgroupWorkPlugin.java new file mode 100644 index 0000000..e533baa --- /dev/null +++ b/uapbd/src/private/nc/bs/uapbd/bip/workplugin/ErpSaleOrderToBIPBackgroupWorkPlugin.java @@ -0,0 +1,597 @@ +package nc.bs.uapbd.bip.workplugin; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.google.gson.Gson; +import com.yonyou.cloud.utils.StringUtils; +import nc.bs.dao.BaseDAO; +import nc.bs.dao.DAOException; +import nc.bs.pub.pa.PreAlertObject; +import nc.bs.pub.taskcenter.BgWorkingContext; +import nc.bs.pub.taskcenter.IBackgroundWorkPlugin; +import nc.bs.trade.business.HYSuperDMO; +import nc.jdbc.framework.processor.MapListProcessor; +import nc.util.mmf.framework.base.MMValueCheck; +import nc.vo.bd.defdoc.DefdocVO; +import nc.vo.pub.BusinessException; +import org.apache.http.client.config.CookieSpecs; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.util.EntityUtils; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.zip.GZIPInputStream; + + + +public class ErpSaleOrderToBIPBackgroupWorkPlugin implements IBackgroundWorkPlugin { + + + private HYSuperDMO superDMO = null; + private String baseUrl="https://www.tkkfbip.com"; + + + public HYSuperDMO getSuperDMO() { + + if(superDMO == null) { + superDMO = new HYSuperDMO(); + } + + return superDMO; + } + + /** + * 检查bip参数是否完整 + * @return + */ + private Map checkBipParam(){ + + Map map = new HashMap(); + + String strWhere=" pk_defdoclist in (select pk_defdoclist from bd_defdoclist where code='BIP-sq' and dr=0 ) and dr=0"; + + try { + DefdocVO[] defdocVOs=(DefdocVO[]) getSuperDMO().queryByWhereClause(DefdocVO.class, strWhere); + if(defdocVOs!=null&&defdocVOs.length>0) { + for (DefdocVO defdocVO : defdocVOs) { + + map.put(defdocVO.getCode().trim(), defdocVO.getName()); + + } + } + } catch (DAOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return map; + + } + + @Override + public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException { + // TODO Auto-generated method stub + + Date date = new Date(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String dateTime = dateFormat.format(date); + + Map bipParamMap= checkBipParam(); + if(bipParamMap.isEmpty()) { + return null; + } + + baseUrl=bipParamMap.get("baseUrl"); + Map userMap=arg0.getKeyMap(); + String usercustcode=""; + + if(!userMap.isEmpty()) { + if(userMap.get("code")!=null) { + usercustcode=userMap.get("code").toString(); + } + + } + + String invoiceReceiveUrl = bipParamMap.get("contractSaleOrder");//开票申请单回传 + if(StringUtils.isEmpty(invoiceReceiveUrl)) { + return null; + } + + + Gson gson = new Gson(); + + + //获取未推送合同销售订单更新的销售发票信息; + String sql=" select so_saleinvoice_b.cfirstbid,csaleinvoicebid from so_saleinvoice,so_saleinvoice_b where so_saleinvoice.csaleinvoiceid=so_saleinvoice_b.csaleinvoiceid and so_saleinvoice.fstatusflag=2 and nvl(so_saleinvoice_b.vbdef10,'N') in ('~','N') and nvl(so_saleinvoice.vdef13,'~') not in ('~') "; + + List> l_pk = (List>)new BaseDAO().executeQuery(sql.toString(),new MapListProcessor()); + if(l_pk.isEmpty()) { + return null; + } + + for (Map map : l_pk) { + + + StringBuffer sb = new StringBuffer(); + sb.append(" select so_saleinvoice_b.csaleinvoicebid,so_saleorder.vdef9,so_saleorder_b.vbdef11,so_saleorder_exe.ntotalarmny as totalaccrued ,so_saleorder_exe.ntotalinvoicenum as billqty "); + sb.append(" from so_saleorder_exe,so_saleinvoice_b,so_saleinvoice,so_saleorder_b,so_saleorder "); + sb.append(" where so_saleorder_exe.csaleorderbid=so_saleinvoice_b.csrcbid "); + sb.append(" and so_saleinvoice.csaleinvoiceid=so_saleinvoice_b.csaleinvoiceid and so_saleinvoice.fstatusflag=2 "); + sb.append(" and so_saleorder_exe.csaleorderbid=so_saleorder_b.csaleorderbid "); + sb.append(" and so_saleorder.csaleorderid=so_saleorder_b.csaleorderid "); + sb.append(" and so_saleorder_exe.dr=0 and so_saleorder.dr=0 and so_saleorder_b.dr=0 "); + sb.append(" and nvl(so_saleorder.vdef9,'~') not in ('~') "); + sb.append(" and so_saleorder_exe.csaleorderbid='").append(map.get("cfirstbid")).append("'"); + List> ll_saleorder = (List>)new BaseDAO().executeQuery(sb.toString(),new MapListProcessor()); + + if(ll_saleorder.isEmpty()) { + continue; + } + + + try { + String accessToken=getAccessToken(baseUrl,bipParamMap); + Map tokenParam = new HashMap<>(); + tokenParam.put("access_token", accessToken); + Map headers = new HashMap(); + headers.put("Content-Type", "application/json"); + String custUpdateUrl=baseUrl+invoiceReceiveUrl; + if(accessToken!="") { + JSONObject js_apct_detail =new JSONObject(); + JSONObject js_apct =new JSONObject(); + String hid=ll_saleorder.get(0).get("vdef9");//BIP合同销售订单更新 主表id + List> l_map_f = new ArrayList>(); + for (Map custpkmap : ll_saleorder) { + + Map b_map = new HashMap(); + + b_map.put("id", custpkmap.get("vbdef11"));//BIP合同销售订单更新 子表id + b_map.put("billQty", custpkmap.get("billqty"));//累计开票主数量 + b_map.put("totalAccrued", custpkmap.get("totalaccrued"));//累计确认应收金额 + + l_map_f.add(b_map); + + js_apct.put("contractOrdersList", l_map_f); + js_apct.put("id", hid); + + js_apct_detail.put("HTXSDD", js_apct); + + String resultString=doSendHttp(custUpdateUrl,"POST",tokenParam,"",headers, js_apct_detail.toJSONString()); + Map updateMap = gson.fromJson(resultString, Map.class); + String updateSql=""; + if (StringUtils.equals("200", updateMap.get("code").toString())) {//保存更新成功后需要更新日志表 + updateSql="update so_saleinvoice_b set vbdef10='Y' where csaleinvoiceid='"+map.get("csaleinvoicebid")+"'"; + }else { + updateSql="update so_saleinvoice_b set vbdef10='N' where csaleinvoiceid='"+map.get("csaleinvoicebid")+"'"; + } + getSuperDMO().executeUpdateSQL(updateSql); + + } + } + + } catch (InvalidKeyException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }//获取BIP的token + catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + + + } + + + return null; + } + + + + /** + * 查询bip 中开户行及银行类型id + * @param map + * @param bipUrl + * @return + */ + private static Map getBIPBankID(HashMap map,String bipUrl,String accessToken,String bankdocname) { + + Gson gson = new Gson(); + + Map tokenParam = new HashMap<>(); + tokenParam.put("access_token", accessToken); + String jsonData= JSON.toJSONString(map); + String resultString=doSendHttp(bipUrl,"POST",tokenParam,"",new HashMap<>(), jsonData); +// Map custDetailresult = gson.fromJson(resultString, Map.class); + JSONObject custDetailresult= JSON.parseObject(resultString); + if (StringUtils.equals("200", custDetailresult.get("code").toString())) {//数据存在 + Map dataInfo = (Map)custDetailresult.get("data"); + if(!MMValueCheck.isEmpty(dataInfo.get("recordList"))){ + List> l_record=(List>) dataInfo.get("recordList"); + return l_record.get(0); + }else { + map.remove("linenumber"); + map.put("name", bankdocname); + jsonData= JSON.toJSONString(map); + resultString=doSendHttp(bipUrl,"POST",tokenParam,"",new HashMap<>(), jsonData); +// custDetailresult = gson.fromJson(resultString, Map.class); + custDetailresult= JSON.parseObject(resultString); + if (StringUtils.equals("200", custDetailresult.get("code").toString())) {//数据存在 + dataInfo = (Map)custDetailresult.get("data"); + if(!MMValueCheck.isEmpty(dataInfo.get("recordList"))){ + List> l_record=(List>) dataInfo.get("recordList"); + return l_record.get(0); + } + } + } + } + return null; + } + + + public static String doSendHttp(String baseUrl, String method, Map paramMap, String mediaType, Map headers, String json){ + HttpURLConnection urlConnection = null; + InputStream in = null; + OutputStream out = null; + BufferedReader bufferedReader = null; + String result = null; + try { + StringBuffer sb = new StringBuffer(); + sb.append(baseUrl); + if(paramMap != null){ + sb.append("?"); + for (Map.Entry entry : paramMap.entrySet()){ + String key = entry.getKey(); + String value = entry.getValue(); + sb.append(key + "="+ value).append("&"); + baseUrl = sb.toString().substring(0,sb.toString().length()- 1); + } + } +// log.error("uRL:" + baseUrl); +// // 打印请求URL和方法 +// log.error("Request URL: " + baseUrl); +// log.error("Request Method: " + method); + URL urlobj = new URL(baseUrl); + if("https".equalsIgnoreCase(urlobj.getProtocol())) {//判定网址是否信任,不信任则调用忽略信任工具类SslUtil + IgnoreSslUtil.ignoreSsl(); + } + urlConnection = (HttpURLConnection)urlobj.openConnection(); + urlConnection.setConnectTimeout(50000); + urlConnection.setRequestMethod(method); + urlConnection.setDoInput(true); + urlConnection.setUseCaches(false); + // 如果设置了自定义头,则打印它们 + if (headers != null && !headers.isEmpty()) { + for (Map.Entry entry : headers.entrySet()) { + urlConnection.addRequestProperty(entry.getKey(), entry.getValue()); +// log.error("Request Header: " + entry.getKey() + ": " + entry.getValue()); + } + } + if(json !=null&&json.length()>0){ +// log.error("Request Body: " + json); + urlConnection.setDoInput(true); + urlConnection.setDoOutput(true); + out=urlConnection.getOutputStream(); + out.write(json.getBytes("utf-8")); + out.flush(); + } + int resCode=urlConnection.getResponseCode(); + String ecod=urlConnection.getContentEncoding(); + if(resCode==HttpURLConnection.HTTP_OK||resCode== HttpURLConnection.HTTP_CREATED + ||resCode==HttpURLConnection.HTTP_ACCEPTED){ + if(StringUtils.isNotEmpty(ecod)&&ecod.equals("gzip")) { + in=new GZIPInputStream(urlConnection.getInputStream()); + }else{ + in=urlConnection.getInputStream(); + } + }else { + in=urlConnection.getErrorStream(); + } + bufferedReader =new BufferedReader(new InputStreamReader(in,"utf-8")); + StringBuffer temp=new StringBuffer(); + String line=bufferedReader.readLine(); + while (line!=null){ + temp.append(line).append("\r\n"); + line=bufferedReader.readLine(); + } + if(ecod==null||ecod.equals("gzip")){ + ecod= Charset.forName("utf-8").name(); + } + result=new String(temp.toString().getBytes("utf-8"),ecod); + }catch(Exception e) { + JSONObject js=new JSONObject(); + js.put("",-1); + js.put("message","调用外系统接口失败:"+e.getMessage()); + result=js.toString(); + e.printStackTrace(); + }finally { + if(null!=bufferedReader) { + try{ + bufferedReader.close(); + }catch (IOException e) { + e.printStackTrace(); + } + } + if(null!=out) { + try{ + out.close(); + }catch (IOException e) { + e.printStackTrace(); + } + } + if(null!=in) { + try{ + in.close(); + }catch (IOException e) { + e.printStackTrace(); + } + } + urlConnection.disconnect(); + } + return result; + } + + +// private String createCustBankMap(Map map,String access_token) { +// Gson gson = new Gson(); +// //获取BIP中的客户详情 +// String custcode=map.get("custcode"); +// String custurl=baseUrl+"/yonbip/digitalModel/merchant/detail?access_token="+access_token; +// HashMap custMap = new HashMap(); +// custMap.put("code", custcode); +// try { +// String custdetailString=doGet(custurl,custMap); +// +// Map custDetailresult = gson.fromJson(custdetailString, Map.class); +// if (StringUtils.equals("200", custDetailresult.get("code").toString())) {//客户存在 +// +// Map dataInfo = (Map) custDetailresult.get("data"); +// +// if (!MMValueCheck.isEmpty(dataInfo.get("merchantAgentFinancialInfos"))){//客户中已经存在银行账户 +// +// }else { +// +// } +// +// } +// +// +// //开户行 +// String bankdoccode=map.get("bankdoccode"); +// HashMap paramap = new HashMap(); +// paramap.put("pageIndex", 0); +// paramap.put("pageSize", 10); +// paramap.put("linenumber", bankdoccode); +// String url=baseUrl+"/yonbip/digitalModel/bankdot/list?access_token="+access_token; +// +// String responseString=doPost(url,paramap); +// +// Map result = gson.fromJson(responseString, Map.class); +// +// } catch (IOException e1) { +// // TODO Auto-generated catch block +// e1.printStackTrace(); +// } +// +// } + + + private String getAccessToken(String baseUrl,Map bipParamMap) throws NoSuchAlgorithmException, InvalidKeyException, IOException { + String tokenUrl=baseUrl+"/iuap-api-auth/open-auth/selfAppAuth/getAccessToken"; +// String appKey = "f10c4bf17b1d4e1fb08eb82bf8540eab"; + String appKey = bipParamMap.get("appKey"); + +// String appSecret = "71dc2a58ca378c1a1143231a62e73e75a60e9236"; + String appSecret = bipParamMap.get("appSecret"); + String accessToken=""; + Map params = new HashMap<>(); + + + params.put("appKey", appKey); + String timestamp = String.valueOf(System.currentTimeMillis()); + params.put("timestamp", timestamp); + // 计算签名 + Map treeMap; + if (params instanceof TreeMap) { + treeMap = params; + } else { + treeMap = new TreeMap<>(params); + } + StringBuilder stringBuilder = new StringBuilder(); + for (Map.Entry entry : treeMap.entrySet()) { + stringBuilder.append(entry.getKey()).append(entry.getValue()); + } + Mac mac = Mac.getInstance("HmacSHA256"); + + mac.init(new SecretKeySpec(appSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); + byte[] signData = mac.doFinal(stringBuilder.toString().getBytes(StandardCharsets.UTF_8)); + String base64String = Base64.getEncoder().encodeToString(signData); + String signature = URLEncoder.encode(base64String, "UTF-8"); + params.put("signature", signature); + + String responseString = doGet(tokenUrl, params); + + Gson gson = new Gson(); + Map result = gson.fromJson(responseString, Map.class); + if (StringUtils.equals("00000", result.get("code").toString())) { + Map tokenInfo = (Map) result.get("data"); + accessToken = (String) tokenInfo.get("access_token"); + + } + + + return accessToken; + } + + + + private String doPost(String requestUrl,JSONObject jsonObject) throws IOException { + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); + cm.setMaxTotal(500); + cm.setDefaultMaxPerRoute(50); + + RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时 + .setConnectTimeout(5000) // 连接建立超时 + .setSocketTimeout(20000) // 等待响应超时 + .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); + + CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm) + .setDefaultRequestConfig(globalConfig).build(); + HttpPost post = new HttpPost(requestUrl); + post.setHeader("Content-Type", "application/json;charset=UTF-8"); + post.setEntity(new StringEntity(JSONObject.toJSONString(jsonObject), "utf-8")); + String responseString = httpClient.execute(post, response -> EntityUtils.toString(response.getEntity())); + return responseString; + + } + + + private String doPost(String requestUrl, Map param) throws IOException { + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); + cm.setMaxTotal(500); + cm.setDefaultMaxPerRoute(50); + + RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时 + .setConnectTimeout(5000) // 连接建立超时 + .setSocketTimeout(20000) // 等待响应超时 + .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); + + CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm) + .setDefaultRequestConfig(globalConfig).build(); + HttpPost post = new HttpPost(requestUrl); + post.setHeader("Content-Type", "application/json;charset=UTF-8"); + post.setEntity(new StringEntity(JSONObject.toJSONString(param), "utf-8")); + String responseString = httpClient.execute(post, response -> EntityUtils.toString(response.getEntity())); + return responseString; + + } + + + private static String doGet(String path, Map params) throws IOException { + HttpURLConnection conn = null; + InputStream is = null; + BufferedReader br = null; + StringBuilder result = new StringBuilder(); + try { + if (params != null) { + String paramStr = ""; + for (String key : params.keySet()) { + if (!paramStr.isEmpty()) { + paramStr += '&'; + } + paramStr += key + '=' + params.get(key); + } + + if (path.indexOf('?') > 0) { + path += '&' + paramStr; + } else { + path += '?' + paramStr; + } + } + //创建远程url连接对象 + URL url = new URL(path); + if("https".equalsIgnoreCase(url.getProtocol())){//判定网址是否信任,不信任则调用忽略信任工具类SslUtil + IgnoreSslUtil.ignoreSsl(); + } + //通过远程url连接对象打开一个连接,强转成HTTPURLConnection类 + conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + //设置连接超时时间和读取超时时间 + conn.setConnectTimeout(120000); + conn.setReadTimeout(120000); + conn.setRequestProperty("Accept", "application/json"); + //发送请求 + conn.connect(); + //通过conn取得输入流,并使用Reader读取 + if (200 == conn.getResponseCode()) { + is = conn.getInputStream(); + br = new BufferedReader(new InputStreamReader(is, "UTF-8")); + String line; + while ((line = br.readLine()) != null) { + result.append(line); + System.out.println(line); + } + } else { + System.out.println("ResponseCode is an error code:" + conn.getResponseCode()); + } + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } +// finally { +// try { +// if (br != null) { +// br.close(); +// } +// if (is != null) { +// is.close(); +// } +// } catch (IOException ioe) { +// ioe.printStackTrace(); +// } +// conn.disconnect(); +// } + return result.toString(); + } + + + + + +// private String doGet(String requestUrl, Map paramMap) throws IOException { +// PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); +// cm.setMaxTotal(500); +// cm.setDefaultMaxPerRoute(50); +// +// RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时 +// .setConnectTimeout(5000) // 连接建立超时 +// .setSocketTimeout(20000) // 等待响应超时 +// .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); +// +// CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm) +// .setDefaultRequestConfig(globalConfig).build(); +// StringBuilder param = new StringBuilder("?"); +// if (paramMap != null) { +// for (Map.Entry entry : paramMap.entrySet()) { +// param.append(entry.getKey()); +// param.append("="); +// param.append(entry.getValue()); +// param.append("&"); +// } +// param.deleteCharAt(param.length() - 1); +// } +// String url = requestUrl + param; +// HttpGet get = new HttpGet(url); +// String responseString = httpClient.execute(get, response -> EntityUtils.toString(response.getEntity())); +// get.releaseConnection(); +// return responseString; +// } + +}