package com.synutra.orderauto.main;
import static watij.finders.SymbolFactory.id;
import static watij.finders.SymbolFactory.name;
import static watij.finders.SymbolFactory.text;
import static watij.finders.SymbolFactory.value;
import java.util.List;
import watij.elements.Button;
import watij.elements.Buttons;
import watij.runtime.ie.IE;
import com.synutra.orderauto.model.Orderinfo;
import com.synutra.orderauto.model.Taskinfo;
import com.synutra.orderauto.service.OrderInfoService;
public class OrderAuto {
private OrderInfoService orderInfoService;
private List<Orderinfo> orderlist;
public void OrderTask(int length, int offset) {
if (null != this.getOrderlist() && this.getOrderlist().size() > 0) {
} else {
this.setOrderlist(this.orderInfoService.findOrders(offset, length));
}
IE ie = new IE();
try {
ie.start();
for (Orderinfo o : this.getOrderlist()) {
offset++;
Taskinfo task = new Taskinfo(
"http://wuliu.taobao.com/user/order_list_new.htm?order_status_show=send&nekot=",
o.getDdbh().trim(), o.getXm().trim(), "宏品", o.getWlbh()
.trim());
System.out.println(offset + ": 订单编号:" + o.getDdbh().trim()
+ " 收货人:" + o.getXm().trim() + " 物流编号"
+ o.getWlbh().trim());
// 打开发货页面
ie.goTo(task.get_url());
// 填写订单编号
ie.textField(name, "taobaoTradeId").set(task.get_tradeId());
ie.button("搜索").click();
// 点击搜索
if (ie.table(id, "J_Express").text().contains(task.get_name())) {
// 确认收货人姓名,点击发货
ie.table(id, "J_Express").link(text, "发货").click();
} else {
// 未找到发货人,发货人不正确,已发货
System.out.println("未找到发货人,发货人不正确,已发货");
o.setZt("未找到发货人/发货人不正确/已发货");
o.setClg(Integer.valueOf(1));
this.orderInfoService.updateOrders(o);
continue;
}
ie.selectList(id, "logis:LeSelector").option(value, "OTHER")
.select();
if (ie.textField(id, "logis:LeText").enabled()) {
ie.textField(id, "logis:LeText").click();
ie.textField(id, "logis:LeText").set(task.get_letext());// 物流公司名称
ie.textField(id, "logis:other").set(task.get_other());// 物流编号
Buttons c = ie.div(id, "logis:offline").buttons();
for (Button button : c) {
if ("".equals(button.id())) {
button.click();
}
}
}
if (ie.containsText("恭喜您,操作成功")) {
// 成功
System.out.println("成功");
o.setZt("成功");
o.setClg(Integer.valueOf(1));
this.orderInfoService.updateOrders(o);
} else {
// 失败
System.out.println("失败");
o.setZt("失败");
o.setClg(Integer.valueOf(0));//需要重新处理
this.orderInfoService.updateOrders(o);
}
}
ie.close();
} catch (Exception e) {
try {
ie.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
// e1.printStackTrace();
} finally {
this.setOrderlist(null);
OrderTask(offset - 1, length);
}
}
}
public List<Orderinfo> getOrderlist() {
return orderlist;
}
public void setOrderlist(List<Orderinfo> orderlist) {
this.orderlist = orderlist;
}
public OrderInfoService getOrderInfoService() {
return orderInfoService;
}
public void setOrderInfoService(OrderInfoService orderInfoService) {
this.orderInfoService = orderInfoService;
}