schedule.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends, Query, HTTPException, status
  4. from common.security import valid_access_token
  5. from fastapi.responses import JSONResponse
  6. from sqlalchemy.orm import Session
  7. from sqlalchemy import and_, or_
  8. from sqlalchemy.sql import func
  9. from common.auth_user import *
  10. from pydantic import BaseModel
  11. from database import get_db
  12. from typing import List
  13. from models import *
  14. from utils import *
  15. from utils.ry_system_util import *
  16. from utils.video_util import *
  17. import traceback
  18. import json
  19. router = APIRouter()
  20. def name_get_user_id(db,keywords):
  21. query = db.query(EmergencyContactUser)
  22. query = query.filter(EmergencyContactUser.del_flag != '2')
  23. query = query.filter(EmergencyContactUser.name.like(f"%{keywords}%"))
  24. data = query.all()
  25. return [info.id for info in data]
  26. def user_id_get_duty_id(db,user_id_list):
  27. query = db.query(DutyPersonnelArrangement)
  28. query = query.filter(DutyPersonnelArrangement.del_flag != '2')
  29. query = query.filter(DutyPersonnelArrangement.personnel_id._in(user_id_list))
  30. data = query.all()
  31. return [info.duty_id for info in data]
  32. def duty_id_get_user_id(db,duty_id):
  33. query = db.query(DutyPersonnelArrangement)
  34. query = query.filter(DutyPersonnelArrangement.del_flag != '2')
  35. query = query.filter(DutyPersonnelArrangement.duty_id==duty_id)
  36. data = query.all()
  37. return data
  38. def user_id_get_info(db,user_id):
  39. query = db.query(EmergencyContactUser)
  40. query = query.filter(EmergencyContactUser.del_flag != '2')
  41. query = query.filter(EmergencyContactUser.id==user_id)
  42. data = query.first()
  43. return data
  44. @router.get('/list')
  45. async def get_dict_data_by_type(
  46. keywords:str =Query(None),
  47. duty_type:str =Query(None),
  48. start_time:str =Query(None),
  49. end_time:str =Query(None),
  50. duty_unit:str =Query(None),
  51. page: int = Query(1, gt=0),
  52. pageSize: int = Query(10, gt=0),
  53. db: Session = Depends(get_db),
  54. body = Depends(remove_xss_json),
  55. user_id = Depends(valid_access_token)
  56. ):
  57. try:
  58. # 根据 dict_type 查询字典数据
  59. # dict_data = db.query(SysDictData).filter_by(dict_type=dictType).all()
  60. query = db.query(DutySchedule)
  61. query = query.filter(DutySchedule.del_flag != '2')
  62. # 添加查询条件
  63. if keywords:
  64. user_list= name_get_user_id(db,keywords)
  65. duty_list = user_id_get_duty_id(db,user_list)
  66. query = query.filter(DutySchedule.id._in(duty_list))
  67. if duty_type:
  68. query = query.filter(DutySchedule.duty_type==duty_type)
  69. if start_time:
  70. start_time = datetime.strptime(start_time, "%Y-%m-%d").date()
  71. query = query.filter(DutySchedule.duty_date>=start_time)
  72. if end_time:
  73. end_time = datetime.strptime(end_time, "%Y-%m-%d").date()
  74. query = query.filter(DutySchedule.duty_date<=end_time)
  75. if duty_unit:
  76. query = query.filter(DutySchedule.duty_unit==duty_unit)
  77. # 计算总记录数
  78. total_count = query.count()
  79. # 计算分页
  80. offset = (page - 1) * pageSize
  81. query = query.order_by(DutySchedule.update_time.desc())
  82. duty_data = query.offset(offset).limit(pageSize).all()
  83. # 转换为字典
  84. data_list = []
  85. for d in duty_data:
  86. user_data = []
  87. user_list = duty_id_get_user_id(db,d.id)
  88. for user_info in user_list:
  89. contact_info = user_id_get_info(db, user_info.personnel_id)
  90. user_data.append({"position_id":user_info.position_id,
  91. "personnel_id":user_info.personnel_id,
  92. "name":contact_info.name,
  93. "position":contact_info.position,
  94. "mobile_phone":contact_info.mobile_phone,
  95. "office_phone":contact_info.office_phone,
  96. "department_id":contact_info.department_id,
  97. "yzy_userid":contact_info.userid})
  98. data_list.append({
  99. "id": d.id,
  100. "start_time": d.start_time,
  101. "end_time":d.end_time,
  102. "duty_date": d.duty_date,
  103. "shift_type":d.shift_type,
  104. "duty_unit": d.duty_unit,
  105. "duty_type": d.duty_type,
  106. "createTime": d.create_time.strftime('%Y-%m-%d %H:%M:%S') if d.create_time else '',
  107. "user_data":user_data
  108. })
  109. # 构建返回结果
  110. result = {
  111. "total": total_count,
  112. "page": page,
  113. "pageSize": pageSize,
  114. "totalPages": (total_count + pageSize - 1) // pageSize,
  115. "data": data_list,
  116. "code": 200,
  117. "msg": "查询成功"
  118. }
  119. return result
  120. except Exception as e:
  121. # 处理异常
  122. traceback.print_exc()
  123. return JSONResponse(status_code=404, content={
  124. 'code': 404,
  125. 'msg': str(e)
  126. })
  127. @router.get('/latest_by_group')
  128. async def get_dict_data_by_type(
  129. keywords:str =Query(None),
  130. start_time:str =Query(None),
  131. end_time:str =Query(None),
  132. duty_unit:str =Query(None),
  133. page: int = Query(1, gt=0),
  134. pageSize: int = Query(10, gt=0),
  135. db: Session = Depends(get_db),
  136. body = Depends(remove_xss_json),
  137. user_id = Depends(valid_access_token)
  138. ):
  139. try:
  140. # 根据 dict_type 查询字典数据
  141. # dict_data = db.query(SysDictData).filter_by(dict_type=dictType).all()
  142. query = db.query(DutySchedule)
  143. query = query.filter(DutySchedule.del_flag != '2')
  144. # 添加查询条件
  145. if keywords:
  146. user_list= name_get_user_id(db,keywords)
  147. duty_list = user_id_get_duty_id(db,user_list)
  148. query = query.filter(DutySchedule.id._in(duty_list))
  149. if start_time:
  150. start_time = datetime.strptime(start_time, "%Y-%m-%d").date()
  151. query = query.filter(DutySchedule.duty_date>=start_time)
  152. if end_time:
  153. end_time = datetime.strptime(end_time, "%Y-%m-%d").date()
  154. query = query.filter(DutySchedule.duty_date<=end_time)
  155. if duty_unit:
  156. query = query.filter(DutySchedule.duty_unit==duty_unit)
  157. subquery = db.query(
  158. DutySchedule.duty_date,
  159. DutySchedule.shift_type,
  160. func.max(DutySchedule.update_time).label("latest_create_time")
  161. ).group_by(
  162. DutySchedule.duty_date,
  163. DutySchedule.shift_type
  164. ).subquery()
  165. query = query.join(
  166. subquery,
  167. and_(
  168. DutySchedule.duty_date == subquery.c.duty_date,
  169. DutySchedule.shift_type == subquery.c.shift_type,
  170. DutySchedule.update_time == subquery.c.latest_create_time
  171. )
  172. )
  173. # 计算总记录数
  174. total_count = query.count()
  175. # 计算分页
  176. offset = (page - 1) * pageSize
  177. query = query.order_by(DutySchedule.update_time.desc())
  178. duty_data = query.offset(offset).limit(pageSize).all()
  179. # 转换为字典
  180. data_list = []
  181. for d in duty_data:
  182. user_data = []
  183. user_list = duty_id_get_user_id(db,d.id)
  184. for user_info in user_list:
  185. contact_info = user_id_get_info(db, user_info.personnel_id)
  186. user_data.append({"position_id":user_info.position_id,
  187. "personnel_id":user_info.personnel_id,
  188. "name":contact_info.name,
  189. "position":contact_info.position,
  190. "mobile_phone":contact_info.mobile_phone,
  191. "office_phone":contact_info.office_phone,
  192. "department_id":contact_info.department_id,
  193. "yzy_userid":contact_info.yzy_userid})
  194. data_list.append({
  195. "id": d.id,
  196. "start_time": d.start_time,
  197. "end_time":d.end_time,
  198. "duty_date": d.duty_date,
  199. "shift_type":d.shift_type,
  200. "duty_unit": d.duty_unit,
  201. "duty_type": d.duty_type,
  202. "createTime": d.create_time.strftime('%Y-%m-%d %H:%M:%S') if d.create_time else '',
  203. "user_data":user_data
  204. })
  205. # 构建返回结果
  206. result = {
  207. "total": total_count,
  208. "page": page,
  209. "pageSize": pageSize,
  210. "totalPages": (total_count + pageSize - 1) // pageSize,
  211. "data": data_list,
  212. "code": 200,
  213. "msg": "查询成功"
  214. }
  215. return result
  216. except Exception as e:
  217. # 处理异常
  218. traceback.print_exc()
  219. return JSONResponse(status_code=404, content={
  220. 'code': 404,
  221. 'msg': str(e)
  222. })
  223. @router.get('/info/{id}')
  224. async def get_dict_data_by_type(
  225. id: str ,
  226. db: Session = Depends(get_db),
  227. body = Depends(remove_xss_json),
  228. user_id = Depends(valid_access_token)
  229. ):
  230. try:
  231. # 根据 dict_type 查询字典数据
  232. # dict_data = dict_type_get_dict_data_info(db,'three_proofing')
  233. query = db.query(DutySchedule)
  234. # 添加查询条件
  235. # if dictType:
  236. query = query.filter(DutySchedule.id==id)
  237. query = query.filter(DutySchedule.del_flag != '2')
  238. d = query.first()
  239. user_data = []
  240. user_list = duty_id_get_user_id(db, d.id)
  241. for user_info in user_list:
  242. contact_info = user_id_get_info(db, user_info.personnel_id)
  243. user_data.append({"position_id": user_info.position_id,
  244. "personnel_id": user_info.personnel_id,
  245. "name": contact_info.name,
  246. "position": contact_info.position,
  247. "mobile_phone": contact_info.mobile_phone,
  248. "office_phone": contact_info.office_phone,
  249. "department_id": contact_info.department_id,
  250. "yzy_userid": contact_info.yzy_userid})
  251. data_list={
  252. "id": d.id,
  253. "start_time": d.start_time,
  254. "end_time": d.end_time,
  255. "duty_date": d.duty_date,
  256. "shift_type": d.shift_type,
  257. "duty_unit": d.duty_unit,
  258. "duty_type": d.duty_type,
  259. "createTime": d.create_time.strftime('%Y-%m-%d %H:%M:%S') if d.create_time else '',
  260. "user_data": user_data
  261. }
  262. # 构建返回结果
  263. result = {
  264. "data": data_list,
  265. "code": 200,
  266. "msg": "查询成功"
  267. }
  268. return result
  269. except Exception as e:
  270. # 处理异常
  271. traceback.print_exc()
  272. return JSONResponse(status_code=404, content={
  273. 'code': 404,
  274. 'msg': str(e)
  275. })
  276. @router.post('/create')
  277. async def create_dict_data(
  278. db: Session = Depends(get_db),
  279. body = Depends(remove_xss_json),
  280. user_id = Depends(valid_access_token)
  281. ):
  282. try:
  283. # 创建一个新的 SysDictData 实例
  284. user_data = body['user_data']
  285. if len(user_data)==0:
  286. return JSONResponse(status_code=404, content={
  287. 'errcode': 404,
  288. 'errmsg': '值班人员不能为空'
  289. })
  290. new_duty_data = DutySchedule(
  291. start_time=body['start_time'],
  292. end_time=body['end_time'],
  293. duty_date=body['duty_date'],
  294. shift_type=body['shift_type'],
  295. duty_unit=body['duty_unit'],
  296. duty_type=body['duty_type'],
  297. create_by=user_id
  298. )
  299. # 添加到会话并提交
  300. db.add(new_duty_data)
  301. db.commit()
  302. db.refresh(new_duty_data)
  303. user_list = []
  304. for user_info in user_data:
  305. user_list.append( DutyPersonnelArrangement(
  306. duty_id=new_duty_data.id,
  307. position_id=user_info['position_id'],
  308. personnel_id=user_info['personnel_id'],
  309. create_by=user_id
  310. ))
  311. db.add_all(user_list)
  312. db.commit()
  313. # 构建返回结果
  314. result = {
  315. "code": 200,
  316. "msg": "操作成功",
  317. "data": None
  318. }
  319. return result
  320. except Exception as e:
  321. # 处理异常
  322. traceback.print_exc()
  323. return JSONResponse(status_code=404, content={
  324. 'code': 404,
  325. 'msg': str(e)
  326. })
  327. @router.put("/update")
  328. async def updata_dict_type(
  329. db: Session = Depends(get_db),
  330. body = Depends(remove_xss_json),
  331. user_id = Depends(valid_access_token)
  332. ):
  333. try:
  334. # 从请求数据创建一个新的 SysDictType 实例
  335. query = db.query(DutySchedule)
  336. query = query.filter(DutySchedule.id == body['id'])
  337. query = query.filter(DutySchedule.del_flag != '2')
  338. # query = db.query(SysDictData).filter(SysDictData.dict_code == form_data.dictCode)
  339. # query = db.query(SysDictData).filter(SysDictData.del_flag != '2')
  340. duty_data = query.first()
  341. old_user_list = duty_id_get_user_id(db,duty_data.id)
  342. for info in old_user_list:
  343. info.del_flag = '2'
  344. db.commit()
  345. user_data = body['user_data']
  346. if len(user_data)==0:
  347. return JSONResponse(status_code=404, content={
  348. 'errcode': 404,
  349. 'errmsg': '值班人员不能为空'
  350. })
  351. user_list = []
  352. for user_info in user_data:
  353. user_list.append(DutyPersonnelArrangement(
  354. duty_id=duty_data.id,
  355. position_id=user_info['position_id'],
  356. personnel_id=user_info['personnel_id'],
  357. create_by=user_id
  358. ))
  359. db.add_all(user_list)
  360. if not dutyn_data:
  361. return JSONResponse(status_code=404, content={
  362. 'errcode': 404,
  363. 'errmsg': '值班不存在'
  364. })
  365. duty_data.start_time=body['start_time']
  366. duty_data.end_time = body['end_time']
  367. duty_data.duty_date = body['duty_date']
  368. duty_data.shift_type=body['shift_type']
  369. duty_data.duty_unit = body['duty_unit']
  370. duty_data.duty_type = body['duty_type']
  371. duty_data.update_by = user_id
  372. # 添加到数据库会话并提交
  373. db.commit()
  374. # db.refresh(new_dict_type) # 可选,如果需要刷新实例状态
  375. # 构建并返回响应
  376. return {
  377. "code": 200,
  378. "msg": "操作成功",
  379. "data": None # 根据你的响应示例,data 为 null
  380. }
  381. except Exception as e:
  382. # 处理异常
  383. traceback.print_exc()
  384. return JSONResponse(status_code=404, content={
  385. 'code': 404,
  386. 'msg': str(e)
  387. })
  388. @router.delete("/delete/{id}") # 使用 ID 来标识要删除的接口
  389. async def delete_dict_data(
  390. id: str,
  391. db: Session = Depends(get_db),
  392. body = Depends(remove_xss_json),
  393. user_id = Depends(valid_access_token)
  394. ):
  395. try:
  396. # 从数据库中获取要删除的 OneShareApiEntity 实例
  397. query = db.query(DutySchedule)
  398. query = query.filter(DutySchedule.id == id)
  399. query = query.filter(DutySchedule.del_flag != '2')
  400. position_data = query.first()
  401. # dict_data = db.query(SysDictData).filter(SysDictData.dict_code == dictCode and SysDictData.del_flag != '2').first()
  402. if not position_data:
  403. return JSONResponse(status_code=404, content={
  404. 'code': 404,
  405. 'msg': '值班不存在'
  406. })
  407. position_data.del_flag = '2'
  408. # 删除实例
  409. # db.delete(api)
  410. db.commit()
  411. # 构建并返回响应
  412. return {
  413. "code": 200,
  414. "msg": "操作成功",
  415. "data": None
  416. }
  417. except Exception as e:
  418. traceback.print_exc()
  419. return JSONResponse(status_code=404, content={
  420. 'code': 404,
  421. 'msg': str(e)
  422. })
  423. @router.delete("/delete_list/{id_list}") # 使用 ID 来标识要删除的接口
  424. async def delete_dict_data(
  425. id_list: str,
  426. db: Session = Depends(get_db),
  427. body = Depends(remove_xss_json),
  428. user_id = Depends(valid_access_token)
  429. ):
  430. try:
  431. # 从数据库中获取要删除的 OneShareApiEntity 实例
  432. id_list = [int(i) for i in id_list.split(',')]
  433. query = db.query(DutySchedule)
  434. query = query.filter(DutySchedule.id._in(id_list))
  435. query = query.filter(DutySchedule.del_flag != '2')
  436. position_data = query.all()
  437. # dict_data = db.query(SysDictData).filter(SysDictData.dict_code == dictCode and SysDictData.del_flag != '2').first()
  438. if not position_data:
  439. return JSONResponse(status_code=404, content={
  440. 'code': 404,
  441. 'msg': '值班不存在'
  442. })
  443. for info in position_data:
  444. info.del_flag = '2'
  445. # 删除实例
  446. # db.delete(api)
  447. db.commit()
  448. # 构建并返回响应
  449. return {
  450. "code": 200,
  451. "msg": "操作成功",
  452. "data": None
  453. }
  454. except Exception as e:
  455. traceback.print_exc()
  456. return JSONResponse(status_code=404, content={
  457. 'code': 404,
  458. 'msg': str(e)
  459. })