Files
bbps-backend/src/main/java/com/billpaymentservice/bbps/util/BillerMapperHelper.java
T
2025-11-12 17:03:07 +05:30

538 lines
22 KiB
Java

package com.billpaymentservice.bbps.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.billpaymentservice.bbps.dto.AmountOptionDTO;
import com.billpaymentservice.bbps.dto.BillerIconDTO;
import com.billpaymentservice.bbps.dto.ComplainDetailsDto;
import com.billpaymentservice.bbps.dto.ComplaintHistoryDTO;
import com.billpaymentservice.bbps.dto.CustomerParamGroupsDTO;
import com.billpaymentservice.bbps.dto.GroupDTO;
import com.billpaymentservice.bbps.dto.MasterCategoryBillerResponse;
import com.billpaymentservice.bbps.dto.PlanDTO;
import com.billpaymentservice.bbps.dto.response.AmountOptionsResponse;
import com.billpaymentservice.bbps.dto.response.ComplaintHistoryStatusResponse;
import com.billpaymentservice.bbps.dto.response.ComplaintStatusResponse;
import com.billpaymentservice.bbps.dto.response.PlanResponse;
import com.billpaymentservice.bbps.entites.AdditionalInfoPlans;
import com.billpaymentservice.bbps.entites.AmountOptionByBillerId;
import com.billpaymentservice.bbps.entites.AmountOptionDetails;
import com.billpaymentservice.bbps.entites.BillerCategoryIcon;
import com.billpaymentservice.bbps.entites.BillerCategoryWise;
import com.billpaymentservice.bbps.entites.CategoryResponseList;
import com.billpaymentservice.bbps.entites.ComplainStatus;
import com.billpaymentservice.bbps.entites.ComplaintHistory;
import com.billpaymentservice.bbps.entites.CustomerParamGroupById;
import com.billpaymentservice.bbps.entites.GroupParam;
import com.billpaymentservice.bbps.entites.Plan;
import com.billpaymentservice.bbps.entites.PlanCategorySubType;
import com.billpaymentservice.bbps.entites.PlanResponses;
import com.billpaymentservice.bbps.entites.PlansList;
import com.billpaymentservice.bbps.entites.ReOpenComplain;
import com.billpaymentservice.bbps.repository.AmountOptionByBillerIdRepository;
import com.billpaymentservice.bbps.repository.BillerCategoryWiseRepository;
import com.billpaymentservice.bbps.repository.CategoryResponseListRepository;
import com.billpaymentservice.bbps.repository.ComplaintHistoryRepository;
import com.billpaymentservice.bbps.repository.ComplaintStatusRepository;
import com.billpaymentservice.bbps.repository.CustomerParamGroupByBillerIdRepository;
import com.billpaymentservice.bbps.repository.FetchBillValidateRepository;
import com.billpaymentservice.bbps.repository.PlanResponseRepository;
import com.billpaymentservice.bbps.repository.ReOpenComplaintRepository;
import com.billpaymentservice.bbps.serviceImpl.AgentServiceImpl;
@Component
public class BillerMapperHelper {
private static final Logger LOGGER = LoggerFactory.getLogger(BillerMapperHelper.class);
private final BillerCategoryWiseRepository billerRepo;
private final CustomerParamGroupByBillerIdRepository customerParamGroupByBillerIdRepo;
private final AmountOptionByBillerIdRepository amountOptionByBillerIdRepository;
private final PlanResponseRepository planResponseRepository;
private final CategoryResponseListRepository categoryResponseListRepository;
private final FetchBillValidateRepository fetchBillValidateRepository;
private final ComplaintStatusRepository complaintStatusRepository;
private final ReOpenComplaintRepository reOpenComplaintRepository;
private final ComplaintHistoryRepository complaintHistoryRepository;
public BillerMapperHelper(BillerCategoryWiseRepository billerRepo,
CustomerParamGroupByBillerIdRepository customerParamGroupByBillerIdRepo,
AmountOptionByBillerIdRepository amountOptionByBillerIdRepository,
PlanResponseRepository planResponseRepository,
CategoryResponseListRepository categoryResponseListRepository,
FetchBillValidateRepository fetchBillValidateRepository,
ComplaintStatusRepository complaintStatusRepository, ReOpenComplaintRepository reOpenComplaintRepository,
ComplaintHistoryRepository complaintHistoryRepository) {
this.billerRepo = billerRepo;
this.customerParamGroupByBillerIdRepo = customerParamGroupByBillerIdRepo;
this.amountOptionByBillerIdRepository = amountOptionByBillerIdRepository;
this.planResponseRepository = planResponseRepository;
this.categoryResponseListRepository = categoryResponseListRepository;
this.fetchBillValidateRepository = fetchBillValidateRepository;
this.complaintStatusRepository = complaintStatusRepository;
this.reOpenComplaintRepository = reOpenComplaintRepository;
this.complaintHistoryRepository = complaintHistoryRepository;
}
/**
* Maps a list of MasterCategoryBillerResponse DTOs to entities, saves them, and
* returns the saved list
*/
public List<BillerCategoryWise> mapAndSaveAll(List<MasterCategoryBillerResponse> dtos, String requestId,
String txnId) {
if (dtos == null || dtos.isEmpty()) {
return Collections.emptyList();
}
List<BillerCategoryWise> entities = new ArrayList<>();
for (MasterCategoryBillerResponse dto : dtos) {
// Check if biller already exists
BillerCategoryWise biller = billerRepo.findById(dto.getBillerId()).orElse(new BillerCategoryWise()); // create
// Set/update fields
biller.setBillerId(dto.getBillerId());
biller.setBillerName(dto.getBillerName());
biller.setBillerType(dto.getBillerType());
biller.setBillerCategory(dto.getBillerCategory());
biller.setBillerCoverage(dto.getBillerCoverage());
biller.setBillerResponseType(dto.getBillerResponseType());
biller.setBillerDescription(dto.getBillerDescription());
biller.setPlanMDMRequirement(dto.getPlanMDMRequirement());
biller.setAdhocBiller(dto.getAdhocBiller());
biller.setPaymentAmountExactness(dto.getPaymentAmountExactness());
biller.setCreateDateTime(new Date());
biller.setRequestId(requestId);
biller.setTxnId(txnId);
// Handle icon
BillerIconDTO iconDTO = dto.getBiller_icon();
if (iconDTO != null) {
BillerCategoryIcon icon;
if (biller.getIcon() != null) {
icon = biller.getIcon(); // update existing icon
} else {
icon = new BillerCategoryIcon();
icon.setBiller(biller);
}
icon.setIconData(iconDTO.getIcon_data());
icon.setIconType(iconDTO.getIcon_type());
biller.setIcon(icon); // set to biller
}
entities.add(biller);
}
// Save all at once
return billerRepo.saveAll(entities);
}
public CustomerParamGroupsDTO saveCustomerParamGroups(CustomerParamGroupsDTO dtos, String requestId, String txnId) {
if (dtos == null || dtos.getGroups() == null || dtos.getGroups().isEmpty()) {
return new CustomerParamGroupsDTO(); // Return empty DTO if no groups exist
}
List<CustomerParamGroupById> entities = new ArrayList<>();
// Loop through the top-level groups and save them (including nested groups)
for (GroupDTO groupDTO : dtos.getGroups()) {
CustomerParamGroupById entity = saveGroupWithChildren(groupDTO, null, requestId, txnId); // null is for root
// (no
// parent group)
entities.add(entity); // Add the saved entity to the list
}
// Save all entities at once using the repository
List<CustomerParamGroupById> savedEntities = customerParamGroupByBillerIdRepo.saveAll(entities);
// Convert saved entities back into DTOs to return
return convertEntitiesToDTO(savedEntities);
}
private CustomerParamGroupById saveGroupWithChildren(GroupDTO groupDTO, CustomerParamGroupById parentGroup,
String requestId, String txnId) {
// Create a new entity for the group
CustomerParamGroupById groupEntity = new CustomerParamGroupById();
groupEntity.setGroupId(groupDTO.getGroupId());
groupEntity.setName(groupDTO.getName());
groupEntity.setInput(groupDTO.getInput());
groupEntity.setParentGroup(parentGroup); // Link to the parent group (if exists)
groupEntity.setRequestId(requestId);
groupEntity.setTxnId(txnId);
groupEntity.setCreateDateTime(new Date());
// Save group parameters (if any)
if (groupDTO.getParams() != null && !groupDTO.getParams().isEmpty()) {
List<GroupParam> groupParams = new ArrayList<>();
for (String paramName : groupDTO.getParams()) {
GroupParam param = new GroupParam();
param.setParamName(paramName); // Set the parameter name (e.g., "Admission No")
param.setGroup(groupEntity); // Link the parameter to the current group
groupParams.add(param);
}
groupEntity.setParams(groupParams); // Link parameters to the group
}
// Recursively save child groups if they exist
if (groupDTO.getGroups() != null && !groupDTO.getGroups().isEmpty()) {
List<CustomerParamGroupById> childEntities = new ArrayList<>();
for (GroupDTO childGroupDTO : groupDTO.getGroups()) {
CustomerParamGroupById childEntity = saveGroupWithChildren(childGroupDTO, groupEntity, requestId,
txnId); // Pass
// current
// group
// as
// parent
childEntities.add(childEntity);
}
groupEntity.setChildGroups(childEntities); // Set the child groups
}
// Save the current group entity and return it
return groupEntity;
}
private CustomerParamGroupsDTO convertEntitiesToDTO(List<CustomerParamGroupById> savedEntities) {
if (savedEntities == null || savedEntities.isEmpty()) {
// Return an empty or default DTO if no entities are found
return new CustomerParamGroupsDTO(); // return an empty DTO object
}
CustomerParamGroupsDTO responseDTO = new CustomerParamGroupsDTO();
List<GroupDTO> savedGroups = new ArrayList<>();
// Convert each saved entity to DTO
for (CustomerParamGroupById savedEntity : savedEntities) {
GroupDTO groupDTO = new GroupDTO();
groupDTO.setGroupId(savedEntity.getGroupId());
groupDTO.setName(savedEntity.getName());
groupDTO.setInput(savedEntity.getInput());
// Recursively map child groups if they exist
if (savedEntity.getChildGroups() != null) {
List<GroupDTO> childGroupDTOs = new ArrayList<>();
for (CustomerParamGroupById childEntity : savedEntity.getChildGroups()) {
GroupDTO childGroupDTO = new GroupDTO();
childGroupDTO.setGroupId(childEntity.getGroupId());
childGroupDTO.setName(childEntity.getName());
childGroupDTO.setInput(childEntity.getInput());
// childGroupDTO.setParams(childEntity.getParams()); // Set parameters of child
// group
childGroupDTOs.add(childGroupDTO);
}
groupDTO.setGroups(childGroupDTOs);
}
// Add the DTO to the response
savedGroups.add(groupDTO);
}
responseDTO.setGroups(savedGroups);
return responseDTO;
}
// Method to save AmountOptionsResponse and AmountOption entities
public String saveAmountOptionsResponse(AmountOptionsResponse responseWrapper, String requestId, String txnId) {
// Convert AmountOptionsResponseDTO to AmountOptionsResponse entity
if (responseWrapper == null) {
// Return an empty or default DTO if no entities are found
return "Data Is Empty"; // return an empty DTO object
}
AmountOptionByBillerId entity = new AmountOptionByBillerId();
entity.setAmountOptions(responseWrapper.getAmountOptions());
// Map AmountOptionDTO list to AmountOptionDetails entities
List<AmountOptionDetails> amountOptionDetailsList = mapAmountOptionDTOToEntity(
responseWrapper.getAmountOptionDAOS(), requestId, txnId);
// Set the mapped AmountOptionDetails list to AmountOptionByBillerId entity
entity.setAmountOptionDetails(amountOptionDetailsList);
entity.setCreateDateTime(new Date());
// Save AmountOptionByBillerId entity (this will also save related
// AmountOptionDetails entities)
amountOptionByBillerIdRepository.save(entity); // Saves the entity and cascades to AmountOptionDetails
// Return success message
return "Data saved successfully!";
}
private List<AmountOptionDetails> mapAmountOptionDTOToEntity(List<AmountOptionDTO> amountOptionDTOs,
String requestId, String txnId) {
// If the list is null or empty, return an empty list of entities
if (amountOptionDTOs == null || amountOptionDTOs.isEmpty()) {
return new ArrayList<>(); // Return an empty list of AmountOptionDetails
}
// AmountOptionDetails entities
return amountOptionDTOs.stream().flatMap(dto ->
// For each DTO, convert its breakup sets to AmountOptionDetails entities
dto.getAmountBreakupSets().stream().map(breakup -> {
// Create a new AmountOptionDetails entity for each breakup set
AmountOptionDetails amountOptionDetails = new AmountOptionDetails();
// Set the breakup set as a separate entity (each breakup set becomes a new
// record)
amountOptionDetails.setAmountBreakupSet(breakup); // Store each breakup set as an individual record
// You can optionally set the amountOptionId here, if you want to link this
// detail to the parent.
// Assuming you want to set it directly here or pass it via the entity later:
amountOptionDetails.setAmountOptionId(dto.getAmountOptionId());
amountOptionDetails.setRequestId(requestId);
amountOptionDetails.setTxnId(txnId);
// Return the mapped AmountOptionDetails entity
return amountOptionDetails;
})).collect(Collectors.toList()); // Collect them into a list and return
}
public PlanResponses savePlanResponse(PlanResponse planResponse, String reqId, String txnId) {
PlanResponses response = new PlanResponses();
response.setRequestId(reqId);
response.setPageNo(planResponse.getPageNo());
response.setPageSize(planResponse.getPageSize());
response.setTotalElements(planResponse.getTotalElements());
response.setTotalPages(planResponse.getTotalPages());
response.setTxnId(txnId);
response.setCreateDateTime(new Date());
PlansList plansList = new PlansList();
List<Plan> planEntities = new ArrayList<>();
for (PlanDTO dto : planResponse.getPlansList().getPlans()) {
Plan plan = new Plan();
plan.setPlanId(dto.getPlanId());
plan.setDescription(dto.getDescription());
plan.setBillerId(dto.getBillerId());
plan.setCategoryType(dto.getCategoryType());
plan.setAmountInRupees(dto.getAmountInRupees());
plan.setEffctvFrom(dto.getEffctvFrom());
plan.setEffctvTo(dto.getEffctvTo());
plan.setStatus(dto.getStatus());
plan.setPlansList(plansList);
// categorySubType
List<PlanCategorySubType> subs = new ArrayList<>();
if (dto.getCategorySubType() != null) {
for (String s : dto.getCategorySubType()) {
PlanCategorySubType sub = new PlanCategorySubType();
sub.setCategorySubType(s);
sub.setPlan(plan);
subs.add(sub);
}
}
plan.setCategorySubTypes(subs);
// additionalInfo + tags
// ✅ additionalInfo + tags
if (dto.getAdditionalInfo() != null) {
AdditionalInfoPlans info = new AdditionalInfoPlans();
info.setPlanAdditionalInfoId(dto.getAdditionalInfo().getPlanAdditionalInfoId().toString());
List<com.billpaymentservice.bbps.entites.PlanTag> tagEntities = new ArrayList<>();
if (dto.getAdditionalInfo().getTags() != null && !dto.getAdditionalInfo().getTags().isEmpty()) {
for (com.billpaymentservice.bbps.dto.PlanTag tagDto : dto.getAdditionalInfo().getTags()) {
com.billpaymentservice.bbps.entites.PlanTag tagEntity = new com.billpaymentservice.bbps.entites.PlanTag();
tagEntity.setTagId(tagDto.getTagId().toString());
tagEntity.setName(tagDto.getName());
tagEntity.setValue(tagDto.getValue());
tagEntity.setAdditionalInfo(info); // ✅ maintain relationship
tagEntities.add(tagEntity);
}
}
// ✅ Now set the entity list
info.setTags(tagEntities);
plan.setAdditionalInfo(info);
}
planEntities.add(plan);
}
plansList.setPlans(planEntities);
response.setPlansList(plansList);
return planResponseRepository.save(response);
}
public String saveCategoryItems(List<String> categoryRespList, String requestId, String txnId) {
int savedCount = 0; // To track how many items were saved
if (categoryRespList == null || categoryRespList.isEmpty()) {
return "No items to save.";
}
for (String item : categoryRespList) {
CategoryResponseList category = new CategoryResponseList();
category.setCategoryRespList(item);
category.setRequestId(txnId);
category.setRequestId(requestId);
category.setCreateDateTime(new Date());
categoryResponseListRepository.save(category);
savedCount++;
}
return savedCount + " items were successfully saved.";
}
public String saveComplaintStatus(ComplaintStatusResponse response, String reqId, String txnId) {
if (response == null || response.getComplaints() == null) {
throw new IllegalArgumentException("Invalid complaint response received!");
}
ComplainDetailsDto dto = response.getComplaints();
// ✅ Create and populate entity
ComplainStatus entity = new ComplainStatus();
entity.setComplaintId(dto.getComplaintId());
entity.setComplaintDescr(dto.getComplaintDescr());
entity.setComplaintDispos(dto.getComplaintDispos());
entity.setComplaintType(dto.getComplaintType());
entity.setTxnId(dto.getTxnId());
entity.setComplaintStatus(dto.getComplaintStatus());
entity.setAssignedTo(dto.getAssignedTo());
entity.setComplaintNpciId(dto.getComplaintNpciId());
entity.setComplaintRaisedFrom(dto.getComplaintRaisedFrom());
entity.setUserId(dto.getUserId());
entity.setStatus(dto.getStatus());
entity.setRefId(dto.getRefId());
entity.setEstimatedTAT(dto.getEstimated_TAT()); // ✅ added missing field
entity.setCreationDate(dto.getCreationDate());
entity.setModifiedDate(dto.getModifiedDate());
entity.setBillerName(dto.getBillerName());
entity.setTxnAmount(dto.getTxnAmount());
entity.setSuperLevelEsc(dto.getSuperLevelEsc());
entity.setOpenComplaint(dto.getOpenComplaint());
entity.setReopenComplaint(dto.getReopenComplaint());
entity.setRemarks(dto.getRemarks());
entity.setAgentId(dto.getAgentId());
entity.setCategory(dto.getCategory());
entity.setCreatedEpochDate(dto.getCreatedEpochDate());
entity.setModifiedEpochDate(dto.getModifiedEpochDate());
entity.setRefId(reqId);
entity.setSystemTxnId(txnId);
try {
ComplainStatus savedEntity = complaintStatusRepository.save(entity);
if (savedEntity != null && savedEntity.getId() != null) {
return "Complaint saved successfully with ID: " + savedEntity.getId();
} else {
return " Complaint not saved — unexpected error!";
}
} catch (Exception e) {
e.printStackTrace();
return " Error saving complaint: " + e.getMessage();
}
}
public String saveReOpenComplaint(ComplaintStatusResponse response, String reqId, String txnId) {
if (response == null || response.getComplaints() == null) {
throw new IllegalArgumentException("Invalid complaint response received!");
}
ComplainDetailsDto dto = response.getComplaints();
// ✅ Create and populate entity
ReOpenComplain entity = new ReOpenComplain();
entity.setComplaintId(dto.getComplaintId());
entity.setComplaintDescr(dto.getComplaintDescr());
entity.setComplaintDispos(dto.getComplaintDispos());
entity.setComplaintType(dto.getComplaintType());
entity.setTxnId(dto.getTxnId());
entity.setComplaintStatus(dto.getComplaintStatus());
entity.setAssignedTo(dto.getAssignedTo());
entity.setComplaintNpciId(dto.getComplaintNpciId());
entity.setComplaintRaisedFrom(dto.getComplaintRaisedFrom());
entity.setUserId(dto.getUserId());
entity.setStatus(dto.getStatus());
entity.setRefId(dto.getRefId());
entity.setEstimatedTAT(dto.getEstimated_TAT()); // ✅ added missing field
entity.setCreationDate(dto.getCreationDate());
entity.setModifiedDate(dto.getModifiedDate());
entity.setBillerName(dto.getBillerName());
entity.setTxnAmount(dto.getTxnAmount());
entity.setSuperLevelEsc(dto.getSuperLevelEsc());
entity.setOpenComplaint(dto.getOpenComplaint());
entity.setReopenComplaint(dto.getReopenComplaint());
entity.setRemarks(dto.getRemarks());
entity.setAgentId(dto.getAgentId());
entity.setCategory(dto.getCategory());
entity.setCreatedEpochDate(dto.getCreatedEpochDate());
entity.setModifiedEpochDate(dto.getModifiedEpochDate());
entity.setRefId(reqId);
entity.setSystemTxnId(txnId);
try {
ReOpenComplain savedEntity = reOpenComplaintRepository.save(entity);
if (savedEntity != null && savedEntity.getId() != null) {
return "ReOpen Complaint saved successfully with ID: " + savedEntity.getId();
} else {
return " ReOpen Complaint not saved — unexpected error!";
}
} catch (Exception e) {
e.printStackTrace();
return " Error saving complaint: " + e.getMessage();
}
}
public String saveComplaintHistory(ComplaintHistoryStatusResponse responseWrapper, String reqId, String txnId) {
if (responseWrapper == null || responseWrapper.getComplaintsHistories() == null
|| responseWrapper.getComplaintsHistories().isEmpty()) {
return "No complaint history data found to save.";
}
try {
int savedCount = 0;
for (ComplaintHistoryDTO dto : responseWrapper.getComplaintsHistories()) {
ComplaintHistory entity = new ComplaintHistory();
entity.setComplaintHistoryId(dto.getComplaintHistoryId());
entity.setComplaintId(dto.getComplaintId());
entity.setComplaintDescr(dto.getComplaintDescr());
entity.setComplaintDispos(dto.getComplaintDispos());
entity.setTxnId(dto.getTxnId());
entity.setAssignedTo(dto.getAssignedTo());
entity.setComplaintNpciId(dto.getComplaintNpciId());
entity.setComplaintType(dto.getComplaintType());
entity.setComplaintRaisedFrom(dto.getComplaintRaisedFrom());
entity.setUserId(dto.getUserId());
entity.setStatus(dto.getStatus());
entity.setModifiedDate(dto.getModifiedDate());
entity.setModifiedEpochDate(dto.getModifiedEpochDate());
entity.setReqId(reqId);
entity.setSystemTxnId(txnId);
entity.setCreateDateTime(new Date());
entity.setPageNo(responseWrapper.getPageNo());
entity.setPageSize(responseWrapper.getPageSize());
entity.setTotalElements(responseWrapper.getTotalElements());
entity.setTotalPages(responseWrapper.getTotalPages());
complaintHistoryRepository.save(entity);
savedCount++;
}
return "✅ " + savedCount + " Complaint history record(s) saved successfully.";
} catch (Exception e) {
// Log the error for debugging
LOGGER.error("❌ Error saving complaint history data: {}", e.getMessage(), e);
return "❌ Failed to save complaint history data due to: " + e.getMessage();
}
}
}