Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
13111 kshitij.so 1
package com.amazonaws.mws.samples;
2
 
3
import in.shop2020.model.v1.order.AmazonFCWarehouseLocation;
4
import in.shop2020.model.v1.order.AmazonFbaSalesSnapshot;
5
import in.shop2020.model.v1.order.AmazonHourlySaleSnapshot;
6
 
7
import java.net.UnknownHostException;
8
import java.util.ArrayList;
9
import java.util.Calendar;
10
import java.util.List;
11
 
12
import org.apache.thrift.TException;
13
import org.json.JSONException;
14
 
15
import com.google.gson.Gson;
16
import com.mongodb.BasicDBObject;
17
import com.mongodb.DB;
18
import com.mongodb.DBCollection;
19
import com.mongodb.DBCursor;
20
import com.mongodb.DBObject;
21
import com.mongodb.MongoClient;
22
import com.mongodb.util.JSON;
23
 
24
 
25
public class Consumer 
26
{
27
    private static MongoClient mongo;
28
    private static final String AMAZON_SALES_SNAPSHOT = "AmazonSaleSnapshot";
29
    private static final String AMAZON_DAILY_SALE_SNAPSHOT = "AmazonDailySaleSnapshot";
30
    private static final String AMAZON_HOURLY_SALE_SNAPSHOT = "AmazonHourlySaleSnapshot";
31
 
32
 
33
    static {
34
        try {
35
            mongo = new MongoClient( "localhost" , 27017 );
36
        } catch (UnknownHostException e) {
37
            e.printStackTrace();
38
        }
39
    }
40
 
41
    public static AmazonFbaSalesSnapshot getAmazonFbaSalesLatestSnapshotForItemLocationWise(long item_id, long fcLocation){
42
        AmazonFbaSalesSnapshot salesObj = new AmazonFbaSalesSnapshot();
43
        DB db = mongo.getDB(AMAZON_SALES_SNAPSHOT);
44
        DBCollection collection = db.getCollection(AMAZON_DAILY_SALE_SNAPSHOT);
45
        BasicDBObject andQuery = new BasicDBObject();
46
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
47
        obj.add(new BasicDBObject("item_id", item_id));
48
        obj.add(new BasicDBObject("fcLocation", AmazonFCWarehouseLocation.findByValue((int) fcLocation).toString()));
49
        Calendar cal = Calendar.getInstance();
50
        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);
51
        obj.add(new BasicDBObject("dateOfSale", cal.getTimeInMillis()));
52
        andQuery.put("$and", obj);
53
        BasicDBObject orderBy = new BasicDBObject();
54
        orderBy.put("dateOfSale", -1);
55
        DBCursor cursor = collection.find(andQuery).sort(orderBy).limit(1);
56
        Gson gson = new Gson();
57
        while (cursor.hasNext()) {
58
            salesObj = gson.fromJson(cursor.next().toString(), AmazonFbaSalesSnapshot.class);
59
        }
60
        return salesObj;
61
 
62
    }
63
 
64
    public static AmazonFbaSalesSnapshot getAmazonFbaSalesLatestSnapshotForItem(long item_id){
65
        AmazonFbaSalesSnapshot salesObj = new AmazonFbaSalesSnapshot();
66
        DB db = mongo.getDB(AMAZON_SALES_SNAPSHOT);
67
        DBCollection collection = db.getCollection(AMAZON_DAILY_SALE_SNAPSHOT);
68
        BasicDBObject andQuery = new BasicDBObject();
69
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
70
        obj.add(new BasicDBObject("item_id", item_id));
71
        Calendar cal = Calendar.getInstance();
72
        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);
73
        obj.add(new BasicDBObject("dateOfSale", cal.getTimeInMillis()));
74
        andQuery.put("$and", obj);
75
        BasicDBObject orderBy = new BasicDBObject();
76
        orderBy.put("dateOfSale", -1);
77
        DBCursor cursor = collection.find(andQuery).sort(orderBy).limit(1);
78
        Gson gson = new Gson();
79
        while (cursor.hasNext()) {
80
            salesObj = gson.fromJson(cursor.next().toString(), AmazonFbaSalesSnapshot.class);
81
        }
82
        return salesObj;
83
 
84
    }
85
 
86
    public static List<AmazonFbaSalesSnapshot> getAmazonFbaSalesSnapshotForDays(int interval){
87
        DB db = mongo.getDB(AMAZON_SALES_SNAPSHOT);
88
        DBCollection collection = db.getCollection(AMAZON_DAILY_SALE_SNAPSHOT);
89
        Calendar cal = Calendar.getInstance();
90
        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);
91
        long toDate = cal.getTimeInMillis();
92
        cal.add(Calendar.DATE, -interval);
93
        long fromDate = cal.getTimeInMillis();
94
        BasicDBObject query = new BasicDBObject();
95
        BasicDBObject obj = new BasicDBObject();
96
        obj.put("$gte", fromDate);
97
        obj.put("$lte", toDate);
98
        query.put("dateOfSale",obj);
99
        BasicDBObject orderBy = new BasicDBObject();
100
        orderBy.put("dateOfSale", 1);
101
        Gson gson = new Gson();
102
        List<AmazonFbaSalesSnapshot> salesSnapshot = new ArrayList<AmazonFbaSalesSnapshot>();
103
        DBCursor cursor = collection.find(query).sort(orderBy);
104
        while (cursor.hasNext()) {
105
            AmazonFbaSalesSnapshot salesObj = gson.fromJson(cursor.next().toString(), AmazonFbaSalesSnapshot.class);
106
            salesSnapshot.add(salesObj);
107
        }
108
        return salesSnapshot;
109
    }
110
 
111
 
112
    public static boolean bulkAddOrUpdateAmazonFbaSalesSnapshot(List<AmazonFbaSalesSnapshot> sales) throws JSONException{
113
        DB db = mongo.getDB(AMAZON_SALES_SNAPSHOT);
114
        DBCollection collection = db.getCollection(AMAZON_DAILY_SALE_SNAPSHOT);
115
        Gson gs = new Gson();
116
        for (AmazonFbaSalesSnapshot sale :sales){
117
            DBObject dbObject = (DBObject) JSON.parse(gs.toJson(sale));
118
            dbObject.put("_id", generateId(sale.getDateOfSale(), sale.getItem_id(), sale.getFcLocation().getValue()));
119
            BasicDBObject searchObject = new BasicDBObject();
120
            searchObject.put("_id",generateId(sale.getDateOfSale(), sale.getItem_id(), sale.getFcLocation().getValue()));
121
            DBCursor cursor = collection.find(searchObject);
122
            if (cursor.count() > 0){
123
                BasicDBObject updateObject = new BasicDBObject();
124
                updateObject.put("promotionOrderCount",sale.getPromotionOrderCount());
125
                updateObject.put("totalSale",sale.getTotalSale());
126
                updateObject.put("promotionSale",sale.getPromotionSale());
127
                updateObject.put("totalOrderCount",sale.getTotalOrderCount());
128
                updateObject.put("fcLocation",sale.getFcLocation().toString());
129
                if (sale.getTotalOrderCount() > 0){
130
                    updateObject.put("isOutOfStock",false);
131
                }
132
                collection.update(searchObject, new BasicDBObject("$set", updateObject));
133
            }
134
            else{
135
                collection.insert(dbObject);
136
            }
137
        }
138
        collection.createIndex((DBObject) JSON.parse("{'dateOfSale':1}"));
139
        return true;
140
    }
141
 
142
    public static boolean addHourlySnapshot(List<AmazonHourlySaleSnapshot> sales){
143
        DB db = mongo.getDB(AMAZON_SALES_SNAPSHOT);
144
        DBCollection collection = db.getCollection(AMAZON_HOURLY_SALE_SNAPSHOT);
145
        Calendar cal = Calendar.getInstance();
146
        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);
147
        long toDate = cal.getTimeInMillis();
148
        BasicDBObject query = new BasicDBObject("snapshotTime", new BasicDBObject("$lt", toDate));
149
        collection.remove(query);
150
        Gson gs = new Gson();
151
        for (AmazonHourlySaleSnapshot sale :sales){
152
            DBObject dbObject = (DBObject) JSON.parse(gs.toJson(sale));
153
            dbObject.put("_id", generateId(sale.getSnapshotTime(), sale.getItem_id(), sale.getFcLocation().getValue()));
154
            collection.insert(dbObject);
155
        }
156
        collection.createIndex((DBObject) JSON.parse("{'snapshotTime':1}"));
157
        return true;
158
    }
159
 
160
    private static long generateId(long dateOfSale, long item_id, int warehouseLocation){
161
        return Long.valueOf(String.valueOf(dateOfSale)+String.valueOf(item_id)+String.valueOf(warehouseLocation));
162
    }
163
 
164
    public static void main (String[] args) throws JSONException, TException{
13117 kshitij.so 165
        getAmazonFbaSalesLatestSnapshotForItem(174);
166
        getAmazonFbaSalesSnapshotForDays(0);
13111 kshitij.so 167
    }
168
}