| 37166 |
amit |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
4 |
import com.spice.profitmandi.dao.entity.user.AgendaInstance;
|
|
|
5 |
import com.spice.profitmandi.dao.entity.user.AgendaVisitLog;
|
|
|
6 |
import com.spice.profitmandi.dao.enumuration.dtr.AgendaSource;
|
|
|
7 |
import com.spice.profitmandi.dao.enumuration.dtr.AgendaStatus;
|
|
|
8 |
import com.spice.profitmandi.dao.repository.dtr.AgendaInstanceRepository;
|
|
|
9 |
import com.spice.profitmandi.dao.repository.dtr.AgendaVisitLogRepository;
|
|
|
10 |
import org.apache.logging.log4j.LogManager;
|
|
|
11 |
import org.apache.logging.log4j.Logger;
|
|
|
12 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
13 |
import org.springframework.stereotype.Service;
|
|
|
14 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
15 |
|
|
|
16 |
import java.time.LocalDate;
|
|
|
17 |
import java.time.LocalDateTime;
|
|
|
18 |
|
|
|
19 |
@Service
|
|
|
20 |
@Transactional
|
|
|
21 |
public class AgendaInstanceServiceImpl implements AgendaInstanceService {
|
|
|
22 |
|
|
|
23 |
private static final Logger LOGGER = LogManager.getLogger(AgendaInstanceServiceImpl.class);
|
|
|
24 |
|
|
|
25 |
@Autowired
|
|
|
26 |
AgendaInstanceRepository agendaInstanceRepository;
|
|
|
27 |
|
|
|
28 |
@Autowired
|
|
|
29 |
AgendaVisitLogRepository agendaVisitLogRepository;
|
|
|
30 |
|
|
|
31 |
@Override
|
|
|
32 |
public AgendaInstance open(int fofoId, String agendaType, AgendaSource source, Integer openedBy,
|
|
|
33 |
String openMetricJson) throws ProfitMandiBusinessException {
|
|
|
34 |
AgendaInstance existing = agendaInstanceRepository.selectOpenByFofoIdAndType(fofoId, agendaType);
|
|
|
35 |
if (existing != null) return existing;
|
|
|
36 |
|
|
|
37 |
AgendaInstance instance = new AgendaInstance();
|
|
|
38 |
instance.setFofoId(fofoId);
|
|
|
39 |
instance.setAgendaType(agendaType);
|
|
|
40 |
instance.setSource(source.name());
|
|
|
41 |
instance.setStatus(AgendaStatus.OPEN.name());
|
|
|
42 |
instance.setOpenedOn(LocalDateTime.now());
|
|
|
43 |
instance.setOpenedBy(openedBy);
|
|
|
44 |
instance.setOpenMetric(openMetricJson);
|
|
|
45 |
try {
|
|
|
46 |
agendaInstanceRepository.persist(instance);
|
|
|
47 |
return instance;
|
|
|
48 |
} catch (Exception e) {
|
|
|
49 |
// uq_one_open race: someone opened it between select and insert.
|
|
|
50 |
AgendaInstance winner = agendaInstanceRepository.selectOpenByFofoIdAndType(fofoId, agendaType);
|
|
|
51 |
if (winner != null) {
|
|
|
52 |
LOGGER.info("Agenda open race for fofoId={} type={} — using existing instance {}",
|
|
|
53 |
fofoId, agendaType, winner.getId());
|
|
|
54 |
return winner;
|
|
|
55 |
}
|
|
|
56 |
throw new ProfitMandiBusinessException("agendaType", agendaType,
|
|
|
57 |
"Could not open agenda instance for fofoId " + fofoId + ": " + e.getMessage());
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
@Override
|
|
|
62 |
public void closeByMetric(AgendaInstance instance, String closeMetricJson) {
|
|
|
63 |
instance.setStatus(AgendaStatus.CLOSED.name());
|
|
|
64 |
instance.setClosedOn(LocalDateTime.now());
|
|
|
65 |
instance.setClosedBy(null);
|
|
|
66 |
instance.setCloseMetric(closeMetricJson);
|
|
|
67 |
agendaInstanceRepository.persist(instance);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
@Override
|
|
|
71 |
public void closeManual(AgendaInstance instance, int closedBy, String remark)
|
|
|
72 |
throws ProfitMandiBusinessException {
|
|
|
73 |
if (AgendaSource.AUTO.name().equals(instance.getSource())) {
|
|
|
74 |
throw new ProfitMandiBusinessException("instanceId", instance.getId(),
|
|
|
75 |
"Auto agendas close only when the metric normalizes");
|
|
|
76 |
}
|
|
|
77 |
if (!AgendaStatus.OPEN.name().equals(instance.getStatus())) {
|
|
|
78 |
throw new ProfitMandiBusinessException("instanceId", instance.getId(),
|
|
|
79 |
"Agenda instance is already closed");
|
|
|
80 |
}
|
|
|
81 |
instance.setStatus(AgendaStatus.CLOSED.name());
|
|
|
82 |
instance.setClosedOn(LocalDateTime.now());
|
|
|
83 |
instance.setClosedBy(closedBy);
|
|
|
84 |
instance.setCloseRemark(remark);
|
|
|
85 |
agendaInstanceRepository.persist(instance);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
@Override
|
|
|
89 |
public void logVisit(int instanceId, Integer locationTrackingId, int visitorUserId,
|
|
|
90 |
LocalDate visitDate, String discussion) throws ProfitMandiBusinessException {
|
|
|
91 |
AgendaInstance instance = agendaInstanceRepository.selectById(instanceId);
|
|
|
92 |
if (instance == null) {
|
|
|
93 |
throw new ProfitMandiBusinessException("instanceId", instanceId, "Agenda instance not found");
|
|
|
94 |
}
|
|
|
95 |
if (!AgendaStatus.OPEN.name().equals(instance.getStatus())) {
|
|
|
96 |
throw new ProfitMandiBusinessException("instanceId", instanceId,
|
|
|
97 |
"Agenda instance is closed — discussions attach to open agendas only");
|
|
|
98 |
}
|
|
|
99 |
AgendaVisitLog log = new AgendaVisitLog();
|
|
|
100 |
log.setAgendaInstanceId(instanceId);
|
|
|
101 |
log.setLocationTrackingId(locationTrackingId);
|
|
|
102 |
log.setVisitorUserId(visitorUserId);
|
|
|
103 |
log.setVisitDate(visitDate != null ? visitDate : LocalDate.now());
|
|
|
104 |
log.setDiscussion(discussion);
|
|
|
105 |
agendaVisitLogRepository.persist(log);
|
|
|
106 |
}
|
|
|
107 |
}
|