Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
13584 manish.sha 1
package in.shop2020.utils;
2
 
3
import in.shop2020.model.v1.order.FlipkartFaSalesSnapshot;
4
 
5
import java.io.FileReader;
6
import java.net.UnknownHostException;
7
import java.util.ArrayList;
8
import java.util.Calendar;
9
import java.util.HashMap;
10
import java.util.List;
11
import java.util.Map;
12
 
13
import org.json.JSONException;
14
 
15
import au.com.bytecode.opencsv.CSVReader;
16
 
17
import com.google.gson.Gson;
18
import com.mongodb.BasicDBObject;
19
import com.mongodb.DB;
20
import com.mongodb.DBCollection;
21
import com.mongodb.DBCursor;
22
import com.mongodb.DBObject;
23
import com.mongodb.MongoClient;
24
import com.mongodb.util.JSON;
25
 
26
public class FlipkartConsumer{
27
	private static MongoClient mongo;
28
    private static final String FLIPKART_SALES_SNAPSHOT = "FlipkartSaleSnapshot";
29
    private static final String FLIPKART_DAILY_SALE_SNAPSHOT = "FlipkartDailySaleSnapshot";
30
 
31
    static {
32
        try {
33
            mongo = new MongoClient( "localhost" , 27017 );
34
        } catch (UnknownHostException e) {
35
            e.printStackTrace();
36
        }
37
    }
38
 
39
    public static boolean bulkAddOrUpdateFlipkartFbaSalesSnapshot(List<FlipkartFaSalesSnapshot> sales) throws JSONException{
40
    	DB db = mongo.getDB(FLIPKART_SALES_SNAPSHOT);
41
        DBCollection collection = db.getCollection(FLIPKART_DAILY_SALE_SNAPSHOT);
42
        Gson gs = new Gson();
43
        for (FlipkartFaSalesSnapshot sale :sales){
44
        	DBObject dbObject = (DBObject) JSON.parse(gs.toJson(sale));
45
        	dbObject.put("_id", generateId(sale.getDateOfSale(), sale.getItem_id(), sale.getFcLocation().getValue()));
46
        	BasicDBObject searchObject = new BasicDBObject();
47
        	searchObject.put("_id",generateId(sale.getDateOfSale(), sale.getItem_id(), sale.getFcLocation().getValue()));
48
        	DBCursor cursor = collection.find(searchObject);
49
        	if (cursor.count() > 0){
50
        		BasicDBObject updateObject = new BasicDBObject();
51
        		updateObject.put("totalSale",sale.getTotalSale());
52
                updateObject.put("totalOrderCount",sale.getTotalOrderCount());
53
                if (sale.getTotalOrderCount() > 0){
54
                    updateObject.put("isOutOfStock",false);
55
                }
56
                collection.update(searchObject, new BasicDBObject("$set", updateObject));
57
        	}
58
        	else{
59
                collection.insert(dbObject);
60
            }
61
        }
62
        collection.createIndex((DBObject) JSON.parse("{'dateOfSale':1}"));
63
        return true;
64
    }
65
 
66
    public static List<FlipkartFaSalesSnapshot> getFlipkartFbaSalesSnapshotForDays(int interval){
67
    	DB db = mongo.getDB(FLIPKART_SALES_SNAPSHOT);
68
        DBCollection collection = db.getCollection(FLIPKART_DAILY_SALE_SNAPSHOT);
69
        Calendar cal = Calendar.getInstance();
70
        cal.add(Calendar.DATE, -1);cal.set(Calendar.HOUR_OF_DAY, 00);cal.set(Calendar.MINUTE,00);cal.set(Calendar.SECOND,00);cal.set(Calendar.MILLISECOND,00);
71
        long toDate = cal.getTimeInMillis();
72
        cal.add(Calendar.DATE, -interval);
73
        long fromDate = cal.getTimeInMillis();
74
        BasicDBObject query = new BasicDBObject();
75
        BasicDBObject obj = new BasicDBObject();
76
        obj.put("$gte", fromDate);
77
        obj.put("$lte", toDate);
78
        query.put("dateOfSale",obj);
79
        System.out.println(fromDate +" "+ toDate);
80
        BasicDBObject orderBy = new BasicDBObject();
81
        orderBy.put("dateOfSale", 1);
82
        Gson gson = new Gson();
83
        List<FlipkartFaSalesSnapshot> salesSnapshot = new ArrayList<FlipkartFaSalesSnapshot>();
84
        DBCursor cursor = collection.find(query).sort(orderBy);
85
        while (cursor.hasNext()) {
86
        	FlipkartFaSalesSnapshot salesObj = gson.fromJson(cursor.next().toString(), FlipkartFaSalesSnapshot.class);
87
        	salesSnapshot.add(salesObj);
88
        }
89
        return salesSnapshot;
90
    }
91
 
92
    public static FlipkartFaSalesSnapshot getFlipkartFaSalesLatestSnapshotForItem(long item_id){
93
    	FlipkartFaSalesSnapshot salesObj = new FlipkartFaSalesSnapshot();
94
    	DB db = mongo.getDB(FLIPKART_SALES_SNAPSHOT);
95
        DBCollection collection = db.getCollection(FLIPKART_DAILY_SALE_SNAPSHOT);
96
        BasicDBObject andQuery = new BasicDBObject();
97
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
98
        obj.add(new BasicDBObject("item_id", item_id));
99
        Calendar cal = Calendar.getInstance();
100
        cal.add(Calendar.DATE, -1);cal.set(Calendar.HOUR_OF_DAY, 00);cal.set(Calendar.MINUTE,00);cal.set(Calendar.SECOND,00);cal.set(Calendar.MILLISECOND,00);
101
        obj.add(new BasicDBObject("dateOfSale", cal.getTimeInMillis()));
102
        andQuery.put("$and", obj);
103
        BasicDBObject orderBy = new BasicDBObject();
104
        orderBy.put("dateOfSale", -1);
105
        DBCursor cursor = collection.find(andQuery).sort(orderBy).limit(1);
106
        Gson gson = new Gson();
107
        while (cursor.hasNext()) {
108
            salesObj = gson.fromJson(cursor.next().toString(), FlipkartFaSalesSnapshot.class);
109
        }
110
        return salesObj;
111
    }
112
 
113
    private static long generateId(long dateOfSale, long item_id, int warehouseLocation){
114
        return Long.valueOf(String.valueOf(dateOfSale)+String.valueOf(item_id)+String.valueOf(warehouseLocation));
115
    }
116
 
117
    public static Map<Long, Long> getCurrentFAInventory(){
118
    	Map<Long,Long> currentFAInventoryMap = new HashMap<Long, Long>();
119
    	try {
120
    		int i=1;
13586 manish.sha 121
			CSVReader csvInventoryReader = new CSVReader(new FileReader("/home/FAInventory.csv"));
13584 manish.sha 122
			String[] values;
123
			while ((values = csvInventoryReader.readNext()) != null) {
124
				if(i==1){
125
					i++;
126
					continue;
127
				}
128
				long itemId = Long.parseLong(values[0]);
129
				long available = Long.parseLong(values[4]);
13601 manish.sha 130
				currentFAInventoryMap.put(itemId, available);
13584 manish.sha 131
				i++;
132
			}
133
 
134
    	} catch(Exception e){
135
			e.printStackTrace();
136
		}
137
    	return currentFAInventoryMap;
138
    }
139
 
140
    public static Map<Long, Double> getCurrentFAInventorySellingPrice(){
141
    	Map<Long,Double> currentFAInventorySellingPriceMap = new HashMap<Long, Double>();
142
    	try {
143
    		int i=1;
13586 manish.sha 144
			CSVReader csvInventoryReader = new CSVReader(new FileReader("/home/FAInventory.csv"));
13584 manish.sha 145
			String[] values;
146
			while ((values = csvInventoryReader.readNext()) != null) {
147
				if(i==1){
148
					i++;
149
					continue;
150
				}
151
				long itemId = Long.parseLong(values[0]);
152
				double sellingPrice = Double.parseDouble(values[2]);
153
				currentFAInventorySellingPriceMap.put(itemId, sellingPrice);
154
				i++;
155
			}
156
 
157
    	} catch(Exception e){
158
			e.printStackTrace();
159
		}
160
    	return currentFAInventorySellingPriceMap;
161
    }
162
}