'''
Created on 2012-9-15

@author: jezhang
'''
#from ebp.ebputil import getChinaNow
from google.appengine.ext import db


class User(db.Model):
    userid = db.StringProperty()
    passwd = db.StringProperty()
    cre_date = db.DateTimeProperty()
    email = db.StringProperty()
    times = db.IntegerProperty()
    last = db.DateTimeProperty()
    create_ip = db.StringProperty()
    last_login_ip = db.StringProperty()
    
    @staticmethod
    def checkExistUser(userid):
        q = User.all()
        q.filter("userid =", userid)
        user = q.get()
        if user:
            return True
        else:
            return False
    
    def addNew(self):
            self.times = 1            
            self.put()            
        
    def delUser(self):
        q = User.all()
        q.filter("userid =", self.userid)
        user = q.get()
        user.delete()        
    
    @staticmethod    
    def get_login_user(userid, passwd):
        q = User.all()
        q.filter("userid =", userid)
        q.filter("passwd =", passwd)
        user = q.get()
        if user:
            return user
        else:
            return None      

class Config(db.Model):   
    sid = db.StringProperty() 
    type = db.StringProperty()
    name = db.StringProperty()
    value = db.StringProperty()
    create_date = db.DateTimeProperty()
    update_date = db.DateTimeProperty()
    create_by = db.StringProperty()
    update_by = db.StringProperty() 
    create_ip = db.StringProperty()
    update_ip = db.StringProperty()
    
    @staticmethod
    def getEbpServerInfo():
        dictLst = {'userID':'',
                 'passwd':'',
                 'login_url':'',
                 'order_url':''}
        for item in dictLst:
            q = Config.all().filter("name =", item)
            dictLst[item] = q.get().value
        return dictLst
    
    @staticmethod
    def deleteBySid(sid):
        q = Config.all()
        q.filter("sid =", sid)
        for config in q:
            config.delete()
    
    @staticmethod
    def get_config_by_name(name):
        q = Config.all()
        q.filter("name =", name)
        config = q.get()
        return config
    
    @staticmethod
    def get_configs():
        q = Config.all()
        q.order("-create_date")
        return q
    
    
        

class OrderHdr(db.Model):
    token = db.StringProperty()
    times = db.IntegerProperty()
    cre_date = db.DateTimeProperty()
    upd_date = db.DateTimeProperty()
    create_by = db.StringProperty()
    update_by = db.StringProperty()
    create_ip = db.StringProperty()
    update_ip = db.StringProperty()
    count_ordered = db.IntegerProperty()
    sum_ordered = db.FloatProperty()
    
    def addNew(self):
        self.put()
        
    @staticmethod
    def get_order_hdr_orderby_cre_date():
        q = OrderHdr.all()
        q.order("-cre_date")
        return q
    
    @staticmethod
    def get_order_hdr_by_token(token):
        q = OrderHdr.all()
        q.filter("token", token)
        return q
    
            

class OrderDetail(db.Model):
    token = db.StringProperty()
    sn = db.IntegerProperty()
    name = db.StringProperty()
    price = db.FloatProperty()
    qty_assigned = db.IntegerProperty()
    qty_ordered = db.IntegerProperty()
    sum_ordered = db.FloatProperty()
    createdate = db.DateTimeProperty()
    updatedate = db.DateTimeProperty()
    
    def addNew(self):
        self.put()
        
    @staticmethod
    def delete_order_detail_by_token(token):
        q = OrderDetail.all()
        q.filter("token =", token)
        for item in q:
            item.delete() 
    
    @staticmethod
    def get_order_detail_by_token_orderby_sn(token):
        result = OrderDetail.all()
        result.filter("token =", token)
        result.order("sn")
        return result
    
    