|
|
|
|
|
|
|
|
package vn.azteam.tpf.service; |
|
|
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 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.IntegerFilter; |
|
|
import io.github.jhipster.service.filter.LongFilter; |
|
|
import io.github.jhipster.service.filter.LongFilter; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.Logger; |
|
|
|
|
|
|
|
|
import org.springframework.data.jpa.domain.Specification; |
|
|
import org.springframework.data.jpa.domain.Specification; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
import io.github.jhipster.service.QueryService; |
|
|
|
|
|
|
|
|
|
|
|
import vn.azteam.tpf.Constants.Constants; |
|
|
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.TBCropRepository; |
|
|
import vn.azteam.tpf.repository.search.TBCropSearchRepository; |
|
|
import vn.azteam.tpf.repository.search.TBCropSearchRepository; |
|
|
import vn.azteam.tpf.service.dto.*; |
|
|
import vn.azteam.tpf.service.dto.*; |
|
|
|
|
|
|
|
|
import vn.azteam.tpf.service.mapper.TBGuidelineDetailsMapper; |
|
|
import vn.azteam.tpf.service.mapper.TBGuidelineDetailsMapper; |
|
|
import vn.azteam.tpf.service.util.UserRoleUtil; |
|
|
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 java.time.temporal.ChronoUnit.DAYS; |
|
|
import static vn.azteam.tpf.web.rest.util.ConvertCharacterUtil.removeAccent; |
|
|
import static vn.azteam.tpf.web.rest.util.ConvertCharacterUtil.removeAccent; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Service for executing complex queries for TBCrop entities in the database. |
|
|
* Service for executing complex queries for TBCrop entities in the database. |
|
|
* The main input is a {@link TBCropCriteria} which gets converted to {@link Specification}, |
|
|
* The main input is a {@link TBCropCriteria} which gets converted to {@link Specification}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (areaId != null) { |
|
|
if (areaId != null) { |
|
|
crops = crops.stream() |
|
|
crops = crops.stream() |
|
|
.filter(item ->item.getAreaId() == areaId) |
|
|
|
|
|
|
|
|
.filter(item -> item.getAreaId() == areaId) |
|
|
.collect(Collectors.toList()); |
|
|
.collect(Collectors.toList()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Long> listActivityTypeId = new ArrayList<>(); |
|
|
List<Long> listActivityTypeId = new ArrayList<>(); |
|
|
if (currentUser.get().getCustomerId() != null) { |
|
|
if (currentUser.get().getCustomerId() != null) { |
|
|
TBCropDTO tBCropDTO = tBCropService.findOne(cropId).get(); |
|
|
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) { |
|
|
for (TBGuidelineDetailsDTO guidelineDetailsDTO : guidelineDetails) { |
|
|
GuidelineDetailWrapperDTO guidelineDetailWrapperDTO = new GuidelineDetailWrapperDTO(); |
|
|
GuidelineDetailWrapperDTO guidelineDetailWrapperDTO = new GuidelineDetailWrapperDTO(); |
|
|
|
|
|
|
|
|
if (!guidelinePeriodDTOs.isEmpty()) { |
|
|
if (!guidelinePeriodDTOs.isEmpty()) { |
|
|
activityTimelineDTO.setGuidelinePeriods(guidelinePeriodDTOs); |
|
|
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); |
|
|
Comparator<TBActivityDTO> compareTimeline = compareByAgeDay.thenComparing(compareByExecuteDate); |
|
|
List<TBActivityDTO> activities = allActivities.stream() |
|
|
List<TBActivityDTO> activities = allActivities.stream() |
|
|
.sorted(compareTimeline) |
|
|
.sorted(compareTimeline) |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private List<TBGuidelineActivityDTO> findActivityTypesInGuidelineDetail(Long guidelineDetailsId, List<Long> listActivityTypeId){ |
|
|
|
|
|
|
|
|
private List<TBGuidelineActivityDTO> findActivityTypesInGuidelineDetail(Long guidelineDetailsId, List<Long> listActivityTypeId) { |
|
|
String sqlString = "SELECT * \n" + |
|
|
String sqlString = "SELECT * \n" + |
|
|
"FROM smf.tb_guideline_activity \n" + |
|
|
"FROM smf.tb_guideline_activity \n" + |
|
|
"WHERE tb_guideline_details_id = :guidelineDetailsId "; |
|
|
"WHERE tb_guideline_details_id = :guidelineDetailsId "; |
|
|
if(listActivityTypeId.size() > 0){ |
|
|
|
|
|
|
|
|
if (listActivityTypeId.size() > 0) { |
|
|
sqlString += "AND tb_activity_type_id IN (:listActivityTypeId)"; |
|
|
sqlString += "AND tb_activity_type_id IN (:listActivityTypeId)"; |
|
|
} |
|
|
} |
|
|
Query query = em.createNativeQuery(sqlString, TBGuidelineActivity.class); |
|
|
Query query = em.createNativeQuery(sqlString, TBGuidelineActivity.class); |
|
|
query.setParameter("guidelineDetailsId", guidelineDetailsId); |
|
|
query.setParameter("guidelineDetailsId", guidelineDetailsId); |
|
|
if(listActivityTypeId.size() > 0){ |
|
|
|
|
|
|
|
|
if (listActivityTypeId.size() > 0) { |
|
|
query.setParameter("listActivityTypeId", listActivityTypeId); |
|
|
query.setParameter("listActivityTypeId", listActivityTypeId); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return tbGuidelineActivityMapper.toDto(query.getResultList()); |
|
|
return tbGuidelineActivityMapper.toDto(query.getResultList()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private List<TBGuidelineDetailsDTO> findGuidelineDetails(Long cropId) { |
|
|
private List<TBGuidelineDetailsDTO> findGuidelineDetails(Long cropId) { |
|
|
String sqlString = |
|
|
String sqlString = |
|
|
" select guideline_details.* \n" + |
|
|
" select guideline_details.* \n" + |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return tBGuidelineDetailsMapper.toDto(query.getResultList()); |
|
|
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) { |
|
|
private List<Object[]> getActivityGuidelineDetails(List<Long> guidelineDetailIds, String tableName) { |
|
|
Query nativeQuery = em.createNativeQuery( |
|
|
Query nativeQuery = em.createNativeQuery( |
|
|
String.format( |
|
|
String.format( |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Transactional |
|
|
@Transactional |
|
|
public void sendNotificationPersonInCharge(CropPersonInChargeDTO notificationPersonInChargeDTO,List<Long> listAssignmentPrev) { |
|
|
|
|
|
|
|
|
public void sendNotificationPersonInCharge(CropPersonInChargeDTO notificationPersonInChargeDTO, List<Long> listAssignmentPrev) { |
|
|
List<Long> listPersonRemovedInCharge = new ArrayList<Long>(listAssignmentPrev); |
|
|
List<Long> listPersonRemovedInCharge = new ArrayList<Long>(listAssignmentPrev); |
|
|
TBCropDTO tbCropDTO = tBCropService.findOne(notificationPersonInChargeDTO.getCropId()).get(); |
|
|
TBCropDTO tbCropDTO = tBCropService.findOne(notificationPersonInChargeDTO.getCropId()).get(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String titleContentEnv = ""; |
|
|
String titleContentEnv = ""; |
|
|
|
|
|
|
|
|
for (Long personInCharge : notificationPersonInChargeDTO.getDetailUserIds()) { |
|
|
for (Long personInCharge : notificationPersonInChargeDTO.getDetailUserIds()) { |
|
|
if(listAssignmentPrev.contains(personInCharge)){ |
|
|
|
|
|
|
|
|
if (listAssignmentPrev.contains(personInCharge)) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
TBDetailUserDTO tbDetailUserDTO = tbDetailUserService.findOne(personInCharge).get(); |
|
|
TBDetailUserDTO tbDetailUserDTO = tbDetailUserService.findOne(personInCharge).get(); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(listPersonRemovedInCharge.size() > 0){ |
|
|
|
|
|
|
|
|
if (listPersonRemovedInCharge.size() > 0) { |
|
|
String titleContent = ""; |
|
|
String titleContent = ""; |
|
|
for (Long personRemovedInCharge: listPersonRemovedInCharge) { |
|
|
|
|
|
|
|
|
for (Long personRemovedInCharge : listPersonRemovedInCharge) { |
|
|
TBDetailUserDTO tbDetailUserDTO = tbDetailUserService.findOne(personRemovedInCharge).get(); |
|
|
TBDetailUserDTO tbDetailUserDTO = tbDetailUserService.findOne(personRemovedInCharge).get(); |
|
|
titleContent = genTitleRemovedPersonInCharge(tbCropDTO); |
|
|
titleContent = genTitleRemovedPersonInCharge(tbCropDTO); |
|
|
if (tbDetailUserDTO.getFcmToken() != null && !tbDetailUserDTO.getFcmToken().isEmpty()) { |
|
|
if (tbDetailUserDTO.getFcmToken() != null && !tbDetailUserDTO.getFcmToken().isEmpty()) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<TBCropDTO> updateListToHasGuidelinePeriod(List<TBCropDTO> tbCropDTOs) { |
|
|
public List<TBCropDTO> updateListToHasGuidelinePeriod(List<TBCropDTO> tbCropDTOs) { |
|
|
List<TBCropDTO> newTBCrops = new ArrayList<>(); |
|
|
List<TBCropDTO> newTBCrops = new ArrayList<>(); |
|
|
for(TBCropDTO tbCropDTO : tbCropDTOs) { |
|
|
|
|
|
|
|
|
for (TBCropDTO tbCropDTO : tbCropDTOs) { |
|
|
|
|
|
|
|
|
//Get current guideline period of tbCrop |
|
|
//Get current guideline period of tbCrop |
|
|
TBGuidelinePeriodDTO tbGuidelinePeriodDTO = getTBGuidelinePeriodByCrop(tbCropDTO); |
|
|
TBGuidelinePeriodDTO tbGuidelinePeriodDTO = getTBGuidelinePeriodByCrop(tbCropDTO); |
|
|
|
|
|
|
|
|
//Set current guideline period to tbCrop |
|
|
//Set current guideline period to tbCrop |
|
|
if(tbGuidelinePeriodDTO != null) { |
|
|
|
|
|
|
|
|
if (tbGuidelinePeriodDTO != null) { |
|
|
tbCropDTO.setTbGuidelinePeriodId(tbGuidelinePeriodDTO.getId()); |
|
|
tbCropDTO.setTbGuidelinePeriodId(tbGuidelinePeriodDTO.getId()); |
|
|
tbCropDTO.setTbGuidelinePeriodName(tbGuidelinePeriodDTO.getName()); |
|
|
tbCropDTO.setTbGuidelinePeriodName(tbGuidelinePeriodDTO.getName()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public TBGuidelinePeriodDTO getTBGuidelinePeriodByCrop(TBCropDTO tbCropDTO) { |
|
|
public TBGuidelinePeriodDTO getTBGuidelinePeriodByCrop(TBCropDTO tbCropDTO) { |
|
|
Long ageDay = Math.abs(ChronoUnit.DAYS.between(Instant.now(), tbCropDTO.getStartDate())); |
|
|
Long ageDay = Math.abs(ChronoUnit.DAYS.between(Instant.now(), tbCropDTO.getStartDate())); |
|
|
Integer ageDayStartAt = tbCropDTO.getAgeDayStartAt() != null ? tbCropDTO.getAgeDayStartAt() : 0; |
|
|
Integer ageDayStartAt = tbCropDTO.getAgeDayStartAt() != null ? tbCropDTO.getAgeDayStartAt() : 0; |
|
|
Integer finalAgeDay = ((Long)(ageDayStartAt + ageDay)).intValue(); |
|
|
|
|
|
|
|
|
Integer finalAgeDay = ((Long) (ageDayStartAt + ageDay)).intValue(); |
|
|
|
|
|
|
|
|
TBGuidelinePeriodCriteria criteria = new TBGuidelinePeriodCriteria(); |
|
|
TBGuidelinePeriodCriteria criteria = new TBGuidelinePeriodCriteria(); |
|
|
IntegerFilter ageDayFilter = new IntegerFilter(); |
|
|
IntegerFilter ageDayFilter = new IntegerFilter(); |
|
|
ageDayFilter.equals(finalAgeDay); |
|
|
ageDayFilter.equals(finalAgeDay); |
|
|
criteria.setAgeDayStart(ageDayFilter); |
|
|
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; |
|
|
return null; |
|
|
} |
|
|
} |