diff --git a/core/src/main/java/org/sadtech/consultant/database/entity/Message.java b/core/src/main/java/org/sadtech/consultant/database/entity/Message.java index b1d0ff3..69afff2 100644 --- a/core/src/main/java/org/sadtech/consultant/database/entity/Message.java +++ b/core/src/main/java/org/sadtech/consultant/database/entity/Message.java @@ -14,6 +14,7 @@ public class Message { private String text; private Long date; private String sourceMessage; + private String sex; @ManyToOne @JoinColumn(name = "idUser", nullable = false) diff --git a/core/src/main/java/org/sadtech/consultant/database/entity/Person.java b/core/src/main/java/org/sadtech/consultant/database/entity/Person.java index 5a3e580..7ab5275 100644 --- a/core/src/main/java/org/sadtech/consultant/database/entity/Person.java +++ b/core/src/main/java/org/sadtech/consultant/database/entity/Person.java @@ -3,8 +3,7 @@ package org.sadtech.consultant.database.entity; import lombok.Data; import javax.persistence.*; -import java.util.HashMap; -import java.util.Map; +import java.util.*; @Entity @Data @@ -30,4 +29,7 @@ public class Person { @MapKeyColumn(name="KeysSocialNetworks", length = 20) private Map socialNetworks = new HashMap<>(); +// @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="idMess") +// private List messages; + } diff --git a/core/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java b/core/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java index f5d967b..284a997 100644 --- a/core/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java +++ b/core/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java @@ -5,6 +5,7 @@ import org.sadtech.consultant.database.repository.MessageRepository; import org.sadtech.consultant.database.service.MessageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; diff --git a/core/src/main/java/org/sadtech/consultant/processing/MessageHandler.java b/core/src/main/java/org/sadtech/consultant/processing/MessageHandler.java index 232b33e..e486947 100644 --- a/core/src/main/java/org/sadtech/consultant/processing/MessageHandler.java +++ b/core/src/main/java/org/sadtech/consultant/processing/MessageHandler.java @@ -27,9 +27,7 @@ public class MessageHandler { while (true) { List messages = messageLogicService.getMessageRange(data); for (Message message : messages) { - System.out.println(message); - - + log.info(message); data = message.getDate(); } } diff --git a/core/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java b/core/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java index 1ed910a..7b37ba7 100644 --- a/core/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java +++ b/core/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java @@ -1,6 +1,7 @@ package org.sadtech.consultant.processing; import org.sadtech.consultant.database.entity.Message; +import org.sadtech.consultant.database.entity.Person; import org.sadtech.consultant.database.service.MessageService; import org.springframework.stereotype.Component; diff --git a/core/src/main/java/org/sadtech/consultant/processing/PersonLogicService.java b/core/src/main/java/org/sadtech/consultant/processing/PersonLogicService.java index 28c672c..4da66f7 100644 --- a/core/src/main/java/org/sadtech/consultant/processing/PersonLogicService.java +++ b/core/src/main/java/org/sadtech/consultant/processing/PersonLogicService.java @@ -1,9 +1,11 @@ package org.sadtech.consultant.processing; +import lombok.extern.log4j.Log4j; import org.sadtech.consultant.database.entity.Person; import org.sadtech.consultant.database.service.PersonService; import org.springframework.stereotype.Component; +@Log4j @Component public class PersonLogicService { @@ -13,9 +15,30 @@ public class PersonLogicService { this.personService = userService; } - public boolean addUser(Person user) { + public void addUser(Person user) { personService.addUser(user); - return true; + log.info("Пользователь добавлен в бд"); + } + + public boolean checkPersonBySocialNetworksId(String socType, Integer socId) { + Person checkPerson = personService.getUserBySocialNetworksId(socType, socId); + if (checkPerson != null) { + log.info("Пользователь уже есть в бд"); + return true; + } + log.info("Пользователя нет в бд"); + return false; + } + + public Person getPersonBySocialNetworksId(Person person) { + for (String key : person.getSocialNetworks().keySet()) { + Person getPerson = personService.getUserBySocialNetworksId(key, person.getSocialNetworks().get(key)); + if (getPerson != null) { + log.info("Пользователь уже есть в бд"); + return getPerson; + } + } + return null; } public Person getUserById(Long id) { diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/Main.java b/vk-bot/src/main/java/org/sadtech/vkbot/Main.java index 2f5d11e..801fc20 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/Main.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/Main.java @@ -16,8 +16,6 @@ public class Main { } public void run() throws Exception { - log.info("\n\n\n\n=== Запуск прогарммы ===\n\n"); - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigVk.class); EventListenable eventListener = context.getBean(EventListenable.class); eventListener.listen(); @@ -25,7 +23,5 @@ public class Main { dispetcherHandler.sortAndSend(); // MessageHandler messageHandler = context.getBean(MessageHandler.class); // messageHandler.processing(); - - log.info("\n\n=== Конец программы ===\n\n"); } } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/SourceMessage.java b/vk-bot/src/main/java/org/sadtech/vkbot/SourceMessage.java index aca1153..d76fef1 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/SourceMessage.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/SourceMessage.java @@ -1,5 +1,16 @@ package org.sadtech.vkbot; public enum SourceMessage { - VK, FB + VK("VK"), + FB("FB"); + + private final String value; + + private SourceMessage(String value) { + this.value = value; + } + + public String getValue() { + return this.value; + } } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/VkApi.java b/vk-bot/src/main/java/org/sadtech/vkbot/VkApi.java index 659b304..960820b 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/VkApi.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/VkApi.java @@ -6,12 +6,16 @@ import com.vk.api.sdk.client.actors.GroupActor; import com.vk.api.sdk.exceptions.ApiException; import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.objects.users.User; -import com.vk.api.sdk.objects.users.UserFull; +import com.vk.api.sdk.objects.users.UserMin; import com.vk.api.sdk.objects.users.UserXtrCounters; +import com.vk.api.sdk.queries.users.UserField; import lombok.extern.log4j.Log4j; import org.springframework.stereotype.Component; +import java.lang.reflect.Field; +import java.util.HashMap; import java.util.List; +import java.util.Map; @Log4j @Component @@ -33,15 +37,14 @@ public class VkApi { } } - public static String getUserName(Integer id) { - List user = null; - UserFull userFull = null; + public static UserXtrCounters getUserVk(Integer id) { + List temp = null; try { - user = vk.users().get(actor).userIds(String.valueOf(id)).execute(); + temp = vk.users().get(actor).userIds(String.valueOf(id)).execute(); } catch (ApiException | ClientException e) { e.printStackTrace(); } - return user.get(0).getLastName() + " " + user.get(0).getFirstName(); + return temp.get(0); } } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/handlers/impl/MessageHandlerVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/handlers/impl/MessageHandlerVk.java index dd1aaa5..04c25ac 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/handlers/impl/MessageHandlerVk.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/handlers/impl/MessageHandlerVk.java @@ -2,26 +2,34 @@ package org.sadtech.vkbot.handlers.impl; import com.google.gson.Gson; import com.google.gson.JsonObject; +import com.vk.api.sdk.objects.users.UserXtrCounters; +import com.vk.api.sdk.queries.users.UserField; import lombok.extern.log4j.Log4j; +import org.sadtech.consultant.database.entity.Message; import org.sadtech.consultant.database.entity.Person; import org.sadtech.consultant.processing.MessageLogicService; import org.sadtech.consultant.processing.PersonLogicService; -import org.sadtech.consultant.database.entity.Message; import org.sadtech.vkbot.SourceMessage; +import org.sadtech.vkbot.VkApi; import org.sadtech.vkbot.listener.Observable; import org.sadtech.vkbot.listener.Observer; import org.springframework.stereotype.Component; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + @Log4j @Component public class MessageHandlerVk implements Observer { private MessageLogicService messageLogicService; - private PersonLogicService userLogicService; + private PersonLogicService personLogicService; - public MessageHandlerVk(Observable dispetcherHandler, PersonLogicService userLogicService, MessageLogicService messageLogicService) { - this.userLogicService = userLogicService; + public MessageHandlerVk(Observable dispetcherHandler, PersonLogicService personLogicService, MessageLogicService messageLogicService) { this.messageLogicService = messageLogicService; + this.personLogicService = personLogicService; dispetcherHandler.registerObserver(this); } @@ -29,23 +37,29 @@ public class MessageHandlerVk implements Observer { public void update(JsonObject object) { if (object.get("type").toString().equals("\"message_new\"")) { Gson gson = new Gson(); + log.info(object.getAsJsonObject("object")); com.vk.api.sdk.objects.messages.Message message = gson.fromJson(object.getAsJsonObject("object"), com.vk.api.sdk.objects.messages.Message.class); sendProcessing(message); } } private void sendProcessing(com.vk.api.sdk.objects.messages.Message userMessage) { - log.info(userMessage.getBody()); - Message message = new Message(); -// Person user = userLogicService.getUserBySocialId(SourceMessage.VK.name(), Long.valueOf(userMessage.getUserId())); Person user; - user = new Person(); - user.getSocialNetworks().put(SourceMessage.VK.name(), userMessage.getUserId()); - user.setName("Name"); - user.setLastName("LastName"); - user.setCity("City"); - userLogicService.addUser(user); - log.info(user); + Integer userVkId = userMessage.getUserId(); + log.info(VkApi.getUserVk(userVkId)); + if (personLogicService.checkPersonBySocialNetworksId(SourceMessage.VK.name(), userVkId)) { + user = personLogicService.getUserBySocialId(SourceMessage.VK.name(), userMessage.getUserId()); + } else { + user = new Person(); + UserXtrCounters userXtrCounters = VkApi.getUserVk(userVkId); + user.setCity("ыыыы"); + user.setName(userXtrCounters.getFirstName()); + user.setLastName(userXtrCounters.getLastName()); + user.getSocialNetworks().put(String.valueOf(SourceMessage.VK), userMessage.getUserId()); + personLogicService.addUser(user); + } + + Message message = new Message(); message.setUser(user); message.setText(userMessage.getBody()); message.setDate(Long.valueOf(userMessage.getDate()));