Subversion Repositories SmartDukaan

Rev

Rev 13120 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.amazonaws.mws.samples;

import in.shop2020.model.v1.order.AmazonFCWarehouseLocation;
import in.shop2020.model.v1.order.AmazonFbaOrderItem;
import in.shop2020.model.v1.order.AmazonFbaSalesSnapshot;
import in.shop2020.model.v1.order.AmazonHourlySaleSnapshot;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.thrift.TException;
import org.json.JSONException;

import com.google.gson.Gson;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.util.JSON;


public class Consumer 
{
    private static MongoClient mongo;
    private static final String AMAZON_SALES_SNAPSHOT = "AmazonSaleSnapshot";
    private static final String AMAZON_DAILY_SALE_SNAPSHOT = "AmazonDailySaleSnapshot";
    private static final String AMAZON_HOURLY_SALE_SNAPSHOT = "AmazonHourlySaleSnapshot";
    private static final String AMAZON_SALE_DUMP = "AmazonSaleData";


    static {
        try {
            mongo = new MongoClient( "localhost" , 27017 );
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    public static AmazonFbaSalesSnapshot getAmazonFbaSalesLatestSnapshotForItemLocationWise(long item_id, long fcLocation){
        AmazonFbaSalesSnapshot salesObj = new AmazonFbaSalesSnapshot();
        DB db = mongo.getDB(AMAZON_SALES_SNAPSHOT);
        DBCollection collection = db.getCollection(AMAZON_DAILY_SALE_SNAPSHOT);
        BasicDBObject andQuery = new BasicDBObject();
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
        obj.add(new BasicDBObject("item_id", item_id));
        obj.add(new BasicDBObject("fcLocation", AmazonFCWarehouseLocation.findByValue((int) fcLocation).toString()));
        Calendar cal = Calendar.getInstance();
        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);
        obj.add(new BasicDBObject("dateOfSale", cal.getTimeInMillis()));
        andQuery.put("$and", obj);
        BasicDBObject orderBy = new BasicDBObject();
        orderBy.put("dateOfSale", -1);
        DBCursor cursor = collection.find(andQuery).sort(orderBy).limit(1);
        Gson gson = new Gson();
        while (cursor.hasNext()) {
            salesObj = gson.fromJson(cursor.next().toString(), AmazonFbaSalesSnapshot.class);
        }
        return salesObj;

    }
    
    public static AmazonFbaSalesSnapshot getAmazonFbaSalesLatestSnapshotForItem(long item_id){
        AmazonFbaSalesSnapshot salesObj = new AmazonFbaSalesSnapshot();
        DB db = mongo.getDB(AMAZON_SALES_SNAPSHOT);
        DBCollection collection = db.getCollection(AMAZON_DAILY_SALE_SNAPSHOT);
        BasicDBObject andQuery = new BasicDBObject();
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
        obj.add(new BasicDBObject("item_id", item_id));
        Calendar cal = Calendar.getInstance();
        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);
        obj.add(new BasicDBObject("dateOfSale", cal.getTimeInMillis()));
        andQuery.put("$and", obj);
        BasicDBObject orderBy = new BasicDBObject();
        orderBy.put("dateOfSale", -1);
        DBCursor cursor = collection.find(andQuery).sort(orderBy).limit(1);
        Gson gson = new Gson();
        while (cursor.hasNext()) {
            salesObj = gson.fromJson(cursor.next().toString(), AmazonFbaSalesSnapshot.class);
        }
        return salesObj;

    }
    
    public static List<AmazonFbaSalesSnapshot> getAmazonFbaSalesSnapshotForDays(int interval){
        DB db = mongo.getDB(AMAZON_SALES_SNAPSHOT);
        DBCollection collection = db.getCollection(AMAZON_DAILY_SALE_SNAPSHOT);
        Calendar cal = Calendar.getInstance();
        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);
        long toDate = cal.getTimeInMillis();
        cal.add(Calendar.DATE, -interval);
        long fromDate = cal.getTimeInMillis();
        BasicDBObject query = new BasicDBObject();
        BasicDBObject obj = new BasicDBObject();
        obj.put("$gte", fromDate);
        obj.put("$lte", toDate);
        query.put("dateOfSale",obj);
        BasicDBObject orderBy = new BasicDBObject();
        orderBy.put("dateOfSale", 1);
        Gson gson = new Gson();
        List<AmazonFbaSalesSnapshot> salesSnapshot = new ArrayList<AmazonFbaSalesSnapshot>();
        DBCursor cursor = collection.find(query).sort(orderBy);
        while (cursor.hasNext()) {
            AmazonFbaSalesSnapshot salesObj = gson.fromJson(cursor.next().toString(), AmazonFbaSalesSnapshot.class);
            salesSnapshot.add(salesObj);
        }
        return salesSnapshot;
    }


    public static boolean bulkAddOrUpdateAmazonFbaSalesSnapshot(List<AmazonFbaSalesSnapshot> sales) throws JSONException{
        DB db = mongo.getDB(AMAZON_SALES_SNAPSHOT);
        DBCollection collection = db.getCollection(AMAZON_DAILY_SALE_SNAPSHOT);
        Gson gs = new Gson();
        for (AmazonFbaSalesSnapshot sale :sales){
            DBObject dbObject = (DBObject) JSON.parse(gs.toJson(sale));
            dbObject.put("_id", generateId(sale.getDateOfSale(), sale.getItem_id(), sale.getFcLocation().getValue()));
            BasicDBObject searchObject = new BasicDBObject();
            searchObject.put("_id",generateId(sale.getDateOfSale(), sale.getItem_id(), sale.getFcLocation().getValue()));
            DBCursor cursor = collection.find(searchObject);
            if (cursor.count() > 0){
                BasicDBObject updateObject = new BasicDBObject();
                updateObject.put("promotionOrderCount",sale.getPromotionOrderCount());
                updateObject.put("totalSale",sale.getTotalSale());
                updateObject.put("promotionSale",sale.getPromotionSale());
                updateObject.put("totalOrderCount",sale.getTotalOrderCount());
                updateObject.put("fcLocation",sale.getFcLocation().toString());
                if (sale.getTotalOrderCount() > 0){
                    updateObject.put("isOutOfStock",false);
                }
                collection.update(searchObject, new BasicDBObject("$set", updateObject));
            }
            else{
                collection.insert(dbObject);
            }
        }
        collection.createIndex((DBObject) JSON.parse("{'dateOfSale':1}"));
        return true;
    }
    
    public static boolean addOrUpdateFbaSales(List<AmazonFbaOrderItem> sales, long startTime, long endTime) throws JSONException{
        DB db = mongo.getDB(AMAZON_SALES_SNAPSHOT);
        DBCollection collection = db.getCollection(AMAZON_SALE_DUMP);
        BasicDBObject query = new BasicDBObject();
        BasicDBObject obj = new BasicDBObject();
        obj.put("$gte", startTime);
        obj.put("$lte", endTime);
        query.put("purchaseDate",obj);
        collection.remove(query);
        System.out.println(query);
        Gson gs = new Gson();
        for (AmazonFbaOrderItem sale :sales){
            DBObject dbObject = (DBObject) JSON.parse(gs.toJson(sale));
            collection.insert(dbObject);
        }
        collection.createIndex((DBObject) JSON.parse("{'purchaseDate':1, 'amazonOrderId':1}"));
        return true;
    }
    
    public static boolean addHourlySnapshot(List<AmazonHourlySaleSnapshot> sales){
        DB db = mongo.getDB(AMAZON_SALES_SNAPSHOT);
        DBCollection collection = db.getCollection(AMAZON_HOURLY_SALE_SNAPSHOT);
        Calendar cal = Calendar.getInstance();
        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);
        long toDate = cal.getTimeInMillis();
        BasicDBObject query = new BasicDBObject("snapshotTime", new BasicDBObject("$lt", toDate));
        collection.remove(query);
        Gson gs = new Gson();
        for (AmazonHourlySaleSnapshot sale :sales){
            DBObject dbObject = (DBObject) JSON.parse(gs.toJson(sale));
            dbObject.put("_id", generateId(sale.getSnapshotTime(), sale.getItem_id(), sale.getFcLocation().getValue()));
            collection.insert(dbObject);
        }
        collection.createIndex((DBObject) JSON.parse("{'snapshotTime':1}"));
        return true;
    }

    private static long generateId(long dateOfSale, long item_id, int warehouseLocation){
        return Long.valueOf(String.valueOf(dateOfSale)+String.valueOf(item_id)+String.valueOf(warehouseLocation));
    }

    public static void main (String[] args) throws JSONException, TException{
        getAmazonFbaSalesLatestSnapshotForItem(174);
        getAmazonFbaSalesSnapshotForDays(0);
    }
}