|
|
|
@@ -1,7 +1,9 @@ |
|
|
|
package vn.azteam.tpf.service.impl; |
|
|
|
|
|
|
|
import io.github.jhipster.service.filter.LongFilter; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.data.domain.Pageable; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import vn.azteam.tpf.domain.TBCode; |
|
|
|
@@ -10,18 +12,22 @@ import vn.azteam.tpf.domain.TBCodeStatusEnum; |
|
|
|
import vn.azteam.tpf.repository.TBCodeDetailsRepository; |
|
|
|
import vn.azteam.tpf.repository.TBDetailUserRepository; |
|
|
|
import vn.azteam.tpf.repository.search.TBCodeDetailsSearchRepository; |
|
|
|
import vn.azteam.tpf.service.TBCodeDetailsService; |
|
|
|
import vn.azteam.tpf.service.dto.TBCodeDetailsCreationDTO; |
|
|
|
import vn.azteam.tpf.service.dto.TBCodeDetailsDTO; |
|
|
|
import vn.azteam.tpf.service.*; |
|
|
|
import vn.azteam.tpf.service.dto.*; |
|
|
|
import vn.azteam.tpf.service.mapper.TBCodeDetailsMapper; |
|
|
|
import vn.azteam.tpf.service.mapper.TBCropMapper; |
|
|
|
import vn.azteam.tpf.service.util.UserRoleUtil; |
|
|
|
import vn.azteam.tpf.web.rest.errors.BadRequestAlertException; |
|
|
|
|
|
|
|
import java.time.Instant; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Comparator; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import static vn.azteam.tpf.Constants.Constants.*; |
|
|
|
|
|
|
|
/** |
|
|
|
* Service Implementation for managing TBCodeDetails. |
|
|
|
*/ |
|
|
|
@@ -31,21 +37,50 @@ import java.util.stream.Collectors; |
|
|
|
public class TBCodeDetailsImpl implements TBCodeDetailsService { |
|
|
|
private final Logger log = LoggerFactory.getLogger(TBCodeDetailsImpl.class); |
|
|
|
|
|
|
|
private static final String ENTITY_NAME = "tBCrop"; |
|
|
|
|
|
|
|
private final TBCodeDetailsRepository tbCodeDetailsRepository; |
|
|
|
|
|
|
|
private final TBCodeDetailsMapper tBCodeDetailsMapper; |
|
|
|
|
|
|
|
private final TBDetailUserRepository tBDetailUserRepository; |
|
|
|
|
|
|
|
private final UserService userService; |
|
|
|
|
|
|
|
private final TBObjectUpdateService tbObjectUpdateService; |
|
|
|
|
|
|
|
private final UserRoleUtil userRoleUtil; |
|
|
|
|
|
|
|
private final TBActivityQueryService tBActivityQueryService; |
|
|
|
|
|
|
|
private final TBCropService tbCropService; |
|
|
|
|
|
|
|
private final TBCropQueryService tBCropQueryService; |
|
|
|
|
|
|
|
private final TBCropMapper tbCropMapper; |
|
|
|
|
|
|
|
private final TBCodeDetailsSearchRepository tBCodeDetailsSearchRepository; |
|
|
|
|
|
|
|
public TBCodeDetailsImpl(TBCodeDetailsRepository tbCodeDetailsRepository, |
|
|
|
TBCodeDetailsMapper tBCodeDetailsMapper, |
|
|
|
TBDetailUserRepository tBDetailUserRepository, |
|
|
|
UserService userService, |
|
|
|
TBObjectUpdateService tbObjectUpdateService, |
|
|
|
UserRoleUtil userRoleUtil, |
|
|
|
TBActivityQueryService tBActivityQueryService, |
|
|
|
TBCropService tbCropService, |
|
|
|
TBCropQueryService tBCropQueryService, TBCropMapper tbCropMapper, |
|
|
|
TBCodeDetailsSearchRepository tBCodeDetailsSearchRepository) { |
|
|
|
this.tbCodeDetailsRepository = tbCodeDetailsRepository; |
|
|
|
this.tBCodeDetailsMapper = tBCodeDetailsMapper; |
|
|
|
this.tBDetailUserRepository = tBDetailUserRepository; |
|
|
|
this.userService = userService; |
|
|
|
this.tbObjectUpdateService = tbObjectUpdateService; |
|
|
|
this.userRoleUtil = userRoleUtil; |
|
|
|
this.tBActivityQueryService = tBActivityQueryService; |
|
|
|
this.tbCropService = tbCropService; |
|
|
|
this.tBCropQueryService = tBCropQueryService; |
|
|
|
this.tbCropMapper = tbCropMapper; |
|
|
|
this.tBCodeDetailsSearchRepository = tBCodeDetailsSearchRepository; |
|
|
|
} |
|
|
|
|
|
|
|
@@ -74,7 +109,7 @@ public class TBCodeDetailsImpl implements TBCodeDetailsService { |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
public void updateNumberScan(String code) throws BadRequestAlertException { |
|
|
|
public TBCropDetailDTOV2 updateNumberScan(String code) throws BadRequestAlertException { |
|
|
|
Optional<TBCodeDetails> tbCodeDetailsOptional = tbCodeDetailsRepository.findByCode(code); |
|
|
|
if (tbCodeDetailsOptional.isPresent()) { |
|
|
|
TBCodeDetails tbCodeDetails = tbCodeDetailsOptional.get(); |
|
|
|
@@ -88,10 +123,120 @@ public class TBCodeDetailsImpl implements TBCodeDetailsService { |
|
|
|
tbCodeDetails.setNumberScan(numberScan.equals(0) ? 1 : numberScan + 1); |
|
|
|
tbCodeDetails.setLastUpdatedDate(Instant.now()); |
|
|
|
tbCodeDetailsRepository.save(tbCodeDetails); |
|
|
|
this.getCodeDetailDTOResponseEntity(tbCropMapper.toDto(tbCodeDetailsOptional.get().getTbCode().getTbCrop()), Pageable.unpaged(), true); |
|
|
|
log.debug("update number scan find by criteria code : {}", code); |
|
|
|
} else { |
|
|
|
throw new BadRequestAlertException("Scan code not found", "tb code details with code:", code); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
private TBCropDetailDTOV2 getCodeDetailDTOResponseEntity( |
|
|
|
TBCropDTO tBCropDTO, |
|
|
|
Pageable pageable, |
|
|
|
Boolean isRequestedFromWeb) { |
|
|
|
// Optional<TBCropDTO> tBCropDTO = tBCropService.findOne(id); |
|
|
|
if (!userRoleUtil.currentUserHasPermissionByCropId(tBCropDTO.getId())) { |
|
|
|
throw new BadRequestAlertException("1018", ENTITY_NAME, "1018"); |
|
|
|
} |
|
|
|
Optional<UserDTO> currentUser = userService.getCurrentUserDTO(); |
|
|
|
TBCropDetailDTOV2 tbCropDetailDTO = new TBCropDetailDTOV2(); |
|
|
|
tbCropDetailDTO.setTbCropDTO(tBCropDTO); |
|
|
|
// Get list of activity |
|
|
|
TBActivityCriteria criteria = new TBActivityCriteria(); |
|
|
|
LongFilter cropFilter = new LongFilter(); |
|
|
|
cropFilter.setEquals(tBCropDTO.getId()); |
|
|
|
criteria.setCropId(cropFilter); |
|
|
|
List<TBActivityDTO> allActivities = tBActivityQueryService.findByCriteria(criteria).stream() |
|
|
|
.sorted(Comparator.comparing( |
|
|
|
TBActivityDTO::getExecuteDate, |
|
|
|
Comparator.nullsLast(Comparator.naturalOrder())).reversed()) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
//remove env activities automatically |
|
|
|
allActivities |
|
|
|
.removeIf(activity -> ACTIVE_TYPE_UPDATE_ENV.equals(activity.getActivityTypeName()) |
|
|
|
&& activity.getCreatedById() == null); |
|
|
|
|
|
|
|
// Lo Uom |
|
|
|
if (tBCropDTO.getTbCropTypeId() == 1) { |
|
|
|
// Get sowing Date |
|
|
|
TBActivityDTO nurseryActivity = allActivities.stream() |
|
|
|
.sorted(Comparator.comparing( |
|
|
|
TBActivityDTO::getExecuteDate, |
|
|
|
Comparator.nullsLast(Comparator.naturalOrder()))) |
|
|
|
.filter(activity -> ACTIVE_TYPE_NURSERY.equals(activity.getActivityTypeName())) |
|
|
|
.findAny() |
|
|
|
.orElse(null); |
|
|
|
if (nurseryActivity != null) { |
|
|
|
tbCropDetailDTO.setSowingDate(nurseryActivity.getExecuteDate()); |
|
|
|
List<TBObjectUpdateDTO> tbObjectUpdateDTOs = tbObjectUpdateService.findByTBActivityId(nurseryActivity.getId(), |
|
|
|
Pageable.unpaged()); |
|
|
|
if (tbObjectUpdateDTOs != null && tbObjectUpdateDTOs.size() > 0) { |
|
|
|
tbObjectUpdateDTOs.forEach(item -> { |
|
|
|
if (item.getTbObjectParameterDTO().getName().equals(THOI_GIAN_NGAM_HAT)) { |
|
|
|
item.setIndex(item.getIndex() != null && !item.getIndex().isEmpty() ? item.getIndex() : "0"); |
|
|
|
tbCropDetailDTO.setSeedIncubationTime(Double.valueOf(item.getIndex())); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//Lo Trong |
|
|
|
if (tBCropDTO.getTbCropTypeId() == 2) { |
|
|
|
// Get planting Date |
|
|
|
TBActivityDTO plantingActivity = allActivities.stream() |
|
|
|
.sorted(Comparator.comparing( |
|
|
|
TBActivityDTO::getExecuteDate, |
|
|
|
Comparator.nullsLast(Comparator.naturalOrder()))) |
|
|
|
.filter(activity -> ACTIVE_TYPE_PLANTING.equals(activity.getActivityTypeName())) |
|
|
|
.findAny() |
|
|
|
.orElse(null); |
|
|
|
|
|
|
|
tbCropDetailDTO.setPlantingDate(plantingActivity != null ? plantingActivity.getExecuteDate() : null); |
|
|
|
List<Long> listActivities = allActivities.stream().map(TBActivityDTO::getId).collect(Collectors.toList()); |
|
|
|
List<TBObjectUpdateDTO> tbObjectUpdateDTOs = tbObjectUpdateService.findByInListTBActivityId(listActivities, Pageable.unpaged()); |
|
|
|
|
|
|
|
if (tbObjectUpdateDTOs != null && tbObjectUpdateDTOs.size() > 0) { |
|
|
|
tbObjectUpdateDTOs.forEach(item -> { |
|
|
|
if (item.getTbObjectParameterDTO().getName().equals(SO_LUONG_CAY_TRONG)) { |
|
|
|
item.setIndex(item.getIndex() != null && !item.getIndex().isEmpty() ? item.getIndex() : "0"); |
|
|
|
tbCropDetailDTO.setNumberPlants(Integer.parseInt(item.getIndex().replaceAll("[^0-9]", ""))); |
|
|
|
tbCropDetailDTO.setNumberCurrentPlants(Integer.parseInt(item.getIndex().replaceAll("[^0-9]", ""))); |
|
|
|
} |
|
|
|
if (item.getTbObjectParameterDTO().getName().equals(SO_LUONG_LOAI_BO)) { |
|
|
|
item.setIndex(item.getIndex() != null && !item.getIndex().isEmpty() ? item.getIndex() : "0"); |
|
|
|
Integer currentNumberPlants = tbCropDetailDTO.getNumberPlants() - Integer.parseInt(item.getIndex().replaceAll("[^0-9]", "")); |
|
|
|
tbCropDetailDTO.setNumberCurrentPlants(currentNumberPlants > 0 ? currentNumberPlants : 0); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//Filter assignment croptype by customer |
|
|
|
if (currentUser.get().getCustomerId() != null) { |
|
|
|
List<Long> ListActivityTypeId = tbCropService.getListActivityTypeIdByByCustomerAndCropType(currentUser.get().getCustomerId(), tBCropDTO.getTbCropTypeId()); |
|
|
|
allActivities = allActivities.stream().filter(item -> ListActivityTypeId.contains(item.getActivityTypeId())).collect(Collectors.toList()); |
|
|
|
} |
|
|
|
if (isRequestedFromWeb) { |
|
|
|
ActivityTimelineDTO activityTimeline = |
|
|
|
tBCropQueryService.getActivityTimeline(tBCropDTO.getId(), allActivities, pageable); |
|
|
|
tbCropDetailDTO.setActivityTimeline(activityTimeline); |
|
|
|
} else { |
|
|
|
//allActivities = pageableUtil.changeTBActivityDTOToPageFromList(allActivities, pageable).getContent(); |
|
|
|
tbCropDetailDTO.setActivities(allActivities); |
|
|
|
} |
|
|
|
|
|
|
|
TBActivityDTO endActivity = allActivities.stream() |
|
|
|
.sorted(Comparator.comparing( |
|
|
|
TBActivityDTO::getExecuteDate, |
|
|
|
Comparator.nullsLast(Comparator.naturalOrder())).reversed()) |
|
|
|
.filter(activity -> ACTIVE_TYPE_END.equals(activity.getActivityTypeName())) |
|
|
|
.findAny() |
|
|
|
.orElse(null); |
|
|
|
if (endActivity != null) { |
|
|
|
tbCropDetailDTO.setEndOfFarmingDate(endActivity.getExecuteDate()); |
|
|
|
} |
|
|
|
return tbCropDetailDTO; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |