| 30859 |
tejbeer |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import java.math.BigDecimal;
|
|
|
4 |
import java.time.LocalDate;
|
|
|
5 |
import java.time.LocalDateTime;
|
|
|
6 |
import java.util.ArrayList;
|
|
|
7 |
import java.util.List;
|
|
|
8 |
import java.util.Map;
|
|
|
9 |
import java.util.Map.Entry;
|
|
|
10 |
import java.util.Set;
|
|
|
11 |
import java.util.stream.Collectors;
|
|
|
12 |
|
|
|
13 |
import javax.servlet.http.HttpServletRequest;
|
|
|
14 |
import javax.transaction.Transactional;
|
|
|
15 |
|
|
|
16 |
import org.apache.logging.log4j.LogManager;
|
|
|
17 |
import org.apache.logging.log4j.Logger;
|
|
|
18 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
19 |
import org.springframework.http.MediaType;
|
|
|
20 |
import org.springframework.http.ResponseEntity;
|
|
|
21 |
import org.springframework.stereotype.Controller;
|
|
|
22 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
23 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
24 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
25 |
|
|
|
26 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
27 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
|
|
28 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
|
|
29 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
|
|
30 |
import com.spice.profitmandi.dao.entity.dtr.CreditAccount;
|
|
|
31 |
import com.spice.profitmandi.dao.entity.transaction.Loan;
|
|
|
32 |
import com.spice.profitmandi.dao.entity.transaction.SDCreditRequirement;
|
|
|
33 |
import com.spice.profitmandi.dao.entity.transaction.SanctionRequest;
|
|
|
34 |
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
|
|
|
35 |
import com.spice.profitmandi.dao.enumuration.transaction.SanctionStatus;
|
|
|
36 |
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
|
|
|
37 |
import com.spice.profitmandi.dao.repository.cs.CsService;
|
|
|
38 |
import com.spice.profitmandi.dao.repository.dtr.CreditAccountRepository;
|
|
|
39 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
|
|
40 |
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
|
|
|
41 |
import com.spice.profitmandi.dao.repository.transaction.SDCreditRequirementRepository;
|
|
|
42 |
import com.spice.profitmandi.dao.repository.transaction.SanctionRequestRepository;
|
|
|
43 |
import com.spice.profitmandi.service.NotificationService;
|
|
|
44 |
import com.spice.profitmandi.service.transaction.SDCreditService;
|
|
|
45 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
46 |
import com.spice.profitmandi.service.user.SDCreditModel;
|
|
|
47 |
|
|
|
48 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
49 |
import io.swagger.annotations.ApiImplicitParams;
|
|
|
50 |
|
|
|
51 |
@Controller
|
|
|
52 |
@Transactional(rollbackOn = Throwable.class)
|
|
|
53 |
public class SDCreditController {
|
|
|
54 |
|
|
|
55 |
@Autowired
|
|
|
56 |
private SDCreditRequirementRepository sdCreditRequirementRepository;
|
|
|
57 |
|
|
|
58 |
@Autowired
|
|
|
59 |
private RetailerService retailerService;
|
|
|
60 |
|
|
|
61 |
@Autowired
|
|
|
62 |
private AuthRepository authRepository;
|
|
|
63 |
|
|
|
64 |
@Autowired
|
|
|
65 |
private CsService csService;
|
|
|
66 |
|
|
|
67 |
@Autowired
|
|
|
68 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
69 |
|
|
|
70 |
@Autowired
|
|
|
71 |
private SanctionRequestRepository sanctionRequestRepository;
|
|
|
72 |
|
|
|
73 |
@Autowired
|
|
|
74 |
private ResponseSender<?> responseSender;
|
|
|
75 |
|
|
|
76 |
@Autowired
|
|
|
77 |
private SDCreditService sdCreditService;
|
|
|
78 |
|
|
|
79 |
@Autowired
|
|
|
80 |
private CreditAccountRepository creditAccountRepository;
|
|
|
81 |
|
|
|
82 |
@Autowired
|
|
|
83 |
private LoanRepository loanRepository;
|
|
|
84 |
|
|
|
85 |
@Autowired
|
|
|
86 |
private NotificationService notificationService;
|
|
|
87 |
|
|
|
88 |
private static final Logger LOGGER = LogManager.getLogger(SDCreditController.class);
|
|
|
89 |
|
|
|
90 |
@RequestMapping(value = "/getSDCreditReq", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
91 |
@ApiImplicitParams({
|
|
|
92 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
93 |
public ResponseEntity<?> getSDCreditReq(HttpServletRequest request, @RequestParam(name = "gmailId") String gmailId)
|
|
|
94 |
throws ProfitMandiBusinessException {
|
|
|
95 |
AuthUser authUser = authRepository.selectByGmailId(gmailId);
|
|
|
96 |
Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
|
|
|
97 |
List<Integer> fofoIds = new ArrayList<>(storeGuyMap.get(authUser.getEmailId()));
|
|
|
98 |
List<SDCreditModel> partnersSDCredit = new ArrayList<>();
|
|
|
99 |
|
|
|
100 |
if (fofoIds != null && fofoIds.size() > 0) {
|
|
|
101 |
|
|
|
102 |
List<Integer> fofoIdList = fofoStoreRepository.selectByRetailerIds(fofoIds).stream()
|
|
|
103 |
.filter(x -> !x.isInternal()).map(x -> x.getId()).collect(Collectors.toList());
|
|
|
104 |
|
|
|
105 |
Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
|
|
|
106 |
|
|
|
107 |
Map<Integer, CustomRetailer> customRetailers = fofoIdList.stream().map(x -> customRetailerMap.get(x))
|
|
|
108 |
.filter(x -> x != null).collect(Collectors.toList()).stream()
|
|
|
109 |
.collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
|
|
|
110 |
|
|
|
111 |
Map<Integer, SDCreditRequirement> sdCreditRequirementMap = sdCreditRequirementRepository.selectAll()
|
|
|
112 |
.stream().collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
|
|
|
113 |
|
|
|
114 |
Map<Integer, SanctionRequest> sanctionRequestMap = sanctionRequestRepository.selectByDate(LocalDate.now())
|
|
|
115 |
.stream().collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
|
|
|
116 |
|
|
|
117 |
for (Entry<Integer, CustomRetailer> customRetailerEntry : customRetailers.entrySet()) {
|
|
|
118 |
|
|
|
119 |
int fofoId = customRetailerEntry.getKey();
|
|
|
120 |
CustomRetailer customRetailer = customRetailerEntry.getValue();
|
|
|
121 |
|
|
|
122 |
CreditAccount creditAccount = creditAccountRepository.selectByFofoIdAndGateway(fofoId,
|
|
|
123 |
Gateway.SDDIRECT);
|
|
|
124 |
|
|
|
125 |
SDCreditRequirement sdCreditRequirement = sdCreditRequirementMap.get(customRetailerEntry.getKey());
|
|
|
126 |
SanctionRequest sanctionRequest = sanctionRequestMap.get(customRetailerEntry.getKey());
|
|
|
127 |
SDCreditModel sdCreditModel = new SDCreditModel();
|
|
|
128 |
sdCreditModel.setFofoId(fofoId);
|
|
|
129 |
sdCreditModel.setBusinessName(customRetailer.getBusinessName());
|
|
|
130 |
sdCreditModel.setMobile(customRetailer.getMobileNumber());
|
|
|
131 |
if (sdCreditRequirement != null) {
|
|
|
132 |
sdCreditModel.setLimit(sdCreditRequirement.getLimit());
|
|
|
133 |
if (sdCreditRequirement.getSecurityCheck() > 0 && creditAccount.isActive()) {
|
|
|
134 |
sdCreditModel.setEligibility("yes");
|
|
|
135 |
} else {
|
|
|
136 |
sdCreditModel.setEligibility("no");
|
|
|
137 |
|
|
|
138 |
}
|
|
|
139 |
sdCreditModel.setInterest(sdCreditRequirement.getInterestRate());
|
|
|
140 |
long utilizationAmount = sdCreditService.getUtilizationAmount(fofoId);
|
|
|
141 |
sdCreditModel.setAvailability(
|
|
|
142 |
sdCreditRequirement.getLimit().subtract(new BigDecimal(utilizationAmount)));
|
|
|
143 |
sdCreditModel.setRisk(sdCreditModel.getRisk());
|
|
|
144 |
sdCreditModel.setCreditDuration(sdCreditRequirement.getCreditDays());
|
|
|
145 |
|
|
|
146 |
List<Loan> loans = loanRepository.selectActiveLoan(fofoId);
|
|
|
147 |
sdCreditModel.setTotalOpenLoan(loans.size());
|
| 30913 |
tejbeer |
148 |
|
|
|
149 |
sdCreditModel.setLoan(loans);
|
| 30859 |
tejbeer |
150 |
}
|
|
|
151 |
if (sanctionRequest != null) {
|
|
|
152 |
sdCreditModel.setStatus(sanctionRequest.getStatus());
|
|
|
153 |
}
|
|
|
154 |
partnersSDCredit.add(sdCreditModel);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
}
|
|
|
158 |
return responseSender.ok(partnersSDCredit);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
@RequestMapping(value = "/createSanctionRequest", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
162 |
@ApiImplicitParams({
|
|
|
163 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
164 |
public ResponseEntity<?> createSanctionRequest(HttpServletRequest request,
|
|
|
165 |
@RequestParam(name = "gmailId") String gmailId, @RequestParam(name = "fofoId") int fofoId,
|
|
|
166 |
@RequestParam(name = "requestedlimit") int requestedlimit) throws ProfitMandiBusinessException {
|
|
|
167 |
|
|
|
168 |
SDCreditRequirement sdCreditReq = sdCreditRequirementRepository.selectByFofoId(fofoId);
|
|
|
169 |
if (sdCreditReq != null) {
|
|
|
170 |
AuthUser authUser = authRepository.selectByGmailId(gmailId);
|
|
|
171 |
|
|
|
172 |
SanctionRequest sanctionRequest = new SanctionRequest();
|
|
|
173 |
sanctionRequest.setAuthId(authUser.getId());
|
|
|
174 |
sanctionRequest.setFofoId(fofoId);
|
|
|
175 |
sanctionRequest.setRequestedAmount(BigDecimal.valueOf(requestedlimit));
|
|
|
176 |
sanctionRequest.setApprovalAmount(BigDecimal.valueOf(requestedlimit));
|
|
|
177 |
sanctionRequest.setCreatedTimestamp(LocalDateTime.now());
|
|
|
178 |
sanctionRequest.setStatus(SanctionStatus.PENDING);
|
|
|
179 |
sanctionRequest.setFreeDays(sdCreditReq.getFreeDays());
|
| 34356 |
ranu |
180 |
sanctionRequest.setRbmL2ApprovalStatus(SanctionStatus.PENDING);
|
| 30859 |
tejbeer |
181 |
sanctionRequestRepository.persist(sanctionRequest);
|
|
|
182 |
|
|
|
183 |
return responseSender.ok(true);
|
|
|
184 |
|
|
|
185 |
} else {
|
|
|
186 |
|
|
|
187 |
return responseSender.ok(false);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
}
|
|
|
191 |
}
|