person.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. file_info.error_num +=1
  488. return reslte ,False
  489. return reslte ,True
  490. def import_data(db,file_path,user_id,file_info):
  491. import_status = True
  492. try:
  493. book = xlrd.open_workbook(file_path)
  494. sheet = book.sheet_by_index(0)
  495. except :
  496. file_info.remark = file_info.remark + f'\n文件打开失败,请核实文件格式为xlsx/xlx>'
  497. file_info.error_num +=1
  498. import_status = False
  499. data = []
  500. for row in range(4, sheet.nrows):
  501. # 姓名
  502. name = sheet.cell(row, 0).value
  503. if name == '':
  504. file_info.remark = file_info.remark+f'\n行<{row+1}>姓名不能为空<{name}>'
  505. file_info.error_num +=1
  506. import_status = False
  507. # 所属单位
  508. unit_name = sheet.cell(row, 1).value
  509. if unit_name == '':
  510. file_info.remark = file_info.remark+f'\n行<{row+1}>所属单位不能为空<{unit_name}>'
  511. file_info.error_num +=1
  512. import_status = False
  513. unit_id = db_dept.get_dept_id_by_name(db, unit_name)
  514. # 职务
  515. position = sheet.cell(row, 2).value
  516. if position =='':
  517. file_info.remark = file_info.remark+f'\n行<{row+1}>职务不能为空<{position}>'
  518. file_info.error_num +=1
  519. import_status = False
  520. # 电话号码(如有多个手机号请用“,”分隔)
  521. phone = str(sheet.cell(row, 3).value)
  522. if phone =='':
  523. file_info.remark = file_info.remark+f'\n行<{row+1}>电话号码不能为空<{phone}>'
  524. file_info.error_num +=1
  525. import_status = False
  526. phone_list = [i for i in phone.split(',')]
  527. user_id_1=-1
  528. for i in phone_list:
  529. user_id_1 = db_user.get_user_id_by_phonenumber(db,i)
  530. if user_id_1 != -1:
  531. break
  532. # 办公电话
  533. # (选填,格式:区号-电话号码)
  534. telephone = sheet.cell(row, 4).value
  535. # 排位顺序
  536. # (选填,请输入排序号1-9999,排序号数值越小越靠前,不填则默认排至最末)
  537. order_num = sheet.cell(row, 5).value
  538. if order_num == '':
  539. order_num=-1
  540. area_name = sheet.cell(row, 7).value
  541. if area_name=='':
  542. area_code=2
  543. area_code2='440900000000'
  544. else:
  545. area_code=get_area_info_by_area_name(db,area_name)
  546. if area_code is None:
  547. file_info.remark = file_info.remark+f'\n行<{row+1}>责任区域未找到'
  548. file_info.error_num +=1
  549. import_status = False
  550. area_code2 = area_code.area_code
  551. area_code = area_code.id
  552. new_person = ThreeProofingResponsiblePerson(
  553. unit_id=unit_id,
  554. unit_name=unit_name,
  555. name=name,
  556. area_code2=area_code2,
  557. area_code=area_code,
  558. position=position,
  559. phone=phone,
  560. telephone=telephone,
  561. user_id=user_id_1,
  562. order_num=order_num,
  563. create_by=user_id
  564. )
  565. data.append(new_person)
  566. db.add(new_person)
  567. db.commit()
  568. # 党委政府
  569. a1 = sheet.cell(row, 6).value
  570. if a1 != '':
  571. new_type_list,status = string_type_parent_id_create_data(db,a1,'1',file_info,new_person,user_id,row)
  572. if status:
  573. db.add_all(new_type_list)
  574. data +=new_type_list
  575. else:
  576. import_status = status
  577. # type_name_list = [i for i in a1.split(',')]
  578. # for type_name in type_name_list:
  579. # type_id = get_type_id_by_type_parent_id_and_type_name(db, '1', type_name)
  580. # if type_id:
  581. # pass
  582. # 三防指挥部
  583. b1 = sheet.cell(row, 8).value
  584. if b1 != '':
  585. new_type_list,status = string_type_parent_id_create_data(db,b1,'2',file_info,new_person,user_id,row)
  586. if status:
  587. db.add_all(new_type_list)
  588. data +=new_type_list
  589. else:
  590. import_status = status
  591. b2 = sheet.cell(row, 9).value
  592. # 应急部门
  593. c1 = sheet.cell(row, 10).value
  594. if c1!='':
  595. new_type_list, status = string_type_parent_id_create_data(db, c1, '3', file_info, new_person, user_id,row)
  596. if status:
  597. db.add_all(new_type_list)
  598. data += new_type_list
  599. else:
  600. import_status = status
  601. # 成员单位
  602. d1 = sheet.cell(row, 11).value
  603. if d1!='':
  604. new_type_list, status = string_type_parent_id_create_data(db, d1, '4', file_info, new_person, user_id,row)
  605. if status:
  606. db.add_all(new_type_list)
  607. data += new_type_list
  608. else:
  609. import_status = status
  610. d2 = sheet.cell(row, 12).value
  611. if d2!='':
  612. dept_name = d2
  613. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  614. type_parent_id='4',
  615. dept_name=dept_name,
  616. person_id=new_person.id,
  617. create_by=user_id
  618. )
  619. db.add(new_person_other_info)
  620. data.append(new_person_other_info)
  621. # 重点部门
  622. e1 = sheet.cell(row, 13).value
  623. if e1!='':
  624. new_type_list, status = string_type_parent_id_create_data(db, e1, '5', file_info, new_person, user_id,row)
  625. if status:
  626. db.add_all(new_type_list)
  627. data += new_type_list
  628. else:
  629. import_status = status
  630. e2 = sheet.cell(row, 14).value
  631. if e2!='':
  632. dept_name = e2
  633. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  634. type_parent_id='5',
  635. dept_name=dept_name,
  636. person_id=new_person.id,
  637. create_by=user_id
  638. )
  639. db.add(new_person_other_info)
  640. data.append(new_person_other_info)
  641. e3 = sheet.cell(row, 15).value
  642. if e3!='':
  643. new_type_list,status = other_type_string_type_parent_id_create_data(db,e3,'5',file_info,new_person,user_id,row)
  644. if status:
  645. db.add_all(new_type_list)
  646. data +=new_type_list
  647. else:
  648. import_status = status
  649. # 行政村
  650. f1 = sheet.cell(row, 16).value
  651. if f1!='':
  652. new_type_list, status = string_type_parent_id_create_data(db, f1, '6', file_info, new_person, user_id,row)
  653. if status:
  654. db.add_all(new_type_list)
  655. data += new_type_list
  656. else:
  657. import_status = status
  658. # 水利工程
  659. g1 = sheet.cell(row, 17).value
  660. if g1!='':
  661. new_type_list, status = string_type_parent_id_create_data(db, g1, '7', file_info, new_person, user_id,row)
  662. if status:
  663. db.add_all(new_type_list)
  664. data += new_type_list
  665. else:
  666. import_status = status
  667. g2 = sheet.cell(row, 18).value
  668. if g2!='':
  669. dept_name = g2
  670. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  671. type_parent_id='7',
  672. dept_name=dept_name,
  673. person_id=new_person.id,
  674. create_by=user_id
  675. )
  676. db.add(new_person_other_info)
  677. data.append(new_person_other_info)
  678. g3 = sheet.cell(row, 19).value
  679. if g3!='':
  680. new_type_list, status = other_type_string_type_parent_id_create_data(db, g3, '11', file_info, new_person, user_id,
  681. row)
  682. if status:
  683. db.add_all(new_type_list)
  684. data += new_type_list
  685. else:
  686. import_status = status
  687. # 受威胁转移
  688. h1 = sheet.cell(row, 20).value
  689. if h1!='':
  690. new_type_list, status = string_type_parent_id_create_data(db, h1, '8', file_info, new_person, user_id,row)
  691. if status:
  692. db.add_all(new_type_list)
  693. data += new_type_list
  694. else:
  695. import_status = status
  696. # 抢险队伍
  697. j1 = sheet.cell(row, 21).value
  698. if j1!='':
  699. new_type_list, status = string_type_parent_id_create_data(db, j1, '9', file_info, new_person, user_id,row)
  700. if status:
  701. db.add_all(new_type_list)
  702. data += new_type_list
  703. else:
  704. import_status = status
  705. j2 = sheet.cell(row, 22).value
  706. if j2!='':
  707. new_type_list, status = other_type_string_type_parent_id_create_data(db, j2, '9', file_info, new_person, user_id,
  708. row)
  709. if status:
  710. db.add_all(new_type_list)
  711. data += new_type_list
  712. else:
  713. import_status = status
  714. # 地质灾害
  715. k1 = sheet.cell(row, 23).value
  716. if k1!='':
  717. new_type_list, status = string_type_parent_id_create_data(db, k1, '10', file_info, new_person, user_id,row)
  718. if status:
  719. db.add_all(new_type_list)
  720. data += new_type_list
  721. else:
  722. import_status = status
  723. k2 = sheet.cell(row, 24).value
  724. if k2!='':
  725. denger_point_name = k2
  726. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  727. type_parent_id='10',
  728. denger_point_name=denger_point_name,
  729. person_id=new_person.id,
  730. create_by=user_id
  731. )
  732. db.add(new_person_other_info)
  733. data.append(new_person_other_info)
  734. # 其他
  735. l1 = sheet.cell(row, 25).value
  736. if l1!='':
  737. new_type_list, status = string_type_parent_id_create_data(db, l1, '11', file_info, new_person, user_id,row)
  738. if status:
  739. db.add_all(new_type_list)
  740. data += new_type_list
  741. else:
  742. import_status = status
  743. l2 = sheet.cell(row, 26).value
  744. if l2!='':
  745. other_type_2_name = l2
  746. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  747. type_parent_id='11',
  748. other_type_2_name=other_type_2_name,
  749. person_id=new_person.id,
  750. create_by=user_id
  751. )
  752. db.add(new_person_other_info)
  753. data.append(new_person_other_info)
  754. db.commit()
  755. if import_status == False:
  756. # print(1111111)
  757. for info in data:
  758. db.delete(info)
  759. file_info.status = 2
  760. db.commit()
  761. @router.post('/createImport')
  762. async def create_contact(
  763. background_tasks: BackgroundTasks,
  764. db: Session = Depends(get_db),
  765. body=Depends(remove_xss_json),
  766. user_id=Depends(valid_access_token),
  767. ):
  768. try:
  769. # 提取请求数据
  770. filename = body['filename']
  771. file_name_desc = body['file_name_desc']
  772. if len(filename) == 0:
  773. raise Exception()
  774. file_path = f'/data/upload/mergefile/uploads/{filename}'
  775. # 检查文件是否存在
  776. if not os.path.isfile(file_path):
  777. return JSONResponse(status_code=404, content={
  778. 'errcode': 404,
  779. 'errmsg': f'{filename}不存在'
  780. })
  781. new_file = ThreeProofingResponsiblePersonImportFileStatus(
  782. file_uuid=filename,
  783. file_name = file_name_desc,
  784. status = '1',
  785. remark = '',
  786. user_id=user_id
  787. )
  788. db.add(new_file)
  789. db.commit()
  790. background_tasks.add_task(import_data,db,file_path, user_id,new_file)
  791. # 返回创建成功的响应
  792. return {
  793. "code": 200,
  794. "msg": "成功",
  795. "data": None
  796. }
  797. except AppException as e:
  798. return {
  799. "code": 500,
  800. "msg": e.msg
  801. }
  802. except Exception as e:
  803. traceback.print_exc()
  804. # 处理异常
  805. db.rollback()
  806. raise HTTPException(status_code=500, detail=str(e))
  807. @router.get("/export")
  808. async def download_file(filename: str,filenameDesc: str = None):
  809. """
  810. 根据提供的文件名下载文件。
  811. :param filename: 要下载的文件的名称。
  812. """
  813. try:
  814. # 构造文件的完整路径
  815. file_path = os.path.join('', 'uploads/', filename)
  816. # 检查文件是否存在
  817. if not os.path.isfile(file_path):
  818. raise HTTPException(status_code=404, detail="文件未找到")
  819. if not filenameDesc:
  820. filenameDesc = filename
  821. # 设置文件头部和内容类型
  822. headers = {
  823. 'Content-Disposition': f'attachment; filename={filenameDesc}'
  824. }
  825. # 使用FileResponse返回文件流
  826. return FileResponse(
  827. path=file_path,
  828. headers=headers,
  829. media_type='application/octet-stream' # 可以按需更改为适当的MIME类型
  830. )
  831. except HTTPException as e:
  832. raise e
  833. except Exception as e:
  834. # 处理其他异常情况
  835. raise HTTPException(status_code=500, detail=str(e))