import logging
from gm_types.mimas import TRACTATE_STATUS

from talos.models.tractate.tractate import Tractate
from talos.services.user import UserService
from data_sync.utils import to_epoch, tzlc
from qa.models.toutiao import by_contentz_type_id_get_keywords, get_content_star_keywords, get_content_title_keywords, get_content_star_first_keyword


def get_tractate(pks):
    tractates = Tractate.objects.filter(id__in=pks)
    data = []
    user_ids = list(set(tractates.values_list("user_id", flat=True)))
    user_infos = UserService.get_users_by_user_ids(user_ids)
    for tractate in tractates:
        item = {}
        user = user_infos.get(tractate.user_id, None)
        user_name = user and user.nickname or ""
        cut_bool = False
        cut_word = [" 医生", " 机构"]
        for i in cut_word:
            if user_name.find(i) >= 0:
                cut_bool = True
        if cut_bool == True:
            user_name = user_name.split()[0]

        item['id'] = tractate.id
        item["user_id"] = tractate.user_id
        item["content"] = tractate.content
        item["content_pre"] = tractate.content
        item["is_online"] = tractate.is_online
        item["status"] = tractate.status
        item["platform"] = tractate.platform
        item["content_level"] = tractate.content_level
        item["is_excellent"] = tractate.is_excellent
        item["create_time"] = tzlc(tractate.create_time)
        item["last_modified"] = tzlc(tractate.last_modified)
        item["audit_time"] = tzlc(tractate.audit_time)
        item["tractate_score"] = tractate.get_tractate_score
        item['good_click'] = tractate.get_goodclick
        item["good_click_tractate_score"] = tractate.get_good_click_tractate_score
        item["tractate_tag_list"] = tractate.get_tag_list
        item["post_time"] = tzlc(tractate.audit_time) if tractate.status == TRACTATE_STATUS.AUDIT_SUCCESS else None
        item["author"] = user_name
        tag_list = tractate.get_tag_list
        item["tractate_tag"] = tractate.get_tag(tag_list)
        item["tractate_tag_name"] = tractate.get_tag_names(tag_list)
        item["is_video"] = tractate.get_is_video
        item["tractate_tag_name_content"] = tractate.get_tag_names_content(tag_list)
        item["hot_score"] = tractate.get_hot_score()
        # 新标签
        fresh_tag_list = tractate.get_fresh_tag_list
        item["fresh_tractate_tag_list"] = fresh_tag_list
        item["fresh_tractate_tag_name"] = tractate.get_fresh_tag_names(fresh_tag_list)
        item["fresh_tractate_tag_name_content"] = tractate.get_tag_names_content(fresh_tag_list)
        item["last_any_reply_time"] = tzlc(tractate.get_tractate_last_any_reply_time())
        item["is_office"] = tractate.get_user_info_office()
        item["content_keyword"] = by_contentz_type_id_get_keywords(id=tractate.id, content_type="usertopic")
        item["content_star_keyword"] = get_content_star_keywords(id=tractate.id, content_type="usertopic")
        item["content_star_first_keyword"] = get_content_star_first_keyword(id=tractate.id, content_type="usertopic")
        # item["title_keyword"] = get_content_title_keywords(id=tractate.id, content_type="usertopic")

        data.append(item)
        logging.info("get data:%s" % data)
    return data
