schedule.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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.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.all()
  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. page: int = Query(1, gt=0),
  51. pageSize: int = Query(10, gt=0),
  52. db: Session = Depends(get_db),
  53. body = Depends(remove_xss_json),
  54. user_id = Depends(valid_access_token)
  55. ):
  56. try:
  57. # 根据 dict_type 查询字典数据
  58. # dict_data = db.query(SysDictData).filter_by(dict_type=dictType).all()
  59. query = db.query(DutySchedule)
  60. query = query.filter(DutySchedule.del_flag != '2')
  61. # 添加查询条件
  62. if keywords:
  63. user_list= name_get_user_id(db,keywords)
  64. duty_list = user_id_get_duty_id(db,user_list)
  65. query = query.filter(DutySchedule.id._in(duty_list))
  66. if duty_type:
  67. query = query.filter(DutySchedule.duty_type==duty_type)
  68. if start_time:
  69. start_time = datetime.strptime(start_time, "%Y-%m-%d").date()
  70. query = query.filter(DutySchedule.duty_date>=start_time)
  71. if end_time:
  72. end_time = datetime.strptime(end_time, "%Y-%m-%d").date()
  73. query = query.filter(DutySchedule.duty_date<=end_time)
  74. # 计算总记录数
  75. total_count = query.count()
  76. # 计算分页
  77. offset = (page - 1) * pageSize
  78. duty_data = query.offset(offset).limit(pageSize).all()
  79. # 转换为字典
  80. data_list = []
  81. for d in duty_data:
  82. user_data = []
  83. user_list = duty_id_get_user_id(db,d.id)
  84. for user_info in user_list:
  85. contact_info = user_id_get_info(db, user_info.personnel_id)
  86. user_data.append({"position_id":user_info.position_id,
  87. "personnel_id":user_info.personnel_id,
  88. "name":contact_info.name,
  89. "position":contact_info.position,
  90. "mobile_phone":contact_info.mobile_phone,
  91. "office_phone":contact_info.office_phone,
  92. "department_id":contact_info.department_id,
  93. "yzy_userid":contact_info.yzy_userid})
  94. data_list.append({
  95. "id": d.id,
  96. "start_time": d.start_time,
  97. "end_time":d.end_time,
  98. "duty_date": d.duty_date,
  99. "shift_type":d.shift_type,
  100. "duty_unit": d.duty_unit,
  101. "duty_type": d.duty_type,
  102. "createTime": d.create_time.strftime('%Y-%m-%d %H:%M:%S') if d.create_time else '',
  103. "user_data":user_data
  104. })
  105. # 构建返回结果
  106. result = {
  107. "total": total_count,
  108. "page": page,
  109. "pageSize": pageSize,
  110. "totalPages": (total_count + pageSize - 1) // pageSize,
  111. "data": data_list,
  112. "code": 200,
  113. "msg": "查询成功"
  114. }
  115. return result
  116. except Exception as e:
  117. # 处理异常
  118. traceback.print_exc()
  119. return JSONResponse(status_code=404, content={
  120. 'code': 404,
  121. 'msg': str(e)
  122. })
  123. @router.get('/info/{id}')
  124. async def get_dict_data_by_type(
  125. id: str ,
  126. db: Session = Depends(get_db),
  127. body = Depends(remove_xss_json),
  128. user_id = Depends(valid_access_token)
  129. ):
  130. try:
  131. # 根据 dict_type 查询字典数据
  132. # dict_data = dict_type_get_dict_data_info(db,'three_proofing')
  133. query = db.query(DutySchedule)
  134. # 添加查询条件
  135. # if dictType:
  136. query = query.filter(DutySchedule.id==id)
  137. query = query.filter(DutySchedule.del_flag != '2')
  138. d = query.first()
  139. user_data = []
  140. user_list = duty_id_get_user_id(db, d.id)
  141. for user_info in user_list:
  142. contact_info = user_id_get_info(db, user_info.personnel_id)
  143. user_data.append({"position_id": user_info.position_id,
  144. "personnel_id": user_info.personnel_id,
  145. "name": contact_info.name,
  146. "position": contact_info.position,
  147. "mobile_phone": contact_info.mobile_phone,
  148. "office_phone": contact_info.office_phone,
  149. "department_id": contact_info.department_id,
  150. "yzy_userid": contact_info.yzy_userid})
  151. data_list={
  152. "id": d.id,
  153. "start_time": d.start_time,
  154. "end_time": d.end_time,
  155. "duty_date": d.duty_date,
  156. "shift_type": d.shift_type,
  157. "duty_unit": d.duty_unit,
  158. "duty_type": d.duty_type,
  159. "createTime": d.create_time.strftime('%Y-%m-%d %H:%M:%S') if d.create_time else '',
  160. "user_data": user_data
  161. }
  162. # 构建返回结果
  163. result = {
  164. "data": data_list,
  165. "code": 200,
  166. "msg": "查询成功"
  167. }
  168. return result
  169. except Exception as e:
  170. # 处理异常
  171. traceback.print_exc()
  172. return JSONResponse(status_code=404, content={
  173. 'code': 404,
  174. 'msg': str(e)
  175. })
  176. @router.post('/create')
  177. async def create_dict_data(
  178. db: Session = Depends(get_db),
  179. body = Depends(remove_xss_json),
  180. user_id = Depends(valid_access_token)
  181. ):
  182. try:
  183. # 创建一个新的 SysDictData 实例
  184. user_data = body['user_data']
  185. if len(user_data)==0:
  186. return JSONResponse(status_code=404, content={
  187. 'errcode': 404,
  188. 'errmsg': '值班人员不能为空'
  189. })
  190. new_duty_data = DutySchedule(
  191. start_time=body['start_time'],
  192. end_time=body['end_time'],
  193. duty_date=body['duty_date'],
  194. shift_type=body['shift_type'],
  195. duty_unit=body['duty_unit'],
  196. duty_type=body['duty_type'],
  197. create_by=user_id
  198. )
  199. # 添加到会话并提交
  200. db.add(new_duty_data)
  201. db.commit()
  202. db.refresh(new_duty_data)
  203. user_list = []
  204. for user_info in user_data:
  205. user_list.appent( DutyPersonnelArrangement(
  206. duty_id=new_duty_data.id,
  207. position_id=user_info['position_id'],
  208. personnel_id=user_info['personnel_id'],
  209. create_by=user_id
  210. ))
  211. db.add_all(user_list)
  212. db.commit()
  213. # 构建返回结果
  214. result = {
  215. "code": 200,
  216. "msg": "操作成功",
  217. "data": None
  218. }
  219. return result
  220. except Exception as e:
  221. # 处理异常
  222. traceback.print_exc()
  223. return JSONResponse(status_code=404, content={
  224. 'code': 404,
  225. 'msg': str(e)
  226. })
  227. @router.put("/update")
  228. async def updata_dict_type(
  229. db: Session = Depends(get_db),
  230. body = Depends(remove_xss_json),
  231. user_id = Depends(valid_access_token)
  232. ):
  233. try:
  234. # 从请求数据创建一个新的 SysDictType 实例
  235. query = db.query(DutySchedule)
  236. query = query.filter(DutySchedule.id == body['id'])
  237. query = query.filter(DutySchedule.del_flag != '2')
  238. # query = db.query(SysDictData).filter(SysDictData.dict_code == form_data.dictCode)
  239. # query = db.query(SysDictData).filter(SysDictData.del_flag != '2')
  240. duty_data = query.first()
  241. old_user_list = duty_id_get_user_id(db,duty_data.id)
  242. for info in old_user_list:
  243. info.del_flag = '2'
  244. db.commit()
  245. user_data = body['user_data']
  246. if len(user_data)==0:
  247. return JSONResponse(status_code=404, content={
  248. 'errcode': 404,
  249. 'errmsg': '值班人员不能为空'
  250. })
  251. user_list = []
  252. for user_info in user_data:
  253. user_list.appent(DutyPersonnelArrangement(
  254. duty_id=duty_data.id,
  255. position_id=user_info['position_id'],
  256. personnel_id=user_info['personnel_id'],
  257. create_by=user_id
  258. ))
  259. db.add_all(user_list)
  260. if not dutyn_data:
  261. return JSONResponse(status_code=404, content={
  262. 'errcode': 404,
  263. 'errmsg': '值班不存在'
  264. })
  265. duty_data.start_time=body['start_time']
  266. duty_data.end_time = body['end_time']
  267. duty_data.duty_date = body['duty_date']
  268. duty_data.shift_type=body['shift_type']
  269. duty_data.duty_unit = body['duty_unit']
  270. duty_data.duty_type = body['duty_type']
  271. duty_data.update_by = user_id
  272. # 添加到数据库会话并提交
  273. db.commit()
  274. # db.refresh(new_dict_type) # 可选,如果需要刷新实例状态
  275. # 构建并返回响应
  276. return {
  277. "code": 200,
  278. "msg": "操作成功",
  279. "data": None # 根据你的响应示例,data 为 null
  280. }
  281. except Exception as e:
  282. # 处理异常
  283. traceback.print_exc()
  284. return JSONResponse(status_code=404, content={
  285. 'code': 404,
  286. 'msg': str(e)
  287. })
  288. @router.delete("/delete/{id}") # 使用 ID 来标识要删除的接口
  289. async def delete_dict_data(
  290. id: str,
  291. db: Session = Depends(get_db),
  292. body = Depends(remove_xss_json),
  293. user_id = Depends(valid_access_token)
  294. ):
  295. try:
  296. # 从数据库中获取要删除的 OneShareApiEntity 实例
  297. query = db.query(DutySchedule)
  298. query = query.filter(DutySchedule.id == id)
  299. query = query.filter(DutySchedule.del_flag != '2')
  300. position_data = query.first()
  301. # dict_data = db.query(SysDictData).filter(SysDictData.dict_code == dictCode and SysDictData.del_flag != '2').first()
  302. if not position_data:
  303. return JSONResponse(status_code=404, content={
  304. 'code': 404,
  305. 'msg': '值班不存在'
  306. })
  307. position_data.del_flag = '2'
  308. # 删除实例
  309. # db.delete(api)
  310. db.commit()
  311. # 构建并返回响应
  312. return {
  313. "code": 200,
  314. "msg": "操作成功",
  315. "data": None
  316. }
  317. except Exception as e:
  318. traceback.print_exc()
  319. return JSONResponse(status_code=404, content={
  320. 'code': 404,
  321. 'msg': str(e)
  322. })
  323. @router.delete("/delete_list/{id_list}") # 使用 ID 来标识要删除的接口
  324. async def delete_dict_data(
  325. id_list: str,
  326. db: Session = Depends(get_db),
  327. body = Depends(remove_xss_json),
  328. user_id = Depends(valid_access_token)
  329. ):
  330. try:
  331. # 从数据库中获取要删除的 OneShareApiEntity 实例
  332. id_list = [int(i) for i in id_list.split(',')]
  333. query = db.query(DutySchedule)
  334. query = query.filter(DutySchedule.id._in(id_list))
  335. query = query.filter(DutySchedule.del_flag != '2')
  336. position_data = query.all()
  337. # dict_data = db.query(SysDictData).filter(SysDictData.dict_code == dictCode and SysDictData.del_flag != '2').first()
  338. if not position_data:
  339. return JSONResponse(status_code=404, content={
  340. 'code': 404,
  341. 'msg': '值班不存在'
  342. })
  343. for info in position_data:
  344. info.del_flag = '2'
  345. # 删除实例
  346. # db.delete(api)
  347. db.commit()
  348. # 构建并返回响应
  349. return {
  350. "code": 200,
  351. "msg": "操作成功",
  352. "data": None
  353. }
  354. except Exception as e:
  355. traceback.print_exc()
  356. return JSONResponse(status_code=404, content={
  357. 'code': 404,
  358. 'msg': str(e)
  359. })