Subversion Repositories SmartDukaan

Rev

Rev 15796 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15795 manish.sha 1
#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"
2
#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"
3
import os
4
import re
5
import smtplib
6
import csv
7
import MySQLdb
8
from dtr.storage import DataService
9
from dtr.storage.DataService import MerchantSubOrders
10
from elixir import *
11
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
12
from sqlalchemy.sql import func
13
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \
14
    between
15
import datetime
16
from pymongo.mongo_client import MongoClient
17
 
18
db = MySQLdb.connect('localhost',"root","shop2020","dtr" )
19
cursor = db.cursor()
20
 
21
DataService.initialize(db_hostname="localhost")
22
client = MongoClient('mongodb://localhost:27017/') 
23
 
24
def to_py_date(java_timestamp):
25
    date = datetime.datetime.fromtimestamp(java_timestamp)
26
    return date
27
 
28
class MerchantSubOrder():
29
    orderId = None
30
    merchantOrderId = None
31
    merchantSubOrderId = None
32
    storeId = None
33
    userId = None
34
    productCode = None
35
    brand = None
36
    productName = None
37
    amountPaid = None
38
    quantity = None
39
    unitPrice = None
40
    status = None
41
    createdTime = None
42
 
43
    def __init__(self,orderId, merchantOrderId, merchantSubOrderId, storeId, userId, productCode, productName, amountPaid, quantity,unitPrice, status, createdTime):
44
        self.orderId = orderId
45
        self.merchantOrderId = merchantOrderId
46
        self.merchantSubOrderId = merchantSubOrderId
47
        self.storeId = storeId
48
        self.userId = userId 
49
        self.productCode = productCode
50
        self.productName= productName
51
        self.amountPaid = amountPaid
52
        self.quantity = quantity
53
        self.unitPrice = unitPrice
54
        self.status = status
55
        self.createdTime = to_py_date(createdTime)
56
        self.userId = userId
57
 
58
def getAndStoreMerchantSubOrders():
59
    db = client.Dtr
60
    collection = db.merchantOrder
61
    orders = collection.find()
62
    catalogdb = client.Catalog
63
    masterDataCollection = catalogdb.MasterData
64
    for order in orders:
65
        if order.has_key('subOrders'):
66
            orderId = order['orderId']
67
            merchantOrderId = order['merchantOrderId']
68
            storeId = order['storeId']
69
            userId = order['userId']
70
            subOrders = order['subOrders']
71
            for subOrder in subOrders:
72
                merchantSubOrder = MerchantSubOrder(orderId, merchantOrderId, subOrder['merchantSubOrderId'], storeId, userId, subOrder['productCode'], subOrder['productTitle'], subOrder['amountPaid'], subOrder['quantity'], subOrder['status'], subOrder['createdTime'])
73
                product = None
74
                if storeId in (1,2,4,5):
75
                    product = list(masterDataCollection.find({'identifier':subOrder['productCode'].strip(), 'source_id':storeId}))
76
                elif storeId == 3:
77
                    product = list(masterDataCollection.find({'secondaryIdentifier':subOrder['productCode'].strip(), 'source_id':storeId}))
78
                if product is not None:
79
                    merchantSubOrder.brand = product[0]["brand"]
80
                else:
81
                    merchantSubOrder.brand = 'NA'
82
 
83
                existingMerchantSubOrder = MerchantSubOrders.query.filter(MerchantSubOrders.orderId == orderId).filter(MerchantSubOrders.merchantOrderId == merchantOrderId).filter(MerchantSubOrders.merchantSubOrderId == merchantSubOrder.merchantSubOrderId).first()
84
                if existingMerchantSubOrder is None:
85
                    merchantSubOrderDetail = MerchantSubOrders()
86
                    merchantSubOrderDetail.orderId = merchantSubOrder.orderId
87
                    merchantSubOrderDetail.merchantOrderId = merchantSubOrder.merchantOrderId
88
                    merchantSubOrderDetail.merchantSubOrderId = merchantSubOrder.merchantSubOrderId
89
                    merchantSubOrderDetail.storeId = merchantSubOrder.storeId
90
                    merchantSubOrderDetail.userId = merchantSubOrder.userId
91
                    merchantSubOrderDetail.productCode = merchantSubOrder.productCode
92
                    merchantSubOrderDetail.brand = merchantSubOrder.brand
93
                    merchantSubOrderDetail.productName = merchantSubOrder.productName
94
                    merchantSubOrderDetail.amountPaid = merchantSubOrder.amountPaid
95
                    merchantSubOrderDetail.quantity = merchantSubOrder.quantity
96
                    merchantSubOrderDetail.unitPrice = merchantSubOrder.unitPrice
97
                    merchantSubOrderDetail.status = merchantSubOrder.status
98
                    merchantSubOrderDetail.createdTime = merchantSubOrder.createdTime
99
                    session.commit()          
100
 
101
 
102
#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"
103
 
104
def main():
105
    getAndStoreMerchantSubOrders()
106
    db.close()
107
    if session.is_active:
108
        print "session is active. closing it."
109
        session.close()
110
    client.close()
111
 
112
if __name__=='__main__':
113
    main()