Files
airflow/dags/poi/business/district_bank.py
T
2024-12-23 15:26:44 +08:00

125 lines
6.5 KiB
Python

"""
小区周边1公里银行入库业务逻辑
"""
import logging
from goojia_utils import MAIN_MONGO_CLIENT, process_dataframe_multithread
import pandas as pd
from pymongo import UpdateOne
import poiutils
log = logging.getLogger(__name__)
district_facilitys_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'district_facilitys')
surrounding_facilities_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_base', 'surrounding_facilities')
seaweed_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed')
administrative_mapping_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_base', 'administrative_mapping')
# 银行code
bank_typecode = ["160100", "160101", "160102", "160103", "160104", "160105", "160106", "160107", "160108", "160109",
"160110",
"160111", "160112", "160113", "160114", "160115", "160117", "160118", "160119", "160120", "160121",
"160122",
"160123", "160124", "160125", "160126", "160127", "160128", "160129", "160130", "160131", "160132",
"160133",
"160134", "160135", "160136", "160137", "160138", "160139", "160140", "160141", "160142", "160143",
"160144",
"160145", "160146", "160147", "160148", "160149", "160150", "160151", "160152", "160200"]
small_type = ["银行", "中国人民银行", "国家开发银行", "中国进出口银行", "中国银行", "中国工商银行", "中国建设银行",
"中国农业银行", "交通银行", "招商银行", "华夏银行", "中信银行", "中国民生银行", "中国光大银行",
"上海银行", "上海浦东发展银行", "平安银行", "兴业银行", "北京银行", "广发银行", "农村商业银行",
"香港恒生银行", "东亚银行", "花旗银行", "渣打银行", "汇丰银行", "荷兰银行", "美国运通银行",
"瑞士友邦银行", "美国银行", "蒙特利尔银行", "纽约银行", "苏格兰皇家银行", "法国兴业银行", "德意志银行",
"日本三菱东京日联银行", "巴克莱银行", "摩根大通银行", "中国邮政储蓄银行", "香港星展银行", "南洋商业银行",
"上海商业银行", "永亨银行", "香港永隆银行", "创兴银行", "大新银行", "中信银行(国际)", "大众银行(香港)",
"北京农商银行", "上海农商银行", "广州农商银行", "深圳农村商业银行", "银行相关"]
def deal3(item):
location = [item['lng'], item['lat']]
item['bank'] = poiutils.find_around_by_location(item['city_display'], location, small_type, 3000)
return item
def deal(item):
location = [item['lng'], item['lat']]
item['bank'] = poiutils.find_around_by_location(item['city_display'], location, small_type, 1000)
return item
def do():
sync_new()
sync1()
def sync_new(distance=3000):
"""
周边3公里POI入库
"""
city_dict = list(administrative_mapping_conn.find({'type': '城市'}))
for city_item in city_dict:
city = city_item['city_short']
city_display = city_item['city']
seaweed_df = pd.DataFrame(
list(seaweed_conn.find({'city': city, 'cat': 'district', 'status': 0, 'lng': {'$ne': None}},
{'city': 1, 'region': 1, 'name': 1, 'lng': 1, 'lat': 1}).batch_size(10000)))
if len(seaweed_df) <= 0:
continue
seaweed_df['lng'] = seaweed_df['lng'] / 10 ** 10
seaweed_df['lat'] = seaweed_df['lat'] / 10 ** 10
seaweed_df['city_display'] = city_display
seaweed_df = process_dataframe_multithread(seaweed_df, deal3, 30)
#seaweed_df['bank'] = seaweed_df.apply(lambda x: deal(city_item['city'], x['lng'], x['lat'], distance), axis=1)
actions = list()
for index, item in seaweed_df.iterrows():
try:
# 周边配套库中历史原因,id可能不为小区id
action = UpdateOne({'city': item['city'], 'region': item['region'],
'district_name': item['name']},
{'$set': {'bank': item['bank'], 'city': item['city'], 'region': item['region'],
'district_name': item['name'], 'lng': item['lng'], 'lat': item['lat'], 'distance': distance}},
upsert=True)
actions.append(action)
except Exception as e:
print(e)
if len(actions) == 5000:
surrounding_facilities_conn.bulk_write(actions)
actions.clear()
if len(actions) > 0:
surrounding_facilities_conn.bulk_write(actions)
actions.clear()
log.info(city + '周边银行入库完毕')
def sync1():
city_dict = list(administrative_mapping_conn.find({'type': '城市'}))
for city_item in city_dict:
city = city_item['city_short']
city_display = city_item['city']
seaweed_df = pd.DataFrame(
list(seaweed_conn.find({'city': city, 'cat': 'district', 'status': 0, 'lng': {'$ne': None}},
{'city': 1, 'region': 1, 'name': 1, 'lng': 1, 'lat': 1}).batch_size(10000)))
if len(seaweed_df) <= 0:
continue
seaweed_df['lng'] = seaweed_df['lng'] / 10 ** 10
seaweed_df['lat'] = seaweed_df['lat'] / 10 ** 10
seaweed_df['city_display'] = city_display
seaweed_df = process_dataframe_multithread(seaweed_df, deal, 30)
#seaweed_df['bank'] = seaweed_df.apply(lambda x: deal(city_item['city'], x['lng'], x['lat']), axis=1)
actions = list()
for index, item in seaweed_df.iterrows():
try:
# 周边配套库中历史原因,id可能不为小区id
action = UpdateOne({'city': item['city'], 'region': item['region'],
'district_name': item['name']},
{'$set': {'bank': item['bank'], 'city': item['city'], 'region': item['region'],
'district_name': item['name'], 'lng': item['lng'], 'lat': item['lat']}},
upsert=True)
actions.append(action)
except Exception as e:
print(e)
if len(actions) == 5000:
district_facilitys_conn.bulk_write(actions)
actions.clear()
if len(actions) > 0:
district_facilitys_conn.bulk_write(actions)
actions.clear()
log.info(city + '周边银行入库完毕')