person.py 33 KB

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