house_building.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. """不动产建筑数据处理
  2. """
  3. # 导入必要的库
  4. import re # 正则表达式库,用于字符串处理
  5. from datetime import datetime # 日期时间库,用于处理日期和时间
  6. from dateutil.relativedelta import relativedelta # 日期时间相对偏移库,用于计算相对日期
  7. from decimal import Decimal # 高精度小数库,用于精确的数值计算
  8. from loguru import logger # 日志库,用于记录日志信息
  9. import pandas as pd # 数据分析库,用于处理数据框
  10. import psycopg # PostgreSQL数据库连接库,用于与PostgreSQL交互
  11. import subprocess
  12. import paramiko
  13. # 配置日志记录器,将日志写入文件a.log
  14. logger.add(sink='a.log')
  15. ssh_hostname = '172.16.107.4' # 定义远程主机地址
  16. ssh_port = 22 # 定义SSH服务的端口号
  17. ssh_username = 'app' # 定义登录远程主机的用户名
  18. ssh_password = '(l4w0ST_' # 定义登录远程主机的密码
  19. # 服务器文件夹路径
  20. remote_dir_path = '/data/history/house/building/'
  21. # 数据库连接信息
  22. db_host = "172.16.107.5" # 数据库主机地址
  23. db_port = 5432 # 数据库端口号
  24. db_username = "finance" # 数据库用户名
  25. db_password = "Finance@unicom23" # 数据库密码
  26. dbname = "financialdb" # 数据库名称
  27. conn_info = f"host='{db_host}' port={db_port} user='{db_username}' password='{db_password}' dbname='{dbname}'"
  28. # 获取当前日期,并计算上个月的第一天
  29. today = datetime.today()
  30. start_date = today - relativedelta(months=1, day=1)
  31. year_month = start_date.strftime('%Y%m')
  32. # 数据文件路径
  33. input_path = 'data.xlsx'
  34. # 输出文件路径
  35. output_path = 'output.csv'
  36. def data_process():
  37. # 初始化全局变量,用于存储组织、区域等映射关系
  38. org_map = {} # 组织ID到组织信息的映射
  39. third_org_list_map = {} # 二级组织ID到其下属三级组织列表的映射
  40. area_map = {} # 区域ID到区域信息的映射
  41. districts_list_map = {} # 城市ID到其下属区县列表的映射
  42. # 连接到PostgreSQL数据库,并获取组织和区域数据
  43. with psycopg.connect(
  44. conninfo=conn_info,
  45. row_factory=psycopg.rows.dict_row
  46. ) as conn:
  47. with conn.cursor() as curs:
  48. # 查询所有一级组织(grade=1)
  49. sql = """
  50. select * from common.organization where grade = 1
  51. """
  52. logger.info(f"sql: {sql}")
  53. curs.execute(sql)
  54. second_orgs = curs.fetchall()
  55. for x in second_orgs:
  56. third_org_list_map[x['id']] = [] # 初始化每个二级组织的三级组织列表为空
  57. # 查询所有组织
  58. sql = """
  59. select * from common.organization
  60. """
  61. logger.info(f"sql: {sql}")
  62. curs.execute(sql)
  63. orgs = curs.fetchall()
  64. for x in orgs:
  65. if x['parent_id'] in third_org_list_map:
  66. third_org_list_map[x['parent_id']].append(x) # 将三级组织添加到对应的二级组织列表中
  67. org_map[x['id']] = x # 构建组织ID到组织信息的映射
  68. # 查询所有省级区域(area_grade=1)
  69. sql = """
  70. select * from common.area where area_grade = 1 order by area_id
  71. """
  72. logger.info(f"sql: {sql}")
  73. curs.execute(sql)
  74. cities = curs.fetchall()
  75. for x in cities:
  76. districts_list_map[x['area_id']] = [] # 初始化每个城市的区县列表为空
  77. # 查询所有区域
  78. sql = """
  79. select * from common.area
  80. """
  81. logger.info(f"sql: {sql}")
  82. curs.execute(sql)
  83. areas = curs.fetchall()
  84. for x in areas:
  85. if x['parent_id'] in districts_list_map:
  86. districts_list_map[x['parent_id']].append(x) # 将区县添加到对应的城市列表中
  87. area_map[x['area_id']] = x # 构建区域ID到区域信息的映射
  88. # 读取Excel文件中的数据并进行预处理
  89. df = pd.read_excel(io=input_path) # 读取Excel文件
  90. df = df.map(lambda x: re.sub(r'\s+', '', x) if type(x) is str else x) # 去除字符串字段中的多余空格
  91. df.drop_duplicates(subset=['建筑ID'], keep='last', inplace=True) # 去重,保留最后一条记录
  92. # 定义函数:根据资产所属单位获取二级组织机构编码
  93. def get_area_no(x):
  94. second_unit = x['资产所属单位(二级)']
  95. third_unit = x['资产所属单位(三级)']
  96. if '河北' == second_unit:
  97. return '-12'
  98. if '长途通信传输局' == second_unit:
  99. return '-11'
  100. if '保定' in second_unit and ('雄县' in third_unit or '容城' in third_unit or '安新' in third_unit):
  101. return '782'
  102. for second_org in second_orgs:
  103. area_name = second_org['name']
  104. area_no = second_org['id']
  105. if area_name in second_unit:
  106. return area_no
  107. raise RuntimeError(f'二级组织机构编码匹配失败: {second_unit}')
  108. df['二级组织机构编码'] = df.apply(get_area_no, axis=1) # 应用函数,生成二级组织机构编码列
  109. # 定义函数:根据二级组织机构编码获取二级组织机构名称
  110. def get_area_name(x):
  111. area_no = x['二级组织机构编码']
  112. second_org = org_map[area_no]
  113. area_name = second_org['name']
  114. return area_name
  115. df['二级组织机构名称'] = df.apply(get_area_name, axis=1) # 应用函数,生成二级组织机构名称列
  116. # 定义函数:根据资产所属单位获取三级组织机构编码
  117. def get_city_no(x):
  118. third_unit = x['资产所属单位(三级)']
  119. area_name = x['二级组织机构名称']
  120. area_no = x['二级组织机构编码']
  121. if area_name == '石家庄':
  122. if '矿区' in third_unit:
  123. return 'D0130185'
  124. if '井陉' in third_unit:
  125. return 'D0130121'
  126. if area_name == '秦皇岛':
  127. if '北戴河新区' in third_unit:
  128. return 'D0130185'
  129. if '北戴河' in third_unit:
  130. return 'D0130304'
  131. if area_name == '唐山':
  132. if '滦县' in third_unit:
  133. return 'D0130223'
  134. if '高新技术开发区' in third_unit:
  135. return 'D0130205'
  136. if area_name == '邢台':
  137. if '内丘' in third_unit:
  138. return 'D0130523'
  139. if '任泽' in third_unit:
  140. return 'D0130526'
  141. if area_name == '邯郸':
  142. if '峰峰' in third_unit:
  143. return 'D0130406'
  144. if area_name == '省机动局':
  145. if '沧州' in third_unit:
  146. return 'HECS180'
  147. if '唐山' in third_unit:
  148. return 'HECS181'
  149. if '秦皇岛' in third_unit:
  150. return 'HECS182'
  151. if '廊坊' in third_unit:
  152. return 'HECS183'
  153. if '张家口' in third_unit:
  154. return 'HECS184'
  155. if '邢台' in third_unit:
  156. return 'HECS185'
  157. if '邯郸' in third_unit:
  158. return 'HECS186'
  159. if '保定' in third_unit:
  160. return 'HECS187'
  161. if '石家庄' in third_unit:
  162. return 'HECS188'
  163. if '承德' in third_unit:
  164. return 'HECS189'
  165. if '衡水' in third_unit:
  166. return 'HECS720'
  167. if '雄安' in third_unit:
  168. return 'HECS728'
  169. return 'HECS018'
  170. if '雄安' == area_name:
  171. third_unit = third_unit.replace('雄安新区', '')
  172. third_org_list = third_org_list_map[area_no]
  173. for third_org in third_org_list:
  174. city_name = third_org['name']
  175. if city_name in third_unit:
  176. return third_org['id']
  177. if '沧州' == area_name:
  178. return 'D0130911'
  179. if '唐山' == area_name:
  180. return 'D0130202'
  181. if '秦皇岛' == area_name:
  182. return 'D0130302'
  183. if '廊坊' == area_name:
  184. return 'D0131000'
  185. if '张家口' == area_name:
  186. return 'D0130701'
  187. if '邢台' == area_name:
  188. return 'D0130502'
  189. if '邯郸' == area_name:
  190. return 'D0130402'
  191. if '保定' == area_name:
  192. return 'D0130601'
  193. if '石家庄' == area_name:
  194. return 'D0130186'
  195. if '承德' == area_name:
  196. return 'D0130801'
  197. if '衡水' == area_name:
  198. return 'D0133001'
  199. if '雄安' == area_name:
  200. return 'D0130830'
  201. return 'HE001'
  202. df['三级组织机构编码'] = df.apply(get_city_no, axis=1) # 应用函数,生成三级组织机构编码列
  203. # 定义函数:根据三级组织机构编码获取三级组织机构名称
  204. def get_city_name(x):
  205. city_no = x['三级组织机构编码']
  206. third_org = org_map[city_no]
  207. city_name = third_org['name']
  208. return city_name
  209. df['三级组织机构名称'] = df.apply(get_city_name, axis=1) # 应用函数,生成三级组织机构名称列
  210. # 定义函数:根据标准地址获取城市ID
  211. def get_city_id(x):
  212. address = x['标准地址']
  213. second_unit = x['资产所属单位(二级)']
  214. third_unit = x['资产所属单位(三级)']
  215. if '雄安' in address or ('保定' in address and ('雄县' in address or '容城' in address or '安新' in address)):
  216. return '133100'
  217. for city in cities:
  218. area_name = city['short_name']
  219. area_id = city['area_id']
  220. if area_name in second_unit:
  221. return area_id
  222. if area_name in third_unit:
  223. return area_id
  224. if area_name in address:
  225. return area_id
  226. return ''
  227. df['city_id'] = df.apply(get_city_id, axis=1) # 应用函数,生成城市ID列
  228. # 定义函数:根据城市ID获取城市名称
  229. def get_city(x):
  230. city_id = x['city_id']
  231. area = area_map.get(city_id)
  232. if pd.notna(area):
  233. city = area['area_name']
  234. return city
  235. return ''
  236. df['city'] = df.apply(get_city, axis=1) # 应用函数,生成城市名称列
  237. # 定义函数:根据标准地址获取区县ID
  238. def get_district_id(x):
  239. address = x['标准地址']
  240. city = x['city']
  241. city_id = x['city_id']
  242. if pd.isna(city) or pd.isna(address):
  243. return ''
  244. if city == '石家庄':
  245. if '矿区' in address:
  246. return '130107'
  247. if '井陉' in address:
  248. return '130121'
  249. if city == '唐山':
  250. if '滦县' in address:
  251. return '130284'
  252. if city == '邢台':
  253. if '内邱' in address:
  254. return '130523'
  255. if '任县' in address:
  256. return '130505'
  257. if city == '雄安':
  258. address = address.replace('雄安新区', '')
  259. districts = districts_list_map.get(city_id)
  260. if not districts:
  261. return ''
  262. for district in districts:
  263. district_name = district['short_name']
  264. if district_name in address:
  265. return district['area_id']
  266. return ''
  267. df['district_id'] = df.apply(get_district_id, axis=1) # 应用函数,生成区县ID列
  268. # 定义函数:根据区县ID获取区县名称
  269. def get_district(x):
  270. district_id = x['district_id']
  271. area = area_map.get(district_id)
  272. if pd.notna(area):
  273. district = area['area_name']
  274. return district
  275. return ''
  276. df['district'] = df.apply(get_district, axis=1) # 应用函数,生成区县名称列
  277. # 定义函数:将百分比字符串转换为小数
  278. def convert_percentage_to_number(x):
  279. if pd.notna(x) and isinstance(x, str) and x.endswith('%'):
  280. return Decimal(x[:-1]) / Decimal('100')
  281. return x
  282. df['得房率'] = df['得房率'].apply(convert_percentage_to_number) # 应用函数,将得房率转换为小数
  283. df['year_no'] = start_date.year # 年份列
  284. df['month_no'] = start_date.month # 月份列
  285. def get_int(x):
  286. try:
  287. return int(x)
  288. except Exception:
  289. return ""
  290. df['房龄开始年份'] = df['房龄开始年份'].apply(get_int)
  291. # 定义函数:计算房龄
  292. def get_house_age(x):
  293. house_year_began = x['房龄开始年份']
  294. if pd.notna(house_year_began) and house_year_began:
  295. current_year = start_date.year
  296. return current_year - house_year_began
  297. return ''
  298. df['house_age'] = df.apply(get_house_age, axis=1) # 应用函数,生成房龄列
  299. df.insert(0, '年月', year_month) # 在数据框第一列插入年月列
  300. # 打印数据框信息
  301. print(df.info())
  302. # 将结果保存为CSV文件
  303. df.to_csv(
  304. path_or_buf=output_path,
  305. index=False,
  306. header=[
  307. 'year_month', 'first_unit', 'second_unit', 'third_unit', 'building_name', 'building_id',
  308. 'housing_acquisition_rate', 'site_name', 'site_id', 'land_name', 'housing_source', 'acquisition_date',
  309. 'house_year_began', 'investor', 'management_level', 'building_structure', 'total_floors', 'frontage',
  310. 'courtyard', 'whole_building', 'property_ownership_certificate',
  311. 'no_property_ownership_certificate_reason', 'unrelated_assets', 'assets_num', 'assets_tag_num',
  312. 'usage_status', 'building_use', 'ownership_status', 'floor_area', 'building_area',
  313. 'building_area_self_use', 'building_area_rent', 'building_area_idle', 'building_area_unusable',
  314. 'usable_area', 'usable_area_self_use', 'usable_area_rent', 'usable_area_idle', 'usable_area_unusable',
  315. 'community_assistant_name', 'community_assistant_unit', 'lng_jt', 'lat_jt', 'address',
  316. 'property_owner', 'checked', 'area_no', 'area_name', 'city_no', 'city_name', 'city_id', 'city',
  317. 'district_id', 'district', 'year_no', 'month_no', 'house_age'
  318. ],
  319. encoding='utf-8-sig'
  320. )
  321. def data_import():
  322. # 定义 PowerShell 脚本的路径
  323. script_path = r"../../copy.ps1"
  324. # 目标表和文件信息
  325. table = "house.building_month" # 数据库目标表名
  326. # 表字段列名,用于指定导入数据的列顺序
  327. columns = "year_month,first_unit,second_unit,third_unit,building_name,building_id,housing_acquisition_rate,site_name,site_id,land_name,housing_source,acquisition_date,house_year_began,investor,management_level,building_structure,total_floors,frontage,courtyard,whole_building,property_ownership_certificate,no_property_ownership_certificate_reason,unrelated_assets,assets_num,assets_tag_num,usage_status,building_use,ownership_status,floor_area,building_area,building_area_self_use,building_area_rent,building_area_idle,building_area_unusable,usable_area,usable_area_self_use,usable_area_rent,usable_area_idle,usable_area_unusable,community_assistant_name,community_assistant_unit,lng_jt,lat_jt,address,property_owner,checked,area_no,area_name,city_no,city_name,city_id,city,district_id,district,year_no,month_no,house_age"
  328. # 构造执行 PowerShell 脚本的命令
  329. command = f"powershell -File {script_path} -db_host {db_host} -db_port {db_port} -db_username {db_username} -db_password {db_password} -dbname {dbname} -table {table} -filename {output_path} -columns {columns}"
  330. # 打印生成的命令,方便调试和日志记录
  331. logger.info("command: {}", command)
  332. # 使用 subprocess 模块运行 PowerShell 命令,并捕获输出
  333. completed_process = subprocess.run(
  334. command, # 执行的命令
  335. check=False, # 如果命令执行失败,不抛出异常
  336. text=True, # 将输出作为字符串处理
  337. capture_output=True, # 捕获标准输出和标准错误
  338. )
  339. # 打印命令执行的结果,包括返回码、标准输出和标准错误
  340. logger.info("导入结果:\n{}\n{}\n{}", completed_process.returncode, completed_process.stdout,
  341. completed_process.stderr)
  342. # 定义正则表达式,用于匹配标准输出中的 COPY 结果
  343. p = re.compile(r"^(COPY) (\d+)$")
  344. count = None # 初始化计数变量
  345. matcher = p.match(completed_process.stdout) # 匹配标准输出中的 COPY 结果
  346. if matcher:
  347. count = int(matcher.group(2)) # 提取导入的数据行数
  348. # 如果没有成功提取到导入数据的行数,抛出运行时异常
  349. if count is None:
  350. raise RuntimeError("导入数据失败")
  351. def upload_file():
  352. remote_path = f'{remote_dir_path}{year_month}.xlsx' # 定义远程主机的目标文件路径
  353. # 使用paramiko.SSHClient创建一个SSH客户端对象,并通过with语句管理其上下文
  354. with paramiko.SSHClient() as ssh:
  355. # 设置自动添加主机密钥策略,避免因未知主机密钥导致连接失败
  356. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  357. # 连接到远程主机,传入主机地址、端口、用户名和密码
  358. ssh.connect(ssh_hostname, port=ssh_port, username=ssh_username, password=ssh_password)
  359. # 执行远程命令,创建远程目录(如果不存在)
  360. ssh.exec_command(f'mkdir -p {remote_dir_path}')
  361. # 打开SFTP会话,用于文件传输,并通过with语句管理其上下文
  362. with ssh.open_sftp() as sftp:
  363. # 记录日志,提示即将上传的本地文件和远程目标路径
  364. logger.info("upload {} to {}", input_path, remote_path)
  365. # 使用SFTP的put方法将本地文件上传到远程主机
  366. sftp.put(input_path, remote_path)
  367. # 记录日志,提示文件已成功上传
  368. logger.info("uploaded {}", input_path)
  369. def data_update():
  370. with psycopg.connect(
  371. conninfo=conn_info,
  372. ) as conn:
  373. with conn.cursor() as curs:
  374. # 更新局址信息
  375. sql = f"""
  376. update
  377. house.building_month a
  378. set
  379. site_num = b.site_num,
  380. city_level = b.city_level,
  381. city_region = b.city_region,
  382. area_sector = b.area_sector,
  383. has_land = b.has_land
  384. from
  385. house.site_month b
  386. where
  387. a.site_id = b.site_id
  388. and a.year_month = b.year_month
  389. and a.year_month = {year_month}
  390. """
  391. logger.info(f"sql: {sql}")
  392. curs.execute(sql)
  393. logger.info(f"update {curs.rowcount}")
  394. # 更新经纬度
  395. sql = f"""
  396. with
  397. t101 as (
  398. select
  399. *
  400. from
  401. house.building_month
  402. where
  403. year_month = 202312
  404. )
  405. update
  406. house.building_month a
  407. set
  408. lng_wgs84 = b.lng_wgs84,
  409. lat_wgs84 = b.lat_wgs84,
  410. lng_bd09 = b.lng_bd09,
  411. lat_bd09 = b.lat_bd09,
  412. building_img = b.building_img
  413. from
  414. t101 b
  415. where
  416. a.year_month = {year_month}
  417. and a.building_id = b.building_id
  418. """
  419. logger.info(f"sql: {sql}")
  420. curs.execute(sql)
  421. logger.info(f"update {curs.rowcount}")
  422. # 更新闲置建筑面积超过1000平米策略
  423. sql = f"""
  424. insert
  425. into
  426. house.building_idle_strategy
  427. (
  428. year_month,
  429. building_id,
  430. first_unit,
  431. second_unit,
  432. third_unit,
  433. site_num,
  434. site_name,
  435. address,
  436. city_level,
  437. city_region,
  438. area_sector,
  439. has_land,
  440. site_id,
  441. building_name,
  442. housing_acquisition_rate,
  443. housing_source,
  444. acquisition_date,
  445. house_year_began,
  446. investor,
  447. management_level,
  448. building_structure,
  449. total_floors,
  450. assets_num,
  451. assets_tag_num,
  452. usage_status,
  453. building_use,
  454. ownership_status,
  455. floor_area,
  456. building_area,
  457. building_area_self_use,
  458. building_area_rent,
  459. building_area_idle,
  460. building_area_unusable,
  461. usable_area,
  462. usable_area_self_use,
  463. usable_area_rent,
  464. usable_area_idle,
  465. usable_area_unusable,
  466. city,
  467. district,
  468. lng_wgs84,
  469. lat_wgs84,
  470. lng_bd09,
  471. lat_bd09,
  472. building_img,
  473. area_no,
  474. area_name,
  475. city_no,
  476. city_name,
  477. year_no,
  478. month_no,
  479. house_age,
  480. land_name,
  481. frontage,
  482. courtyard,
  483. whole_building,
  484. property_ownership_certificate,
  485. no_property_ownership_certificate_reason,
  486. unrelated_assets,
  487. community_assistant_name,
  488. community_assistant_unit,
  489. lng_jt,
  490. lat_jt,
  491. property_owner,
  492. checked,
  493. city_id,
  494. district_id
  495. )
  496. select
  497. year_month,
  498. building_id,
  499. first_unit,
  500. second_unit,
  501. third_unit,
  502. site_num,
  503. site_name,
  504. address,
  505. city_level,
  506. city_region,
  507. area_sector,
  508. has_land,
  509. site_id,
  510. building_name,
  511. housing_acquisition_rate,
  512. housing_source,
  513. acquisition_date,
  514. house_year_began,
  515. investor,
  516. management_level,
  517. building_structure,
  518. total_floors,
  519. assets_num,
  520. assets_tag_num,
  521. usage_status,
  522. building_use,
  523. ownership_status,
  524. floor_area,
  525. building_area,
  526. building_area_self_use,
  527. building_area_rent,
  528. building_area_idle,
  529. building_area_unusable,
  530. usable_area,
  531. usable_area_self_use,
  532. usable_area_rent,
  533. usable_area_idle,
  534. usable_area_unusable,
  535. city,
  536. district,
  537. lng_wgs84,
  538. lat_wgs84,
  539. lng_bd09,
  540. lat_bd09,
  541. building_img,
  542. area_no,
  543. area_name,
  544. city_no,
  545. city_name,
  546. year_no,
  547. month_no,
  548. house_age,
  549. land_name,
  550. frontage,
  551. courtyard,
  552. whole_building,
  553. property_ownership_certificate,
  554. no_property_ownership_certificate_reason,
  555. unrelated_assets,
  556. community_assistant_name,
  557. community_assistant_unit,
  558. lng_jt,
  559. lat_jt,
  560. property_owner,
  561. checked,
  562. city_id,
  563. district_id
  564. from
  565. house.building_month
  566. where
  567. building_area_idle > 1000
  568. and year_month = {year_month}
  569. order by
  570. building_area_idle desc
  571. """
  572. logger.info(f"sql: {sql}")
  573. curs.execute(sql)
  574. logger.info(f"update {curs.rowcount}")
  575. sql = f"""
  576. with
  577. t101 as (
  578. select
  579. *,
  580. row_number() over (
  581. order by building_area_idle desc) as sort
  582. from
  583. house.building_idle_strategy
  584. where
  585. year_month = {year_month}
  586. ),
  587. t201 as (
  588. select
  589. area_no,
  590. area_name,
  591. city_no,
  592. city_name,
  593. 'kpi_301320_155_01' as kpi_code,
  594. '闲置建筑面积' as kpi_name,
  595. round(building_area_idle, 2)::varchar as kpi_value,
  596. '1' as kpi_type,
  597. building_id as jk_object_no,
  598. building_name as jk_object,
  599. sort
  600. from
  601. t101
  602. ),
  603. t202 as (
  604. select
  605. area_no,
  606. area_name,
  607. city_no,
  608. city_name,
  609. 'kpi_301320_155_02' as kpi_code,
  610. '房产名称' as kpi_name,
  611. building_name as kpi_value,
  612. '0' as kpi_type,
  613. building_id as jk_object_no,
  614. building_name as jk_object,
  615. sort
  616. from
  617. t101
  618. ),
  619. t203 as (
  620. select
  621. area_no,
  622. area_name,
  623. city_no,
  624. city_name,
  625. 'kpi_301320_155_03' as kpi_code,
  626. '房产编号' as kpi_name,
  627. building_id as kpi_value,
  628. '0' as kpi_type,
  629. building_id as jk_object_no,
  630. building_name as jk_object,
  631. sort
  632. from
  633. t101
  634. ),
  635. t204 as (
  636. select
  637. area_no,
  638. area_name,
  639. city_no,
  640. city_name,
  641. 'kpi_301320_155_04' as kpi_code,
  642. '房产总建筑面积' as kpi_name,
  643. round(building_area, 2)::varchar as kpi_value,
  644. '0' as kpi_type,
  645. building_id as jk_object_no,
  646. building_name as jk_object,
  647. sort
  648. from
  649. t101
  650. ),
  651. t301 as (
  652. select
  653. *
  654. from
  655. t201
  656. union all
  657. select
  658. *
  659. from
  660. t202
  661. union all
  662. select
  663. *
  664. from
  665. t203
  666. union all
  667. select
  668. *
  669. from
  670. t204
  671. )
  672. insert
  673. into
  674. publish.house_building_idle_strategy
  675. (
  676. acct_date,
  677. dept_code,
  678. dept_name,
  679. strategy_code,
  680. area_no,
  681. area_name,
  682. city_no,
  683. city_name,
  684. sale_no,
  685. sale_name,
  686. jk_object_no,
  687. jk_object,
  688. kpi_code,
  689. kpi_name,
  690. kpi_value,
  691. kpi_type,
  692. sort
  693. )
  694. select
  695. {year_month} as acct_date,
  696. '301320' as dept_code,
  697. '河北省分公司纵横运营中心' as dept_name,
  698. '301320_155' as strategy_code,
  699. area_no,
  700. area_name,
  701. city_no,
  702. city_name,
  703. '' as sale_no,
  704. '' as sale_name,
  705. jk_object_no,
  706. jk_object,
  707. kpi_code,
  708. kpi_name,
  709. kpi_value,
  710. kpi_type,
  711. sort
  712. from
  713. t301
  714. order by
  715. sort,
  716. kpi_code
  717. """
  718. logger.info(f"sql: {sql}")
  719. curs.execute(sql)
  720. logger.info(f"update {curs.rowcount}")
  721. data_process()
  722. data_import()
  723. upload_file()
  724. data_update()