Переглянути джерело

Implement-send-notification

new-feature
Viet.LeQ2 1 рік тому
джерело
коміт
dfc8eee99a
4 змінених файлів з 111 додано та 33 видалено
  1. BIN
      build/libs/smart-farm-0.0.1-SNAPSHOT.war
  2. +2
    -0
      src/main/java/vn/azteam/tpf/Constants/Constants.java
  3. +58
    -0
      src/main/java/vn/azteam/tpf/service/NotificationReminderService.java
  4. +51
    -33
      src/main/java/vn/azteam/tpf/service/TBCropQueryService.java

BIN
build/libs/smart-farm-0.0.1-SNAPSHOT.war Переглянути файл


+ 2
- 0
src/main/java/vn/azteam/tpf/Constants/Constants.java Переглянути файл

@@ -138,6 +138,8 @@ public class Constants {
public static final String NOTIFICATION_TITLE_ENV_UPDATE = "Cập nhật thông số môi trường";
public static final String NOTIFICATION_TITLE_PERSON_IN_CHARGE_UPDATE = "Cập nhật người phụ trách";

public static final String NOTIFICATION_TITLE_REMINDER = "Thông báo";
public static final String NOTIFICATION_REQUEST_TYPE_REMINDER = "REMINDER";
public static final String NOTIFICATION_REQUEST_TYPE_ENV_UPDATE = "ENV_UPDATE";
public static final String NOTIFICATION_REQUEST_TYPE_PIC_UPDATE = "PIC_UPDATE"; //PIC-Person In Charge
public static final String NOTIFICATION_REQUEST_TYPE_DEVICE = "DEVICE_LOST_CONNECTION";

+ 58
- 0
src/main/java/vn/azteam/tpf/service/NotificationReminderService.java Переглянути файл

@@ -0,0 +1,58 @@
package vn.azteam.tpf.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vn.azteam.tpf.Constants.Constants;
import vn.azteam.tpf.service.dto.PushNotificationRequest;
import vn.azteam.tpf.service.dto.PushNotificationService;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class NotificationReminderService {

private final Logger log = LoggerFactory.getLogger(NotificationReminderService.class);

@Value("${app.is-test-send-notification}")
private Integer isTestSendNotification;
private final TBCropQueryService tbCropQueryService;

private final PushNotificationService pushNotificationService;

public NotificationReminderService(TBCropQueryService tbCropQueryService, PushNotificationService pushNotificationService) {
this.tbCropQueryService = tbCropQueryService;
this.pushNotificationService = pushNotificationService;
}

//At 00:10:00am every day
@Scheduled(cron = "0 10 0 * * ?")
@Transactional
public void sendNotificationReminder() {
log.info("----Start function sendNotificationReminder");
List<Object[]> data = tbCropQueryService.loadDataToNotification();

if (!data.isEmpty()) {
data.forEach(notification -> {
PushNotificationRequest request = new PushNotificationRequest(Constants.NOTIFICATION_TITLE_REMINDER
, String.valueOf(notification[3])
, String.valueOf(notification[5])
, Constants.NOTIFICATION_REQUEST_TYPE_REMINDER
, Long.parseLong(String.valueOf(notification[4]))
, null
, Long.parseLong(String.valueOf(notification[0]))
, Long.parseLong(String.valueOf(notification[2])));
if (isTestSendNotification == 0) {
pushNotificationService.sendPushNotificationToToken(request);
} else {
log.info("----Send notification----- : sendNotificationReminder");
}

});
}
}
}

+ 51
- 33
src/main/java/vn/azteam/tpf/service/TBCropQueryService.java Переглянути файл

@@ -1,17 +1,7 @@
package vn.azteam.tpf.service;

import java.math.BigInteger;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;

import com.google.api.client.util.Lists;
import io.github.jhipster.service.QueryService;
import io.github.jhipster.service.filter.IntegerFilter;
import io.github.jhipster.service.filter.LongFilter;
import org.slf4j.Logger;
@@ -23,12 +13,8 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.github.jhipster.service.QueryService;

import vn.azteam.tpf.Constants.Constants;
import vn.azteam.tpf.domain.TBCrop;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.domain.*;
import vn.azteam.tpf.repository.TBCropRepository;
import vn.azteam.tpf.repository.search.TBCropSearchRepository;
import vn.azteam.tpf.service.dto.*;
@@ -37,8 +23,21 @@ import vn.azteam.tpf.service.mapper.TBGuidelineActivityMapper;
import vn.azteam.tpf.service.mapper.TBGuidelineDetailsMapper;
import vn.azteam.tpf.service.util.UserRoleUtil;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

import static java.time.temporal.ChronoUnit.DAYS;
import static vn.azteam.tpf.web.rest.util.ConvertCharacterUtil.removeAccent;

/**
* Service for executing complex queries for TBCrop entities in the database.
* The main input is a {@link TBCropCriteria} which gets converted to {@link Specification},
@@ -164,7 +163,7 @@ public class TBCropQueryService extends QueryService<TBCrop> {

if (areaId != null) {
crops = crops.stream()
.filter(item ->item.getAreaId() == areaId)
.filter(item -> item.getAreaId() == areaId)
.collect(Collectors.toList());
}

@@ -405,7 +404,7 @@ public class TBCropQueryService extends QueryService<TBCrop> {
List<Long> listActivityTypeId = new ArrayList<>();
if (currentUser.get().getCustomerId() != null) {
TBCropDTO tBCropDTO = tBCropService.findOne(cropId).get();
listActivityTypeId = tBCropService.getListActivityTypeIdByByCustomerAndCropType(currentUser.get().getCustomerId(),tBCropDTO.getTbCropTypeId());
listActivityTypeId = tBCropService.getListActivityTypeIdByByCustomerAndCropType(currentUser.get().getCustomerId(), tBCropDTO.getTbCropTypeId());
}
for (TBGuidelineDetailsDTO guidelineDetailsDTO : guidelineDetails) {
GuidelineDetailWrapperDTO guidelineDetailWrapperDTO = new GuidelineDetailWrapperDTO();
@@ -432,8 +431,8 @@ public class TBCropQueryService extends QueryService<TBCrop> {
if (!guidelinePeriodDTOs.isEmpty()) {
activityTimelineDTO.setGuidelinePeriods(guidelinePeriodDTOs);
}
Comparator<TBActivityDTO> compareByAgeDay = Comparator.comparing( TBActivityDTO::getAgeDay );
Comparator<TBActivityDTO> compareByExecuteDate = Comparator.comparing( TBActivityDTO::getExecuteDate );
Comparator<TBActivityDTO> compareByAgeDay = Comparator.comparing(TBActivityDTO::getAgeDay);
Comparator<TBActivityDTO> compareByExecuteDate = Comparator.comparing(TBActivityDTO::getExecuteDate);
Comparator<TBActivityDTO> compareTimeline = compareByAgeDay.thenComparing(compareByExecuteDate);
List<TBActivityDTO> activities = allActivities.stream()
.sorted(compareTimeline)
@@ -471,21 +470,22 @@ public class TBCropQueryService extends QueryService<TBCrop> {
}
}

private List<TBGuidelineActivityDTO> findActivityTypesInGuidelineDetail(Long guidelineDetailsId, List<Long> listActivityTypeId){
private List<TBGuidelineActivityDTO> findActivityTypesInGuidelineDetail(Long guidelineDetailsId, List<Long> listActivityTypeId) {
String sqlString = "SELECT * \n" +
"FROM smf.tb_guideline_activity \n" +
"WHERE tb_guideline_details_id = :guidelineDetailsId ";
if(listActivityTypeId.size() > 0){
if (listActivityTypeId.size() > 0) {
sqlString += "AND tb_activity_type_id IN (:listActivityTypeId)";
}
Query query = em.createNativeQuery(sqlString, TBGuidelineActivity.class);
query.setParameter("guidelineDetailsId", guidelineDetailsId);
if(listActivityTypeId.size() > 0){
if (listActivityTypeId.size() > 0) {
query.setParameter("listActivityTypeId", listActivityTypeId);
}

return tbGuidelineActivityMapper.toDto(query.getResultList());
}

private List<TBGuidelineDetailsDTO> findGuidelineDetails(Long cropId) {
String sqlString =
" select guideline_details.* \n" +
@@ -499,6 +499,24 @@ public class TBCropQueryService extends QueryService<TBCrop> {

return tBGuidelineDetailsMapper.toDto(query.getResultList());
}

public List<Object[]> loadDataToNotification() {
String sqlString =
" select crop.id, crop.code, crop.tb_crop_type_id, activity_type.description, ta.tb_detail_users_id, tdu.fcm_token \n" +
"from tb_activity_type as activity_type \n" +
" inner join tb_activity as activity on activity.activity_type = activity_type.id \n" +
" inner join tb_crop as crop on crop.id = activity.tb_crop_id \n" +
" inner join tb_guideline as guideline on guideline.id = crop.tb_guideline_id \n" +
" inner join tb_guideline_details tgd on guideline.id = tgd.tb_guideline_period_id \n" +
" left join tb_guideline_period as guideline_period on guideline_period.id = tgd.tb_guideline_period_id \n" +
" left join tb_assignment ta on crop.id = ta.tb_crops_id \n" +
" left join tb_detail_user tdu on ta.tb_detail_users_id = tdu.id \n" +
"where ta.tb_detail_users_id is not null and tdu.fcm_token is not null and DATE(activity.execute_date) = CURDATE() \n" +
"group by crop.id, crop.code, crop.tb_crop_type_id, crop.start_date, crop.end_date, activity_type.description, activity.execute_date, ta.tb_detail_users_id, tdu.fcm_token \n";
Query nativeQuery = em.createNativeQuery(sqlString);
return (List<Object[]>) nativeQuery.getResultList();
}

private List<Object[]> getActivityGuidelineDetails(List<Long> guidelineDetailIds, String tableName) {
Query nativeQuery = em.createNativeQuery(
String.format(
@@ -528,7 +546,7 @@ public class TBCropQueryService extends QueryService<TBCrop> {
}

@Transactional
public void sendNotificationPersonInCharge(CropPersonInChargeDTO notificationPersonInChargeDTO,List<Long> listAssignmentPrev) {
public void sendNotificationPersonInCharge(CropPersonInChargeDTO notificationPersonInChargeDTO, List<Long> listAssignmentPrev) {
List<Long> listPersonRemovedInCharge = new ArrayList<Long>(listAssignmentPrev);
TBCropDTO tbCropDTO = tBCropService.findOne(notificationPersonInChargeDTO.getCropId()).get();

@@ -537,7 +555,7 @@ public class TBCropQueryService extends QueryService<TBCrop> {
String titleContentEnv = "";

for (Long personInCharge : notificationPersonInChargeDTO.getDetailUserIds()) {
if(listAssignmentPrev.contains(personInCharge)){
if (listAssignmentPrev.contains(personInCharge)) {
continue;
}
TBDetailUserDTO tbDetailUserDTO = tbDetailUserService.findOne(personInCharge).get();
@@ -560,9 +578,9 @@ public class TBCropQueryService extends QueryService<TBCrop> {
}
}

if(listPersonRemovedInCharge.size() > 0){
if (listPersonRemovedInCharge.size() > 0) {
String titleContent = "";
for (Long personRemovedInCharge: listPersonRemovedInCharge) {
for (Long personRemovedInCharge : listPersonRemovedInCharge) {
TBDetailUserDTO tbDetailUserDTO = tbDetailUserService.findOne(personRemovedInCharge).get();
titleContent = genTitleRemovedPersonInCharge(tbCropDTO);
if (tbDetailUserDTO.getFcmToken() != null && !tbDetailUserDTO.getFcmToken().isEmpty()) {
@@ -598,13 +616,13 @@ public class TBCropQueryService extends QueryService<TBCrop> {

public List<TBCropDTO> updateListToHasGuidelinePeriod(List<TBCropDTO> tbCropDTOs) {
List<TBCropDTO> newTBCrops = new ArrayList<>();
for(TBCropDTO tbCropDTO : tbCropDTOs) {
for (TBCropDTO tbCropDTO : tbCropDTOs) {

//Get current guideline period of tbCrop
TBGuidelinePeriodDTO tbGuidelinePeriodDTO = getTBGuidelinePeriodByCrop(tbCropDTO);

//Set current guideline period to tbCrop
if(tbGuidelinePeriodDTO != null) {
if (tbGuidelinePeriodDTO != null) {
tbCropDTO.setTbGuidelinePeriodId(tbGuidelinePeriodDTO.getId());
tbCropDTO.setTbGuidelinePeriodName(tbGuidelinePeriodDTO.getName());
}
@@ -616,16 +634,16 @@ public class TBCropQueryService extends QueryService<TBCrop> {
public TBGuidelinePeriodDTO getTBGuidelinePeriodByCrop(TBCropDTO tbCropDTO) {
Long ageDay = Math.abs(ChronoUnit.DAYS.between(Instant.now(), tbCropDTO.getStartDate()));
Integer ageDayStartAt = tbCropDTO.getAgeDayStartAt() != null ? tbCropDTO.getAgeDayStartAt() : 0;
Integer finalAgeDay = ((Long)(ageDayStartAt + ageDay)).intValue();
Integer finalAgeDay = ((Long) (ageDayStartAt + ageDay)).intValue();

TBGuidelinePeriodCriteria criteria = new TBGuidelinePeriodCriteria();
IntegerFilter ageDayFilter = new IntegerFilter();
ageDayFilter.equals(finalAgeDay);
criteria.setAgeDayStart(ageDayFilter);

List<TBGuidelinePeriodDTO> tbGuidelinePeriodDTOs= tbGuidelinePeriodQueryService.findByCriteria(criteria);
if(tbGuidelinePeriodDTOs != null && tbGuidelinePeriodDTOs.size() > 0) {
return tbGuidelinePeriodDTOs.get(0);
List<TBGuidelinePeriodDTO> tbGuidelinePeriodDTOs = tbGuidelinePeriodQueryService.findByCriteria(criteria);
if (tbGuidelinePeriodDTOs != null && tbGuidelinePeriodDTOs.size() > 0) {
return tbGuidelinePeriodDTOs.get(0);
}
return null;
}

Завантаження…
Відмінити
Зберегти