Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.utils;

import in.shop2020.model.v1.order.FlipkartFaSalesSnapshot;

import java.io.FileReader;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import au.com.bytecode.opencsv.CSVReader;

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 FlipkartConsumer{
        private static MongoClient mongo;
    private static final String FLIPKART_SALES_SNAPSHOT = "FlipkartSaleSnapshot";
    private static final String FLIPKART_DAILY_SALE_SNAPSHOT = "FlipkartDailySaleSnapshot";
    
    static {
        try {
            mongo = new MongoClient( "localhost" , 27017 );
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    
    public static boolean bulkAddOrUpdateFlipkartFbaSalesSnapshot(List<FlipkartFaSalesSnapshot> sales) throws JSONException{
        DB db = mongo.getDB(FLIPKART_SALES_SNAPSHOT);
        DBCollection collection = db.getCollection(FLIPKART_DAILY_SALE_SNAPSHOT);
        Gson gs = new Gson();
        for (FlipkartFaSalesSnapshot 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("totalSale",sale.getTotalSale());
                updateObject.put("totalOrderCount",sale.getTotalOrderCount());
                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 List<FlipkartFaSalesSnapshot> getFlipkartFbaSalesSnapshotForDays(int interval){
        DB db = mongo.getDB(FLIPKART_SALES_SNAPSHOT);
        DBCollection collection = db.getCollection(FLIPKART_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);
        System.out.println(fromDate +" "+ toDate);
        BasicDBObject orderBy = new BasicDBObject();
        orderBy.put("dateOfSale", 1);
        Gson gson = new Gson();
        List<FlipkartFaSalesSnapshot> salesSnapshot = new ArrayList<FlipkartFaSalesSnapshot>();
        DBCursor cursor = collection.find(query).sort(orderBy);
        while (cursor.hasNext()) {
                FlipkartFaSalesSnapshot salesObj = gson.fromJson(cursor.next().toString(), FlipkartFaSalesSnapshot.class);
                salesSnapshot.add(salesObj);
        }
        return salesSnapshot;
    }
    
    public static FlipkartFaSalesSnapshot getFlipkartFaSalesLatestSnapshotForItem(long item_id){
        FlipkartFaSalesSnapshot salesObj = new FlipkartFaSalesSnapshot();
        DB db = mongo.getDB(FLIPKART_SALES_SNAPSHOT);
        DBCollection collection = db.getCollection(FLIPKART_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(), FlipkartFaSalesSnapshot.class);
        }
        return salesObj;
    }
    
    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 Map<Long, Long> getCurrentFAInventory(){
        Map<Long,Long> currentFAInventoryMap = new HashMap<Long, Long>();
        try {
                int i=1;
                        CSVReader csvInventoryReader = new CSVReader(new FileReader("/home/FAInventory.csv"));
                        String[] values;
                        while ((values = csvInventoryReader.readNext()) != null) {
                                if(i==1){
                                        i++;
                                        continue;
                                }
                                if(values[values.length-1].equalsIgnoreCase("FA")){
                                        long itemId = Long.parseLong(values[0]);
                                        long available = Long.parseLong(values[4]);
                                        currentFAInventoryMap.put(itemId, available);
                                }
                                i++;
                        }
                        
        } catch(Exception e){
                        e.printStackTrace();
                }
        return currentFAInventoryMap;
    }
    
    public static Map<Long, Double> getCurrentFAInventorySellingPrice(){
        Map<Long,Double> currentFAInventorySellingPriceMap = new HashMap<Long, Double>();
        try {
                int i=1;
                        CSVReader csvInventoryReader = new CSVReader(new FileReader("/home/FAInventory.csv"));
                        String[] values;
                        while ((values = csvInventoryReader.readNext()) != null) {
                                if(i==1){
                                        i++;
                                        continue;
                                }
                                if(values[values.length-1].equalsIgnoreCase("FA")){
                                        long itemId = Long.parseLong(values[0]);
                                        double sellingPrice = Double.parseDouble(values[2]);
                                        currentFAInventorySellingPriceMap.put(itemId, sellingPrice);
                                }
                                i++;
                        }
                        
        } catch(Exception e){
                        e.printStackTrace();
                }
        return currentFAInventorySellingPriceMap;
    }
}