public void getRefund(Connection conn) throws Exception { String resultText = ""; for (int k = 0; k < 5; ) { try { // 获取到退货订单号 String apiMethod = "suning.custom.batchrejectedOrd.query"; HashMap<String, String> reqMap = new HashMap<String, String>(); reqMap.put( "startTime", Formatter.format( new Date(System.currentTimeMillis() - daymillis), Formatter.DATE_TIME_FORMAT)); reqMap.put("endTime", Formatter.format(new Date(), Formatter.DATE_TIME_FORMAT)); HashMap<String, String> map = new HashMap<String, String>(); map.put("appSecret", Params.appsecret); map.put("appMethod", apiMethod); map.put("format", Params.format); map.put("versionNo", "v1.2"); map.put("appKey", Params.appKey); // 发送请求 String responseText = CommHelper.doRequest(map, Params.url); Log.info("退换货数据: " + responseText); // 把返回的数据转成json对象 JSONObject responseObj = new JSONObject(responseText).getJSONObject("sn_responseContent"); // 错误对象 if (responseText.indexOf("sn_error") != -1) { // 发生错误 String operCode = responseObj.getJSONObject("sn_error").getString("error_code"); if (!"".equals(operCode)) { Log.error("苏宁获取退货订单", "获取退货订单失败,operCode:" + operCode); } return; } JSONArray ReturnCodeList = responseObj.getJSONObject("sn_body").getJSONArray("batchQueryRejectedOrd"); for (int i = 0; i < ReturnCodeList.length(); i++) { try { String orderCode = ReturnCodeList.getJSONObject(i).getString("orderCode"); Order o = new Order(); OrderUtils.createRefundOrder( "生成苏宁退换货接口订单", conn, Params.tradecontactid, o, Params.url, Params.appKey, Params.appsecret, Params.format); } catch (Exception ex) { if (conn != null && !conn.getAutoCommit()) { conn.rollback(); } Log.error(jobName, ex.getMessage()); continue; } } break; } catch (Exception e) { if (++k >= 5) throw e; if (conn != null && !conn.getAutoCommit()) { conn.rollback(); } Log.warn(jobName + " ,远程连接失败[" + k + "], 10秒后自动重试. " + Log.getErrorMessage(e)); Thread.sleep(10000L); } } }
private void getInStockItems() throws Exception { int pageno = 1; Log.info("开始取一号店仓库商品资料"); String sql = "select orgid from ecs_tradecontactorgcontrast with(nolock) where tradecontactid=" + tradecontactid; int orgid = this.getDao().intSelect(sql); for (int k = 0; k < 10; ) { try { while (true) { Map<String, String> productparams = new HashMap<String, String>(); // 系统级参数设置 productparams.put("appKey", app_key); productparams.put("sessionKey", token); productparams.put("format", format); productparams.put("method", "yhd.serial.products.search"); productparams.put("ver", ver); productparams.put("timestamp", Formatter.format(new Date(), Formatter.DATE_TIME_FORMAT)); productparams.put("canShow", "1"); productparams.put("canSale", "0"); productparams.put("curPage", String.valueOf(pageno)); productparams.put("pageRows", "20"); productparams.put("verifyFlg", "2"); String responseProductData = Utils.sendByPost(productparams, app_secret, url, ""); Log.info("取仓库商品返回数据: " + responseProductData); JSONObject responseproduct = new JSONObject(responseProductData); int totalCount = responseproduct.getJSONObject("response").getInt("totalCount"); JSONArray productlist = responseproduct .getJSONObject("response") .getJSONObject("serialProductList") .getJSONArray("serialProduct"); for (int i = 0; i < productlist.length(); i++) { JSONObject product = productlist.getJSONObject(i); long productId = product.optLong("productId"); String productCode = product.optString("productCode"); String productCname = product.optString("productCname"); Log.info("货号:" + productCode + ",产品名称:" + productCname); StockManager.stockConfig( this.getDao(), orgid, Integer.valueOf(tradecontactid), String.valueOf(productId), productCode, productCname, 0); Map<String, String> stockparams = new HashMap<String, String>(); // 系统级参数设置 stockparams.put("appKey", app_key); stockparams.put("sessionKey", token); stockparams.put("format", format); stockparams.put("method", "yhd.serial.product.get"); stockparams.put("ver", ver); stockparams.put("timestamp", Formatter.format(new Date(), Formatter.DATE_TIME_FORMAT)); stockparams.put("productId", String.valueOf(productId)); String responseData = Utils.sendByPost(stockparams, app_secret, url, ""); Log.info("取仓库商品详情返回数据: " + responseData); JSONObject responsestock = new JSONObject(responseData); JSONArray childseriallist = responsestock .getJSONObject("response") .getJSONObject("serialChildProdList") .getJSONArray("serialChildProd"); for (int m = 0; m < childseriallist.length(); m++) { JSONObject childserial = childseriallist.optJSONObject(m); String sku = childserial.optString("outerId"); long skuid = childserial.optLong("productId"); JSONArray stocklist = childserial.getJSONObject("allWareHouseStocList").getJSONArray("pmStockInfo"); for (int j = 0; j < stocklist.length(); j++) { JSONObject stock = stocklist.optJSONObject(j); int quantity = stock.optInt("vs"); long warehouseId = stock.optLong("warehouseId"); StockManager.addStockConfigSku( this.getDao(), Integer.valueOf(tradecontactid), String.valueOf(productId), String.valueOf(skuid) + "-" + String.valueOf(warehouseId), sku, quantity); } } } // 判断是否有下一页 if (pageno == (Double.valueOf(Math.ceil(totalCount / 20.0))).intValue()) break; pageno++; } break; } catch (Exception e) { if (++k >= 10) throw e; Log.warn(jobName + ", 远程连接失败[" + k + "], 10秒后自动重试. " + Log.getErrorMessage(e)); Thread.sleep(10000L); } } }
/** 检查未入待发货订单 orderStatus=10 等待发货 这里检查一天时间的未入订单 */ public void checkWaitStockOutOrders() throws Exception { Log.info(jobName + "任务开始!"); Connection conn = this.getDao().getConnection(); int pageIndex = 1; boolean hasNextPage = true; for (int k = 0; k < 5; ) { try { while (hasNextPage) { Date startdate = new Date(new Date().getTime() - daymillis); Date enddate = new Date(); // 方法名 String apimethod = "search_order_list"; HashMap<String, Object> reqMap = new HashMap<String, Object>(); reqMap.put("last_modify_st_time", startdate.getTime() / 1000L); reqMap.put("last_modify_en_time", enddate.getTime() / 1000L); reqMap.put("pages", String.valueOf(pageIndex)); reqMap.put("counts", pageSize); reqMap.put("return_data", "json"); reqMap.put("act", apimethod); reqMap.put("api_version", "1.0"); // 发送请求 Log.info("第" + pageIndex + "页"); String responseText = CommHelper.doRequest(reqMap, url); Log.info("返回数据为: " + responseText); // 把返回的数据转成json对象 JSONObject responseObj = new JSONObject(responseText.replaceAll(":null", ":\"\"")); // 错误对象 if (!"success".equals(responseObj.getString("result"))) { // 发生错误 String operCode = responseObj.getJSONObject("sn_error").getString("error_code"); if (operCode.indexOf("no-result") != -1) { // 没有数据直接退出方法体 Log.error("获取ecshop订单列表", "获取订单列表失败,操作码:" + operCode); return; } hasNextPage = false; break; } JSONObject orderInfos = responseObj.getJSONObject("info"); String orderTotal = String.valueOf(orderInfos.getString("counts")); int orderTotaltemp = Integer.parseInt(orderTotal); int pageTotalTemp = Double.valueOf(Math.ceil(orderTotaltemp / Double.parseDouble(Params.pageSize))) .intValue(); String pageTotal = String.valueOf(pageTotalTemp); Log.info("总订单数为: " + orderTotal); Log.info("总页数为: " + pageTotal); if (orderTotal == null || orderTotal.equals("") || orderTotal.equals("0")) { break; } // 订单元素 JSONArray ordersList = orderInfos.getJSONArray("data_info"); for (int i = 0; i < ordersList.length(); i++) { // 某个订单 JSONObject orderInfo = ordersList.getJSONObject(i); // 订单编号 String orderCode = (String) orderInfo.get("order_sn"); if (orderInfo.isNull("shop_info")) continue; // 订单商品集合 JSONArray items = orderInfo.getJSONArray("shop_info"); // 构造一个订单对象 Order o = new Order(); o.setObjValue(o, orderInfo); o.setFieldValue(o, "shop_info", items); Log.info( "订单号: " + o.getOrder_sn() + ", 发货状态: " + o.getShipping_status() + "付款状态: " + o.getPay_status()); if (o != null) { if ("0".equals(o.getShipping_status()) && "2".equals(o.getPay_status())) { // 正常订单 Log.info("检查到一条订单:" + orderCode); // 如果是等待发货订单,创建接口订单成功,减少其它店的库存 if (!OrderManager.isCheck(jobName, conn, orderCode)) { if (!OrderManager.TidLastModifyIntfExists( jobName, conn, orderCode, new Date(o.getAdd_time() * 1000L))) { try { OrderUtils.createInterOrder(conn, o, tradecontactid, username); for (Iterator ito = o.getShop_info().getRelationData().iterator(); ito.hasNext(); ) { OrderItem item = (OrderItem) ito.next(); String sku = item.getProduct_sn(); StockManager.deleteWaitPayStock( jobName, conn, tradecontactid, orderCode, sku); long qty = (long) item.getGoods_number(); // 在ecs_rationconfig表中存在机构添加一条库存同步记录(不包括自己) StockManager.addSynReduceStore( jobName, conn, tradecontactid, "未发货", o.getOrder_sn(), sku, qty, false); } } catch (SQLException sqle) { throw new JException("生成接口订单出错!" + sqle.getMessage()); } } } // 订单状态 10待发货,20已发货,21部分发货,30交易成功 ,40交易关闭 } } } // 判断是否有下一页 if ("".equals(pageTotal) || pageTotal == null) pageTotal = "0"; if (pageIndex >= Integer.parseInt(pageTotal)) hasNextPage = false; else pageIndex++; break; } Log.info(jobName + "执行完毕!"); break; } catch (Exception e) { if (++k >= 5) throw e; if (conn != null && !conn.getAutoCommit()) { conn.rollback(); } Log.warn(jobName + " ,远程连接失败[" + k + "], 10秒后自动重试. " + Log.getErrorMessage(e)); Thread.sleep(10000L); } } }
// 检查取消订单 4 取消 private void checkCancleOrders() throws Exception { int pageIndex = 1; boolean hasNextPage = true; for (int k = 0; k < 10; ) { try { int n = 1; while (hasNextPage) { Date enddate = new Date(); Date startdate = new Date(new Date().getTime() - daymillis); // 方法名 String apimethod = "search_order_list"; HashMap<String, Object> reqMap = new HashMap<String, Object>(); reqMap.put("last_modify_st_time", startdate.getTime() / 1000L); reqMap.put("last_modify_en_time", enddate.getTime() / 1000L); reqMap.put("pages", String.valueOf(pageIndex)); reqMap.put("counts", pageSize); reqMap.put("return_data", "json"); reqMap.put("act", apimethod); reqMap.put("api_version", "1.0"); Log.info("第" + pageIndex + "页"); String responseText = CommHelper.doRequest(reqMap, url); Log.info("返回数据为: " + responseText); // 把返回的数据转成json对象 JSONObject responseObj = new JSONObject(responseText); // sn_error if (!"success".equals(responseObj.getString("result"))) { // 发生错误 String operCode = responseObj.getJSONObject("sn_error").getString("error_code"); if ("biz.handler.data-get:no-result".equals(operCode)) { // 没有结果 Log.info("没有可用的订单!"); } else { Log.warn("取订单出错了,错误码: " + operCode); } break; } JSONObject orderInfos = responseObj.getJSONObject("info"); // 总页数 String orderTotal = String.valueOf(orderInfos.getString("counts")); int orderTotaltemp = Integer.parseInt(orderTotal); int pageTotalTemp = orderTotaltemp < Integer.parseInt(pageSize) ? 1 : orderTotaltemp / Integer.parseInt(pageSize) == 0 ? Integer.parseInt(pageSize) : orderTotaltemp / Integer.parseInt(pageSize) + 1; String pageTotal = String.valueOf(pageTotalTemp); Log.info("总订单数为: " + orderTotal); Log.info("总页数为: " + pageTotal); if (orderTotal == null || orderTotal.equals("") || orderTotal.equals("0")) { break; } // 订单元素 JSONArray ordersList = orderInfos.getJSONArray("data_info"); for (int i = 0; i < ordersList.length(); i++) { // 某个订单 JSONObject orderInfo = ordersList.getJSONObject(i); int returnOrderCount = 0; // 订单编号 String orderCode = (String) orderInfo.get("order_sn"); if (orderInfo.isNull("shop_info")) continue; // 订单商品集合 JSONArray items = orderInfo.getJSONArray("shop_info"); // 构造一个订单对象 Order o = new Order(); o.setObjValue(o, orderInfo); o.setFieldValue(o, "shop_info", items); Log.info("发货状态: " + o.getShipping_status() + "付款状态: " + o.getPay_status()); if (o != null) { if ("4".equals(o.getPay_status())) { // 正常订单 Log.info("订单号: " + o.getOrder_sn()); // 如果是等待发货订单,创建接口订单成功,减少其它店的库存 try { OrderUtils.createInterOrder( this.getDao().getConnection(), o, tradecontactid, username); } catch (SQLException sqle) { throw new JException("生成退货订单出错!" + sqle.getMessage()); } } } } // 判断是否有下一页 if ("".equals(pageTotal) || pageTotal == null) pageTotal = "0"; if (pageIndex >= Integer.parseInt(pageTotal)) hasNextPage = false; else pageIndex++; n++; } break; } catch (Exception e) { e.printStackTrace(); if (++k >= 10) throw e; if (this.getDao() != null && !this.getDao().getConnection().getAutoCommit()) { this.getDao().rollback(); } Log.warn(jobName + " ,远程连接失败[" + k + "], 10秒后自动重试. " + Log.getErrorMessage(e)); Thread.sleep(10000L); } } Log.info("本次取ecshop退货订单任务处理完毕!"); }
// 家装订单发货 private void jzSend( String post_no, String orderid, String post_company, Connection conn, String sheetid) { try { TaobaoClient client = new DefaultTaobaoClient(Params.url, Params.appkey, Params.appsecret); WlbOrderJzwithinsConsignRequest req = new WlbOrderJzwithinsConsignRequest(); req.setTid(TranTid(orderid)); JSONObject obj = new JSONObject(); obj.put("mail_no ", post_no); obj.put("zy_company ", post_company); req.setTmsPartner(obj.toString()); JSONArray arr = new JSONArray(Params.jzParams); WlbOrderJzwithinsConsignResponse rsp; for (int i = 0; i < arr.length(); i++) { req.setInsPartner(arr.get(i).toString()); rsp = client.execute(req, Params.authcode); String sql; if (rsp.isSuccess()) { try { conn.setAutoCommit(false); sql = "insert into IT_UpNoteBak(Owner,SheetID,SheetType,Sender,Receiver,Notetime,HandleTime,Flag) " + " select Owner , SheetID , SheetType , Sender , Receiver , Notetime , getdate() , 1 from IT_UpNote " + " where SheetID = '" + sheetid + "' and SheetType = 3"; SQLHelper.executeSQL(conn, sql); sql = "delete from IT_UpNote where SheetID='" + sheetid + "' and sheettype=3"; SQLHelper.executeSQL(conn, sql); conn.commit(); conn.setAutoCommit(true); } catch (SQLException sqle) { if (!conn.getAutoCommit()) try { conn.rollback(); } catch (Exception e1) { } try { conn.setAutoCommit(true); } catch (Exception e1) { } // throw new JSQLException(sql, sqle); } Log.info( username, "处理订单【" + orderid + "】发货成功,快递公司【" + post_company + "】,快递单号【" + post_no + "】", null); Log.info(username, "家装发货参数: " + arr.get(i).toString(), null); break; } else { Log.info(username, "家装订单发货失败,错误信息: " + rsp.getResultInfo(), null); continue; } } } catch (ApiException e) { Log.error( username, "处理订单【" + orderid + "】发货失败,快递公司【" + post_company + "】,快递单号【" + post_no + "】,错误信息:" + e.getMessage(), null); } catch (Exception e) { Log.error(username, "同步发货状态出错了, 错误信息: " + e.getMessage(), null); } }