| 13755 |
kshitij.so |
1 |
import pymongo
|
|
|
2 |
from elixir import *
|
|
|
3 |
from shop2020.model.v1.catalog.impl import DataService
|
|
|
4 |
from shop2020.model.v1.catalog.impl.DataService import Item
|
|
|
5 |
from shop2020.clients.InventoryClient import InventoryClient
|
|
|
6 |
from dtr.utils.utils import to_java_date
|
|
|
7 |
from datetime import datetime, timedelta
|
|
|
8 |
import time
|
|
|
9 |
|
|
|
10 |
DataService.initialize(db_hostname='localhost')
|
|
|
11 |
|
|
|
12 |
con = None
|
|
|
13 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4}
|
|
|
14 |
DISCOUNT_TYPE = {'MRP':1,'DP':2}
|
|
|
15 |
LATEST_UPDATED_ITEMS = []
|
|
|
16 |
|
|
|
17 |
now = datetime.now()
|
|
|
18 |
|
|
|
19 |
class __SkuInfo:
|
|
|
20 |
|
|
|
21 |
def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \
|
|
|
22 |
maxDiscount, discountType, dp, nlcPoints, bestSellerPoints, totalPoints):
|
|
|
23 |
self._id = _id
|
|
|
24 |
self.skuBundleId = skuBundleId
|
|
|
25 |
self.category_id = category_id
|
|
|
26 |
self.mrp = mrp
|
|
|
27 |
self.available_price = available_price
|
|
|
28 |
self.source_id = source_id
|
|
|
29 |
self.rank = rank
|
|
|
30 |
self.maxNlc = maxNlc
|
|
|
31 |
self.minNlc = minNlc
|
|
|
32 |
self.schemeAmount = schemeAmount
|
|
|
33 |
self.minDiscount = minDiscount
|
|
|
34 |
self.maxDiscount = maxDiscount
|
|
|
35 |
self.discountType = discountType
|
|
|
36 |
self.dp = dp
|
|
|
37 |
self.nlcPoints = nlcPoints
|
|
|
38 |
self.bestSellerPoints = bestSellerPoints
|
|
|
39 |
self.totalPoints = totalPoints
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
43 |
global con
|
|
|
44 |
if con is None:
|
|
|
45 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
46 |
try:
|
|
|
47 |
con = pymongo.MongoClient(host, port)
|
|
|
48 |
except Exception, e:
|
|
|
49 |
print e
|
|
|
50 |
return None
|
|
|
51 |
return con
|
|
|
52 |
|
|
|
53 |
def populateStuff():
|
|
|
54 |
print "Inside populate"
|
|
|
55 |
global LATEST_UPDATED_ITEMS
|
|
|
56 |
"""Fetch latest updated items across portals
|
|
|
57 |
and calculate max and min R-Nlc"""
|
|
|
58 |
offset= 0
|
|
|
59 |
while(True):
|
|
|
60 |
print "Fetching records offset %d and limit %d" %(offset,300)
|
|
|
61 |
topSkus = list(get_mongo_connection().Catalog.MasterData.find( {"$and":[{'updatedOn': { "$gt": to_java_date(now - timedelta(hours=4))} }, { 'source_id' : { "$in": SOURCE_MAP.values() } }] }).skip(offset).limit(300))
|
|
|
62 |
if len((topSkus)) == 0:
|
|
|
63 |
break
|
|
|
64 |
#topSkus = collection.find( {'_id':664})
|
|
|
65 |
for sku in topSkus:
|
|
|
66 |
info = __SkuInfo(sku['_id'], sku['skuBundleId'], sku['category_id'], sku['mrp'], sku['available_price'], sku['source_id'], sku['rank'], None, None, 0.0, None, \
|
|
|
67 |
None, None, None, None, None, None)
|
|
|
68 |
exceptionalNlc = list(get_mongo_connection().Catalog.ExceptionalNlc.find( {"$and" : [ {'sku':info._id}, {'overrideNlc':1} ]} ))
|
|
|
69 |
if len(exceptionalNlc) > 0:
|
|
|
70 |
"""Exceptional nlc found, no need to calculate max and min R-nlc"""
|
|
|
71 |
info.maxNlc = exceptionalNlc[0]['maxNlc']
|
|
|
72 |
info.minNlc = exceptionalNlc[0]['minNlc']
|
|
|
73 |
LATEST_UPDATED_ITEMS.append(info)
|
|
|
74 |
continue
|
|
|
75 |
|
|
|
76 |
skuSchemeDetails = list(get_mongo_connection().Catalog.SkuSchemeDetails.find( {'sku':info._id}).sort([('addedOn',pymongo.DESCENDING)]).limit(1))
|
|
|
77 |
if len(skuSchemeDetails) > 0:
|
|
|
78 |
"""Sku scheme details, populate scheme amount (Recently added)"""
|
|
|
79 |
info.schemeAmount = float(skuSchemeDetails[0]['schemeAmount'])
|
|
|
80 |
|
|
|
81 |
skuDealerPrices = list(get_mongo_connection().Catalog.SkuDealerPrices.find( {'sku':info._id} ) )
|
|
|
82 |
if len(skuDealerPrices) > 0:
|
|
|
83 |
info.dp = skuDealerPrices[0]['dp']
|
|
|
84 |
skuDiscount = list(get_mongo_connection().Catalog.SkuDiscountInfo.find( {'sku':info._id} ) )
|
|
|
85 |
if len(skuDiscount) > 0:
|
|
|
86 |
"""Sku rule found, populate max , min Discount and discount type"""
|
|
|
87 |
info.maxDiscount = skuDiscount[0]['max_discount']
|
|
|
88 |
info.minDiscount = skuDiscount[0]['min_discount']
|
|
|
89 |
info.discountType = DISCOUNT_TYPE.get(skuDiscount[0]['discountType'].upper())
|
|
|
90 |
LATEST_UPDATED_ITEMS.append(info)
|
|
|
91 |
continue
|
|
|
92 |
|
|
|
93 |
categoryDiscount = list(get_mongo_connection().Dtr.CategoryDiscount.find( {"$and" : [{'brand':sku['brand'].upper()}, {'category_id':sku['category_id']} ]} ))
|
|
|
94 |
if len(categoryDiscount) > 0:
|
|
|
95 |
info.maxDiscount = categoryDiscount[0]['max_discount']
|
|
|
96 |
info.minDiscount = categoryDiscount[0]['min_discount']
|
|
|
97 |
info.discountType = DISCOUNT_TYPE.get(categoryDiscount[0]['discountType'].upper())
|
|
|
98 |
|
|
|
99 |
LATEST_UPDATED_ITEMS.append(info)
|
|
|
100 |
offset = offset + 300
|
|
|
101 |
for lol in LATEST_UPDATED_ITEMS:
|
|
|
102 |
print lol.__dict__
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
def calculateNlc():
|
|
|
106 |
global LATEST_UPDATED_ITEMS
|
|
|
107 |
populated = 0
|
|
|
108 |
while(populated <= len(LATEST_UPDATED_ITEMS)):
|
|
|
109 |
inventory_client = InventoryClient().get_client()
|
|
|
110 |
for obj in LATEST_UPDATED_ITEMS[populated:300+populated]:
|
|
|
111 |
if obj.maxNlc > 0 and obj.minNlc > 0:
|
|
|
112 |
continue
|
|
|
113 |
saholic_sku = list(get_mongo_connection().Catalog.MasterData.find( {"$and":[{'skuBundleId': obj.skuBundleId}, { 'source_id' : SOURCE_MAP.get('SAHOLIC')}] }))
|
|
|
114 |
identifier = None
|
|
|
115 |
if len(saholic_sku) > 0:
|
|
|
116 |
identifier = saholic_sku[0]['identifier']
|
|
|
117 |
if obj.discountType == DISCOUNT_TYPE.get('MRP'):
|
|
|
118 |
if obj.mrp == 0:
|
|
|
119 |
"""Now mrp is zero, so we have to use saholic MRP"""
|
|
|
120 |
if identifier is not None:
|
|
|
121 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
|
|
122 |
obj.mrp = it.mrp
|
|
|
123 |
if obj.mrp > 0:
|
|
|
124 |
obj.minNlc = obj.mrp - (obj.mrp * obj.maxDiscount/100) - obj.schemeAmount
|
|
|
125 |
obj.maxNlc = obj.mrp - (obj.mrp * obj.minDiscount/100) - obj.schemeAmount
|
|
|
126 |
elif obj.discountType == DISCOUNT_TYPE.get('DP'):
|
|
|
127 |
if obj.dp == 0:
|
|
|
128 |
"""Now dp is zero, so we have to use saholic minimum dp for item"""
|
|
|
129 |
if identifier is not None:
|
|
|
130 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
|
|
131 |
try:
|
|
|
132 |
vendorPricing = inventory_client.getAllItemPricing(it.id)
|
|
|
133 |
min_dp = min(pricing.dealerPrice for pricing in vendorPricing)
|
|
|
134 |
obj.dp = min_dp
|
|
|
135 |
except:
|
|
|
136 |
pass
|
|
|
137 |
if obj.dp > 0:
|
|
|
138 |
obj.minNlc = obj.dp - (obj.dp * obj.maxDiscount/100) - obj.schemeAmount
|
|
|
139 |
obj.maxNlc = obj.dp - (obj.dp * obj.minDiscount/100) - obj.schemeAmount
|
|
|
140 |
else:
|
|
|
141 |
"""No rule found, use saholic min nlc as max and min R-Nlc"""
|
|
|
142 |
if identifier is not None:
|
|
|
143 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
|
|
144 |
try:
|
|
|
145 |
vendorPricing = inventory_client.getAllItemPricing(it.id)
|
|
|
146 |
min_nlc = min(pricing.nlc for pricing in vendorPricing)
|
|
|
147 |
obj.maxNlc = min_nlc
|
|
|
148 |
obj.minNlc = min_nlc
|
|
|
149 |
except:
|
|
|
150 |
pass
|
|
|
151 |
populated = populated + 300
|
|
|
152 |
time.sleep(10)
|
|
|
153 |
|
|
|
154 |
def calculateNlcPoints():
|
|
|
155 |
global LATEST_UPDATED_ITEMS
|
|
|
156 |
for sku in LATEST_UPDATED_ITEMS:
|
|
|
157 |
if sku.maxNlc and sku.minNlc:
|
|
|
158 |
discFromMinNlc = (sku.minNlc - sku.available_price)/sku.available_price *100
|
|
|
159 |
discFromMaxNlc = (sku.maxNlc - sku.available_price)/sku.available_price *100
|
|
|
160 |
if discFromMinNlc > 0:
|
|
|
161 |
nlcPoints = 100/5 * discFromMinNlc
|
|
|
162 |
elif discFromMinNlc < 0 and discFromMaxNlc > 0:
|
|
|
163 |
nlcPoints = 0
|
|
|
164 |
else:
|
|
|
165 |
nlcPoints = 100/5 * discFromMinNlc
|
|
|
166 |
sku.nlcPoints = min(nlcPoints,300)
|
|
|
167 |
else:
|
|
|
168 |
sku.nlcPoints = 0
|
|
|
169 |
|
|
|
170 |
def commitData():
|
|
|
171 |
global LATEST_UPDATED_ITEMS
|
|
|
172 |
for sku in LATEST_UPDATED_ITEMS:
|
|
|
173 |
#get_mongo_connection().Catalog.Deals.update({'_id':sku._id},{'$set' : sku.__dict__},upsert=True,multi=True)
|
|
|
174 |
get_mongo_connection().Catalog.Deals.update({'_id':sku._id},sku.__dict__,upsert=True)
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
def addBestSellerPoints():
|
|
|
178 |
allItems = list(get_mongo_connection().Catalog.Deals.find({}))
|
|
|
179 |
for sku in allItems:
|
|
|
180 |
bestSellerPoints = list(get_mongo_connection().Catalog.BestSellerPoints.find( {"$and":[{'min_rank': { "$lte": sku['rank'] } }, {'max_rank': { "$gte": sku['rank'] } } , { 'category_id' : sku['category_id'] }] } ))
|
|
|
181 |
if len(bestSellerPoints) > 0:
|
|
|
182 |
sku['bestSellerPoints'] = bestSellerPoints[0]['points']
|
|
|
183 |
else:
|
|
|
184 |
sku['bestSellerPoints'] = -50
|
|
|
185 |
sku['totalPoints'] = sku['bestSellerPoints'] + sku['nlcPoints']
|
|
|
186 |
get_mongo_connection().Catalog.Deals.update({'_id':sku['_id']},{'$set':{'bestSellerPoints':sku['bestSellerPoints'],'totalPoints':sku['totalPoints']}},multi=False)
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
def main():
|
|
|
190 |
populateStuff()
|
|
|
191 |
calculateNlc()
|
|
|
192 |
calculateNlcPoints()
|
|
|
193 |
commitData()
|
|
|
194 |
addBestSellerPoints()
|
|
|
195 |
|
|
|
196 |
if __name__=='__main__':
|
|
|
197 |
main()
|