Поправил обработку PlannerAllToday.
This commit is contained in:
psemenkov 2023-08-17 16:12:43 +03:00
parent fd22d89ecb
commit 1abbc5f734
4 changed files with 50 additions and 11 deletions

View file

@ -33,6 +33,7 @@ public class PlannerAllTodayBuilder {
public PlannerAllTodayBuilder append(PlannerTemplate plannerTemplate) {
this.task = plannerTemplate.getTask();
this.taskTime = plannerTemplate.getTaskTime();
this.clearingDate = LocalDate.now();
this.market = plannerTemplate.getMarket();
this.taskStatus = plannerTemplate.getTaskStatus();
this.section = plannerTemplate.getSection();

View file

@ -2,6 +2,8 @@ package ru.spcex.clearing.imdg.services;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.query.Predicate;
import com.hazelcast.query.Predicates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@ -52,23 +54,44 @@ public class PlannerAllTodayMaker {
listOfPlannerAllToday.removeIf((allToday) -> needRemoveByPlanner(planner, allToday));
}
});
for (PlannerAllToday plannerAllToday : listOfPlannerAllToday) {
plannerAllToday.setId(hazelcastInstance.getIdGenerator(MAP_SEQUENCE_NAME).newId());
plannerAllTodayMap.put(plannerAllToday.getId(), plannerAllToday);
listOfPlannerAllToday.stream()
.filter(plannerAllToday -> null == getFirstObjectByFieldValues(Map.of(
"parentId = %s", plannerAllToday.getParentId(),
"clearingDate = %s", plannerAllToday.getClearingDate())))
.forEach(plannerAllToday -> {
plannerAllToday.setId(hazelcastInstance.getIdGenerator(MAP_SEQUENCE_NAME).newId());
plannerAllTodayMap.put(plannerAllToday.getId(), plannerAllToday);
});
}
private PlannerAllToday getFirstObjectByFieldValues(Map<String, ? extends Comparable<?>> conditions) {
Predicate<Long, ?>[] predicates = new Predicate[conditions.size()];
final int[] i = {0};
conditions.forEach((key, value) -> {
predicates[i[0]] = Predicates.equal(key, value);
i[0]++;
});
Predicate<Long, PlannerAllToday> and = Predicates.and(predicates);
Set<Map.Entry<Long, PlannerAllToday>> found = plannerAllTodayMap.entrySet(and);
Iterator<Map.Entry<Long, PlannerAllToday>> allFoundByCondition = found.iterator();
if (allFoundByCondition.hasNext()) {
return allFoundByCondition.next().getValue();
} else {
return null;
}
}
private List<PlannerAllToday> getPlannersAllTodayToDate(LocalDate currentDate) {
List<PlannerAllToday> res = new ArrayList<>();
if (needAddFromPlannerTemplate(currentDate, clearingCalendarMap.values())){
if (needAddFromPlannerTemplate(currentDate, clearingCalendarMap.values())) {
plannerTemplateMap.values().stream().filter(plannerTemplate -> Status.Active.equalsByKey(plannerTemplate.getTaskStatus()))
.forEach(plannerTemplate -> res.add(PlannerAllTodayBuilder.builder().append(plannerTemplate).build()));
}
return res;
}
public boolean needAddFromPlannerTemplate(LocalDate currentDate, Collection<ClearingCalendar> clearingCalendar){
public boolean needAddFromPlannerTemplate(LocalDate currentDate, Collection<ClearingCalendar> clearingCalendar) {
boolean isWeekend = Arrays.asList(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY).contains(currentDate.getDayOfWeek());
if (clearingCalendar == null || clearingCalendar.isEmpty()) {

View file

@ -29,6 +29,7 @@ import ru.spcex.platform.utils.validation.IValidator;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;
import static ru.spcex.clearing.scheduler.config.PlannerQueueConfig.addToPlannerQueue;
@ -119,6 +120,11 @@ public class PlannerService extends QueueConsumer implements InitializingBean {
if (requestInfoUpdate != null) return requestInfoUpdate;
Planner planner = plannerMap.getSingleObjectByID(req.getId());
LocalDate currentDate = LocalDate.now();
if (planner.getClearingDate() != null && currentDate.equals(planner.getClearingDate())
&& !currentDate.equals(req.getClearingDate()) && planner.getTaskStatus() != null) {
cudPlannerAllToday(planner);
}
planner.setUpdated(Instant.now());
planner.setTask(req.getTask());
planner.setTaskTime(req.getTaskTime());
@ -130,7 +136,6 @@ public class PlannerService extends QueueConsumer implements InitializingBean {
planner.setCompanyId(req.getCompanyId());
planner.setSecurityId(req.getSecurityId());
plannerMap.update(planner);
LocalDate currentDate = LocalDate.now();
if (req.getClearingDate() != null && currentDate.equals(req.getClearingDate()) && planner.getTaskStatus() != null) {
cudPlannerAllToday(planner);
}
@ -148,7 +153,9 @@ public class PlannerService extends QueueConsumer implements InitializingBean {
Planner planner = plannerMap.getSingleObjectByID(req.getId());
plannerMap.delete(planner);
Collection<PlannerAllToday> values = plannerAllTodayMap.getCollectionObjectsBySQL(String.format("parentId = %s", planner.getId()));
Collection<PlannerAllToday> values = plannerAllTodayMap.getCollectionObjectsByFieldValues(Map.of(
"parentId = %s", planner.getId(),
"clearingDate = %s", planner.getClearingDate() != null ? planner.getClearingDate() : LocalDate.now()));
values.forEach(p -> {
plannerAllTodayMap.delete(p);
addToPlannerQueue(TaskManager.Process.delete, p);
@ -160,7 +167,9 @@ public class PlannerService extends QueueConsumer implements InitializingBean {
public void cudPlannerAllToday(Planner planner) {
if (planner.getTaskStatus().equalsIgnoreCase(Status.Active.getKey())) {
PlannerAllToday plannerAllToday = PlannerAllTodayBuilder.builder().append(planner).build();
PlannerAllToday oldPlannerAllToday = plannerAllTodayMap.getFirstObjectBySQL(String.format("parentId = %s", planner.getId()));
PlannerAllToday oldPlannerAllToday = plannerAllTodayMap.getFirstObjectByFieldValues(Map.of(
"parentId = %s", planner.getId(),
"clearingDate = %s", planner.getClearingDate()));
if (oldPlannerAllToday != null) {
plannerAllToday.setId(oldPlannerAllToday.getId());
plannerAllTodayMap.insert(plannerAllToday);
@ -173,7 +182,9 @@ public class PlannerService extends QueueConsumer implements InitializingBean {
}
if (planner.getTaskStatus().equalsIgnoreCase(Status.Cancel.getKey()) || planner.getTaskStatus().equalsIgnoreCase(Status.Blocked.getKey())) {
Collection<PlannerAllToday> values = plannerAllTodayMap.getCollectionObjectsBySQL(String.format("parentId = %s", planner.getId()));
Collection<PlannerAllToday> values = plannerAllTodayMap.getCollectionObjectsByFieldValues(Map.of(
"parentId = %s", planner.getId(),
"clearingDate = %s", planner.getClearingDate()));
values.forEach(plannerAllToday -> {
plannerAllTodayMap.delete(plannerAllToday);
addToPlannerQueue(TaskManager.Process.delete, plannerAllToday);

View file

@ -138,7 +138,9 @@ public class PlannerTemplateService extends QueueConsumer implements Initializin
if (todayWorkDay()) {
PlannerAllToday plannerAllToday = PlannerAllTodayBuilder.builder().append(plannerTemplate).build();
PlannerAllToday oldPlannerAllToday = plannerAllTodayMap.getFirstObjectBySQL(String.format("parentId = %s", plannerTemplate.getId()));
PlannerAllToday oldPlannerAllToday = plannerAllTodayMap.getFirstObjectByFieldValues(Map.of(
"parentId = %s", plannerTemplate.getId(),
"clearingDate = %s", LocalDate.now()));
if (oldPlannerAllToday != null) {
plannerAllToday.setId(oldPlannerAllToday.getId());
plannerAllTodayMap.insert(plannerAllToday);
@ -164,7 +166,9 @@ public class PlannerTemplateService extends QueueConsumer implements Initializin
PlannerTemplate plannerTemplate = plannerTemplateMap.getSingleObjectByID(req.getId());
if (todayWorkDay()) {
PlannerAllToday plannerAllToday = plannerAllTodayMap.getFirstObjectBySQL(String.format("parentId = %s", plannerTemplate.getId()));
PlannerAllToday plannerAllToday = plannerAllTodayMap.getFirstObjectByFieldValues(Map.of(
"parentId = %s", plannerTemplate.getId(),
"clearingDate = %s", LocalDate.now()));
if (plannerAllToday != null) {
plannerAllTodayMap.delete(plannerAllToday);
addToPlannerQueue(TaskManager.Process.delete, plannerAllToday);