| 8976 |
kshitij.so |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
|
|
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.HashMap;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
|
|
|
7 |
import in.shop2020.model.v1.order.EmiScheme;
|
|
|
8 |
import in.shop2020.model.v1.order.TransactionService.Client;
|
|
|
9 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
10 |
|
|
|
11 |
import org.apache.log4j.Logger;
|
|
|
12 |
import org.json.JSONArray;
|
|
|
13 |
import org.json.JSONException;
|
|
|
14 |
import org.json.JSONObject;
|
|
|
15 |
|
|
|
16 |
public class EmiInfoController extends BaseController{
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
*
|
|
|
20 |
*/
|
|
|
21 |
private static final long serialVersionUID = 1L;
|
|
|
22 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
23 |
private TransactionClient transactionClient = null;
|
|
|
24 |
private Client client;
|
|
|
25 |
private static List<EmiScheme> emiSchemes = null;
|
|
|
26 |
private static JSONObject emiTableObj = null;
|
|
|
27 |
|
|
|
28 |
public String index() throws JSONException {
|
|
|
29 |
HashMap<String, List<EmiScheme>> emiMap = null;
|
|
|
30 |
if (emiSchemes==null){
|
|
|
31 |
log.info("Fetching emi schemes from db");
|
|
|
32 |
try {
|
|
|
33 |
transactionClient = new TransactionClient();
|
|
|
34 |
client = transactionClient.getClient();
|
|
|
35 |
emiSchemes = client.getAvailableEmiSchemes();
|
|
|
36 |
emiMap = new HashMap<String,List<EmiScheme>>();
|
|
|
37 |
for (EmiScheme scheme : emiSchemes){
|
|
|
38 |
if (emiMap.containsKey(scheme.getBankName())) {
|
|
|
39 |
List<EmiScheme> listScheme = emiMap.get(scheme.getBankName());
|
|
|
40 |
listScheme.add(scheme);
|
|
|
41 |
emiMap.put(scheme.getBankName(), listScheme);
|
|
|
42 |
}
|
|
|
43 |
else{
|
|
|
44 |
List<EmiScheme> listScheme = new ArrayList<EmiScheme>();
|
|
|
45 |
listScheme.add(scheme);
|
|
|
46 |
emiMap.put(scheme.getBankName(), listScheme);
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
} catch (Exception e) {
|
|
|
50 |
log.error("Unable to initialize the tranaction service client or unable to get emi schemes", e);
|
|
|
51 |
}
|
|
|
52 |
emiTableObj = new JSONObject();
|
|
|
53 |
JSONArray arr = new JSONArray();
|
|
|
54 |
for (String set : emiMap.keySet()){
|
|
|
55 |
JSONObject obj = new JSONObject();
|
|
|
56 |
obj.put("Bank", set);
|
|
|
57 |
for (EmiScheme scheme :emiMap.get(set)){
|
|
|
58 |
obj.put("T"+scheme.getTenureDescription().replace(" ", ""), scheme.getInterestRate());
|
|
|
59 |
}
|
|
|
60 |
arr.put(obj);
|
|
|
61 |
}
|
|
|
62 |
emiTableObj.put("BankCharges", arr);
|
|
|
63 |
|
|
|
64 |
JSONArray arr1 = new JSONArray();
|
|
|
65 |
for (String set : emiMap.keySet()){
|
|
|
66 |
JSONObject obj = new JSONObject();
|
|
|
67 |
obj.put("Bank", set);
|
|
|
68 |
for (EmiScheme scheme :emiMap.get(set)){
|
|
|
69 |
obj.put("T"+scheme.getTenureDescription().replace(" ", ""), getEmiAmount(scheme));
|
|
|
70 |
}
|
|
|
71 |
arr1.put(obj);
|
|
|
72 |
}
|
|
|
73 |
emiTableObj.put("Value", arr1);
|
|
|
74 |
}
|
|
|
75 |
return "index";
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
public double getEmiAmount(EmiScheme scheme){
|
|
|
80 |
double interestRate = scheme.getInterestRate()/12/100;
|
|
|
81 |
double emiAmount = 10000*interestRate*Math.pow((1+interestRate), scheme.getTenure())/(Math.pow((1+interestRate), scheme.getTenure())-1);
|
|
|
82 |
return Math.ceil(emiAmount);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
public String getEmiDetailsInJson() {
|
|
|
86 |
return EmiInfoController.emiTableObj.toString();
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
public static void main(String[] args) throws JSONException{
|
|
|
90 |
EmiInfoController obj = new EmiInfoController();
|
|
|
91 |
obj.index();
|
|
|
92 |
}
|
|
|
93 |
}
|