Commit 7e406f02 authored by 郑伟's avatar 郑伟

Merge branch 'mr/develop/73688' into 'master'

gaia get_problem_extra接口迁移

See merge request !64
parents 5fc9e94c 484d41e1
...@@ -2,16 +2,17 @@ ...@@ -2,16 +2,17 @@
from talos.models.topic import Problem, TopicRankingScore from talos.models.topic import Problem, TopicRankingScore
from data_sync.utils import tzlc from data_sync.utils import tzlc
from gm_types.gaia import TOPIC_TYPE from gm_types.gaia import TOPIC_TYPE
from django.conf import settings
from utils.rpc import get_rpc_invoker from utils.rpc import get_rpc_invoker
from django.db import connections, connection
def get_problems(pks): def get_problems(pks):
""" """
:param pks: :param pks:
:return: :return:
""" """
r = get_rpc_invoker() # r = get_rpc_invoker()
problems = Problem.objects.filter(pk__in=pks) problems = Problem.objects.filter(pk__in=pks)
data = [] data = []
...@@ -26,19 +27,147 @@ def get_problems(pks): ...@@ -26,19 +27,147 @@ def get_problems(pks):
}) })
data.append(get_problem(p)) data.append(get_problem(p))
extras = r['api/dbmw/get_problem_extra'](params=params).unwrap() # extras = r['api/dbmw/get_problem_extra'](params=params).unwrap()
assert len(extras) == len(data) extras = get_problem_extra_batch(params=params)
# assert len(extras) == len(data)
for topic in data:
topic['user'].update(extras.get(topic["id"], {}).get("user", {}))
topic.update(extras[topic["id"]])
# for idx, topic in enumerate(data):
# topic['user'].update(extras[idx].pop('user', {}))
# topic.update(extras[idx])
return data
for idx, topic in enumerate(data):
topic['user'].update(extras[idx].pop('user', {}))
topic.update(extras[idx])
def get_problem_extra_batch(params):
data = {}
for param in params:
data[param['pid']] = get_problem_extra(
param['pid'], param['uid'], param['tids'],
param['sid'], param['did'])
return data return data
def get_problem_extra(pid, uid, tids, sid, did):
"""
Get extra topic data.
:param did: 帖子关联的医生
:param sid: 帖子关联的美购
:param tids: 帖子关联的TAG
:param pid: topic id.
:param uid: 帖子关联的用户.
:return:
"""
res = {}
cur = connections[settings.ZHENGXING_DB].cursor()
if sid:
service_sql = "select is_online from api_service where id = {}".format(
sid)
cur.execute(service_sql)
if not cur.fetchone():
res['is_sink'] = True
if did:
doctor_sql = "select is_online from api_doctor where id = '{}'".format(did)
cur.execute(doctor_sql)
if not cur.fetchone():
res['is_sink'] = True
if uid:
user_sql = """ select id, last_name from auth_user where id = {}""".format(uid)
userextra_sql = """ select user_id, city_id from api_userextra where user_id = {}""".format(uid)
doctor_user_sql = """ select id from api_doctor where user_id = {}""".format(uid)
cur.execute(user_sql)
user = cur.fetchone()
cur.execute(userextra_sql)
userextra = cur.fetchone()
is_doctor = False
cur.execute(doctor_user_sql)
if cur.fetchall():
is_doctor = True
user_info = {
"is_doctor": is_doctor,
}
city = userextra[1]
if user:
user_info["id"] = user[0]
user_info["last_name"] = user[1]
if city:
user_info.update(get_area_tag_info(city, cur))
else:
user_info = {}
res['user'] = user_info
res['tags'] = []
res['tag_ids'] = []
res['closure_tag_ids'] = []
if tids:
tag_sql = """
select id, name from api_tag
where id in ({}) and
is_online=true""".format(
','.join([str(tid) for tid in tids]))
cur.execute(tag_sql)
tags = cur.fetchall()
res['tags'] = list(set([pt[1] for pt in tags]))
tag_ids = list(set([pt[0] for pt in tags]))
res['tag_ids'] = tag_ids
closure_tag_ids = tag_ids
# TODO
if tag_ids:
# 递归查询
closure_tag_ids = get_v3_parent_tag_list(cur, tag_ids, tag_ids)
res['closure_tag_ids'] = closure_tag_ids
res['in_whitelist'] = False
res['in_multitopic'] = False
return res
def get_v3_parent_tag_list(cur, tag_list, stack):
"""
获取tag_v_3标签的父级标签(递归查询)
"""
if not stack:
return tag_list
parent_v3_tag_id_sql = """
SELECT api_tag_v3_relation.parent_id as parent_id
FROM api_tag_v3_relation
INNER JOIN api_tag_3_0 ON api_tag_v3_relation.parent_id = api_tag_3_0.id
WHERE api_tag_v3_relation.is_online = True AND api_tag_3_0.is_online = True
and api_tag_v3_relation.child_id in ({})""".format(
','.join([str(tid) for tid in tag_list]))
cur.execute(parent_v3_tag_id_sql)
parent_ids = cur.fetchall()
v3_parent_tag_ids = [item[0] for item in parent_ids if item[0] not in tag_list]
tag_list = tag_list + v3_parent_tag_ids
return get_v3_parent_tag_list(cur, tag_list, v3_parent_tag_ids)
def get_area_tag_info(city_id, cur):
"""
获取用户关联的城市、省份、国家标签id
"""
if not city_id:
return {}
city_sql = """
select api_city.tag_id as city_tag_id, api_province.tag_id as city_province_tag_id, api_country.tag_id as city_province_country_tag_id
from api_city
left join api_province on api_city.province_id = api_province.id
left join api_country on api_province.country_id = api_country.id
where api_city.id = '{}'
""".format(city_id)
cur.execute(city_sql)
area_tag = cur.fetchone()
if not area_tag:
return {}
return {
'city_tag_id': area_tag[0],
'city_province_tag_id': area_tag[1],
'city_province_country_tag_id': area_tag[2],
}
def get_problem(instance): def get_problem(instance):
p = instance p = instance
res = { res = {
'id': p.id, 'id': p.id,
'diary_id': p.diary_id, 'diary_id': p.diary_id,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment