public static String getShopId(String apiUrl, String appKey, String appSecret, String userId) throws ApiException, IncorrectInputParameterException { final String nick = userId.replace(S.PREFIX_TAOBAO, S.EMPTY_STR); TaobaoClient client = new DefaultTaobaoClient( apiUrl, appKey, appSecret, Constants.FORMAT_JSON, S.FIVE_SECONDS, S.FIVE_SECONDS); ShopGetRequest request = new ShopGetRequest(); request.setFields(UserTaobaoInfoKey.API_SID); request.setNick(nick); ShopGetResponse response = client.execute(request); if (response == null) { throw new IncorrectInputParameterException("ShopGetResponse return is null"); } if (response.isSuccess()) { Long sid = response.getShop().getSid(); return String.valueOf(sid); } throw new IncorrectInputParameterException( "User < " + userId + " > request seller shop id failed with error code and message < " + response.getErrorCode() + "," + response.getMsg() + " > and sub code and message < " + response.getSubCode() + "," + response.getSubMsg() + " >"); }
public static Long getBuyerCreditLevel( String apiUrl, String appKey, String appSecret, String userId, String token) throws ApiException, IncorrectInputParameterException { TaobaoClient client = new DefaultTaobaoClient( apiUrl, appKey, appSecret, Constants.FORMAT_JSON, S.FIVE_SECONDS, S.FIVE_SECONDS); UserBuyerGetRequest request = new UserBuyerGetRequest(); request.setFields(UserTaobaoInfoKey.API_BUYER_CREDIT); UserBuyerGetResponse response = client.execute(request, token); if (response == null) { throw new IncorrectInputParameterException("UserBuyerGetResponse return is null"); } if (response.isSuccess()) { UserCredit buyerCredit = response.getUser().getBuyerCredit(); return buyerCredit.getLevel(); } throw new IncorrectInputParameterException( "User < " + userId + " > request buyer credit failed with error code and message < " + response.getErrorCode() + "," + response.getMsg() + " > and sub code and message < " + response.getSubCode() + "," + response.getSubMsg() + " >"); }
public static void sms() { String url = "https://eco.taobao.com/router/rest"; String appkey = "23377240"; String secret = "ec63296b5e2886ed8d2fa7079fc8b583"; String start = new Date().toString(); TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret); AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest(); // req.setExtend( "extend" ); req.setSmsType("normal"); req.setSmsFreeSignName("方方测试"); req.setSmsParamString( "{\"pwd\":\"1234\",\"startTime\":\"2016-06-01 \" ,\"endTime\":\"2016-07-01 \",\"msg\":\"退房时请保持室内清洁,谢谢 \"}"); req.setRecNum("18910415779"); req.setSmsTemplateCode("SMS_10200803"); AlibabaAliqinFcSmsNumSendResponse rsp; try { rsp = client.execute(req); log.info(start + ">>fangfangtest>> " + rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } }
private boolean sendSingleInfo(Hashtable tab) throws Exception { boolean isSuccess = false; String tidString = tab.get("tid").toString().trim(); if (!StringUtils.isNumeric(tidString)) return true; Long tid = Long.parseLong(tidString); String actionTime = tab.get("actiontime").toString().trim().substring(0, 19); String operator = tab.get("operator").toString().trim(); String status = tab.get("status").toString().trim(); String remark = tab.get("remark").toString().trim(); try { TaobaoClient client = new DefaultTaobaoClient(Params.url, Params.appkey, Params.appsecret); TmcMessageProduceRequest req = new TmcMessageProduceRequest(); req.setTopic("taobao_jds_TradeTrace"); Map<String, Object> map = new HashMap<String, Object>(); map.put("action_time", actionTime); map.put("operator", operator); map.put("remark", remark); map.put("seller_nick", Params.sellernick); map.put("status", status); map.put("tid", tid); String contents = new JSONWriter().write(map); // Log.info("contents: " +contents); req.setContent(contents); TmcMessageProduceResponse rep = client.execute(req, Params.authcode); // Log.info("req: "+rep.getBody()); if (rep.getIsSuccess()) { isSuccess = true; // Log.info("发送单条订单跟踪数据成功,订单号: "+tid+"状态: "+status); } } catch (Exception e) { Log.error( username, "发送单条订单跟踪数据出错,tid: '" + tid + ",status: '" + status + " " + e.getMessage(), null); } return isSuccess; }
public void run() { Properties proper = StringUtil.getStringProperties(this.getExecuteobj().getParams()); tid = proper.getProperty("tid"); tradecontactid = proper.getProperty("tradecontactid"); url = proper.getProperty("url"); appkey = proper.getProperty("appkey"); appsecret = proper.getProperty("appsecret"); authcode = proper.getProperty("authcode"); sellernick = proper.getProperty("sellernick"); try { updateJobFlag(1); String sql = "select isnull(value,0) from config where name='等待付款订单是否进系统'"; if ("1".equals(this.getDao().strSelect(sql))) ; waitbuyerpayisin = true; TaobaoClient client = new DefaultTaobaoClient(url, appkey, appsecret, "xml"); TradeGetRequest req = new TradeGetRequest(); req.setFields("tid,modified,status,orders.outer_sku_id,orders.num"); req.setTid(Long.parseLong(tid)); TradeGetResponse response = client.execute(req, authcode); if (response.getTrade() == null) { return; } Trade td = response.getTrade(); if (td.getStatus().equals("WAIT_SELLER_SEND_GOODS")) { if (!OrderManager.isCheck( "检查淘宝订单", this.getDao().getConnection(), String.valueOf(td.getTid()))) { if (!OrderManager.TidLastModifyIntfExists( "检查淘宝订单", this.getDao().getConnection(), String.valueOf(td.getTid()), td.getModified())) { OrderUtils.createInterOrder( this.getDao().getConnection(), td, Params.tradecontactid, Params.username, true); for (Iterator ito = td.getOrders().iterator(); ito.hasNext(); ) { Order o = (Order) ito.next(); String sku = o.getOuterSkuId(); StockManager.deleteWaitPayStock( jobName, this.getDao().getConnection(), Params.tradecontactid, String.valueOf(td.getTid()), sku); StockManager.addSynReduceStore( jobName, this.getDao().getConnection(), Params.tradecontactid, td.getStatus(), String.valueOf(td.getTid()), sku, -o.getNum(), false); } } } // 等待买家付款时记录锁定库存 } else if (td.getStatus().equals("WAIT_BUYER_PAY") || td.getStatus().equals("TRADE_NO_CREATE_PAY")) { if (waitbuyerpayisin) { if (!OrderManager.TidLastModifyIntfExists( "检查淘宝订单", this.getDao().getConnection(), String.valueOf(td.getTid()), td.getModified())) { OrderUtils.createInterOrder( this.getDao().getConnection(), td, Params.tradecontactid, Params.username, false); } } for (Iterator ito = td.getOrders().iterator(); ito.hasNext(); ) { Order o = (Order) ito.next(); String sku = o.getOuterSkuId(); StockManager.addWaitPayStock( jobName, this.getDao().getConnection(), Params.tradecontactid, String.valueOf(td.getTid()), sku, o.getNum()); StockManager.addSynReduceStore( jobName, this.getDao().getConnection(), Params.tradecontactid, td.getStatus(), String.valueOf(td.getTid()), sku, -o.getNum(), false); } // 付款以后用户退款成功,交易自动关闭 // 释放库存,数量为负数 } else if (td.getStatus().equals("TRADE_CLOSED")) { OrderManager.CancelOrderByCID( jobName, this.getDao().getConnection(), String.valueOf(td.getTid())); for (Iterator ito = td.getOrders().iterator(); ito.hasNext(); ) { Order o = (Order) ito.next(); String sku = o.getOuterSkuId(); StockManager.deleteWaitPayStock( jobName, this.getDao().getConnection(), Params.tradecontactid, String.valueOf(td.getTid()), sku); // StockManager.addSynReduceStore(jobName, this.getDao().getConnection(), // Params.tradecontactid, td.getStatus(),String.valueOf(td.getTid()),sku, // o.getNum(),false); } // 付款以前,卖家或买家主动关闭交易 // 释放等待买家付款时锁定的库存 } else if (td.getStatus().equals("TRADE_CLOSED_BY_TAOBAO")) { if (waitbuyerpayisin) { if (!OrderManager.TidLastModifyIntfExists( "检查淘宝订单", this.getDao().getConnection(), String.valueOf(td.getTid()), td.getModified())) { OrderUtils.createInterOrder( this.getDao().getConnection(), td, Params.tradecontactid, Params.username, false); } } for (Iterator ito = td.getOrders().iterator(); ito.hasNext(); ) { Order o = (Order) ito.next(); String sku = o.getOuterSkuId(); StockManager.deleteWaitPayStock( jobName, this.getDao().getConnection(), Params.tradecontactid, String.valueOf(td.getTid()), sku); if (StockManager.WaitPayStockExists( jobName, this.getDao().getConnection(), Params.tradecontactid, String.valueOf(td.getTid()), sku)) // 有获取到等待买家付款状态时才加库存 { StockManager.addSynReduceStore( jobName, this.getDao().getConnection(), Params.tradecontactid, td.getStatus(), String.valueOf(td.getTid()), sku, o.getNum(), false); } } } else if (td.getStatus().equals("TRADE_FINISHED")) { for (Iterator ito = td.getOrders().iterator(); ito.hasNext(); ) { Order o = (Order) ito.next(); String sku = o.getOuterSkuId(); StockManager.deleteWaitPayStock( jobName, this.getDao().getConnection(), Params.tradecontactid, String.valueOf(td.getTid()), sku); // 更新结束时间 OrderUtils.updateFinishedStatus( this.getDao().getConnection(), Params.tradecontactid, td.getTid(), td.getEndTime()); } } // if(Params.isc) // { // 处理退货 for (Iterator oit = td.getOrders().iterator(); oit.hasNext(); ) { Order o = (Order) oit.next(); // Log.info("订单号:"+String.valueOf(td.getTid())+" 退货ID:"+String.valueOf(o.getRefundId())); if (o.getRefundId() > 0) { OrderUtils.getRefund( jobName, this.getDao().getConnection(), Params.url, Params.appkey, Params.appsecret, Params.authcode, Params.tradecontactid, td, o, String.valueOf(td.getTid()), o.getRefundId()); } } updateJobFlag(0); Log.info( jobName, "执行作业成功 [" + this.getExecuteobj().getActivetimes() + "] [" + this.getExecuteobj().getNotes() + "] 下次处理时间: " + this.datetimeformat.format(this.getExecuteobj().getNextactive())); } catch (Exception e) { try { if (this.getDao().getConnection() != null && !this.getDao().getConnection().getAutoCommit()) { this.getDao().getConnection().rollback(); this.getDao().getConnection().setAutoCommit(true); } } catch (Exception e1) { Log.error(jobName, "回滚事务失败"); Log.error(jobName, e1.getMessage()); } try { if (this.getExecuteobj().getSkip() == 1) { UpdateTimerJob(); } else UpdateTimerJob(Log.getErrorMessage(e)); } catch (Exception ex) { Log.error(jobName, "更新任务信息失败"); Log.error(jobName, ex.getMessage()); } Log.error(jobName, "错误信息:" + Log.getErrorMessage(e)); Log.error( jobName, "执行作业失败 [" + this.getExecuteobj().getActivetimes() + "] [" + this.getExecuteobj().getNotes() + "] \r\n " + Log.getErrorMessage(e)); } finally { try { updateJobFlag(0); } catch (Exception e) { Log.error(jobName, e.getMessage()); Log.error(jobName, "更新处理标志失败"); TimerRunner.modifiedErrVect(this.getExecuteobj().getId()); } try { if (this.getDao().getConnection() != null) { this.getDao().getConnection().setAutoCommit(true); this.getDao().getConnection().close(); } } catch (Exception e) { Log.error(jobName, "关闭数据库连接失败"); } } }
/** 修改物流公司和运单号 已经发过货的订单才能进行此操作 api : taobao.logistics.consign.resend 免费 */ private void resend(Connection conn) throws Exception { String sql = "select a.sheetid,b.tid, upper(ltrim(rtrim(b.companycode))) companycode," + " upper(ltrim(rtrim(b.outsid))) outsid from it_upnote a with(nolock), ns_delivery b with(nolock)" + "where a.sheettype=4 and a.sheetid=b.sheetid and a.receiver='" + Params.tradecontactid + "' and b.iswait=0"; Vector vdeliveryorder = SQLHelper.multiRowSelect(conn, sql); for (int i = 0; i < vdeliveryorder.size(); i++) { String orderid = ""; try { Hashtable hto = (Hashtable) vdeliveryorder.get(i); String sheetid = hto.get("sheetid") != null ? hto.get("sheetid").toString() : ""; orderid = hto.get("tid") != null ? hto.get("tid").toString() : ""; String post_company = hto.get("companycode") != null ? hto.get("companycode").toString().trim() : ""; String post_no = hto.get("outsid").toString() != null ? hto.get("outsid").toString().trim() : ""; // 如果物流公司为空则忽略处理 if (post_company.trim().equals("")) { Log.warn(username, jobName + " 快递公司为空!订单号:" + orderid + ""); continue; } // 如果物流公司为空则忽略处理 if (post_no.trim().equals("")) { Log.warn(username, jobName + "快递单号为空!订单号:" + orderid + ""); continue; } if (!StringUtil.isNumeric(orderid)) { 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 = 4"; SQLHelper.executeSQL(conn, sql); sql = "delete from IT_UpNote where SheetID='" + sheetid + "' and sheettype=4"; SQLHelper.executeSQL(conn, sql); Log.warn(username, jobName + " 订单号【" + orderid + "】不全是数字!"); continue; } TaobaoClient client = new DefaultTaobaoClient(Params.url, Params.appkey, Params.appsecret); LogisticsConsignResendRequest req = new LogisticsConsignResendRequest(); req.setOutSid(post_no); req.setTid(TranTid(orderid)); req.setCompanyCode(post_company); LogisticsConsignResendResponse rsp = client.execute(req, Params.authcode); 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 = 4"; SQLHelper.executeSQL(conn, sql); sql = "delete from IT_UpNote where SheetID='" + sheetid + "' and sheettype=4"; SQLHelper.executeSQL(conn, sql); conn.commit(); conn.setAutoCommit(true); } catch (Exception 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); } else { if (rsp.getSubMsg().indexOf("不能重复发货") >= 0 || rsp.getSubMsg().indexOf("发货类型不匹配") >= 0) { 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 = 4"; SQLHelper.executeSQL(conn, sql); sql = "delete from IT_UpNote where SheetID='" + sheetid + "' and sheettype=4"; SQLHelper.executeSQL(conn, sql); Log.info( username, "订单【" + orderid + "】不能重复发货,快递公司【" + post_company + "】,快递单号【" + post_no + "】", null); } else if (rsp.getSubMsg().indexOf("订单未发货") >= 0) { 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 = 4"; SQLHelper.executeSQL(conn, sql); sql = "delete from IT_UpNote where SheetID='" + sheetid + "' and sheettype=4"; SQLHelper.executeSQL(conn, sql); Log.info( username, "订单【" + orderid + "】可能未发货,或已经退货,快递公司【" + post_company + "】,快递单号【" + post_no + "】", null); } else if ((rsp.getSubMsg().indexOf("没有权限进行发货") >= 0) || rsp.getSubMsg().indexOf("没有权限发货") >= 0 || rsp.getSubMsg().indexOf("当前订单状态不支持修改") >= 0) { 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 = 4"; SQLHelper.executeSQL(conn, sql); sql = "delete from IT_UpNote where SheetID='" + sheetid + "' and sheettype=4"; SQLHelper.executeSQL(conn, sql); Log.info( username, "没有权限发货,订单【" + orderid + "】,快递公司【" + post_company + "】,快递单号【" + post_no + "】", null); } else if (rsp.getSubMsg().indexOf("物流订单不存在") >= 0 || rsp.getSubMsg().indexOf("订单已经被拆单") >= 0 || rsp.getSubMsg().indexOf("当前操作的订单是拆单订单") >= 0) { TaobaoClient subclient = new DefaultTaobaoClient(Params.url, Params.appkey, Params.appsecret); LogisticsOfflineSendRequest subreq = new LogisticsOfflineSendRequest(); subreq.setOutSid(post_no); subreq.setTid(TranTid(orderid)); subreq.setCompanyCode(post_company); subreq.setIsSplit(1L); String subtids = ""; sql = "select oid from customerorderitem a with(nolock),customerorder b with(nolock)," + "customerdelive c with(nolock) where a.sheetid=b.sheetid and a.sheetid=c.refsheetid " + "and c.customersheetid='" + orderid + "' and c.delivery='" + post_company + "' " + "and c.deliverysheetid='" + post_no + "'"; List sublist = SQLHelper.multiRowListSelect(conn, sql); if (sublist.size() > 0) { for (Iterator it = sublist.iterator(); it.hasNext(); ) { String oid = (String) it.next(); subtids = oid + "," + subtids; } subtids = subtids.substring(0, subtids.length() - 1); subreq.setSubTid(subtids); LogisticsOfflineSendResponse subrsp = client.execute(subreq, Params.authcode); if (subrsp.isSuccess()) { Log.info( username, "处理订单【" + orderid + "】,子订单【" + subtids + "】发货成功,快递公司【" + post_company + "】,快递单号【" + post_no + "】", null); } } 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 = 4"; SQLHelper.executeSQL(conn, sql); sql = "delete from IT_UpNote where SheetID='" + sheetid + "' and sheettype=4"; SQLHelper.executeSQL(conn, sql); Log.info( jobName, "物流订单不存在或订单已经被拆,订单【" + orderid + "】,快递公司【" + post_company + "】,快递单号【" + post_no + "】"); } else if (rsp.getSubMsg().indexOf("运单号不符合规则或已经被使用") >= 0) { // 公司内部自提发货处理 if ((post_no.toUpperCase().indexOf("1111111") >= 0) || (post_no.toUpperCase().indexOf("YJ") >= 0)) { 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 = 4"; SQLHelper.executeSQL(conn, sql); sql = "delete from IT_UpNote where SheetID='" + sheetid + "' and sheettype=4"; SQLHelper.executeSQL(conn, sql); } else { String cc = ""; if (post_no.toUpperCase().indexOf("EH") == 0) { cc = "EMS"; } else if (post_no.toUpperCase().indexOf("101") == 0) { cc = "SF"; } else if (post_no.toUpperCase().indexOf("368") == 0) { cc = "STO"; } else if (post_no.toUpperCase().indexOf("W") == 0) { cc = "YTO"; } else cc = post_company; sql = "update ns_delivery set companycode='" + cc + "' where SheetID = '" + sheetid + "'"; SQLHelper.executeSQL(conn, sql); } Log.info( username, "运单号不符合规则或已经被使用,订单【" + orderid + "】,快递公司【" + post_company + "】,快递单号【" + post_no + "】", null); } else if (rsp.getSubMsg().indexOf("该订单不支持修改") >= 0) { 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 = 4"; SQLHelper.executeSQL(conn, sql); sql = "delete from IT_UpNote where SheetID='" + sheetid + "' and sheettype=4"; SQLHelper.executeSQL(conn, sql); Log.info( jobName, "处理订单【" + orderid + "】发货失败,快递公司【" + post_company + "】,快递单号【" + post_no + "】" + "错误信息:" + rsp.getSubMsg() + rsp.getMsg()); } else Log.info( username, "处理订单【" + orderid + "】发货失败,快递公司【" + post_company + "】,快递单号【" + post_no + "】" + "错误信息:" + rsp.getSubMsg() + rsp.getMsg(), null); } } catch (Exception ex) { if (conn != null && !conn.getAutoCommit()) conn.rollback(); Log.info(username, "修改发货信息作业出错,订单号: " + orderid + ",错误信息: " + ex.getMessage(), null); } } }
// 家装订单发货 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); } }
private void normalSend( String post_no, String orderid, String post_company, Connection conn, String sheetid) { try { TaobaoClient client = new DefaultTaobaoClient(Params.url, Params.appkey, Params.appsecret); LogisticsOfflineSendRequest req = new LogisticsOfflineSendRequest(); req.setOutSid(post_no); req.setTid(TranTid(orderid)); req.setCompanyCode(post_company); LogisticsOfflineSendResponse 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); } else { if (rsp.getSubCode().equals("isv.logistics-offline-service-error:B04")) { // 已经同步发货状态或退货成功了 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); Log.info(username, "订单: " + orderid + ", sheetid: " + sheetid + ",状态异常,已备份到备份表", null); } if (rsp.getSubCode().equals("isv.logistics-offline-service-error:P38")) { // 已经同步发货状态或退货成功了 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); Log.info( username, "订单: " + orderid + ", sheetid: " + sheetid + ",状态异常,,拆单校验未通过.已备份到备份表", null); } if (rsp.getSubCode().equals("isv.logistics-offline-service-error:S01")) { // 已经同步发货状态或退货成功了 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); Log.info(username, "订单: " + orderid + ", sheetid: " + sheetid + ",状态异常,已备份到备份表", null); } if (rsp.getSubCode().indexOf("ORDER_NOT_FOUND_ERROR") != -1) { 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); Log.error(username, jobName + ",订单号: " + orderid + ", 无法找到!", null); } if (rsp.getSubMsg().indexOf("不能重复发货") >= 0 || rsp.getSubMsg().indexOf("发货类型不匹配") >= 0) { 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); Log.info( username, "订单【" + orderid + "】不能重复发货,快递公司【" + post_company + "】,快递单号【" + post_no + "】", null); } else if ((rsp.getSubMsg().indexOf("没有权限进行发货") >= 0) || rsp.getSubMsg().indexOf("没有权限发货") >= 0 || rsp.getSubMsg().indexOf("当前订单状态不支持修改") >= 0) { 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); Log.info( username, "没有权限发货,订单【" + orderid + "】,快递公司【" + post_company + "】,快递单号【" + post_no + "】", null); } else if (rsp.getSubMsg().indexOf("物流订单不存在") >= 0 || rsp.getSubMsg().indexOf("订单已经被拆单") >= 0 || rsp.getSubMsg().indexOf("当前操作的订单是拆单订单") >= 0) { TaobaoClient subclient = new DefaultTaobaoClient(Params.url, Params.appkey, Params.appsecret); LogisticsOfflineSendRequest subreq = new LogisticsOfflineSendRequest(); subreq.setOutSid(post_no); subreq.setTid(TranTid(orderid)); subreq.setCompanyCode(post_company); subreq.setIsSplit(1L); String subtids = ""; sql = "select oid from customerorderitem a with(nolock),customerorder b with(nolock)," + "customerdelive c with(nolock) where a.sheetid=b.sheetid and a.sheetid=c.refsheetid " + "and c.customersheetid='" + orderid + "' and c.delivery='" + post_company + "' " + "and c.deliverysheetid='" + post_no + "'"; List sublist = SQLHelper.multiRowListSelect(conn, sql); for (Iterator it = sublist.iterator(); it.hasNext(); ) { String oid = (String) it.next(); subtids = oid + "," + subtids; } subtids = (subtids != null && subtids != "" ? subtids.substring(0, subtids.length() - 1) : ""); subreq.setSubTid(subtids); LogisticsOfflineSendResponse subrsp = client.execute(subreq, Params.authcode); if (subrsp.isSuccess()) { Log.info( jobName, "处理订单【" + orderid + "】,子订单【" + subtids + "】发货成功,快递公司【" + post_company + "】,快递单号【" + post_no + "】"); } 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); Log.info( username, "物流订单不存在或订单已经被拆单,订单【" + orderid + "】,快递公司【" + post_company + "】,快递单号【" + post_no + "】", null); } else if (rsp.getSubMsg().indexOf("运单号不符合规则或已经被使用") >= 0) { // 公司内部自提发货处理 if ((post_no.toUpperCase().indexOf("1111111") >= 0) || (post_no.toUpperCase().indexOf("YJ") >= 0)) { 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); } else { String cc = ""; String memo = ""; if (post_no.toUpperCase().indexOf("EH") == 0) { cc = "EMS"; } else if (post_no.toUpperCase().indexOf("101") == 0) { cc = "SF"; } else if (post_no.toUpperCase().indexOf("368") == 0) { cc = "STO"; } else if (post_no.toUpperCase().indexOf("W") == 0) { cc = "YTO"; } else { cc = post_company; // 把错误的运单号对应的订单号修改一下,让后面修改后的快递信息能正确写入sheettype=3 orderid += "运单号不符合规则或已经被使用"; memo += "运单号不符合规则或已经被使用"; } sql = "update ns_delivery set companycode='" + cc + "',tid='" + orderid + "',memo='" + memo + "' where SheetID = '" + sheetid + "'"; SQLHelper.executeSQL(conn, sql); } 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); Log.info( jobName, "运单号不符合规则或已经被使用,订单【" + orderid + "】,快递公司【" + post_company + "】,快递单号【" + post_no + "】"); } else { Log.info( username, "处理订单【" + orderid + "】发货失败,快递公司【" + post_company + "】,快递单号【" + post_no + "】" + "错误信息:" + rsp.getSubMsg() + rsp.getMsg(), null); String errmsg = rsp.getSubMsg(); Pattern par = Pattern.compile("This ban will last for (\\d{1,9}) more seconds"); Matcher m = par.matcher(errmsg); if (m.find()) { long delayTime = Long.parseLong(m.group(1)) * 1000L; long now = System.currentTimeMillis(); while (System.currentTimeMillis() < now + delayTime) { Thread.sleep(1000L); } } } } } catch (ApiException e) { Log.error( username, "处理订单【" + orderid + "】发货失败,快递公司【" + post_company + "】,快递单号【" + post_no + "】,错误信息:" + e.getMessage(), null); } catch (Exception e) { Log.error(username, "同步发货状态出错了, 错误信息: " + e.getMessage(), null); } }
/* * 获取一天之类的退款订单 */ private void getRefundBillList(Connection conn) throws Exception { long pageno = 1L; Date modified = Formatter.parseDate(lasttime, Formatter.DATE_TIME_FORMAT); for (int k = 0; k < 10; ) { try { TaobaoClient client = new DefaultTaobaoClient(Params.url, Params.appkey, Params.appsecret, "json"); TmallEaiOrderRefundMgetRequest req = new TmallEaiOrderRefundMgetRequest(); Date startdate = new Date(Formatter.parseDate(lasttime, Formatter.DATE_TIME_FORMAT).getTime() + 1000L); Date enddate = new Date( Formatter.parseDate(lasttime, Formatter.DATE_TIME_FORMAT).getTime() + daymillis); req.setStartTime(startdate); req.setEndTime(enddate); req.setPageNo(pageno); req.setPageSize(40L); req.setUseHasNext(true); TmallEaiOrderRefundMgetResponse response = client.execute(req, Params.authcode); while (true) { if (response.getRefundBillList() == null || response.getRefundBillList().size() <= 0) { if (pageno == 1) { try { // 如一天之内都取不到订单,而且当前天大于配置天,则将取订单最新时间更新为当前天的零点 if (this.dateformat .parse(Formatter.format(new Date(), Formatter.DATE_FORMAT)) .compareTo( this.dateformat.parse( Formatter.format( Formatter.parseDate( PublicUtils.getConfig(conn, lasttimeconfvalue, ""), Formatter.DATE_TIME_FORMAT), Formatter.DATE_FORMAT))) > 0) { try { String value = Formatter.format( (new Date( Formatter.parseDate( PublicUtils.getConfig(conn, lasttimeconfvalue, ""), Formatter.DATE_TIME_FORMAT) .getTime() + daymillis)), Formatter.DATE_FORMAT) + " 00:00:00"; PublicUtils.setConfig(conn, lasttimeconfvalue, value); } catch (JException je) { Log.error(jobname, je.getMessage()); } } } catch (ParseException e) { Log.error(jobname, "不可用的日期格式!" + e.getMessage()); } } break; } for (Iterator it = response.getRefundBillList().iterator(); it.hasNext(); ) { RefundBill refundbill = (RefundBill) it.next(); Log.info( refundbill.getTid() + " " + refundbill.getStatus() + " " + refundbill.getModified()); if (!RefundUtil.RefundBillisCheck( conn, String.valueOf(refundbill.getTid()), String.valueOf(refundbill.getRefundId()), refundbill.getModified())) { Trade td = OrderUtils.getFullTrade( String.valueOf(refundbill.getTid()), Params.url, Params.address, Params.appsecret, Params.authcode); RefundUtil.createRefundBill(conn, refundbill, td, Params.tradecontactid); } if (Formatter.parseDate(refundbill.getModified(), Formatter.DATE_TIME_FORMAT) .compareTo(modified) > 0) { modified = Formatter.parseDate(refundbill.getModified(), Formatter.DATE_TIME_FORMAT); } } // 判断是否下一页 if (response.getHasNext()) { pageno++; } else { break; } } if (modified.compareTo(Formatter.parseDate(lasttime, Formatter.DATE_TIME_FORMAT)) > 0) { try { String value = Formatter.format(modified, Formatter.DATE_TIME_FORMAT); PublicUtils.setConfig(conn, lasttimeconfvalue, value); } catch (JException je) { Log.error(jobname, je.getMessage()); } } // 执行成功后不再循环 break; } catch (Exception e) { if (++k >= 10) throw e; Log.warn("远程连接失败[" + k + "], 10秒后自动重试. " + Log.getErrorMessage(e)); Thread.sleep(10000L); } } }