|
@@ -1,21 +1,20 @@
|
|
package in.ocsf.these.days.app.service;/* kpmy 19.02.2017 */
|
|
package in.ocsf.these.days.app.service;/* kpmy 19.02.2017 */
|
|
|
|
|
|
import com.pengrad.telegrambot.model.Update;
|
|
import com.pengrad.telegrambot.model.Update;
|
|
-import in.ocsf.these.days.app.messaging.ChatHelper;
|
|
|
|
import in.ocsf.these.days.app.messaging.UpdateHelper;
|
|
import in.ocsf.these.days.app.messaging.UpdateHelper;
|
|
import in.ocsf.these.days.app.object.Chat;
|
|
import in.ocsf.these.days.app.object.Chat;
|
|
import in.ocsf.these.days.app.object.User;
|
|
import in.ocsf.these.days.app.object.User;
|
|
-import in.ocsf.these.days.app.repo.CardRepositrory;
|
|
|
|
import in.ocsf.these.days.app.repo.ChatRepository;
|
|
import in.ocsf.these.days.app.repo.ChatRepository;
|
|
-import in.ocsf.these.days.app.repo.MessageRepository;
|
|
|
|
import in.ocsf.these.days.app.repo.UserRepository;
|
|
import in.ocsf.these.days.app.repo.UserRepository;
|
|
-import in.ocsf.these.days.app.state.*;
|
|
|
|
|
|
+import in.ocsf.these.days.app.state.IChat;
|
|
|
|
+import in.ocsf.these.days.app.state.StateService;
|
|
|
|
+import in.ocsf.these.days.app.state.account.Accounted;
|
|
import org.apache.log4j.Logger;
|
|
import org.apache.log4j.Logger;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
import org.springframework.statemachine.StateMachine;
|
|
import org.springframework.statemachine.StateMachine;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
@@ -24,13 +23,10 @@ public class UpdateService {
|
|
private final Logger log = Logger.getLogger(getClass());
|
|
private final Logger log = Logger.getLogger(getClass());
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private UserRepository userRepo;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- private MessageRepository msgRepo;
|
|
|
|
|
|
+ private ApplicationContext context;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private CardRepositrory cardRepo;
|
|
|
|
|
|
+ private UserRepository userRepo;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private StateService stateService;
|
|
private StateService stateService;
|
|
@@ -39,51 +35,46 @@ public class UpdateService {
|
|
private ChatRepository chatRepo;
|
|
private ChatRepository chatRepo;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private ChatHelper chat;
|
|
|
|
-
|
|
|
|
- private void handleUpdate(UpdateHelper upd) throws Exception {
|
|
|
|
- if (!upd.isEdit()) {
|
|
|
|
- if (upd.hasText()) {
|
|
|
|
- if (upd.isCommand()) {
|
|
|
|
- List<String> cmd = upd.getSplitCommand();
|
|
|
|
- switch (cmd.get(0)) {
|
|
|
|
- case "/start":
|
|
|
|
- User user = User.fromUser(upd.getUser());
|
|
|
|
- user = Optional.ofNullable(userRepo.findOne(user.getId())).orElse(user);
|
|
|
|
- StateMachine<UserState, UserEvent> state = stateService.getStateFor(user);
|
|
|
|
- if (state.getState() == null) state.start();
|
|
|
|
- switch (state.getState().getId()) {
|
|
|
|
- case unknown:
|
|
|
|
- stateService.setStateFor(user, state);
|
|
|
|
- userRepo.save(user);
|
|
|
|
-
|
|
|
|
- chatRepo.findByUserIdAndThenClose(user.getId());
|
|
|
|
|
|
+ private CommandService commandService;
|
|
|
|
|
|
- Chat chat = new Chat();
|
|
|
|
- chat.setUser(user);
|
|
|
|
- chat.setType(ChatType.welcome);
|
|
|
|
- StateMachine<WelcomeChatState, WelcomeChatEvent> chatState = stateService.getChatStateFor(chat);
|
|
|
|
- chatState.start();
|
|
|
|
- stateService.setChatStateFor(chat, chatState);
|
|
|
|
- chatRepo.save(chat);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private Accounted accounted;
|
|
|
|
+
|
|
|
|
+ private void handleUpdate(UpdateHelper upd) {
|
|
|
|
+ if (!upd.hasText()) throw new RuntimeException();
|
|
|
|
+
|
|
|
|
+ User user = User.fromUser(upd.getUser());
|
|
|
|
+ user = Optional.ofNullable(userRepo.findOne(user.getId())).orElse(user);
|
|
|
|
+ StateMachine<Accounted.State, Accounted.Event> state = stateService.getStateFor(user);
|
|
|
|
+ stateService.put(state, IChat.USER, user.getId());
|
|
|
|
+ stateService.put(state, upd);
|
|
|
|
+
|
|
|
|
+ if (state.getState() == null) {
|
|
|
|
+ userRepo.save(user);
|
|
|
|
+ chatRepo.findByUserIdAndThenClose(user.getId());
|
|
|
|
+ state.start();
|
|
|
|
+ stateService.setStateFor(user, state);
|
|
|
|
+ userRepo.save(user);
|
|
|
|
+ }
|
|
|
|
+ if (upd.isCommand())
|
|
|
|
+ commandService.handle(state);
|
|
|
|
+ else {
|
|
|
|
+ Optional<Chat> chat0 = chatRepo.findByUserIdAndStateThenActualize(user.getId());
|
|
|
|
+ chat0.ifPresent(c -> {
|
|
|
|
+ StateMachine chatState = stateService.getChatStateFor(c);
|
|
|
|
+ stateService.put(chatState, upd);
|
|
|
|
+ stateService.put(chatState, IChat.CHAT, c.getId());
|
|
|
|
+ IChat chat = context.getBean(c.getType().getBean());
|
|
|
|
+ chat.handle(chatState);
|
|
|
|
+ });
|
|
|
|
+ if (!chat0.isPresent())
|
|
|
|
+ accounted.handle(state);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void handleUpdate(Update upd) {
|
|
public void handleUpdate(Update upd) {
|
|
- try {
|
|
|
|
- handleUpdate(UpdateHelper.from(upd));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
- }
|
|
|
|
|
|
+ UpdateHelper helper = UpdateHelper.from(upd);
|
|
|
|
+ if (!helper.isEdit()) handleUpdate(helper);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|