107 lines
5.1 KiB
Plaintext
107 lines
5.1 KiB
Plaintext
async def news_article_search(ns={}):
|
|
if not ns.get('url_link'):
|
|
return {
|
|
'status': False,
|
|
'msg': '请传递url_link'
|
|
}
|
|
domain_name = ns.get('url_link').split("//")[1].split("/")[0]
|
|
if 'localhost' in domain_name:
|
|
domain_name = 'dev.opencomputing.cn'
|
|
|
|
current_page = int(ns.get('current_page', 1))
|
|
page_size = int(ns.get('page_size', 10))
|
|
offset = (current_page - 1) * page_size
|
|
|
|
conditions = ["domain_name = '%s'" % domain_name, "del_flg = '0'"]
|
|
summary_conditions = ["domain_name = '%s'" % domain_name, "del_flg = '0'"]
|
|
if ns.get('title_zh'):
|
|
conditions.append("title_zh like '%%%%%s%%%%'" % ns.get('title_zh'))
|
|
if ns.get('title_en'):
|
|
conditions.append("title_en like '%%%%%s%%%%'" % ns.get('title_en'))
|
|
if ns.get('article_type_zh'):
|
|
conditions.append("article_type_zh = '%s'" % ns.get('article_type_zh'))
|
|
if ns.get('article_type_en'):
|
|
conditions.append("article_type_en = '%s'" % ns.get('article_type_en'))
|
|
if ns.get('status'):
|
|
conditions.append("status = '%s'" % ns.get('status'))
|
|
where_clause = " and ".join(conditions)
|
|
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
try:
|
|
count_sql = """select count(*) as total_count from enterprise_news_article where %s;""" % where_clause
|
|
total_count = (await sor.sqlExe(count_sql, {}))[0]['total_count']
|
|
summary_zh_sql = """
|
|
select article_type_zh as article_type, count(*) as article_count,
|
|
ifnull(sum(read_count), 0) as read_count
|
|
from enterprise_news_article
|
|
where %s
|
|
and article_type_zh is not null and article_type_zh != ''
|
|
group by article_type_zh;
|
|
""" % " and ".join(summary_conditions)
|
|
summary_en_sql = """
|
|
select article_type_en as article_type, count(*) as article_count,
|
|
ifnull(sum(read_count), 0) as read_count
|
|
from enterprise_news_article
|
|
where %s
|
|
and article_type_en is not null and article_type_en != ''
|
|
group by article_type_en
|
|
order by article_type_en;
|
|
""" % " and ".join(summary_conditions)
|
|
summary_zh_result = await sor.sqlExe(summary_zh_sql, {})
|
|
summary_en_result = await sor.sqlExe(summary_en_sql, {})
|
|
summary_zh_mapping = {}
|
|
for summary_dic in summary_zh_result:
|
|
summary_zh_mapping[summary_dic.get('article_type')] = summary_dic
|
|
article_type_summary_zh = []
|
|
for article_type in ['企业动态', '产品动态', '行业洞察', '活动资讯']:
|
|
summary_dic = summary_zh_mapping.get(article_type, {})
|
|
article_type_summary_zh.append({
|
|
'article_type': article_type,
|
|
'article_count': summary_dic.get('article_count', 0),
|
|
'read_count': summary_dic.get('read_count', 0)
|
|
})
|
|
article_type_summary_en = []
|
|
for summary_dic in summary_en_result:
|
|
article_type_summary_en.append({
|
|
'article_type': summary_dic.get('article_type'),
|
|
'article_count': summary_dic.get('article_count', 0),
|
|
'read_count': summary_dic.get('read_count', 0)
|
|
})
|
|
search_sql = """
|
|
select id, domain_name, title_zh, title_en, article_type_zh, article_type_en,
|
|
summary_zh, summary_en, cover_img_zh, cover_img_en, content_zh, content_en,
|
|
status, publish_time_zh, publish_time_en, read_count, update_time, create_at
|
|
from enterprise_news_article
|
|
where %s
|
|
order by greatest(
|
|
ifnull(publish_time_zh, '1000-01-01'),
|
|
ifnull(publish_time_en, '1000-01-01')
|
|
) desc, update_time desc
|
|
limit %s offset %s;
|
|
""" % (where_clause, page_size, offset)
|
|
result = await sor.sqlExe(search_sql, {})
|
|
for data_dic in result:
|
|
data_dic['status_name'] = {'0': '草稿', '1': '已发布'}.get(str(data_dic.get('status')), data_dic.get('status'))
|
|
data_dic['operation_list'] = ['发布', '删除'] if str(data_dic.get('status')) == '0' else ['下架', '删除']
|
|
return {
|
|
'status': True,
|
|
'msg': 'search news article success',
|
|
'data': result,
|
|
'article_type_summary_zh': article_type_summary_zh,
|
|
'article_type_summary_en': article_type_summary_en,
|
|
'pagination': {
|
|
'total': total_count,
|
|
'page_size': page_size,
|
|
'current_page': current_page
|
|
}
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'status': False,
|
|
'msg': 'search news article failed, %s' % str(e)
|
|
}
|
|
|
|
ret = await news_article_search(params_kw)
|
|
return ret
|