improve operator converting

This commit is contained in:
Sola
2016-01-03 12:25:56 +08:00
parent a804b79e6f
commit ea9e78936d
3 changed files with 36 additions and 20 deletions

View File

@@ -109,7 +109,7 @@ public class Register extends HttpServlet {
return "Duplicated_" + dupKey.toUpperCase(); // PHONE ACCOUNT WECHAT
}
// FIXME: 2015/12/30 Temporary converter
converter(user);
converterWithRetry(user);
return "Register_Success";
}
@@ -119,27 +119,17 @@ public class Register extends HttpServlet {
return;
}
private void converter(User u) {
try (Connection conn = SQLCore.ds.getConnection()) {
PreparedStatement ps = conn.prepareStatement("SELECT wechat FROM `convert` WHERE id=?");
ps.setLong(1, u.getId());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
String old = rs.getString(1);
ps = conn.prepareStatement("UPDATE `operators` SET wechat=? WHERE wechat=?");
ps.setString(1, u.getWechatId());
ps.setString(2, old);
ps.executeUpdate();
WxMpServlet.instance.wxMpService
.userUpdateGroup(u.getWechatId(), 100L);
WxMpServlet.instance.wxMpService.customMessageSend(WxMpCustomMessage.TEXT().toUser(u.getWechatId()).content("数据转换成功").build());
return;
} else {
public static void converterWithRetry(User u) {
Throwable last = null;
for (int i = 0; i < 3; i++) {
try {
converter(u);
return;
} catch (WxErrorException | SQLException e) {
last = e;
}
} catch (SQLException | WxErrorException e) {
e.printStackTrace();
}
last.printStackTrace();
try {
WxMpServlet.instance.wxMpService.customMessageSend(WxMpCustomMessage.TEXT().toUser(u.getWechatId()).content("数据转换失败").build());
} catch (WxErrorException e) {
@@ -147,4 +137,27 @@ public class Register extends HttpServlet {
}
}
public static void converter(User u) throws WxErrorException, SQLException {
try (Connection conn = SQLCore.ds.getConnection()) {
PreparedStatement ps = conn.prepareStatement("SELECT wechat FROM `convert` WHERE id=?");
ps.setLong(1, u.getId());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
WxMpServlet.instance.wxMpService
.userUpdateGroup(u.getWechatId(), 100L);
String old = rs.getString(1);
ps = conn.prepareStatement("UPDATE `operators` SET wechat=? WHERE wechat=?");
ps.setString(1, u.getWechatId());
ps.setString(2, old);
if (ps.executeUpdate() == 1) {
WxMpServlet.instance.wxMpService.customMessageSend(WxMpCustomMessage.TEXT().toUser(u.getWechatId()).content("数据转换成功").build());
} else {
WxMpServlet.instance.wxMpService.customMessageSend(WxMpCustomMessage.TEXT().toUser(u.getWechatId()).content("已进行过数据转换").build());
}
}
} catch (SQLException | WxErrorException e) {
throw e;
}
}
}