Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37167 amit 1
package com.spice.profitmandi.service;
2
 
3
import com.spice.profitmandi.dao.entity.user.AgendaInstance;
4
import com.spice.profitmandi.dao.enumuration.dtr.AgendaSource;
5
import com.spice.profitmandi.dao.enumuration.dtr.AgendaType;
6
import com.spice.profitmandi.dao.repository.dtr.AgendaInstanceRepository;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.stereotype.Service;
9
import org.springframework.transaction.annotation.Propagation;
10
import org.springframework.transaction.annotation.Transactional;
11
 
12
import java.util.Map;
13
 
14
/**
15
 * Per-store write step of the nightly agenda sync. Separate bean (not a method
16
 * on the orchestrator) so the REQUIRES_NEW proxy actually applies — one store's
17
 * failure rolls back only its own instance changes.
18
 *
19
 * Only AUTO instances are ever closed here; MANUAL instances belong to the
20
 * visiting team. OPEN on a type that already has any open instance (AUTO or
21
 * MANUAL) is a no-op via AgendaInstanceService.open.
22
 */
23
@Service
24
public class AgendaAutoRuleApplier {
25
 
26
    @Autowired
27
    AgendaInstanceRepository agendaInstanceRepository;
28
 
29
    @Autowired
30
    AgendaInstanceService agendaInstanceService;
31
 
32
    /** Returns {opened, closed} for this store. */
33
    @Transactional(propagation = Propagation.REQUIRES_NEW)
34
    public int[] applyForStore(int fofoId, Map<AgendaType, AgendaRuleDecider.Decision> decisions)
35
            throws Exception {
36
        int opened = 0;
37
        int closed = 0;
38
        for (Map.Entry<AgendaType, AgendaRuleDecider.Decision> e : decisions.entrySet()) {
39
            AgendaType type = e.getKey();
40
            AgendaRuleDecider.Decision d = e.getValue();
41
            if (d.action == AgendaRuleDecider.Action.LEAVE) continue;
42
 
43
            AgendaInstance existing = agendaInstanceRepository.selectOpenByFofoIdAndType(fofoId, type.name());
44
            if (d.action == AgendaRuleDecider.Action.OPEN) {
45
                if (existing == null) {
46
                    agendaInstanceService.open(fofoId, type.name(), AgendaSource.AUTO, null, d.metric);
47
                    opened++;
48
                }
49
            } else { // CLOSE
50
                if (existing != null && AgendaSource.AUTO.name().equals(existing.getSource())) {
51
                    agendaInstanceService.closeByMetric(existing, d.metric);
52
                    closed++;
53
                }
54
            }
55
        }
56
        return new int[]{opened, closed};
57
    }
58
}