Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.dao.entity.user.AgendaRuleConfig;
import com.spice.profitmandi.dao.repository.dtr.AgendaRuleConfigRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;

@Service
@Transactional
public class AgendaConfigServiceImpl implements AgendaConfigService {

    private static final Gson GSON = new Gson();
    private static final Type PARAMS_TYPE = new TypeToken<Map<String, Double>>() {
    }.getType();

    @Autowired
    AgendaRuleConfigRepository agendaRuleConfigRepository;

    @Override
    public Map<String, Double> getParams(String agendaType, int fofoId) throws ProfitMandiBusinessException {
        AgendaRuleConfig defaultRow = agendaRuleConfigRepository.selectByTypeAndFofo(agendaType, 0);
        if (defaultRow == null) {
            throw new ProfitMandiBusinessException("agendaType", agendaType,
                    "No default agenda_rule_config row for " + agendaType);
        }
        AgendaRuleConfig overrideRow = fofoId != 0
                ? agendaRuleConfigRepository.selectByTypeAndFofo(agendaType, fofoId)
                : null;
        return mergeParams(defaultRow.getParams(), overrideRow != null ? overrideRow.getParams() : null);
    }

    // Package-visible + static so it is unit-testable without a Spring context.
    static Map<String, Double> mergeParams(String defaultJson, String overrideJson) {
        Map<String, Double> merged = new LinkedHashMap<>();
        Map<String, Double> defaults = GSON.fromJson(defaultJson, PARAMS_TYPE);
        if (defaults != null) merged.putAll(defaults);
        if (overrideJson != null && !overrideJson.trim().isEmpty()) {
            Map<String, Double> overrides = GSON.fromJson(overrideJson, PARAMS_TYPE);
            if (overrides != null) merged.putAll(overrides);
        }
        return merged;
    }

    @Override
    public void saveParams(String agendaType, int fofoId, Map<String, Double> params, String updatedBy)
            throws ProfitMandiBusinessException {
        AgendaRuleConfig row = agendaRuleConfigRepository.selectByTypeAndFofo(agendaType, fofoId);
        boolean emptyParams = params == null || params.isEmpty();
        if (emptyParams) {
            if (fofoId == 0) {
                throw new ProfitMandiBusinessException("agendaType", agendaType,
                        "Default config cannot be emptied");
            }
            if (row != null) agendaRuleConfigRepository.delete(row);
            return;
        }
        if (row == null) {
            row = new AgendaRuleConfig();
            row.setAgendaType(agendaType);
            row.setFofoId(fofoId);
        }
        row.setParams(GSON.toJson(params));
        row.setUpdatedBy(updatedBy);
        agendaRuleConfigRepository.persist(row);
    }
}