person.py 33 KB

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