tractate.py 17.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from urllib.parse import urljoin

from django.conf import settings
from django.db.models import Q

from gm_dataquery.dataquery import DataBuilder, DataSQLQuery
from gm_dataquery.db import DB
from gm_dataquery.dict_mixin import to_dict
from gm_types.gaia import DOCTOR_TYPE, MEMBERSHIP_LEVEL, USER_CLASSIFY

from gm_types.mimas import (
    TRACTATE_CONTENT_LEVEL,
    TRACTATE_STATUS,
    TRACTATE_PLATFORM,
    SOFT_ARTICLE_RELATION_TYPE,
    SOFT_ARTICLE_TYPE,
)

from talos.models.soft_article import SoftArticleReply
from talos.models.tractate import (
    Tractate,
    TractateExtra,
    TractateTag,
    TractateImages,
    TractateVideo,
    TractateReply,
    TractateCheck,
    TractateNewTag,
    TractateTagV3,
)
from talos.rpc import get_current_rpc_invoker
from talos.services import DoctorService
from talos.models.diary.diary import Diary
from talos.models.soft_article.soft_article import SoftArticle, SoftArticleRelation, SoftArticleImages, SoftArticleVideo
from talos.models.soft_article.check import SoftArticleCheck
from talos.services.agile_tag import AgileTagService
from talos.services.tag import TagService
from talos.services.user import UserService
from talos.services.hospital import HospitalService
from talos.services.goods import GoodsService


rpc_client = get_current_rpc_invoker()


class TractateDB(DataBuilder):
    def getval_tags(self, obj):
        tags = list(TractateTag.objects.filter(
            tractate_id=obj.id).values_list("tag_id", flat=True))
        return tags

    def getval_new_tags(self, obj):
        tags = list(TractateNewTag.objects.filter(
            tractate_id=obj.id).values_list("tag_id", flat=True))
        return tags

    def getval_tags_name(self, obj):
        relation_tags = list(TractateTag.objects.filter(tractate_id=obj.id).values_list("tag_id", flat=True))
        tag_info_list = TagService.get_tags_by_tag_ids(relation_tags)
        return "、".join([tag.name for tag in tag_info_list])

    def getval_new_tags_name(self, obj):
        relation_tags = list(TractateNewTag.objects.filter(tractate_id=obj.id).values_list("tag_id", flat=True))
        tag_info_dic = AgileTagService.get_agile_tags_by_agile_tag_ids(relation_tags)
        return "、".join([tag.name for tag in tag_info_dic.values()])

    def getval_like_num(self, obj):
        return obj.vote_amount

    def getval_favor_num(self, obj):
        return obj.favor_amount

    def getval_reply_num(self, obj):
        return obj.reply_amount

    def getval_fake_reply_num(self, obj):
        return TractateReply.objects.filter(tractate_id=obj.id, is_fake=True).count()

    def getval_images(self, obj):
        return list(TractateImages.objects.filter(tractate_id=obj.id).values_list("image_url", flat=True))

    def getval_video_url(self, obj):
        video_obj = TractateVideo.objects.filter(tractate_id=obj.id, is_online=True).first()
        video_url = ''
        if video_obj:
            _url = video_obj.raw_video_url
            if _url:
                video_url = urljoin(settings.VIDEO_HOST, _url)
        return video_url

    def getval_tractate_id(self, obj):
        return obj.id

    def getval_user_name(self, obj):
        user = UserService.get_user_by_user_id(user_id=obj.user_id)
        return user.nickname

    def getval_is_talent(self, obj):
        user = UserService.get_user_by_user_id(user_id=obj.user_id)
        return True if user.membership_level == MEMBERSHIP_LEVEL.STAR else False

    def getval_user_level(self, obj):
        user = UserService.get_user_by_user_id(user_id=obj.user_id)
        return user.user_rights_level

    def getval_status_desc(self, obj):
        return TRACTATE_STATUS.getDesc(key=obj.status)

    def getval_doctor_type(self, obj):
        doctor = DoctorService.get_doctor_by_user_id(obj.user_id)
        return DOCTOR_TYPE.getDesc(key=doctor.doctor_type)

    def getval_low_quality(self, obj):
        return obj.low_quality

    def getval_low_quality_deal(self, obj):
        return obj.low_quality_deal

    def getval_platform(self, obj):
        return obj.platform

    def getval_tag_v3_ids(self, obj):
        return list(TractateTagV3.objects.filter(tractate_id=obj.id).values_list("tag_v3_id", flat=True))


@DB
class TractateDQ(DataSQLQuery):
    model = Tractate
    data_model = TractateDB
    default_using = getattr(settings, 'SLAVE_DB_NAME', '')

    def filter_user_id(self, srch_key, srch_val, regex=False):
        return Q(user_id=srch_val)

    def filter_last_modified(self, srch_key, srch_val, regex=False):
        return self._qry_time_range(srch_key, srch_val, regex)

    def filter_content_level(self, srch_key, srch_val, regex=False):
        return Q(content_level=srch_val)

    def filter_user_type(self, srch_key, srch_val, regex=False):
        if srch_val == USER_CLASSIFY.NORMAL:
            resp = rpc_client['api/user_classify/ids']().unwrap()
            user_ids = resp.get('user_ids', [])
            resp = rpc_client['api/person/puppet_user']().unwrap()
            user_ids.extend(resp.get('puppet_ids', []))
            return ~Q(user_id__in=user_ids)

        elif srch_val == USER_CLASSIFY.MODEL:
            resp = rpc_client['api/user_classify/ids']().unwrap()
            user_ids = resp.get('user_ids', [])
            return Q(user_id__in=user_ids)
        else:
            return Q()

    def filter_low_quality(self, srch_key, srch_val, regex=False):
        if srch_val == '1':
            return Q(low_quality__gte=1)
        elif srch_val == '0':
            return Q(low_quality=0)

    def filter_low_quality_deal(self, srch_key, srch_val, regex=False):
        if srch_val == '1':
            return Q(low_quality_deal=True)
        elif srch_val == '0':
            return Q(low_quality_deal=False)

    def filter_tags(self, srch_key, srch_val, regex=False):
        tractate_ids = list(TractateTag.objects.filter(
            tag_id=int(srch_val)
        ).values_list('tractate_id', flat=True))
        return Q(id__in=tractate_ids)

    def filter_new_tags(self, srch_key, srch_val, regex=False):
        tractate_ids = list(TractateNewTag.objects.filter(
            tag_id=int(srch_val)
        ).values_list('tractate_id', flat=True))
        return Q(id__in=tractate_ids)

    def filter_tag_v3_ids(self, srch_key, srch_val, regex=False):
        tractate_ids = list(TractateTagV3.objects.filter(
            tag_v3_id=int(srch_val)
        ).values_list('tractate_id', flat=True))
        return Q(id__in=tractate_ids)

    def filter_platform(self, srch_key, srch_val, regex=False):
        return Q(platform=srch_val)

    def filter_tag_type(self, srch_key, srch_val, regex=False):
        # 根据标签类型筛选
        resp = rpc_client['api/tag_v3/by_type'](tag_type=int(srch_val)).unwrap()
        tag_v3_ids = resp.get('tag_v3_ids', [])
        tractate_ids = list(TractateTagV3.objects.using(settings.SLAVE_DB_NAME).filter(
            tag_v3_id__in=tag_v3_ids
        ).values_list('tractate_id', flat=True))

        return Q(id__in=tractate_ids)


class ReplyDB(DataBuilder):

    def getval_action(self, obj):
        tractate = Tractate.objects.get(id=obj.tractate_id)
        reply = TractateReply.objects.filter(replied_id=obj.id, tractate_id=tractate.id, source_id=TRACTATE_PLATFORM.HERA, is_online=True, user_id=tractate.user_id).exists()

        if reply:
            return "已回复"
        else:
            return "回复"

    def getval_reply_id(self, obj):
        return str(obj.id)

    def getval_tractate_user_id(self, obj):
        return Tractate.objects.get(id=obj.tractate_id).user_id

    def getval_tractate_platform(self, obj):
        tractate = Tractate.objects.filter(id=obj.tractate_id, platform=TRACTATE_PLATFORM.HERA).exists()

        if tractate:
            return 1
        else:
            return 0

    def getval_tractate_title(self, obj):
        t = Tractate.objects.get(id=obj.tractate_id)
        return t.title or t.content[:20]

    def getval_replied_user_id(self, obj):
        if obj.replied_id:
            return TractateReply.objects.get(id=obj.replied_id).user_id
        return ''


@DB
class ReplyDQ(DataSQLQuery):
    model = TractateReply
    data_model = ReplyDB

    def filter_create_time(self, srch_key, srch_val, regex=False):
        return self._qry_time_range(srch_key, srch_val, regex)

    def filter_user_type(self, srch_key, srch_val, regex=False):
        if srch_val == USER_CLASSIFY.NORMAL:
            resp = rpc_client['api/user_classify/ids']().unwrap()
            user_ids = resp.get('user_ids', [])
            resp = rpc_client['api/person/puppet_user']().unwrap()
            user_ids.extend(resp.get('puppet_ids', []))
            return ~Q(user_id__in=user_ids)

        elif srch_val == USER_CLASSIFY.MODEL:
            resp = rpc_client['api/user_classify/ids']().unwrap()
            user_ids = resp.get('user_ids', [])
            return Q(user_id__in=user_ids)

        elif srch_val == USER_CLASSIFY.PUPPET:
            resp = rpc_client['api/person/puppet_user']().unwrap()
            puppet_ids = resp.get('puppet_ids', [])
            return Q(user_id__in=puppet_ids)

        else:
            return Q()

    def filter_tractate_id(self, srch_key, srch_val, regex=False):
        tractate = Tractate.objects.using(settings.SLAVE_DB_NAME).filter(
            id=int(srch_val)
        ).first()

        if tractate:
            return Q(tractate_id=tractate.id)
        return Q()

    def update(self, updates, **kwargs):
        user_id = updates.get('tractate_user_id')
        tractate_id = updates.get('tractate_id')
        content = updates.get('reply_content')
        replied_id = kwargs.get('id') or 0
        if not all([user_id, tractate_id, content]):
            return
        init_obj = {
            "user_id": user_id,
            "tractate_id": tractate_id,
            "content": content,
            'replied_id': replied_id,
            "top_id": 0,
            "platform": TRACTATE_PLATFORM.HERA,
        }
        TractateReply.objects.create(**init_obj)

        return super().update(updates, **kwargs)


class TractatecheckDB(DataBuilder):
    def getval_content(self, obj):
        return TRACTATE_CONTENT_LEVEL.getDesc(obj.content_level)

    def getval_audit_status(self, obj):
        return TRACTATE_STATUS.getDesc(obj.status)

    def getval_send_message(self, obj):
        return obj.title + obj.send_message

    def getval_check_time(self, obj):
        return str(obj.check_time).split('.')[0]

    def getval_user_nick_name(self, obj):
        user = UserService.get_user_by_user_id(obj.user_id)
        return user and user.nickname or ""

@DB
class TractatecheckDQ(DataSQLQuery):
    model = TractateCheck
    data_model = TractatecheckDB


class SoftArticleDB(DataBuilder):

    def getval_article_id(self, obj):
        return obj.id

    def getval_service_id(self, obj):
        s_relation = SoftArticleRelation.objects.filter(softarticle_id=obj.id, relation_type=SOFT_ARTICLE_RELATION_TYPE.SERVICE).first()
        return s_relation.relation_id if s_relation else ""

    def getval_diary_id(self, obj):
        s_relation = SoftArticleRelation.objects.filter(softarticle_id=obj.id, relation_type=SOFT_ARTICLE_RELATION_TYPE.DIARY).first()
        return s_relation.relation_id if s_relation else ""

    def getval_diary_name(self, obj):
        s_relation = SoftArticleRelation.objects.filter(softarticle_id=obj.id, relation_type=SOFT_ARTICLE_RELATION_TYPE.DIARY).first()
        diary = Diary.objects.filter(id=s_relation.relation_id).first()
        return diary.title

    def getval_tags_name(self, obj):
        relation_ids = SoftArticleRelation.objects.filter(softarticle_id=obj.id, relation_type=1).values_list('relation_id', flat=True)
        tag_infos = TagService.get_tags_by_tag_ids(ids=relation_ids)
        tag_name = ""
        for tag in tag_infos:
            tag_name += tag.name + ",    "
        return tag_name

    def getval_item_words(self, obj):
        relation_ids = SoftArticleRelation.objects.filter(softarticle_id=obj.id, relation_type=1).values_list('relation_id', flat=True)
        return list(relation_ids)

    def getval_like_num(self, obj):
        article = SoftArticle.objects.filter(id=obj.id).first()
        return article.vote_amount

    def getval_favor_num(self, obj):
        article = SoftArticle.objects.filter(id=obj.id).first()
        return article.favor_amount

    def getval_reply_num(self, obj):
        article = SoftArticle.objects.filter(id=obj.id).first()
        return article.reply_amount

    def getval_last_handle(self, obj):
        article = SoftArticle.objects.filter(id=obj.id).first()
        check_article = SoftArticleCheck.objects.filter(softarticle_id=obj.id).first()
        if check_article and check_article.online_time and check_article.online_time > article.online_time:
            return "运营"
        elif check_article and check_article.online_time and check_article.online_time <= article.online_time:
            return "商家"

        return "商家"

    def getval_hospital(self, obj):
        hospital = HospitalService.get_hospital_from_doctor_id(id=obj.doctor_id)
        return hospital.name if hospital else ""

    def getval_service(self, obj):
        relation = SoftArticleRelation.objects.filter(softarticle_id=obj.id, relation_type=SOFT_ARTICLE_RELATION_TYPE.SERVICE).first()
        service_info = GoodsService.get_service_by_service_id(relation.relation_id)
        return service_info.name if service_info else ""

    def getval_author(self, obj):
        doctor = DoctorService.get_doctor_from_doctor_id(obj.doctor_id)
        return doctor.name if doctor else ""

    def getval_content_level(self, obj):
        return obj.content_level

    def getval_img(self, obj):
        if obj.article_type == SOFT_ARTICLE_TYPE.VIDEO:
            video = SoftArticleVideo.objects.filter(softarticle_id=obj.id).first()
            return video.upload_img_cover
        else:
            img_info = SoftArticleImages.objects.filter(softarticle_id=obj.id).first()
            return img_info.image_url

    def getval_doctor_user_id(self, obj):
        return UserService.get_user_id_by_doctor_id(obj.doctor_id)

    def getval_user_name(self, obj):
        user_id = UserService.get_user_id_by_doctor_id(obj.doctor_id)
        user = UserService.get_user_by_user_id(user_id=user_id)
        return user.nickname

    def getval_is_talent(self, obj):
        user_id = UserService.get_user_id_by_doctor_id(obj.doctor_id)
        user = UserService.get_user_by_user_id(user_id=user_id)
        return True if int(user.membership_level) else False

    def getval_user_level(self, obj):
        user_id = UserService.get_user_id_by_doctor_id(obj.doctor_id)
        user = UserService.get_user_by_user_id(user_id=user_id)
        return user.user_rights_level

    def getval_status_desc(self, obj):
        return TRACTATE_STATUS.getDesc(key=obj.status)

    def getval_check_reason(self, obj):
        s_check = SoftArticleCheck.objects.filter(softarticle_id=obj.id).first()
        return s_check.check_content

    def getval_video_url(self, obj):
        s_video = SoftArticleVideo.objects.filter(softarticle_id=obj.id).first()
        return urljoin(settings.VIDEO_HOST, s_video.water_video_url)

    def getval_doctor_type(self, obj):
        doctor = DoctorService.get_doctor_from_doctor_id(obj.doctor_id)
        return DOCTOR_TYPE.getDesc(key=doctor.doctor_type)

@DB
class SoftArticleDQ(DataSQLQuery):
    model = SoftArticle
    data_model = SoftArticleDB

    def filter_create_time(self, srch_key, srch_val, regex=False):
        return self._qry_time_range(srch_key, srch_val, regex)

    def filter_diary_id(self, srch_key, srch_val, regex=False):
        s_relations = SoftArticleRelation.objects.filter(relation_id=int(srch_val),
                                                         relation_type=SOFT_ARTICLE_RELATION_TYPE.DIARY)\
                                                 .values_list('softarticle_id', flat=True)
        return Q(id__in=list(set(s_relations)))

    def filter_service_id(self, srch_key, srch_val, regex=False):
        s_relations = SoftArticleRelation.objects.filter(relation_id=int(srch_val),
                                                         relation_type=SOFT_ARTICLE_RELATION_TYPE.SERVICE) \
                                                 .values_list('softarticle_id', flat=True)
        return Q(id__in=list(set(s_relations)))

    def filter_doctor_user_id(self, srch_key, srch_val, regex=False):
        doctor_id = DoctorService.get_doctor_by_user_id_v1(user_id=srch_val)
        if not doctor_id:
            return Q()

        return Q(doctor_id=doctor_id)

    def filter_tags_name(self, srch_key, srch_val, regex=False):
        s_relations = SoftArticleRelation.objects.filter(relation_id=int(srch_val),
                                                         relation_type=SOFT_ARTICLE_RELATION_TYPE.TAG) \
            .values_list('softarticle_id', flat=True)
        return Q(id__in=list(set(s_relations)))


class SoftArticleCheckDB(DataBuilder):
    pass


@DB
class SoftArticleCheckDQ(DataSQLQuery):
    model = SoftArticleCheck
    data_model = SoftArticleCheckDB


class SoftArticleReplyDB(DataBuilder):
    def getval_user_name(self, obj):
        user = UserService.get_user_by_user_id(user_id=obj.user_id)
        return user.nickname


@DB
class SoftArticleReplyDQ(DataSQLQuery):
    model = SoftArticleReply
    data_model = SoftArticleReplyDB