Subversion Repositories SmartDukaan

Rev

Rev 30219 | Rev 30265 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30219 tejbeer 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.sql.Timestamp;
4
import java.time.DayOfWeek;
5
import java.time.LocalDate;
6
import java.time.LocalDateTime;
7
import java.time.Month;
8
import java.time.YearMonth;
9
import java.util.ArrayList;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.Map.Entry;
13
import java.util.stream.Collectors;
14
 
15
import javax.servlet.http.HttpServletRequest;
16
 
17
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.stereotype.Controller;
21
import org.springframework.transaction.annotation.Transactional;
22
import org.springframework.ui.Model;
23
import org.springframework.web.bind.annotation.PathVariable;
24
import org.springframework.web.bind.annotation.RequestBody;
25
import org.springframework.web.bind.annotation.RequestMapping;
26
import org.springframework.web.bind.annotation.RequestMethod;
27
 
28
import com.spice.profitmandi.common.enumuration.FofoType;
29
import com.spice.profitmandi.common.model.CustomRetailer;
30
import com.spice.profitmandi.common.services.fundfina.PartnerUserData;
31
import com.spice.profitmandi.common.services.fundfina.PreApprovalRequest;
32
import com.spice.profitmandi.common.services.fundfina.PreApprovalResponse;
33
import com.spice.profitmandi.common.services.fundfina.PreApprovals;
34
import com.spice.profitmandi.common.services.fundfina.PreApprovalsResponse;
35
import com.spice.profitmandi.common.services.fundfina.PushNotificationRequest;
36
import com.spice.profitmandi.common.services.fundfina.PushNotificationResponse;
37
import com.spice.profitmandi.common.services.fundfina.TransactionRequest;
38
import com.spice.profitmandi.common.services.fundfina.TransactionResponse;
39
import com.spice.profitmandi.common.services.fundfina.TransactionSummary;
40
import com.spice.profitmandi.common.services.fundfina.TransactionalData;
41
import com.spice.profitmandi.dao.entity.dtr.CreditAccount;
42
import com.spice.profitmandi.dao.entity.dtr.Document;
43
import com.spice.profitmandi.dao.entity.dtr.FundFinaPreApproval;
44
import com.spice.profitmandi.dao.entity.fofo.FofoKyc;
45
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
46
import com.spice.profitmandi.dao.entity.fofo.PartnerOnBoardingPanel;
47
import com.spice.profitmandi.dao.entity.user.User;
48
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
49
import com.spice.profitmandi.dao.model.UserWalletCountVolumeModel;
50
import com.spice.profitmandi.dao.model.WalletMinMaxAmountModel;
51
import com.spice.profitmandi.dao.repository.dtr.CreditAccountRepository;
52
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
53
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
54
import com.spice.profitmandi.dao.repository.dtr.FundFinaPreApprovalRepository;
55
import com.spice.profitmandi.dao.repository.dtr.PartnerOnBoardingPanelRepository;
56
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
57
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
58
import com.spice.profitmandi.dao.repository.user.AddressRepository;
59
import com.spice.profitmandi.dao.repository.user.FofoKycRepository;
60
import com.spice.profitmandi.dao.repository.user.UserRepository;
61
import com.spice.profitmandi.service.user.RetailerService;
62
import com.spice.profitmandi.web.util.MVCResponseSender;
63
 
64
@Controller
65
@Transactional(rollbackFor = Throwable.class)
66
public class FundFinaController {
67
 
68
	@Autowired
69
	private RetailerService retailerService;
70
 
71
	@Autowired
72
	private UserWalletHistoryRepository userWalletHistoryRepository;
73
 
74
	@Autowired
75
	private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
76
 
77
	@Autowired
78
	private FofoStoreRepository fofoStoreRepository;
79
 
80
	@Autowired
81
	private UserRepository userRepository;
82
 
83
	@Autowired
84
	private AddressRepository addressRepository;
85
 
86
	@Autowired
87
	private MVCResponseSender mvcResponseSender;
88
 
89
	@Autowired
90
	private PartnerOnBoardingPanelRepository partnerOnBoardingPanelRepository;
91
 
92
	@Autowired
93
	private FofoKycRepository fofoKycRepository;
94
 
95
	@Autowired
96
	private DocumentRepository documentRepository;
97
 
98
	@Autowired
99
	private FundFinaPreApprovalRepository fundFinaPreApprovalRepository;
100
 
101
	@Autowired
102
	private CreditAccountRepository creditAccountRepository;
103
	private static final Logger logger = LogManager.getLogger(FundFinaController.class);
104
 
30229 tejbeer 105
	private String authKey = "$mart@123";
30219 tejbeer 106
 
107
	@RequestMapping(value = "/fundfina/getTransactions", method = RequestMethod.POST)
108
	public String getTransactions(HttpServletRequest request, @RequestBody TransactionRequest transactionRequest,
109
			Model model) throws Exception {
110
 
111
		TransactionResponse tr = new TransactionResponse();
112
 
113
		String key = request.getHeader("auth-key");
114
		logger.info("authKey {}", authKey);
115
 
116
		if (key.equals(authKey)) {
117
			int offset = 0;
118
			offset = ((0 + transactionRequest.getPageNumber() - 1) * 100) + 1;
119
 
120
			List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores().stream()
121
					.filter(x -> x.getFofoType().equals(FofoType.FRANCHISE)).collect(Collectors.toList());
122
			int remainder = fofoStores.size() / 100;
123
			int modulas = fofoStores.size() % 100;
124
			if (modulas != 0) {
125
				tr.setTotalPages(remainder + 1);
126
 
127
			} else {
128
				tr.setTotalPages(remainder);
129
			}
130
 
131
			logger.info("offset {}", offset);
132
 
133
			Map<Integer, CustomRetailer> customRetailerMap = retailerService.getFofoRetailersPaginated(true, offset,
134
					100, FofoType.FRANCHISE);
135
 
136
			tr.setStatus("success");
137
			tr.setRequestRef(String.valueOf(Timestamp.valueOf(LocalDateTime.now()).getTime()));
138
			tr.setTotalRecords(fofoStores.size());
139
			tr.setCurrentPage(transactionRequest.getPageNumber());
140
			tr.setCurrentPageRecords(customRetailerMap.size());
141
 
142
			List<TransactionalData> transactionData = new ArrayList<>();
143
 
144
			for (Entry<Integer, CustomRetailer> customRetailerEntry : customRetailerMap.entrySet()) {
145
 
146
				int fofoId = customRetailerEntry.getKey();
147
 
148
				User user = userRepository.selectById(fofoId);
149
				TransactionalData td = new TransactionalData();
150
				td.setJoinedOn(user.getCreateTimestamp().toLocalDate().toString());
151
				td.setUserId(Integer.toString(user.getId()));
152
				td.setMerchantType("Retailer");
153
				td.setEmailId(user.getEmailId());
154
				td.setMobileNo(user.getMobileNumber());
155
				td.setShopType("Mobile Shop");
156
				td.setIpAddress("50.116.3.101");
157
				td.setPincode(Integer.parseInt(addressRepository.selectById(user.getAddressId()).getPinCode()));
158
				List<UserWalletCountVolumeModel> countVolumeModels = userWalletHistoryRepository
159
						.selectCountVolumeTransactionByYearMonth(fofoId);
160
 
161
				Map<YearMonth, WalletMinMaxAmountModel> walletMinMaxModel = partnerDailyInvestmentRepository
162
						.selectMinMaxWalletBalanceGroupByYearMonth(fofoId).stream()
163
						.collect(Collectors.toMap(x -> YearMonth.of(Integer.parseInt(x.getYear()),
164
								Month.of(Integer.parseInt(x.getMonth()))), x -> x));
165
 
166
				List<TransactionSummary> transactionSummmary = new ArrayList<>();
167
 
168
				for (UserWalletCountVolumeModel uwcm : countVolumeModels) {
169
					TransactionSummary ts = new TransactionSummary();
170
					ts.setCount(String.valueOf(uwcm.getCount()));
171
					ts.setVolume(uwcm.getAmount());
172
					ts.setYear(Integer.parseInt(uwcm.getYear()));
173
 
174
					YearMonth yearMonthObject = YearMonth.of(Integer.parseInt(uwcm.getYear()),
175
							Month.of(Integer.parseInt(uwcm.getMonth())));
176
					int daysInMonth = yearMonthObject.lengthOfMonth();
177
					ts.setTotalDays(daysInMonth);
178
 
179
					LocalDate result = yearMonthObject.atDay(1);
180
					int activeDays = 0;
181
					while (activeDays < daysInMonth) {
182
						result = result.plusDays(1);
183
 
184
						logger.info("result {}", result);
185
 
186
						if (!(result.getDayOfWeek() == DayOfWeek.SATURDAY
187
								|| result.getDayOfWeek() == DayOfWeek.SUNDAY)) {
188
							++activeDays;
189
						}
190
					}
191
 
192
					ts.setActiveDays(activeDays);
193
 
194
					ts.setMonth(Integer.parseInt(uwcm.getMonth()));
195
					if (walletMinMaxModel.get(yearMonthObject) != null) {
196
						ts.setMinWalletBalance(walletMinMaxModel.get(yearMonthObject).getMin());
197
						ts.setMaxWalletBalance(walletMinMaxModel.get(yearMonthObject).getMax());
198
					}
199
					transactionSummmary.add(ts);
200
 
201
				}
202
 
203
				td.setTransactionSummary(transactionSummmary);
204
 
205
				transactionData.add(td);
206
 
207
			}
208
			tr.setTransactionalData(transactionData);
209
		} else {
210
			tr.setErrCode("401");
211
			tr.setDesc("auth key is not valid");
212
 
213
		}
214
 
215
		model.addAttribute("response", mvcResponseSender.createResponseString(tr));
216
		return "response";
217
	}
218
 
219
	@RequestMapping(value = "/fundfina/getUserData/{userId}", method = RequestMethod.GET)
220
	public String getUserData(HttpServletRequest request, @PathVariable int userId, Model model) throws Exception {
221
 
222
		FofoStore fs = fofoStoreRepository.selectByRetailerId(userId);
223
 
224
		User user = userRepository.selectById(fs.getId());
225
		String key = request.getHeader("auth-key");
226
		logger.info("authKey {}", authKey);
227
		PartnerUserData pud = new PartnerUserData();
228
 
229
		if (key.equals(authKey)) {
230
			PartnerOnBoardingPanel pobp = partnerOnBoardingPanelRepository.selectByCode(fs.getCode());
231
			CustomRetailer customRetailer = retailerService.getFofoRetailer(fs.getId());
232
			List<FofoKyc> fofoKycs = fofoKycRepository.selectByPartnerOnBoardingId(pobp.getId());
233
			FofoKyc fk = fofoKycs.get(0);
234
			pud.setName(fk.getFirstName() + " " + fk.getLastName());
235
			pud.setShopName(customRetailer.getBusinessName());
236
			pud.setShopType("Mobile Store");
237
			pud.setDob(fk.getDob().toString());
238
			pud.setShopOwnership("rented");
239
			pud.setShopPincode(customRetailer.getAddress().getPinCode());
240
			pud.setShopAddress(customRetailer.getAddress().getLine1() + " " + customRetailer.getAddress().getLine2());
241
			pud.setShopCity(customRetailer.getAddress().getCity());
242
			pud.setShopState(customRetailer.getAddress().getState());
243
			pud.setMobile(fk.getMobile());
244
			pud.setCity(fk.getCity());
245
			pud.setAddress(fk.getAddress1() + " " + fk.getAddress2() + " " + fk.getAddress3());
246
			pud.setEmailId(fk.getEmail());
247
			pud.setPan(fk.getPan());
248
			pud.setPincode(fk.getPincode());
249
			pud.setBusinessActivationDate(user.getCreateTimestamp().toString());
250
			pud.setState(fk.getState());
251
 
252
			if (fk.getPanDoc() != 0) {
253
				Document document = documentRepository.selectById(fk.getPanDoc());
254
				pud.setPanFront(
255
						"http://shop2020.in:8080/profitmandi-web/open-attachment?documentId=" + document.getId());
256
			}
257
			pud.setRequestRef(String.valueOf(Timestamp.valueOf(LocalDateTime.now()).getTime()));
258
			pud.setStatus("success");
259
		} else {
260
			pud.setErrCode("401");
261
			pud.setDesc("auth key is not valid");
262
 
263
		}
264
		model.addAttribute("response", mvcResponseSender.createResponseString(pud));
265
 
266
		return "response";
267
 
268
	}
269
 
270
	@RequestMapping(value = "/fundfina/pushPreApproval", method = RequestMethod.POST)
271
	public String pushPreApproval(HttpServletRequest request, @RequestBody PreApprovalRequest preApprovalRequest,
272
			Model model) throws Exception {
273
 
274
		List<PreApprovals> preApprovals = preApprovalRequest.getPreApprovals();
275
		String key = request.getHeader("auth-key");
276
		logger.info("authKey {}", authKey);
277
		PreApprovalResponse par = new PreApprovalResponse();
278
 
279
		if (key.equals(authKey)) {
280
 
281
			par.setStatus("success");
282
			par.setRequestRef(String.valueOf(Timestamp.valueOf(LocalDateTime.now()).getTime()));
283
 
284
			List<PreApprovalsResponse> pars = new ArrayList<>();
285
 
286
			for (PreApprovals pa : preApprovals) {
287
 
288
				if (pa.getProductId() != null) {
289
					FundFinaPreApproval fpa = fundFinaPreApprovalRepository
290
							.selectByUserIdAndProductId(Integer.parseInt(pa.getUserId()), pa.getProductId());
291
					if (fpa == null) {
292
						fpa = new FundFinaPreApproval();
293
						fpa.setFofoId(Integer.parseInt(pa.getUserId()));
294
						fpa.setProductId(fpa.getProductId());
295
						fpa.setTenureInDays(fpa.getTenureInDays());
296
						fpa.setProcessingFee(fpa.getProcessingFee());
297
						fpa.setAnnualInterestRate(fpa.getAnnualInterestRate());
298
						fpa.setApprovedAmount(fpa.getApprovedAmount());
299
						fpa.setEmiAmount(fpa.getEmiAmount());
300
						fpa.setExpiryDate(fpa.getExpiryDate());
301
						fpa.setCreateTimestamp(LocalDateTime.now());
302
						fpa.setUpdateTimestamp(LocalDateTime.now());
303
						fundFinaPreApprovalRepository.persist(fpa);
304
 
305
					}
306
 
307
					PreApprovalsResponse paar = new PreApprovalsResponse();
308
					paar.setUserId(pa.getUserId());
309
					paar.setProductId(pa.getProductId());
310
					pars.add(paar);
311
 
312
				}
313
			}
314
 
315
			par.setPreApprvalsRes(pars);
316
 
317
			model.addAttribute("response", mvcResponseSender.createResponseString(par));
318
		} else {
319
			par.setErrCode("401");
320
			par.setDesc("auth key is not valid");
321
		}
322
		return "response";
323
 
324
	}
325
 
326
	@RequestMapping(value = "/fundfina/pushNotification", method = RequestMethod.POST)
327
	public String pushNotification(HttpServletRequest request,
328
			@RequestBody PushNotificationRequest pushNotificationRequest, Model model) throws Exception {
329
 
330
		PushNotificationResponse pnr = new PushNotificationResponse();
331
		pnr.setRequestRef(String.valueOf(Timestamp.valueOf(LocalDateTime.now()).getTime()));
332
		pnr.setStatus("success");
333
 
334
		model.addAttribute("response", mvcResponseSender.createResponseString(pnr));
335
 
336
		return "response";
337
	}
338
 
339
}