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;
}
}
}

View File

@@ -59,7 +59,7 @@ public class WxMpServlet extends HttpServlet {
.msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_SUBSCRIBE)
.handler(new SubscribeHandler())
.next();
.end();
wxMpMessageRouter.rule()
.async(false)
.matcher(new CheckSpamMatcher())

View File

@@ -1,5 +1,6 @@
package love.sola.netsupport.wechat.handler;
import love.sola.netsupport.api.user.Register;
import love.sola.netsupport.enums.Attribute;
import love.sola.netsupport.pojo.User;
import love.sola.netsupport.sql.TableUser;
@@ -38,6 +39,7 @@ public class SubscribeHandler implements WxMpMessageHandler {
session.setAttribute(Attribute.WECHAT, fromUser);
session.setAttribute(Attribute.USER, u);
out.content(format("Event_Subscribe", format("Already_Registered", format("User_Profile_Link", id, u.getName(), u.getIsp().id, u.getNetAccount(), u.getBlock(), u.getRoom(), u.getPhone()))));
Register.converterWithRetry(u);
} else {
session.setAttribute(Attribute.AUTHORIZED, Command.REGISTER);
session.setAttribute(Attribute.WECHAT, fromUser);
@@ -45,4 +47,5 @@ public class SubscribeHandler implements WxMpMessageHandler {
}
return out.build();
}
}