Search in sources :

Example 1 with RandomCodeDialog

use of lab.ticket.view.RandomCodeDialog in project 12306-hunter by xautlx.

the class TicketUserThread method run.

@Override
public void run() {
    String title = "[线程: " + this.getId() + ", 登录用户:" + userData.getLoginUser() + "] ";
    TicketMainFrame.appendMessage(title + "刷票日期顺序列表:");
    List<String> trainDates = ticketData.getTrainDateList();
    for (String trainDate : trainDates) {
        TicketMainFrame.appendMessage(title + " - " + trainDate);
    }
    while (!success) {
        try {
            if (terminalSign) {
                TicketMainFrame.appendMessage(title + "终止退出");
                return;
            }
            TicketMainFrame.appendMessage(title + "第 " + (++count) + " 次刷票");
            SingleTrainOrderVO singleTrainOrderVO = null;
            for (String trainDate : trainDates) {
                TicketMainFrame.appendMessage(title + "查询【" + trainDate + "】车票信息响应:");
                List<TrainQueryInfo> trainQueryInfos = httpClientService.queryTrain(ticketData, userData, trainDate);
                for (TrainQueryInfo trainQueryInfo : trainQueryInfos) {
                    if (!trainQueryInfo.isValidForPurchase()) {
                        TicketMainFrame.appendMessage(title + " - :( 当前不可购买或无票车次:" + trainDate + trainQueryInfo);
                    } else {
                        TicketMainFrame.appendMessage(title + " - :) 可购买车次:" + trainDate + trainQueryInfo);
                    }
                }
                // 从查询到车次数据按照优先级找到最匹配的车次和座位类型
                // --:没有该席别;*:未到开始时间;有:有并且数量充足;数字:有但数量有限:无:已售完
                List<TrainData> trainDatas = userData.getTrainDatas();
                for (TrainData trainData : trainDatas) {
                    for (TrainQueryInfo trainQueryInfo : trainQueryInfos) {
                        if (!trainQueryInfo.isValidForPurchase()) {
                            continue;
                        }
                        if (trainQueryInfo.getTrainNo().equals(trainData.getTrainNo())) {
                            String seatTypeValue = trainQueryInfo.getSeatDatas().get(trainData.getSeatType());
                            if (seatTypeValue.equals("无")) {
                                continue;
                            }
                            TicketMainFrame.appendMessage(title + ":) 预定车次有票:" + trainData.getTrainNo() + ",席别:" + trainData.getSeatType() + ",日期:" + trainDate + ",车票:" + seatTypeValue);
                            singleTrainOrderVO = new SingleTrainOrderVO();
                            singleTrainOrderVO.setTrainQueryInfo(trainQueryInfo);
                            singleTrainOrderVO.setLoginUser(userData.getLoginUser());
                            singleTrainOrderVO.setCookieData(userData.getCookieData());
                            singleTrainOrderVO.setSeatType(trainData.getSeatType());
                            singleTrainOrderVO.setTrainDate(trainDate);
                            singleTrainOrderVO.setTrainNo(trainData.getTrainNo());
                            break;
                        }
                    }
                    if (singleTrainOrderVO != null) {
                        break;
                    }
                    TicketMainFrame.appendMessage(title + ":( 预定车次无票:" + trainData.getTrainNo() + ",席别:" + trainData.getSeatType() + ",日期:" + trainDate);
                }
                if (singleTrainOrderVO != null) {
                    break;
                }
            }
            if (singleTrainOrderVO == null) {
                TicketMainFrame.appendMessage(title + "郁闷:所有指定日期车次目前均无法购票或无票。暂停 " + SLEEP_SECONDS + " 秒继续尝试刷票...");
                Thread.sleep(Float.valueOf(SLEEP_SECONDS * 1000).intValue());
            } else {
                TicketMainFrame.appendMessage(title + "暂停 " + SLEEP_SECONDS + " 秒后继续...");
                Thread.sleep(Float.valueOf(SLEEP_SECONDS * 1000).intValue());
                httpClientService.submitOrderRequest(singleTrainOrderVO);
                String responseBody = null;
                do {
                    synchronized (this) {
                        new RandomCodeDialog(singleTrainOrderVO, this);
                        this.wait();
                    }
                    if (StringUtils.isBlank(this.submitCode)) {
                        TicketMainFrame.appendMessage(title + "已取消本次下单验证码输入");
                        break;
                    }
                    responseBody = httpClientService.confirmSingleForQueueOrder(ticketData, singleTrainOrderVO, this.submitCode, true);
                    if (responseBody.contains("验证码")) {
                        TicketMainFrame.appendMessage(title + "下单验证码错误,请重新输入");
                    }
                } while (responseBody.contains("验证码"));
                //如果errMsg不为Y则表示访问错误,继续下一次循环
                if (responseBody == null || responseBody.indexOf("\"errMsg\":\"Y\"") == -1) {
                    continue;
                }
                //下单确认前的余票数量检查请求
                TicketMainFrame.appendMessage(title + "暂停 " + SLEEP_SECONDS + " 秒后继续...");
                Thread.sleep(Float.valueOf(SLEEP_SECONDS * 1000).intValue());
                responseBody = httpClientService.getQueueCount(ticketData, singleTrainOrderVO);
                //此处可考虑添加检查判断逻辑:只有最新返回有余票才继续后续提交订单
                // 提交订单
                TicketMainFrame.appendMessage(title + "暂停 " + SLEEP_SECONDS + " 秒后继续...");
                Thread.sleep(Float.valueOf(SLEEP_SECONDS * 1000).intValue());
                responseBody = httpClientService.confirmSingleForQueueOrder(ticketData, singleTrainOrderVO, this.submitCode, false);
                if (responseBody.indexOf("\"errMsg\":\"Y\"") > -1) {
                    String msg = title + "貌似已成功下单,赶快用浏览器登录此账号访问“未完成订单”中查看确认及进行后续付款操作";
                    JOptionPane.showMessageDialog(ticketMainFrame, msg);
                    TicketMainFrame.appendMessage(msg);
                    TicketMainFrame.appendMessage(title + "已终止当前用户刷票线程,其他用户刷票线程继续运行");
                    success = true;
                    break;
                }
            }
        } catch (InterruptedException e) {
            TicketMainFrame.appendMessage(title + "异常:" + e.getMessage());
            logger.error("Error at thread " + this.getId() + " for user " + userData.getLoginUser(), e);
        }
    }
}
Also used : TrainData(lab.ticket.model.TrainData) RandomCodeDialog(lab.ticket.view.RandomCodeDialog) SingleTrainOrderVO(lab.ticket.model.SingleTrainOrderVO) TrainQueryInfo(lab.ticket.model.TrainQueryInfo)

Aggregations

SingleTrainOrderVO (lab.ticket.model.SingleTrainOrderVO)1 TrainData (lab.ticket.model.TrainData)1 TrainQueryInfo (lab.ticket.model.TrainQueryInfo)1 RandomCodeDialog (lab.ticket.view.RandomCodeDialog)1