| 37165 |
amit |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
|
|
3 |
import com.google.gson.Gson;
|
|
|
4 |
import com.google.gson.reflect.TypeToken;
|
|
|
5 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.user.AgendaRuleConfig;
|
|
|
7 |
import com.spice.profitmandi.dao.repository.dtr.AgendaRuleConfigRepository;
|
|
|
8 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
9 |
import org.springframework.stereotype.Service;
|
|
|
10 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
11 |
|
|
|
12 |
import java.lang.reflect.Type;
|
|
|
13 |
import java.util.LinkedHashMap;
|
|
|
14 |
import java.util.Map;
|
|
|
15 |
|
|
|
16 |
@Service
|
|
|
17 |
@Transactional
|
|
|
18 |
public class AgendaConfigServiceImpl implements AgendaConfigService {
|
|
|
19 |
|
|
|
20 |
private static final Gson GSON = new Gson();
|
|
|
21 |
private static final Type PARAMS_TYPE = new TypeToken<Map<String, Double>>() {
|
|
|
22 |
}.getType();
|
|
|
23 |
|
|
|
24 |
@Autowired
|
|
|
25 |
AgendaRuleConfigRepository agendaRuleConfigRepository;
|
|
|
26 |
|
|
|
27 |
@Override
|
|
|
28 |
public Map<String, Double> getParams(String agendaType, int fofoId) throws ProfitMandiBusinessException {
|
|
|
29 |
AgendaRuleConfig defaultRow = agendaRuleConfigRepository.selectByTypeAndFofo(agendaType, 0);
|
|
|
30 |
if (defaultRow == null) {
|
|
|
31 |
throw new ProfitMandiBusinessException("agendaType", agendaType,
|
|
|
32 |
"No default agenda_rule_config row for " + agendaType);
|
|
|
33 |
}
|
|
|
34 |
AgendaRuleConfig overrideRow = fofoId != 0
|
|
|
35 |
? agendaRuleConfigRepository.selectByTypeAndFofo(agendaType, fofoId)
|
|
|
36 |
: null;
|
|
|
37 |
return mergeParams(defaultRow.getParams(), overrideRow != null ? overrideRow.getParams() : null);
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
// Package-visible + static so it is unit-testable without a Spring context.
|
|
|
41 |
static Map<String, Double> mergeParams(String defaultJson, String overrideJson) {
|
|
|
42 |
Map<String, Double> merged = new LinkedHashMap<>();
|
|
|
43 |
Map<String, Double> defaults = GSON.fromJson(defaultJson, PARAMS_TYPE);
|
|
|
44 |
if (defaults != null) merged.putAll(defaults);
|
|
|
45 |
if (overrideJson != null && !overrideJson.trim().isEmpty()) {
|
|
|
46 |
Map<String, Double> overrides = GSON.fromJson(overrideJson, PARAMS_TYPE);
|
|
|
47 |
if (overrides != null) merged.putAll(overrides);
|
|
|
48 |
}
|
|
|
49 |
return merged;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
@Override
|
|
|
53 |
public void saveParams(String agendaType, int fofoId, Map<String, Double> params, String updatedBy)
|
|
|
54 |
throws ProfitMandiBusinessException {
|
|
|
55 |
AgendaRuleConfig row = agendaRuleConfigRepository.selectByTypeAndFofo(agendaType, fofoId);
|
|
|
56 |
boolean emptyParams = params == null || params.isEmpty();
|
|
|
57 |
if (emptyParams) {
|
|
|
58 |
if (fofoId == 0) {
|
|
|
59 |
throw new ProfitMandiBusinessException("agendaType", agendaType,
|
|
|
60 |
"Default config cannot be emptied");
|
|
|
61 |
}
|
|
|
62 |
if (row != null) agendaRuleConfigRepository.delete(row);
|
|
|
63 |
return;
|
|
|
64 |
}
|
|
|
65 |
if (row == null) {
|
|
|
66 |
row = new AgendaRuleConfig();
|
|
|
67 |
row.setAgendaType(agendaType);
|
|
|
68 |
row.setFofoId(fofoId);
|
|
|
69 |
}
|
|
|
70 |
row.setParams(GSON.toJson(params));
|
|
|
71 |
row.setUpdatedBy(updatedBy);
|
|
|
72 |
agendaRuleConfigRepository.persist(row);
|
|
|
73 |
}
|
|
|
74 |
}
|