# -*- coding: utf-8 -*-

from django.db import models
from gm_types.doris import CARD_TYPE
import traceback
import logging


class TouTiaoTag(models.Model):
    class Meta:
        verbose_name = u'tag标签映射'
        app_label = u'api'

    toutiao = models.CharField(u'源标签', max_length=40, unique=True)
    is_online = models.BooleanField(u'是否在线', default=True)
    is_related = models.BooleanField(u'是否关联', default=False)
    create_time = models.DateTimeField(u'创建时间', auto_now_add=True)
    count = models.IntegerField('关联标签数量', default=0)


class ToutiaoRelation(models.Model):
    class Meta:
        verbose_name = u'趣头条关系表'
        app_label = u'api'

    toutiaotag = models.ForeignKey(TouTiaoTag, verbose_name=u'头条', related_name='relationtags')
    tag = models.CharField(u'关联tag', max_length=40, null=True)


class ContentKeywordExtraction(models.Model):
    class Meta:
        verbose_name = u'不同内容的医美关键词'
        app_label = u'doris'
        db_table = "content_keyword_extraction"

    id = models.TextField(verbose_name="卡片ID")
    keywords = models.TextField(verbose_name="关键词信息", default=None)
    star_keywords = models.TextField(verbose_name="明星关键词信息", default=None)
    title_keywords = models.TextField(verbose_name="标题关键词信息", default=None)
    content_type = models.TextField(verbose_name="类型")
    time = models.TextField(verbose_name="更新时间")


def by_contentz_type_id_get_keywords(id, content_type):
    try:
        all_keyword = []
        data = ContentKeywordExtraction.objects.filter(id=str(id), content_type=content_type).values_list("keywords",
                                                                                                          flat=True).first()
        if data:
            all_keyword = (data.split(","))
        return all_keyword
    except:
        logging.error("catch exception,err_msg:%s" % traceback.format_exc())
        return []


def get_content_star_keywords(id, content_type):
    try:
        keywords = []
        data = ContentKeywordExtraction.objects.filter(id=str(id), content_type=content_type).values_list("star_keywords", flat=True).first()
        if data:
            keywords = (data.split(","))
        return keywords
    except:
        logging.error("catch exception,err_msg:%s" % traceback.format_exc())
        return []


def get_content_star_first_keyword(id, content_type):
    keywords = get_content_star_keywords(id, content_type)
    res = ""
    if len(keywords) > 0:
        res = keywords[0]
    return res


def get_content_title_keywords(id, content_type):
    try:
        keywords = []
        data = ContentKeywordExtraction.objects.filter(id=str(id), content_type=content_type).values_list("title_keywords", flat=True).first()
        if data:
            keywords = (data.split(","))
        return keywords
    except:
        logging.error("catch exception,err_msg:%s" % traceback.format_exc())
        return []
