#!/usr/bin/python  
 | 
# -*- coding: GBK -*-  
 | 
#  
 | 
##@package E:/GameSVN/TeamTool/PythonScribe/PyModule/Coupon/CouponDB.py  
 | 
# @todo:   
 | 
#  
 | 
# @author: Alee  
 | 
# @date 2017-6-2 ÏÂÎç05:21:48  
 | 
# @version 1.0  
 | 
#  
 | 
# @note:   
 | 
#  
 | 
#---------------------------------------------------------------------  
 | 
#-------------------------------------------------------------------------------  
 | 
#¼ì²é²¢¸üÐÂË÷Òý  
 | 
#-------------------------------------------------------------------------------  
 | 
from lib import ReadConfig  
 | 
from lib import DBController  
 | 
from lib import mylog  
 | 
import os  
 | 
  
 | 
g_dbController = None  
 | 
  
 | 
CouponWXColName = "tagCouponWXSC"   # Î¢ÐÅÉÌ³Ç  
 | 
  
 | 
CouponBatchColName = "tagCouponBatch"  
 | 
CouponCodeColName = "tagCouponCode"  
 | 
ModuleName = "CouponDB"  
 | 
  
 | 
Def_ConfigPath = os.getcwd()  
 | 
  
 | 
  
 | 
def InitDB():  
 | 
    global g_dbController  
 | 
      
 | 
    if not g_dbController:  
 | 
        IP = ReadConfig.ReadConfig(Def_ConfigPath + "\\config.ini").GetValue("MongoDB", "IP")  
 | 
        Port = ReadConfig.ReadConfig(Def_ConfigPath + "\\config.ini").GetInt("MongoDB", "Port")  
 | 
        User = ReadConfig.ReadConfig(Def_ConfigPath + "\\config.ini").GetValue("MongoDB", "User")  
 | 
        Pwd = ReadConfig.ReadConfig(Def_ConfigPath + "\\config.ini").GetValue("MongoDB", "Pwd")  
 | 
        DBName = ReadConfig.ReadConfig(Def_ConfigPath + "\\config.ini").GetValue("MongoDB", "DBName")  
 | 
        g_dbController = DBController.DBController(IP, Port, DBName, User, Pwd, None)  
 | 
      
 | 
        if g_dbController.connected:  
 | 
            #Ö»ÔÚÁ´½ÓDB¼ì²éÒ»´Î  
 | 
            result = CheckAndUpdateIndexOnDb(g_dbController.db)  
 | 
            if not result:  
 | 
                mylog.debug('Ë÷ÒýÓÐÎÊÌâ')  
 | 
                return False  
 | 
        mylog.debug('»ñȡеÄÊý¾Ý¿âÁ´½Ó')  
 | 
          
 | 
    if not g_dbController.connected:  
 | 
        mylog.debug('ÎÞ·¨Á´½ÓÊý¾Ý¿â')  
 | 
        return False  
 | 
      
 | 
    return True  
 | 
  
 | 
def GetDBEventCon():  
 | 
    global g_dbController  
 | 
    if not InitDB():  
 | 
        return None  
 | 
    return g_dbController  
 | 
  
 | 
  
 | 
  
 | 
  
 | 
  
 | 
def ReadIndexInfo():  
 | 
    indexInfoDict = {  
 | 
  
 | 
    CouponBatchColName:  
 | 
    {  
 | 
        'CouponBatch_1':  
 | 
        {  
 | 
            'unique':True,  
 | 
            'key':[('couponid', 1)]    #Ë÷ÒýµÄ×ÖµäºÍµÝÔö(1)»òµÝ¼õ(-1)  
 | 
         }  
 | 
     },  
 | 
  
 | 
    }  
 | 
    return indexInfoDict  
 | 
  
 | 
def CompareIndex(curIndexDict, expectIndexDict):  
 | 
    #±È½Ïunique  
 | 
    curUnique = curIndexDict.get('unique', None)  
 | 
    expectUnique = expectIndexDict.get('unique', None)  
 | 
    if curUnique is None:  
 | 
        if expectUnique is None:  
 | 
            pass  
 | 
        elif expectUnique:  
 | 
#            print '#debug 1'  
 | 
            return False  
 | 
        else:  
 | 
            pass  
 | 
    else:  
 | 
        if expectUnique is None:  
 | 
            if (not curUnique):  
 | 
                return True  
 | 
#            print '#debug 2'  
 | 
            return False  
 | 
        if curUnique and (not expectUnique):  
 | 
#            print '#debug 3'  
 | 
            return False  
 | 
        if (not curUnique) and expectUnique:  
 | 
#            print '#debug 4'  
 | 
            return False  
 | 
    #±È½Ïkeylist  
 | 
    curKeyList = curIndexDict.get('key', None)  
 | 
    expectKeyList = expectIndexDict.get('key', None)  
 | 
    if curKeyList is None:  
 | 
        if expectKeyList is None:  
 | 
            pass  
 | 
        else:  
 | 
#            print '#debug 5'  
 | 
            return False  
 | 
    else:  
 | 
        if expectKeyList is None:  
 | 
#            print '#debug 6'  
 | 
            return False  
 | 
        else:  
 | 
            for pair in curKeyList:  
 | 
                if pair not in expectKeyList:  
 | 
#                    print '#debug 7'  
 | 
                    return False  
 | 
            for pair in expectKeyList:  
 | 
                if pair not in curKeyList:  
 | 
#                    print '#debug 8'  
 | 
                    return False  
 | 
    return True  
 | 
  
 | 
def CheckIndexes(db, colName, expectIndexInfo):  
 | 
    col = db[colName]  
 | 
  
 | 
    '''¼ì²é±íÖÐÊÇ·ñÓжàÓàµÄË÷Òý'''  
 | 
    indexDict = col.index_information()  
 | 
    for k, v in indexDict.items():  
 | 
        if k == '_id_':  
 | 
            continue  
 | 
        if k not in expectIndexInfo:  
 | 
            return False  
 | 
        if not CompareIndex(v, expectIndexInfo[k]):  
 | 
            return False  
 | 
    return True  
 | 
  
 | 
def AddIndexes(db, colName, expectIndexes):  
 | 
    '''Ìí¼ÓË÷Òý'''  
 | 
    col = db[colName]  
 | 
    '''¸ø±íÌí¼ÓË÷Òý'''  
 | 
    curIndexesDict = col.index_information()  
 | 
    for k, v in expectIndexes.items():  
 | 
        if k not in curIndexesDict:  
 | 
            expectIndexInfo = expectIndexes[k]  
 | 
            isUnique = expectIndexInfo.get('unique', False)  
 | 
            try:  
 | 
                col.create_index(expectIndexInfo['key'], name = k, unique = isUnique)  
 | 
            except Exception, e:  
 | 
                return False  
 | 
    return True       
 | 
  
 | 
def CheckAndUpdateIndexOnDb(db):  
 | 
    '''¶ÔÓÚË÷ÒýÎļþÖеÄÿ¸ö±í£¬Êý¾Ý¿âÖиñíÓеÄË÷ÒýÐÅÏ¢£¬Ë÷ÒýÎļþÖбØÐëÓв¢ÇÒÆ¥ÅäuniqueºÍ(key, direction)list'''  
 | 
    indexInfoDict = ReadIndexInfo()  
 | 
    for k, v in indexInfoDict.items():  
 | 
        colName = k  
 | 
        colIndexDict = v  
 | 
        if not CheckIndexes(db, colName, colIndexDict):  
 | 
            return False  
 | 
        if not AddIndexes(db, colName, colIndexDict):  
 | 
            return False  
 | 
    return True  
 | 
  
 |