# Path: mod/models/pe_models.py
# class PEUser(Base):
#     __tablename__ = 'pe'
#     cardnum = Column(Integer, primary_key=True)
#     count = Column(String(10), nullable=False)
# 
#     def __repr__(self):
#         return '<PE (%s, %d)' % (self.cardnum, self.count)
# 
# Path: mod/models/tice_cache.py
# class TiceCache(Base):
#     __tablename__ = 'tice'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(4096), nullable=False)
#     date = Column(Integer, nullable=False)


from .._config import *
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from tornado.tcpclient import TCPClient
from tornado.ioloop import IOLoop
from BeautifulSoup import BeautifulSoup
from ..models.pe_models import PEUser
from ..models.tice_cache import TiceCache
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm.exc import NoResultFound
from time import time, localtime, strftime,mktime,strptime
from datetime import datetime, timedelta, date
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor
import tornado.web
import tornado.gen
import urllib
import random
import json,socket,base64,traceback
import functools
        self.finish()

        if state == 'success':
            # 更新缓存
            try:
                user = self.db.query(PEUser).filter(
                    PEUser.cardnum == int(cardnum)).one()
                user.count = int(result)
            except NoResultFound:
                user = PEUser(cardnum=int(cardnum), count=int(result))
                self.db.add(user)
            finally:
                self.db.commit()
        self.db.close()
    def get_rank(self, count):
        global last_refresh
        global all_count
        global all_sum
        try:
            if last_refresh + 300 < int(time()):
                last_refresh = int(time())
                db_instance = self.db.execute("select count, count(*) as all_sum from pe group by count").fetchall()
                all_count = {}
                all_sum = 0
                for item in db_instance:
                    all_count[item.count] = int(item.all_sum)
                    all_sum += int(item.all_sum)
            temp_sum = 0
            for i, j in all_count.items():
                if int(i) >= int(count):

# Path: mod/models/pe_models.py
# class PEUser(Base):
#     __tablename__ = 'pe'
#     cardnum = Column(Integer, primary_key=True)
#     count = Column(String(10), nullable=False)
# 
#     def __repr__(self):
#         return '<PE (%s, %d)' % (self.cardnum, self.count)
# 
# Path: mod/models/tice_cache.py
# class TiceCache(Base):
#     __tablename__ = 'tice'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(4096), nullable=False)
#     date = Column(Integer, nullable=False)


# Path: mod/pe/handler.py
from .._config import *
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from tornado.tcpclient import TCPClient
from tornado.ioloop import IOLoop
from BeautifulSoup import BeautifulSoup
from ..models.pe_models import PEUser
from ..models.tice_cache import TiceCache
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm.exc import NoResultFound
from time import time, localtime, strftime,mktime,strptime
from datetime import datetime, timedelta, date
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor
import tornado.web
import tornado.gen
import urllib
import random
import json,socket,base64,traceback
import functools

        self.finish()

        if state == 'success':
            # 更新缓存
            try:
                user = self.db.query(PEUser).filter(
                    PEUser.cardnum == int(cardnum)).one()
                user.count = int(result)
            except NoResultFound:
                user = PEUser(cardnum=int(cardnum), count=int(result))
                self.db.add(user)
            finally:
                self.db.commit()
        self.db.close()
    def get_rank(self, count):
        global last_refresh
        global all_count
        global all_sum
        try:
            if last_refresh + 300 < int(time()):
                last_refresh = int(time())
                db_instance = self.db.execute("select count, count(*) as all_sum from pe group by count").fetchall()
                all_count = {}
                all_sum = 0
                for item in db_instance:
                    all_count[item.count] = int(item.all_sum)
                    all_sum += int(item.all_sum)
            temp_sum = 0
            for i, j in all_count.items():
                if int(i) >= int(count):

                    temp_sum += all_count[i]