person.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. person.unit_id = db_dept.get_dept_id_by_name(db, body['unit_name'])
  151. type_list = body['type_list']
  152. old_person_type_list=get_person_type_by_person_id(db,person.id)
  153. for old_person_type in old_person_type_list:
  154. old_person_type.del_flag='2'
  155. old_person_other_info_list = get_person_other_info_by_person_id(db,person.id)
  156. for old_person_other_info in old_person_other_info_list:
  157. old_person_other_info.del_flag = '2'
  158. old_person_other_type_list = get_person_other_type_by_person_id(db,person.id)
  159. for old_person_other_type in old_person_other_type_list:
  160. old_person_other_type.del_flag = '2'
  161. for type_info in type_list:
  162. type_parent_id = type_info['type_parent_id'] # get_type_parent_id_by_type_id(db,type_id)
  163. for type_id in type_info['children']:
  164. new_person_type = ThreeProofingResponsiblePersonType(
  165. type_parent_id=type_parent_id,
  166. type_id=type_id,
  167. person_id=id,
  168. create_by=user_id
  169. )
  170. db.add(new_person_type)
  171. if type_parent_id in ('5', '7', '9'):
  172. if 'children2' in type_info:
  173. for other_type_id in type_info['children2']:
  174. new_person_other_type = ThreeProofingResponsiblePersonOtherType(
  175. type_parent_id=type_parent_id,
  176. other_type_id=other_type_id,
  177. person_id=person.id,
  178. create_by=user_id
  179. )
  180. db.add(new_person_other_type)
  181. if type_parent_id in ('4', '5', '7', '10', '11'):
  182. dept_name = None
  183. if 'dept_name' in type_info:
  184. dept_name = type_info['dept_name']
  185. other_type_2_name = None
  186. if 'other_type_2_name' in type_info:
  187. other_type_2_name = type_info['other_type_2_name']
  188. denger_point_name = None
  189. if 'denger_point_name' in type_info:
  190. denger_point_name = type_info['denger_point_name']
  191. new_person_other_info = ThreeProofingResponsiblePersonOtherInfo(
  192. type_parent_id=type_parent_id,
  193. dept_name=dept_name,
  194. other_type_2_name=other_type_2_name,
  195. denger_point_name=denger_point_name,
  196. person_id=person.id,
  197. create_by=user_id
  198. )
  199. db.add(new_person_other_info)
  200. # 更新到数据库会话并提交
  201. db.commit()
  202. # 返回创建成功的响应
  203. return {
  204. "code": 200,
  205. "msg": "更新成功",
  206. "data": None
  207. }
  208. except Exception as e:
  209. # 处理异常
  210. db.rollback()
  211. traceback.print_exc()
  212. raise HTTPException(status_code=500, detail=str(e))
  213. @router.get('/list')
  214. async def get_emergency_contact_list(
  215. type_parent_id: str = Query(None, description='单位名称'),
  216. area_code:str = Query(None, description='单位名称'),
  217. Name: str = Query(None, description='联系人'),
  218. page: int = Query(1, gt=0, description='页码'),
  219. pageSize: int = Query(10, gt=0, description='每页条目数量'),
  220. db: Session = Depends(get_db),
  221. user_id=Depends(valid_access_token)
  222. ):
  223. try:
  224. # 构建查询
  225. query = db.query(ThreeProofingResponsiblePerson)
  226. query = query.filter(ThreeProofingResponsiblePerson.del_flag == '0')
  227. # 应用查询条件
  228. if type_parent_id:
  229. person_list = get_person_list_by_type_parent_id(db,type_parent_id)
  230. query = query.filter(ThreeProofingResponsiblePerson.id.in_(person_list))
  231. if Name:
  232. query = query.filter(ThreeProofingResponsiblePerson.name.like(f'%{Name}%'))
  233. def get_area_chli(area_list : list,parent_id : int):
  234. areas = parent_id_get_area_info(db,parent_id)
  235. if areas:
  236. for area in areas:
  237. area_list.append(area.id)
  238. get_area_chli(area_list, area.id)
  239. return area_list
  240. if area_code:
  241. query = query.filter(ThreeProofingResponsiblePerson.area_code.in_(get_area_chli([area_code],area_code)))
  242. # if area_code:
  243. # query = query.filter(ThreeProofingResponsiblePerson.area_code==area_code)
  244. # 计算总条目数
  245. total_items = query.count()
  246. # 排序
  247. query = query.order_by(ThreeProofingResponsiblePerson.order_num.asc())
  248. query = query.order_by(ThreeProofingResponsiblePerson.create_time.desc())
  249. # 执行分页查询
  250. contact_infos = query.offset((page - 1) * pageSize).limit(pageSize).all()
  251. # 将查询结果转换为列表形式的字典
  252. contact_infos_list = []
  253. for info in contact_infos:
  254. type_parent_id_list = get_type_parent_id_by_person_id(db,info.id)
  255. type_parent_list = []
  256. type_parent_list2 = []
  257. for type_parent in type_parent_id_list:
  258. if type_parent not in type_parent_list2:
  259. dict_data = get_dict_data_info(db,'three_proofing',type_parent)
  260. type_parent_list2.append(type_parent)
  261. type_parent_list.append({"type_parent_id":type_parent,"type_parent":dict_data.dict_label})
  262. area_info = id_get_area_info(db,info.area_code)
  263. user_info = user_id_get_user_info(db,info.create_by)
  264. area_list = db_area.id_get_area_parent_list(db,info.area_code,[])
  265. contact_infos_list.append({
  266. "id": info.id,
  267. "unit_id": info.unit_id,
  268. "unit_name": info.unit_name,
  269. "name": info.name,
  270. "area_list":area_list,
  271. "area_code": info.area_code,
  272. "area_name": area_info.area_name,
  273. "position": info.position,
  274. "phone": info.phone,
  275. "telephone":info.telephone,
  276. "order_num":info.order_num,
  277. "online_status":'0',
  278. "create_time": info.create_time.strftime('%Y-%m-%d %H:%M:%S'),
  279. "create_by":info.create_by,
  280. "create_user":user_info.nick_name,
  281. "type_parent_list":type_parent_list
  282. })
  283. # 返回结果+
  284. return {
  285. "code": 200,
  286. "msg": "成功",
  287. "data": contact_infos_list,
  288. "total": total_items
  289. }
  290. except Exception as e:
  291. # 处理异常
  292. traceback.print_exc()
  293. raise HTTPException(status_code=500, detail=str(e))
  294. @router.get('/info/{id}')
  295. async def get_emergency_contact_id_info(
  296. id: str,
  297. db: Session = Depends(get_db),
  298. user_id=Depends(valid_access_token)
  299. ):
  300. try:
  301. contact = get_person_info_by_id(db,id)
  302. if not contact:
  303. return JSONResponse(status_code=404, content={
  304. 'errcode': 404,
  305. 'errmsg': '联系人不存在'
  306. })
  307. # 将查询结果转换为列表形式的字典
  308. area_info = id_get_area_info(db,contact.area_code)
  309. user_info = user_id_get_user_info(db,contact.create_by)
  310. area_list = db_area.id_get_area_parent_list(db, contact.area_code, [])
  311. contact_result = {
  312. "id": contact.id,
  313. "unit_id": contact.unit_id,
  314. "unit_name": contact.unit_name,
  315. "name": contact.name,
  316. "area_list":area_list,
  317. "area_code":contact.area_code,
  318. "area_name": area_info.area_name,
  319. "position": contact.position,
  320. "phone": contact.phone,
  321. "telephone":contact.telephone,
  322. "order_num":contact.order_num,
  323. "online_status":'0',
  324. "create_time": contact.create_time.strftime('%Y-%m-%d %H:%M:%S'),
  325. "create_by":contact.create_by,
  326. "create_user":user_info.nick_name,
  327. "type_list":[]
  328. }
  329. type_parent_id_list = []
  330. type_list = get_person_type_by_person_id(db,contact.id)
  331. for type_info in type_list:
  332. if type_info.type_parent_id not in type_parent_id_list:
  333. type_parent_id_list.append(type_info.type_parent_id)
  334. for type_parent_id in type_parent_id_list:
  335. dict_data = get_dict_data_info(db, 'three_proofing', type_parent_id)
  336. type_data = {"type_parent_id": type_parent_id, "type_parent": dict_data.dict_label,"children":[],"labelData":[]}
  337. for type_info in get_person_type_by_person_id_and_type_parent_id(db,contact.id,type_parent_id):
  338. type_data_info = get_type_info_by_id(db,type_info.type_id)
  339. # type_data['children'].append({"id": type_info.type_id, "label": type_data_info.type_name})
  340. type_data['children'].append(type_info.type_id)
  341. type_data['labelData'].append( type_data_info.type_name)
  342. other_info = get_person_other_info_by_person_id_and_type_parent_id(db, contact.id, type_parent_id)
  343. if other_info:
  344. type_data['dept_name'] = other_info.dept_name
  345. type_data['denger_point_name'] = other_info.denger_point_name
  346. type_data['other_type_2_name'] = other_info.other_type_2_name
  347. other_type_list = get_person_other_type_by_person_id_and_type_parent_id(db, contact.id, type_parent_id)
  348. if other_type_list:
  349. type_data['children2'] = []
  350. type_data['other_type_label'] = []
  351. for other_type in other_type_list:
  352. other_type_info = get_other_type_info_by_id(db, other_type.other_type_id)
  353. label= ''
  354. if other_type_info:
  355. label = other_type_info.type_name
  356. # type_data['children2'].append({"id": other_type.other_type_id, "label": label})
  357. type_data['children2'].append(other_type.other_type_id)
  358. type_data['other_type_label'].append(label)
  359. contact_result['type_list'].append(type_data)
  360. # 返回结果
  361. return {
  362. "code": 200,
  363. "msg": "成功",
  364. "data": contact_result
  365. }
  366. except Exception as e:
  367. # 处理异常
  368. traceback.print_exc()
  369. raise HTTPException(status_code=500, detail=str(e))
  370. @router.delete('/delete')
  371. async def delete_emergency_plans(
  372. ids: list,
  373. db: Session = Depends(get_db),
  374. body=Depends(remove_xss_json),
  375. user_id=Depends(valid_access_token)
  376. ):
  377. try:
  378. # 提取请求数据
  379. query = db.query(ThreeProofingResponsiblePerson)
  380. query = query.filter(ThreeProofingResponsiblePerson.del_flag != '2')
  381. query = query.filter(ThreeProofingResponsiblePerson.id.in_(ids))
  382. contacts = query.all()
  383. if not contacts:
  384. return JSONResponse(status_code=404, content={
  385. 'errcode': 404,
  386. 'errmsg': '联系人不存在'
  387. })
  388. for contact in contacts:
  389. contact.del_flag = '2'
  390. contact.create_by = user_id
  391. # 更新到数据库会话并提交
  392. db.commit()
  393. # 返回创建成功的响应
  394. return {
  395. "code": 200,
  396. "msg": "删除成功",
  397. "data": None
  398. }
  399. except Exception as e:
  400. # 处理异常
  401. traceback.print_exc()
  402. raise HTTPException(status_code=500, detail=str(e))
  403. @router.delete('/delete/{id}')
  404. async def delete_emergency_plans(
  405. id: int,
  406. db: Session = Depends(get_db),
  407. body=Depends(remove_xss_json),
  408. user_id=Depends(valid_access_token)
  409. ):
  410. try:
  411. contact = get_person_info_by_id(db, id)
  412. if not contact:
  413. return JSONResponse(status_code=404, content={
  414. 'errcode': 404,
  415. 'errmsg': '联系人不存在'
  416. })
  417. contact.del_flag = '2'
  418. contact.create_by = user_id
  419. # 更新到数据库会话并提交
  420. db.commit()
  421. # 返回创建成功的响应
  422. return {
  423. "code": 200,
  424. "msg": "删除成功",
  425. "data": None
  426. }
  427. except Exception as e:
  428. # 处理异常
  429. traceback.print_exc()
  430. raise HTTPException(status_code=500, detail=str(e))
  431. def string_type_parent_id_create_data(db,string,type_parent_id,file_info,new_person,user_id,row) :
  432. type_name_list = [i for i in string.split(',')]
  433. reslte = []
  434. for type_name in type_name_list:
  435. type_id = get_type_id_by_type_parent_id_and_type_name(db, '1', type_name)
  436. if type_id:
  437. new_person_type = ThreeProofingResponsiblePersonType(
  438. type_parent_id=type_parent_id,
  439. type_id=type_id,
  440. person_id=new_person.id,
  441. create_by=user_id
  442. )
  443. reslte.append(new_person_type)
  444. else:
  445. file_info.remark= file_info.remark+f'\n行<{row+1}>责任类别未找到<{type_name}>'
  446. return reslte ,False
  447. return reslte ,True
  448. def import_data(db,file_path,user_id,file_info):
  449. book = xlrd.open_workbook(file_path)
  450. sheet = book.sheet_by_index(0)
  451. data = []
  452. import_status = True
  453. for row in range(4, sheet.nrows):
  454. # 姓名
  455. name = sheet.cell(row, 0).value
  456. if name == '':
  457. file_info.remark = file_info.remark+f'\n行<{row+1}>姓名不能为空<{name}>'
  458. import_status = False
  459. continue
  460. # 所属单位
  461. unit_name = sheet.cell(row, 1).value
  462. if unit_name == '':
  463. file_info.remark = file_info.remark+f'\n行<{row+1}>所属单位不能为空<{unit_name}>'
  464. import_status = False
  465. continue
  466. unit_id = db_dept.get_dept_id_by_name(db, unit_name)
  467. # 职务
  468. position = sheet.cell(row, 2).value
  469. if position =='':
  470. file_info.remark = file_info.remark+f'\n行<{row+1}>职务不能为空<{position}>'
  471. import_status = False
  472. continue
  473. # 电话号码(如有多个手机号请用“,”分隔)
  474. phone = int(sheet.cell(row, 3).value)
  475. if phone =='':
  476. file_info.remark = file_info.remark+f'\n行<{row+1}>电话号码不能为空<{phone}>'
  477. import_status = False
  478. continue
  479. phone_list = [i for i in phone.split(',')]
  480. user_id_1=-1
  481. for i in phone_list:
  482. user_id_1 = db_user.get_user_id_by_phonenumber(db,i)
  483. if user_id_1 != -1:
  484. break
  485. # 办公电话
  486. # (选填,格式:区号-电话号码)
  487. telephone = sheet.cell(row, 4).value
  488. # 排位顺序
  489. # (选填,请输入排序号1-9999,排序号数值越小越靠前,不填则默认排至最末)
  490. order_num = sheet.cell(row, 5).value
  491. if order_num == '':
  492. order_num=-1
  493. area_name = sheet.cell(row, 7).value
  494. if area_name=='':
  495. area_code=2
  496. else:
  497. area_code=get_area_info_by_area_name(db,area_name)
  498. if area_code is None:
  499. file_info.remark = file_info.remark+f'\n行<{row+1}>责任区域未找到<{area_name}>'
  500. import_status = False
  501. continue
  502. new_person = ThreeProofingResponsiblePerson(
  503. unit_id=unit_id,
  504. unit_name=unit_name,
  505. name=name,
  506. area_code=area_code,
  507. position=position,
  508. phone=phone,
  509. telephone=telephone,
  510. user_id=user_id_1,
  511. order_num=order_num,
  512. create_by=user_id
  513. )
  514. data.append(new_person)
  515. db.add(new_person)
  516. db.commit()
  517. # 党委政府
  518. a1 = sheet.cell(row, 6).value
  519. if a1 != '':
  520. new_type_list,status = string_type_parent_id_create_data(db,a1,'1',file_info,new_person,user_id)
  521. import_status = status
  522. if status:
  523. db.add_all(new_type_list)
  524. data +=new_type_list
  525. # type_name_list = [i for i in a1.split(',')]
  526. # for type_name in type_name_list:
  527. # type_id = get_type_id_by_type_parent_id_and_type_name(db, '1', type_name)
  528. # if type_id:
  529. # pass
  530. # 三防指挥部
  531. b1 = sheet.cell(row, 8).value
  532. if b1 != '':
  533. new_type_list,status = string_type_parent_id_create_data(db,b1,'2',file_info,new_person,user_id)
  534. import_status = status
  535. if status:
  536. db.add_all(new_type_list)
  537. data +=new_type_list
  538. b2 = sheet.cell(row, 9).value
  539. # 应急部门
  540. c1 = sheet.cell(row, 10).value
  541. if c1!='':
  542. new_type_list, status = string_type_parent_id_create_data(db, c1, '3', file_info, new_person, user_id)
  543. import_status = status
  544. if status:
  545. db.add_all(new_type_list)
  546. data += new_type_list
  547. # 成员单位
  548. d1 = sheet.cell(row, 11).value
  549. if d1!='':
  550. new_type_list, status = string_type_parent_id_create_data(db, d1, '4', file_info, new_person, user_id)
  551. import_status = status
  552. if status:
  553. db.add_all(new_type_list)
  554. data += new_type_list
  555. d2 = sheet.cell(row, 12).value
  556. if d2!='':
  557. pass
  558. # 重点部门
  559. e1 = sheet.cell(row, 13).value
  560. if e1!='':
  561. new_type_list, status = string_type_parent_id_create_data(db, e1, '5', file_info, new_person, user_id)
  562. import_status = status
  563. if status:
  564. db.add_all(new_type_list)
  565. data += new_type_list
  566. e2 = sheet.cell(row, 14).value
  567. e3 = sheet.cell(row, 15).value
  568. # 行政村
  569. f1 = sheet.cell(row, 16).value
  570. if f1!='':
  571. new_type_list, status = string_type_parent_id_create_data(db, f1, '6', file_info, new_person, user_id)
  572. import_status = status
  573. if status:
  574. db.add_all(new_type_list)
  575. data += new_type_list
  576. # 水利工程
  577. g1 = sheet.cell(row, 10).value
  578. if g1!='':
  579. new_type_list, status = string_type_parent_id_create_data(db, g1, '7', file_info, new_person, user_id)
  580. import_status = status
  581. if status:
  582. db.add_all(new_type_list)
  583. data += new_type_list
  584. g2 = sheet.cell(row, 11).value
  585. g3 = sheet.cell(row, 12).value
  586. # 受威胁转移
  587. h1 = sheet.cell(row, 13).value
  588. if h1!='':
  589. new_type_list, status = string_type_parent_id_create_data(db, h1, '8', file_info, new_person, user_id)
  590. import_status = status
  591. if status:
  592. db.add_all(new_type_list)
  593. data += new_type_list
  594. # 抢险队伍
  595. j1 = sheet.cell(row, 14).value
  596. if j1!='':
  597. new_type_list, status = string_type_parent_id_create_data(db, j1, '9', file_info, new_person, user_id)
  598. import_status = status
  599. if status:
  600. db.add_all(new_type_list)
  601. data += new_type_list
  602. j2 = sheet.cell(row, 15).value
  603. # 地质灾害
  604. k1 = sheet.cell(row, 16).value
  605. if k1!='':
  606. new_type_list, status = string_type_parent_id_create_data(db, k1, '10', file_info, new_person, user_id)
  607. import_status = status
  608. if status:
  609. db.add_all(new_type_list)
  610. data += new_type_list
  611. k2 = sheet.cell(row, 17).value
  612. # 其他
  613. l1 = sheet.cell(row, 18).value
  614. if l1!='':
  615. new_type_list, status = string_type_parent_id_create_data(db, l1, '11', file_info, new_person, user_id)
  616. import_status = status
  617. if status:
  618. db.add_all(new_type_list)
  619. data += new_type_list
  620. l2 = sheet.cell(row, 19).value
  621. if import_status == False:
  622. for info in data:
  623. db.delete(info)
  624. db.commit()
  625. @router.post('/createImport')
  626. async def create_contact(
  627. background_tasks: BackgroundTasks,
  628. db: Session = Depends(get_db),
  629. body=Depends(remove_xss_json),
  630. user_id=Depends(valid_access_token),
  631. ):
  632. try:
  633. # 提取请求数据
  634. filename = body['filename']
  635. file_name_desc = body['file_name_desc']
  636. if len(filename) == 0:
  637. raise Exception()
  638. file_name = filename[0]['url']
  639. file_path = f'/data/upload/mergefile/uploads/{file_name}'
  640. # 检查文件是否存在
  641. if not os.path.isfile(file_path):
  642. return JSONResponse(status_code=404, content={
  643. 'errcode': 404,
  644. 'errmsg': f'{file_name}不存在'
  645. })
  646. new_file = ThreeProofingResponsiblePersonImportFileStatus(
  647. file_uuid=filename,
  648. file_name = file_name_desc,
  649. status = '1',
  650. user_id=user_id
  651. )
  652. db.add(new_file)
  653. db.commit()
  654. background_tasks.add_task(import_data,db,file_path, user_id,new_file)
  655. # 返回创建成功的响应
  656. return {
  657. "code": 200,
  658. "msg": "成功",
  659. "data": None
  660. }
  661. except AppException as e:
  662. return {
  663. "code": 500,
  664. "msg": e.msg
  665. }
  666. except Exception as e:
  667. traceback.print_exc()
  668. # 处理异常
  669. db.rollback()
  670. raise HTTPException(status_code=500, detail=str(e))