person.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. from fastapi import APIRouter, Request, Depends, HTTPException, Query, BackgroundTasks
  2. from sqlalchemy.exc import IntegrityError
  3. from fastapi.responses import HTMLResponse, FileResponse
  4. from fastapi.responses import JSONResponse
  5. from database import get_db
  6. from sqlalchemy import text, exists, and_, or_, not_
  7. from sqlalchemy.orm import Session
  8. from models import *
  9. import json
  10. import random
  11. import os
  12. from sqlalchemy import create_engine, select
  13. from typing import Optional
  14. from utils.StripTagsHTMLParser import *
  15. from utils.three_proofing_responsible_util import *
  16. from utils.ry_system_util import *
  17. from common.db import db_event_management, db_user, db_area, db_emergency_plan
  18. from common.security import valid_access_token
  19. import traceback
  20. from utils import *
  21. from datetime import datetime, timedelta
  22. import pandas as pd
  23. import xlrd
  24. from common.db import db_dept
  25. from exceptions import AppException
  26. router = APIRouter()
  27. @router.post('/create')
  28. async def create_contact(
  29. db: Session = Depends(get_db),
  30. body=Depends(remove_xss_json),
  31. user_id=Depends(valid_access_token)
  32. ):
  33. try:
  34. # 提取请求数据
  35. unit_name = body['unit_name']
  36. name = body['name']
  37. area_code = body['area_code']
  38. position = body['position']
  39. phone = body['phone']
  40. telephone= ''
  41. if 'telephone' in body:
  42. telephone=body['telephone']
  43. order_num = -1
  44. if 'order_num'in body:
  45. order_num=body['order_num']
  46. unit_id = db_dept.get_dept_id_by_name(db, unit_name)
  47. user_id_1 = db_user.get_user_id_by_phonenumber(db,phone)
  48. # 创建新的预案记录
  49. new_person = ThreeProofingResponsiblePerson(
  50. unit_id=unit_id,
  51. unit_name=unit_name,
  52. name=name,
  53. area_code=area_code,
  54. position=position,
  55. phone=phone,
  56. telephone=telephone,
  57. user_id = user_id_1,
  58. order_num=order_num,
  59. create_by=user_id
  60. )
  61. # 添加到数据库会话并提交
  62. type_list = body['type_list']
  63. if isinstance(type_list,list) and len(type_list)>0:
  64. db.add(new_person)
  65. db.commit()
  66. else:
  67. return JSONResponse(status_code=404,content={
  68. 'code': 404,
  69. 'msg': '责任类型不能为空'
  70. })
  71. try:
  72. for type_info in type_list:
  73. type_parent_id = type_info['type_parent_id']#get_type_parent_id_by_type_id(db,type_id)
  74. for type_id in type_info['children']:
  75. new_person_type = ThreeProofingResponsiblePersonType(
  76. type_parent_id = type_parent_id,
  77. type_id = type_id,
  78. person_id = new_person.id,
  79. create_by=user_id
  80. )
  81. db.add(new_person_type)
  82. if type_parent_id in ('5','7','9'):
  83. if 'children2' in type_info:
  84. for other_type_id in type_info['children2']:
  85. new_person_other_type = ThreeProofingResponsiblePersonOtherType(
  86. type_parent_id=type_parent_id,
  87. other_type_id=other_type_id,
  88. person_id=new_person.id,
  89. create_by=user_id
  90. )
  91. db.add(new_person_other_type)
  92. if type_parent_id in ('4','5','7','10','11'):
  93. dept_name = None
  94. if 'dept_name' in type_info:
  95. dept_name = type_info['dept_name']
  96. other_type_2_name = None
  97. if 'other_type_2_name' in type_info:
  98. other_type_2_name = type_info['other_type_2_name']
  99. denger_point_name = None
  100. if 'denger_point_name' in type_info:
  101. denger_point_name = type_info['denger_point_name']
  102. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  103. type_parent_id = type_parent_id,
  104. dept_name = dept_name,
  105. other_type_2_name = other_type_2_name,
  106. denger_point_name = denger_point_name,
  107. person_id = new_person.id,
  108. create_by=user_id
  109. )
  110. db.add(new_person_other_info)
  111. except:
  112. db.rollback()
  113. traceback.print_exc()
  114. new_person.del_flag='2'
  115. db.commit()
  116. # 返回创建成功的响应
  117. return {
  118. "code": 200,
  119. "msg": "创建成功",
  120. "data": None
  121. }
  122. except Exception as e:
  123. # 处理异常
  124. db.rollback()
  125. traceback.print_exc()
  126. raise HTTPException(status_code=500, detail=str(e))
  127. @router.put('/update')
  128. async def update_contact(
  129. db: Session = Depends(get_db),
  130. body=Depends(remove_xss_json),
  131. user_id=Depends(valid_access_token)
  132. ):
  133. try:
  134. # 提取请求数据
  135. id = body['id']
  136. person = get_person_info_by_id(db,id)
  137. if not person:
  138. return JSONResponse(status_code=404, content={
  139. 'errcode': 404,
  140. 'errmsg': '责任人不存在'
  141. })
  142. person.unit_name = body['unit_name']
  143. person.name = body['name']
  144. person.area_code = body['area_code']
  145. person.position = body['position']
  146. person.phone = body['phone']
  147. if 'telephone' in body:
  148. person.telephone=body['telephone']
  149. person.order_num = body['order_num']
  150. if body['order_num']=='':
  151. person.order_num = -1
  152. person.unit_id = db_dept.get_dept_id_by_name(db, body['unit_name'])
  153. type_list = body['type_list']
  154. old_person_type_list=get_person_type_by_person_id(db,person.id)
  155. for old_person_type in old_person_type_list:
  156. old_person_type.del_flag='2'
  157. old_person_other_info_list = get_person_other_info_by_person_id(db,person.id)
  158. for old_person_other_info in old_person_other_info_list:
  159. old_person_other_info.del_flag = '2'
  160. old_person_other_type_list = get_person_other_type_by_person_id(db,person.id)
  161. for old_person_other_type in old_person_other_type_list:
  162. old_person_other_type.del_flag = '2'
  163. for type_info in type_list:
  164. type_parent_id = type_info['type_parent_id'] # get_type_parent_id_by_type_id(db,type_id)
  165. for type_id in type_info['children']:
  166. new_person_type = ThreeProofingResponsiblePersonType(
  167. type_parent_id=type_parent_id,
  168. type_id=type_id,
  169. person_id=id,
  170. create_by=user_id
  171. )
  172. db.add(new_person_type)
  173. if type_parent_id in ('5', '7', '9'):
  174. if 'children2' in type_info:
  175. for other_type_id in type_info['children2']:
  176. new_person_other_type = ThreeProofingResponsiblePersonOtherType(
  177. type_parent_id=type_parent_id,
  178. other_type_id=other_type_id,
  179. person_id=person.id,
  180. create_by=user_id
  181. )
  182. db.add(new_person_other_type)
  183. if type_parent_id in ('4', '5', '7', '10', '11'):
  184. dept_name = None
  185. if 'dept_name' in type_info:
  186. dept_name = type_info['dept_name']
  187. other_type_2_name = None
  188. if 'other_type_2_name' in type_info:
  189. other_type_2_name = type_info['other_type_2_name']
  190. denger_point_name = None
  191. if 'denger_point_name' in type_info:
  192. denger_point_name = type_info['denger_point_name']
  193. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  194. type_parent_id=type_parent_id,
  195. dept_name=dept_name,
  196. other_type_2_name=other_type_2_name,
  197. denger_point_name=denger_point_name,
  198. person_id=person.id,
  199. create_by=user_id
  200. )
  201. db.add(new_person_other_info)
  202. # 更新到数据库会话并提交
  203. db.commit()
  204. # 返回创建成功的响应
  205. return {
  206. "code": 200,
  207. "msg": "更新成功",
  208. "data": None
  209. }
  210. except Exception as e:
  211. # 处理异常
  212. db.rollback()
  213. traceback.print_exc()
  214. raise HTTPException(status_code=500, detail=str(e))
  215. @router.get('/list')
  216. async def get_emergency_contact_list(
  217. type_parent_id: str = Query(None, description='单位名称'),
  218. area_code:str = Query(None, description='单位名称'),
  219. Name: str = Query(None, description='联系人'),
  220. page: int = Query(1, gt=0, description='页码'),
  221. pageSize: int = Query(10, gt=0, description='每页条目数量'),
  222. db: Session = Depends(get_db),
  223. user_id=Depends(valid_access_token)
  224. ):
  225. try:
  226. # 构建查询
  227. query = db.query(ThreeProofingResponsiblePerson)
  228. query = query.filter(ThreeProofingResponsiblePerson.del_flag == '0')
  229. # 应用查询条件
  230. if type_parent_id:
  231. person_list = get_person_list_by_type_parent_id(db,type_parent_id)
  232. query = query.filter(ThreeProofingResponsiblePerson.id.in_(person_list))
  233. if Name:
  234. query = query.filter(ThreeProofingResponsiblePerson.name.like(f'%{Name}%'))
  235. def get_area_chli(area_list : list,parent_id : int):
  236. areas = parent_id_get_area_info(db,parent_id)
  237. if areas:
  238. for area in areas:
  239. area_list.append(area.id)
  240. get_area_chli(area_list, area.id)
  241. return area_list
  242. if area_code:
  243. query = query.filter(ThreeProofingResponsiblePerson.area_code.in_(get_area_chli([area_code],area_code)))
  244. # if area_code:
  245. # query = query.filter(ThreeProofingResponsiblePerson.area_code==area_code)
  246. # 计算总条目数
  247. total_items = query.count()
  248. # 排序
  249. query = query.order_by(ThreeProofingResponsiblePerson.order_num.asc())
  250. query = query.order_by(ThreeProofingResponsiblePerson.create_time.desc())
  251. # 执行分页查询
  252. contact_infos = query.offset((page - 1) * pageSize).limit(pageSize).all()
  253. # 将查询结果转换为列表形式的字典
  254. contact_infos_list = []
  255. for info in contact_infos:
  256. type_parent_id_list = get_type_parent_id_by_person_id(db,info.id)
  257. type_parent_list = []
  258. type_parent_list2 = []
  259. for type_parent in type_parent_id_list:
  260. if type_parent not in type_parent_list2:
  261. dict_data = get_dict_data_info(db,'three_proofing',type_parent)
  262. type_parent_list2.append(type_parent)
  263. type_parent_list.append({"type_parent_id":type_parent,"type_parent":dict_data.dict_label})
  264. area_info = id_get_area_info(db,info.area_code)
  265. user_info = user_id_get_user_info(db,info.create_by)
  266. area_list = db_area.id_get_area_parent_list(db,info.area_code,[])
  267. if info.order_num ==-1:
  268. order_num = ''
  269. else:
  270. order_num = str(info.order_num)
  271. contact_infos_list.append({
  272. "id": info.id,
  273. "unit_id": info.unit_id,
  274. "unit_name": info.unit_name,
  275. "name": info.name,
  276. "area_list":area_list,
  277. "area_code": info.area_code,
  278. "area_name": area_info.area_name,
  279. "position": info.position,
  280. "phone": info.phone,
  281. "telephone":info.telephone,
  282. "order_num": order_num,
  283. "online_status":'0',
  284. "create_time": info.create_time.strftime('%Y-%m-%d %H:%M:%S'),
  285. "create_by":info.create_by,
  286. "create_user":user_info.nick_name,
  287. "type_parent_list":type_parent_list
  288. })
  289. # 返回结果+
  290. return {
  291. "code": 200,
  292. "msg": "成功",
  293. "data": contact_infos_list,
  294. "total": total_items
  295. }
  296. except Exception as e:
  297. # 处理异常
  298. traceback.print_exc()
  299. raise HTTPException(status_code=500, detail=str(e))
  300. @router.get('/info/{id}')
  301. async def get_emergency_contact_id_info(
  302. id: str,
  303. db: Session = Depends(get_db),
  304. user_id=Depends(valid_access_token)
  305. ):
  306. try:
  307. contact = get_person_info_by_id(db,id)
  308. if not contact:
  309. return JSONResponse(status_code=404, content={
  310. 'errcode': 404,
  311. 'errmsg': '联系人不存在'
  312. })
  313. # 将查询结果转换为列表形式的字典
  314. area_info = id_get_area_info(db,contact.area_code)
  315. user_info = user_id_get_user_info(db,contact.create_by)
  316. area_list = db_area.id_get_area_parent_list(db, contact.area_code, [])
  317. if contact.order_num == -1:
  318. order_num = ''
  319. else:
  320. order_num = str(contact.order_num)
  321. contact_result = {
  322. "id": contact.id,
  323. "unit_id": contact.unit_id,
  324. "unit_name": contact.unit_name,
  325. "name": contact.name,
  326. "area_list":area_list,
  327. "area_code":contact.area_code,
  328. "area_name": area_info.area_name,
  329. "position": contact.position,
  330. "phone": contact.phone,
  331. "telephone":contact.telephone,
  332. "order_num":order_num,
  333. "online_status":'0',
  334. "create_time": contact.create_time.strftime('%Y-%m-%d %H:%M:%S'),
  335. "create_by":contact.create_by,
  336. "create_user":user_info.nick_name,
  337. "type_list":[]
  338. }
  339. type_parent_id_list = []
  340. type_list = get_person_type_by_person_id(db,contact.id)
  341. for type_info in type_list:
  342. if type_info.type_parent_id not in type_parent_id_list:
  343. type_parent_id_list.append(type_info.type_parent_id)
  344. for type_parent_id in type_parent_id_list:
  345. dict_data = get_dict_data_info(db, 'three_proofing', type_parent_id)
  346. type_data = {"type_parent_id": type_parent_id, "type_parent": dict_data.dict_label,"children":[],"labelData":[]}
  347. for type_info in get_person_type_by_person_id_and_type_parent_id(db,contact.id,type_parent_id):
  348. type_data_info = get_type_info_by_id(db,type_info.type_id)
  349. # type_data['children'].append({"id": type_info.type_id, "label": type_data_info.type_name})
  350. type_data['children'].append(type_info.type_id)
  351. type_data['labelData'].append( type_data_info.type_name)
  352. other_info = get_person_other_info_by_person_id_and_type_parent_id(db, contact.id, type_parent_id)
  353. if other_info:
  354. type_data['dept_name'] = other_info.dept_name
  355. type_data['denger_point_name'] = other_info.denger_point_name
  356. type_data['other_type_2_name'] = other_info.other_type_2_name
  357. other_type_list = get_person_other_type_by_person_id_and_type_parent_id(db, contact.id, type_parent_id)
  358. if other_type_list:
  359. type_data['children2'] = []
  360. type_data['other_type_label'] = []
  361. for other_type in other_type_list:
  362. other_type_info = get_other_type_info_by_id(db, other_type.other_type_id)
  363. label= ''
  364. if other_type_info:
  365. label = other_type_info.type_name
  366. # type_data['children2'].append({"id": other_type.other_type_id, "label": label})
  367. type_data['children2'].append(other_type.other_type_id)
  368. type_data['other_type_label'].append(label)
  369. contact_result['type_list'].append(type_data)
  370. # 返回结果
  371. return {
  372. "code": 200,
  373. "msg": "成功",
  374. "data": contact_result
  375. }
  376. except Exception as e:
  377. # 处理异常
  378. traceback.print_exc()
  379. raise HTTPException(status_code=500, detail=str(e))
  380. @router.delete('/delete')
  381. async def delete_emergency_plans(
  382. ids: list,
  383. db: Session = Depends(get_db),
  384. body=Depends(remove_xss_json),
  385. user_id=Depends(valid_access_token)
  386. ):
  387. try:
  388. # 提取请求数据
  389. query = db.query(ThreeProofingResponsiblePerson)
  390. query = query.filter(ThreeProofingResponsiblePerson.del_flag != '2')
  391. query = query.filter(ThreeProofingResponsiblePerson.id.in_(ids))
  392. contacts = query.all()
  393. if not contacts:
  394. return JSONResponse(status_code=404, content={
  395. 'errcode': 404,
  396. 'errmsg': '联系人不存在'
  397. })
  398. for contact in contacts:
  399. contact.del_flag = '2'
  400. contact.create_by = user_id
  401. # 更新到数据库会话并提交
  402. db.commit()
  403. # 返回创建成功的响应
  404. return {
  405. "code": 200,
  406. "msg": "删除成功",
  407. "data": None
  408. }
  409. except Exception as e:
  410. # 处理异常
  411. traceback.print_exc()
  412. raise HTTPException(status_code=500, detail=str(e))
  413. @router.delete('/delete/{id}')
  414. async def delete_emergency_plans(
  415. id: int,
  416. db: Session = Depends(get_db),
  417. body=Depends(remove_xss_json),
  418. user_id=Depends(valid_access_token)
  419. ):
  420. try:
  421. contact = get_person_info_by_id(db, id)
  422. if not contact:
  423. return JSONResponse(status_code=404, content={
  424. 'errcode': 404,
  425. 'errmsg': '联系人不存在'
  426. })
  427. contact.del_flag = '2'
  428. contact.create_by = user_id
  429. # 更新到数据库会话并提交
  430. db.commit()
  431. # 返回创建成功的响应
  432. return {
  433. "code": 200,
  434. "msg": "删除成功",
  435. "data": None
  436. }
  437. except Exception as e:
  438. # 处理异常
  439. traceback.print_exc()
  440. raise HTTPException(status_code=500, detail=str(e))
  441. def string_type_parent_id_create_data(db,string,type_parent_id,file_info,new_person,user_id,row) :
  442. type_name_list = [i for i in string.split(',')]
  443. reslte = []
  444. for type_name in type_name_list:
  445. type_id = get_type_id_by_type_parent_id_and_type_name(db, type_parent_id, type_name)
  446. if type_id:
  447. new_person_type = ThreeProofingResponsiblePersonType(
  448. type_parent_id=type_parent_id,
  449. type_id=type_id,
  450. person_id=new_person.id,
  451. create_by=user_id
  452. )
  453. reslte.append(new_person_type)
  454. else:
  455. file_info.remark= file_info.remark+f'\n行<{row+1}>责任类别未找到<{type_name}>'
  456. return reslte ,False
  457. return reslte ,True
  458. def other_type_string_type_parent_id_create_data(db,string,type_parent_id,file_info,new_person,user_id,row) :
  459. type_name_list = [i for i in string.split(',')]
  460. reslte = []
  461. for other_type_name in type_name_list:
  462. other_type_id = get_type_id_by_type_parent_id_and_type_name(db, type_parent_id, other_type_name)
  463. if other_type_id:
  464. new_person_type = ThreeProofingResponsiblePersonOtherType(
  465. type_parent_id=type_parent_id,
  466. other_type_id=other_type_id,
  467. person_id=new_person.id,
  468. create_by=user_id
  469. )
  470. reslte.append(new_person_type)
  471. else:
  472. file_info.remark= file_info.remark+f'\n行<{row+1}>责任类别未找到<{other_type_name}>'
  473. return reslte ,False
  474. return reslte ,True
  475. def import_data(db,file_path,user_id,file_info):
  476. import_status = True
  477. try:
  478. book = xlrd.open_workbook(file_path)
  479. sheet = book.sheet_by_index(0)
  480. except :
  481. file_info.remark = file_info.remark + f'\n文件打开失败,请核实文件格式为xlsx/xlx>'
  482. import_status = False
  483. data = []
  484. for row in range(4, sheet.nrows):
  485. # 姓名
  486. name = sheet.cell(row, 0).value
  487. if name == '':
  488. file_info.remark = file_info.remark+f'\n行<{row+1}>姓名不能为空<{name}>'
  489. import_status = False
  490. continue
  491. # 所属单位
  492. unit_name = sheet.cell(row, 1).value
  493. if unit_name == '':
  494. file_info.remark = file_info.remark+f'\n行<{row+1}>所属单位不能为空<{unit_name}>'
  495. import_status = False
  496. continue
  497. unit_id = db_dept.get_dept_id_by_name(db, unit_name)
  498. # 职务
  499. position = sheet.cell(row, 2).value
  500. if position =='':
  501. file_info.remark = file_info.remark+f'\n行<{row+1}>职务不能为空<{position}>'
  502. import_status = False
  503. continue
  504. # 电话号码(如有多个手机号请用“,”分隔)
  505. phone = int(sheet.cell(row, 3).value)
  506. if phone =='':
  507. file_info.remark = file_info.remark+f'\n行<{row+1}>电话号码不能为空<{phone}>'
  508. import_status = False
  509. continue
  510. phone_list = [i for i in phone.split(',')]
  511. user_id_1=-1
  512. for i in phone_list:
  513. user_id_1 = db_user.get_user_id_by_phonenumber(db,i)
  514. if user_id_1 != -1:
  515. break
  516. # 办公电话
  517. # (选填,格式:区号-电话号码)
  518. telephone = sheet.cell(row, 4).value
  519. # 排位顺序
  520. # (选填,请输入排序号1-9999,排序号数值越小越靠前,不填则默认排至最末)
  521. order_num = sheet.cell(row, 5).value
  522. if order_num == '':
  523. order_num=-1
  524. area_name = sheet.cell(row, 7).value
  525. if area_name=='':
  526. area_code=2
  527. else:
  528. area_code=get_area_info_by_area_name(db,area_name)
  529. if area_code is None:
  530. file_info.remark = file_info.remark+f'\n行<{row+1}>责任区域未找到<{area_name}>'
  531. import_status = False
  532. continue
  533. new_person = ThreeProofingResponsiblePerson(
  534. unit_id=unit_id,
  535. unit_name=unit_name,
  536. name=name,
  537. area_code=area_code,
  538. position=position,
  539. phone=phone,
  540. telephone=telephone,
  541. user_id=user_id_1,
  542. order_num=order_num,
  543. create_by=user_id
  544. )
  545. data.append(new_person)
  546. db.add(new_person)
  547. db.commit()
  548. # 党委政府
  549. a1 = sheet.cell(row, 6).value
  550. if a1 != '':
  551. new_type_list,status = string_type_parent_id_create_data(db,a1,'1',file_info,new_person,user_id,row)
  552. import_status = status
  553. if status:
  554. db.add_all(new_type_list)
  555. data +=new_type_list
  556. # type_name_list = [i for i in a1.split(',')]
  557. # for type_name in type_name_list:
  558. # type_id = get_type_id_by_type_parent_id_and_type_name(db, '1', type_name)
  559. # if type_id:
  560. # pass
  561. # 三防指挥部
  562. b1 = sheet.cell(row, 8).value
  563. if b1 != '':
  564. new_type_list,status = string_type_parent_id_create_data(db,b1,'2',file_info,new_person,user_id,row)
  565. import_status = status
  566. if status:
  567. db.add_all(new_type_list)
  568. data +=new_type_list
  569. b2 = sheet.cell(row, 9).value
  570. # 应急部门
  571. c1 = sheet.cell(row, 10).value
  572. if c1!='':
  573. new_type_list, status = string_type_parent_id_create_data(db, c1, '3', file_info, new_person, user_id,row)
  574. import_status = status
  575. if status:
  576. db.add_all(new_type_list)
  577. data += new_type_list
  578. # 成员单位
  579. d1 = sheet.cell(row, 11).value
  580. if d1!='':
  581. new_type_list, status = string_type_parent_id_create_data(db, d1, '4', file_info, new_person, user_id,row)
  582. import_status = status
  583. if status:
  584. db.add_all(new_type_list)
  585. data += new_type_list
  586. d2 = sheet.cell(row, 12).value
  587. if d2!='':
  588. dept_name = d2
  589. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  590. type_parent_id='4',
  591. dept_name=dept_name,
  592. person_id=new_person.id,
  593. create_by=user_id
  594. )
  595. db.add(new_person_other_info)
  596. data.append(new_person_other_info)
  597. # 重点部门
  598. e1 = sheet.cell(row, 13).value
  599. if e1!='':
  600. new_type_list, status = string_type_parent_id_create_data(db, e1, '5', file_info, new_person, user_id,row)
  601. import_status = status
  602. if status:
  603. db.add_all(new_type_list)
  604. data += new_type_list
  605. e2 = sheet.cell(row, 14).value
  606. if e2!='':
  607. dept_name = e2
  608. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  609. type_parent_id='5',
  610. dept_name=dept_name,
  611. person_id=new_person.id,
  612. create_by=user_id
  613. )
  614. db.add(new_person_other_info)
  615. data.append(new_person_other_info)
  616. e3 = sheet.cell(row, 15).value
  617. if e3!='':
  618. new_type_list,status = other_type_string_type_parent_id_create_data(db,e3,'5',file_info,new_person,user_id,row)
  619. import_status = status
  620. if status:
  621. db.add_all(new_type_list)
  622. data +=new_type_list
  623. # 行政村
  624. f1 = sheet.cell(row, 16).value
  625. if f1!='':
  626. new_type_list, status = string_type_parent_id_create_data(db, f1, '6', file_info, new_person, user_id,row)
  627. import_status = status
  628. if status:
  629. db.add_all(new_type_list)
  630. data += new_type_list
  631. # 水利工程
  632. g1 = sheet.cell(row, 10).value
  633. if g1!='':
  634. new_type_list, status = string_type_parent_id_create_data(db, g1, '7', file_info, new_person, user_id,row)
  635. import_status = status
  636. if status:
  637. db.add_all(new_type_list)
  638. data += new_type_list
  639. g2 = sheet.cell(row, 11).value
  640. if g2!='':
  641. dept_name = g2
  642. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  643. type_parent_id='7',
  644. dept_name=dept_name,
  645. person_id=new_person.id,
  646. create_by=user_id
  647. )
  648. db.add(new_person_other_info)
  649. data.append(new_person_other_info)
  650. g3 = sheet.cell(row, 12).value
  651. if g3!='':
  652. new_type_list, status = other_type_string_type_parent_id_create_data(db, g3, '11', file_info, new_person, user_id,
  653. row)
  654. import_status = status
  655. if status:
  656. db.add_all(new_type_list)
  657. data += new_type_list
  658. # 受威胁转移
  659. h1 = sheet.cell(row, 13).value
  660. if h1!='':
  661. new_type_list, status = string_type_parent_id_create_data(db, h1, '8', file_info, new_person, user_id,row)
  662. import_status = status
  663. if status:
  664. db.add_all(new_type_list)
  665. data += new_type_list
  666. # 抢险队伍
  667. j1 = sheet.cell(row, 14).value
  668. if j1!='':
  669. new_type_list, status = string_type_parent_id_create_data(db, j1, '9', file_info, new_person, user_id,row)
  670. import_status = status
  671. if status:
  672. db.add_all(new_type_list)
  673. data += new_type_list
  674. j2 = sheet.cell(row, 15).value
  675. if j2!='':
  676. new_type_list, status = other_type_string_type_parent_id_create_data(db, j2, '9', file_info, new_person, user_id,
  677. row)
  678. import_status = status
  679. if status:
  680. db.add_all(new_type_list)
  681. data += new_type_list
  682. # 地质灾害
  683. k1 = sheet.cell(row, 16).value
  684. if k1!='':
  685. new_type_list, status = string_type_parent_id_create_data(db, k1, '10', file_info, new_person, user_id,row)
  686. import_status = status
  687. if status:
  688. db.add_all(new_type_list)
  689. data += new_type_list
  690. k2 = sheet.cell(row, 17).value
  691. if k2!='':
  692. denger_point_name = k2
  693. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  694. type_parent_id='10',
  695. denger_point_name=denger_point_name,
  696. person_id=new_person.id,
  697. create_by=user_id
  698. )
  699. db.add(new_person_other_info)
  700. data.append(new_person_other_info)
  701. # 其他
  702. l1 = sheet.cell(row, 18).value
  703. if l1!='':
  704. new_type_list, status = string_type_parent_id_create_data(db, l1, '11', file_info, new_person, user_id,row)
  705. import_status = status
  706. if status:
  707. db.add_all(new_type_list)
  708. data += new_type_list
  709. l2 = sheet.cell(row, 19).value
  710. if l2!='':
  711. other_type_2_name = l2
  712. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  713. type_parent_id='11',
  714. other_type_2_name=other_type_2_name,
  715. person_id=new_person.id,
  716. create_by=user_id
  717. )
  718. db.add(new_person_other_info)
  719. data.append(new_person_other_info)
  720. if import_status == False:
  721. for info in data:
  722. db.delete(info)
  723. db.commit()
  724. @router.post('/createImport')
  725. async def create_contact(
  726. background_tasks: BackgroundTasks,
  727. db: Session = Depends(get_db),
  728. body=Depends(remove_xss_json),
  729. user_id=Depends(valid_access_token),
  730. ):
  731. try:
  732. # 提取请求数据
  733. filename = body['filename']
  734. file_name_desc = body['file_name_desc']
  735. if len(filename) == 0:
  736. raise Exception()
  737. file_path = f'/data/upload/mergefile/uploads/{filename}'
  738. # 检查文件是否存在
  739. if not os.path.isfile(file_path):
  740. return JSONResponse(status_code=404, content={
  741. 'errcode': 404,
  742. 'errmsg': f'{filename}不存在'
  743. })
  744. new_file = ThreeProofingResponsiblePersonImportFileStatus(
  745. file_uuid=filename,
  746. file_name = file_name_desc,
  747. status = '1',
  748. user_id=user_id
  749. )
  750. db.add(new_file)
  751. db.commit()
  752. background_tasks.add_task(import_data,db,file_path, user_id,new_file)
  753. # 返回创建成功的响应
  754. return {
  755. "code": 200,
  756. "msg": "成功",
  757. "data": None
  758. }
  759. except AppException as e:
  760. return {
  761. "code": 500,
  762. "msg": e.msg
  763. }
  764. except Exception as e:
  765. traceback.print_exc()
  766. # 处理异常
  767. db.rollback()
  768. raise HTTPException(status_code=500, detail=str(e))