Rev 15800 | Rev 15802 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#brandAndPriceSql = "select b.brand, GROUP_CONCAT(distinct b.user_id), p.min_price, p.max_price, p.category_id, concat(p.min_price, '-', p.max_price) pricerange, 'BPP' as SubCategory, 'CB' as Main_Category from brand_preferences b join price_preferences p on b.user_id= p.user_id where b.status='show' and trim(coalesce(b.brand, '')) <>'' group by b.brand, pricerange, p.category_id"#clickAndPriceSql = "select b.brand, GROUP_CONCAT(distinct b.user_id), p.min_price, p.max_price, p.category_id, concat(p.min_price, '-', p.max_price) pricerange, 'CPP' as SubCategory, 'CB' as Main_Category from clicks b join price_preferences p on b.user_id= p.user_id where trim(coalesce(b.brand, '')) <>'' group by b.brand, pricerange, p.category_id"import osimport reimport smtplibimport csvimport MySQLdbfrom dtr.storage import DataServicefrom dtr.storage.DataService import MerchantSubOrdersfrom elixir import *from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFoundfrom sqlalchemy.sql import funcfrom sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \betweenimport datetimefrom pymongo.mongo_client import MongoClientdb = MySQLdb.connect('localhost',"root","shop2020","dtr" )cursor = db.cursor()DataService.initialize(db_hostname="localhost")client = MongoClient('mongodb://localhost:27017/')def to_py_date(java_timestamp):date = datetime.datetime.fromtimestamp(java_timestamp)return dateclass MerchantSubOrder():orderId = NonemerchantOrderId = NonemerchantSubOrderId = NonestoreId = NoneuserId = NoneproductCode = Nonebrand = NoneproductName = NoneamountPaid = Nonequantity = NoneunitPrice = Nonestatus = NonecreatedTime = Nonedef __init__(self,orderId, merchantOrderId, merchantSubOrderId, storeId, userId, productCode, productName, amountPaid, quantity,unitPrice, status, createdTime):self.orderId = orderIdself.merchantOrderId = merchantOrderIdself.merchantSubOrderId = merchantSubOrderIdself.storeId = storeIdself.userId = userIdself.productCode = productCodeself.productName= productNameself.amountPaid = amountPaidself.quantity = quantityself.unitPrice = unitPriceself.status = statusif createdTime is not None:self.createdTime = to_py_date(createdTime)else:self.createdTime = Noneself.userId = userIddef getAndStoreMerchantSubOrders():db = client.Dtrcollection = db.merchantOrderorders = collection.find()catalogdb = client.CatalogmasterDataCollection = catalogdb.MasterDatafor order in orders:if order.has_key('subOrders'):orderId = order['orderId']merchantOrderId = order['merchantOrderId']storeId = order['storeId']userId = order['userId']createdOnInt = Noneif order.has_key('createdOnInt'):createdOnInt = order['createdOnInt']subOrders = order['subOrders']for subOrder in subOrders:unitPrice = 0if subOrder.has_key('unitPrice'):unitPrice = subOrder['unitPrice']merchantSubOrder = MerchantSubOrder(orderId, merchantOrderId, subOrder['merchantSubOrderId'], storeId, userId, subOrder['productCode'], subOrder['productTitle'], subOrder['amountPaid'], subOrder['quantity'], unitPrice, subOrder['status'], createdOnInt)product = Noneif storeId in (1,2,4,5):product = list(masterDataCollection.find({'identifier':subOrder['productCode'].strip(), 'source_id':storeId}))elif storeId == 3:product = list(masterDataCollection.find({'secondaryIdentifier':subOrder['productCode'].strip(), 'source_id':storeId}))if product is not None and len(product)>0:merchantSubOrder.brand = product[0]["brand"]else:merchantSubOrder.brand = 'NA'existingMerchantSubOrder = MerchantSubOrders.query.filter(MerchantSubOrders.orderId == orderId).filter(MerchantSubOrders.merchantOrderId == merchantOrderId).filter(MerchantSubOrders.merchantSubOrderId == merchantSubOrder.merchantSubOrderId).first()if existingMerchantSubOrder is None:merchantSubOrderDetail = MerchantSubOrders()merchantSubOrderDetail.orderId = merchantSubOrder.orderIdmerchantSubOrderDetail.merchantOrderId = merchantSubOrder.merchantOrderIdmerchantSubOrderDetail.merchantSubOrderId = merchantSubOrder.merchantSubOrderIdmerchantSubOrderDetail.storeId = merchantSubOrder.storeIdmerchantSubOrderDetail.userId = merchantSubOrder.userIdmerchantSubOrderDetail.productCode = merchantSubOrder.productCodemerchantSubOrderDetail.brand = merchantSubOrder.brandmerchantSubOrderDetail.productName = merchantSubOrder.productNamemerchantSubOrderDetail.amountPaid = merchantSubOrder.amountPaidmerchantSubOrderDetail.quantity = merchantSubOrder.quantitymerchantSubOrderDetail.unitPrice = merchantSubOrder.unitPricemerchantSubOrderDetail.status = merchantSubOrder.statusmerchantSubOrderDetail.createdTime = merchantSubOrder.createdTimesession.commit()#userGroupSegmentationQuery ="select x.brand, GROUP_CONCAT(distinct x.userids) userids, x.min_price, x.max_price, x.pricerange, x.category_id from (select b.brand, GROUP_CONCAT(distinct b.user_id) userids, p.min_price, p.max_price, p.category_id, concat(p.min_price, '-', p.max_price) pricerange from brand_preferences b join price_preferences p on b.user_id= p.user_id where b.status='show' and trim(coalesce(b.brand, '')) <>'' group by b.brand, pricerange, p.category_id UNION select b.brand, GROUP_CONCAT(distinct b.user_id) userids, p.min_price, p.max_price, p.category_id, concat(p.min_price, '-', p.max_price) pricerange from clicks b join price_preferences p on b.user_id= p.user_id where trim(coalesce(b.brand, '')) <>'' group by b.brand, pricerange, p.category_id UNION select b.brand, GROUP_CONCAT(distinct s.user_id) userids, p.min_price, p.max_price, p.category_id, concat(p.min_price, '-', p.max_price) pricerange from search_terms s join (select distinct brand from brand_preferences where trim(coalesce(brand, '')) <>'') b on lower(s.search_term) like concat('%', lower(b.brand), '%') join price_preferences p on s.user_id= p.user_id group by b.brand, pricerange, p.category_id) as x group by x.brand, x.pricerange, x.category_id"def main():getAndStoreMerchantSubOrders()db.close()if session.is_active:print "session is active. closing it."session.close()client.close()if __name__=='__main__':main()