house_building.py 28 KB

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