

__author__ = 'bukun'

import peewee
from torlite.core.base_model import BaseModel
from torlite.model.core_tab import CabMember
from torlite.model.core_tab import CabReply
from torlite.model.core_tab import CabPost
from ext import TabApp



class TabCatalog(BaseModel):
    uid = peewee.IntegerField(null=False, index=True, unique=True, primary_key=True, help_text='', )
    slug = peewee.CharField(null=False, index=True, unique=True, max_length=36, help_text='', )
    name = peewee.CharField(null=False, max_length=255, help_text='', )
    order = peewee.IntegerField()
    post_count = peewee.IntegerField(default=0)
    app_count = peewee.IntegerField(default=0)

# class TabVip(BaseModel):
#     uid = peewee.CharField(max_length=36, null=False, unique=True, help_text='', primary_key=True)
#     user = peewee.ForeignKeyField(CabMember, related_name='vip_user_rel')
#     jifen = peewee.IntegerField()
#     vip_type  = peewee.IntegerField(default=0)  # 0, 1,2,3,4
#     vip_timeout = peewee.IntegerField(default=0)
#     remain_num = peewee.IntegerField(default = 0)
#     last_app_run_time = peewee.IntegerField(default = 0)


class TabApp2Catalog(BaseModel):
    uid = peewee.CharField(null=False, index=True, unique=True, primary_key=True, max_length=36, help_text='', )
    catalog = peewee.ForeignKeyField(TabCatalog, related_name='catalog_id')
    post = peewee.ForeignKeyField(TabApp,  related_name='app_id')
    order = peewee.IntegerField()


class TabLabel(BaseModel):
    uid = peewee.CharField(null=False, index=True, unique=True, primary_key=True, help_text='',  max_length=8)
    name = peewee.CharField(null=False, max_length=255, help_text='', )
    count = peewee.IntegerField()


class TabApp2Label(BaseModel):
    uid = peewee.CharField(null=False, index=True, unique=True, primary_key=True, max_length=36, help_text='', )
    tag = peewee.ForeignKeyField(TabLabel, related_name='tag_app_id')
    app = peewee.ForeignKeyField(TabApp,  related_name='app_tag_id')
    order = peewee.IntegerField()


class TabCollect(BaseModel):
    '''
    用户收藏的算式
    '''
    uid = peewee.CharField(max_length=36, null=False, unique=True, help_text='', primary_key=True)
    app = peewee.ForeignKeyField(TabApp,  related_name='collect_app_rel')
    user = peewee.ForeignKeyField(CabMember, related_name='collect_user_rel')
    timestamp = peewee.IntegerField()

class TabEvaluation(BaseModel):
    '''
    用户评价
    '''
    uid = peewee.CharField(max_length=36, null=False, unique=True, help_text='', primary_key=True)
    app = peewee.ForeignKeyField(TabApp,  related_name='evaluation_app_rel')
    user = peewee.ForeignKeyField(CabMember, related_name='evaluation_user_rel')
    value = peewee.IntegerField() # 用户评价， 1 或 0, 作为计数



class TabUsage(BaseModel):
    uid = peewee.CharField(max_length=36, null=False, unique=True, help_text='', primary_key=True)
    signature = peewee.ForeignKeyField(TabApp, related_name='equa_id')
    user = peewee.ForeignKeyField(CabMember, related_name='user_id')
    count = peewee.IntegerField()
    catalog_id = peewee.IntegerField(null=True)
    timestamp = peewee.IntegerField()

class TabAppRelation(BaseModel):
    '''
    相关应用
    我们认为，相关性，并非是对称操作
    '''
    uid = peewee.CharField(max_length=36, null=False, unique=True, help_text='', primary_key=True)
    app_f = peewee.ForeignKeyField(TabApp, related_name='app_f')
    app_t = peewee.ForeignKeyField(TabApp, related_name='app_t')
    count = peewee.IntegerField()

class TabToolbox(BaseModel):
    uid = peewee.CharField(max_length=36, null=False, unique=True, help_text='', primary_key=True)
    title = peewee.CharField()
    app = peewee.CharField()
    cnt = peewee.CharField()
    user = peewee.ForeignKeyField(CabMember, related_name='user_tbx_id')
    order = peewee.IntegerField()


class TabApp2Reply(BaseModel):
    uid =peewee.CharField(null=False, index=True, unique=True, primary_key=True, max_length=36, help_text='', )
    post_id = peewee.ForeignKeyField(TabApp, related_name='app_post_reply_id')
    reply_id = peewee.ForeignKeyField(CabReply, related_name='app_reply_post_id')
    timestamp = peewee.IntegerField()


class RabPost2App(BaseModel):
    uid = peewee.CharField(max_length=36, null=False, unique=True, help_text='', primary_key=True)
    app_f = peewee.ForeignKeyField(CabPost, related_name='rel_post2app_post')
    app_t = peewee.ForeignKeyField(TabApp, related_name='rel_post2app_app')
    count = peewee.IntegerField()

class RabApp2Post(BaseModel):
    uid = peewee.CharField(max_length=36, null=False, unique=True, help_text='', primary_key=True)
    app_f = peewee.ForeignKeyField(TabApp, related_name='rel_app2post_app')
    app_t = peewee.ForeignKeyField(CabPost, related_name='rel_app2post_post')
    count = peewee.IntegerField()

