baoyubo 1 月之前
父节点
当前提交
c814d21cb1
共有 1 个文件被更改,包括 143 次插入88 次删除
  1. 143 88
      routers/api/dutyManagement/schedule.py

+ 143 - 88
routers/api/dutyManagement/schedule.py

@@ -634,6 +634,8 @@ async def delete_dict_data(
         })
 
 
+# 自定义排班 导出
+@router.post('/zdypb/createImport')
 # 值班排班 导入
 @router.post('/zbpb/createImport')
 async def create_contact(
@@ -649,6 +651,9 @@ async def create_contact(
         filename = body['filename']
         file_name_desc = body['file_name_desc']
         duty_unit = body['duty_unit']
+        duty_type = ''
+        if 'duty_type' in body:
+            duty_type = body['duty_type']
         if len(filename) == 0:
             raise Exception()
 
@@ -660,40 +665,148 @@ async def create_contact(
                 'errcode': 404,
                 'errmsg': f'{filename}不存在'
             })
+        msg = '成功'
+        code =200
+        try:
+            book = xlrd.open_workbook(file_path)
+            sheet = book.sheet_by_index(0)
+        except:
+            msg = f'\n文件打开失败,请核实文件格式为xlsx/xlx>'
+            code = 500
+            return JSONResponse(status_code=code, content={
+                "code": code,
+                "msg": msg,
+                "data": None
+            })
+        duty_data = {}
+        duty_persion_data = {}
+        import_status = True
+        for row in range(2, sheet.nrows):
+            user_data = []
+            start_date = sheet.cell(row, 0).value
+            if start_date == '':
+                msg =  f'\n行<{row + 1}>日期不能为空<{start_date}>'
+                code = 500
+            start_time = sheet.cell(row, 1).value
+            if start_time == '':
+                msg =  f'\n行<{row + 1}>日期不能为空<{start_time}>'
+                code = 500
+            end_date = sheet.cell(row, 2).value
+            if end_date == '':
+                msg =  f'\n行<{row + 1}>日期不能为空<{end_date}>'
+                code = 500
+            end_time = sheet.cell(row, 3).value
+            if end_time == '':
+                msg =  f'\n行<{row + 1}>日期不能为空<{end_time}>'
+                code = 500
+            dbld = sheet.cell(row, 4).value
+            if dbld == '':
+                msg =  f'\n行<{row + 1}>日期不能为空<{dbld}>'
+                code = 500
+            for name in  dbld.split(','):
+                personnel_id_list = name_get_user_id(db, name)
+                if len(personnel_id_list)==0:
+                    import_status  = False
+                    msg = f'\n行<{row + 1}>非应急通讯录人员<{name}>'
+                    code = 500
+                for personnel_id in personnel_id_list:
+                    user_data.append({"position_id":1,"personnel_id":personnel_id})
+            kjdb = sheet.cell(row, 5).value
+            if kjdb == '':
+                msg =  f'\n行<{row + 1}>日期不能为空<{kjdb}>'
+                code = 500
+            for name in  kjdb.split(','):
+                personnel_id_list = name_get_user_id(db, name)
+                if len(personnel_id_list)==0:
+                    import_status  = False
+                    msg = f'\n行<{row + 1}>非应急通讯录人员<{name}>'
+                    code = 500
+                for personnel_id in personnel_id_list:
+                    user_data.append({"position_id":1,"personnel_id":personnel_id})
+            zb = sheet.cell(row, 6).value
+            if zb == '':
+                msg =  f'\n行<{row + 1}>日期不能为空<{zb}>'
+                code = 500
+            for name in  zb.split(','):
+                personnel_id_list = name_get_user_id(db, name)
+                if len(personnel_id_list)==0:
+                    import_status  = False
+                    msg = f'\n行<{row + 1}>非应急通讯录人员<{name}>'
+                    code = 500
+                for personnel_id in personnel_id_list:
+                    user_data.append({"position_id":1,"personnel_id":personnel_id})
+            zz = sheet.cell(row, 7).value
+            if zz == '':
+                msg =  f'\n行<{row + 1}>日期不能为空<{zz}>'
+                code = 500
+            for name in  zz.split(','):
+                personnel_id_list = name_get_user_id(db, name)
+                if len(personnel_id_list)==0:
+                    import_status  = False
+                    msg = f'\n行<{row + 1}>非应急通讯录人员<{name}>'
+                    code = 500
+                for personnel_id in personnel_id_list:
+                    user_data.append({"position_id":1,"personnel_id":personnel_id})
+            if start_date == end_date:
+                shift_type = '1'
+            else:
+                if start_time==end_time:
+                    shift_type = '2'
+                else:
+                    shift_type = '3'
 
-        # new_file = ThreeProofingResponsiblePersonImportFileStatus(
-        #     file_uuid=filename,
-        #     file_name=file_name_desc,
-        #     status='1',
-        #     remark='',
-        #     user_id=user_id
-        # )
-        # db.add(new_file)
-        # db.commit()
-        # background_tasks.add_task(import_data, db, file_path, user_id, new_file)
-        #
-        # db_czrz.log(db, auth_user, "系统管理", f"后台管理导入三防责任人管理人员信息成功", request.client.host)
-
-        # 返回创建成功的响应
-        return {
-            "code": 200,
-            "msg": "成功",
-            "data": None
-        }
-
-    except AppException as e:
-        return {
-            "code": 500,
-            "msg": e.msg
-        }
-
+            new_duty_data = DutySchedule(
+                start_time=f'{start_date} {start_time}',
+                end_time=f'{end_date} {end_time}',
+                duty_date=start_date,
+                shift_type=shift_type,
+                duty_unit=duty_unit,
+                duty_type=duty_type,
+                create_by=user_id
+            )
+            duty_data[start_date+str(shift_type)] = new_duty_data
+            # 添加到会话并提交
+            duty_persion_data[start_date + str(shift_type)] = user_data
+            # db.add(new_duty_data)
+            # db.commit()
+            # db.refresh(new_duty_data)
+            # user_list = []
+            # for user_info in user_data:
+            #     user_list.append(DutyPersonnelArrangement(
+            #         duty_id=new_duty_data.id,
+            #         position_id=user_info['position_id'],
+            #         personnel_id=user_info['personnel_id'],
+            #         create_by=user_id
+            #     ))
+            # db.add_all(user_list)
+            # db.commit()
+        if import_status:
+            for duty in duty_data:
+                new_duty_data = duty_data[duty]
+                db.add(new_duty_data)
+                db.commit()
+                db.refresh(new_duty_data)
+                user_list = []
+                for user_info in duty_persion_data[duty]:
+                    user_list.append(DutyPersonnelArrangement(
+                        duty_id=new_duty_data.id,
+                        position_id=user_info['position_id'],
+                        personnel_id=user_info['personnel_id'],
+                        create_by=user_id
+                    ))
+                db.add_all(user_list)
+                db.commit()
     except Exception as e:
         traceback.print_exc()
         # 处理异常
         db.rollback()
-        raise HTTPException(status_code=500, detail=str(e))
-
-
+        code = 500
+        msg = str(e)
+    return JSONResponse(status_code=code, content={
+        "code": code,
+        "msg": msg,
+        "data": None
+    })
 # 值班排班 导出
 @router.get("/zbpb/export")
 async def download_file(keywords:str =Query(None),
@@ -804,64 +917,6 @@ async def download_file(keywords:str =Query(None),
         raise HTTPException(status_code=500, detail=str(e))
 
 
-# 自定义排班 导出
-@router.post('/zdypb/createImport')
-async def create_contact(
-        request: Request,
-        background_tasks: BackgroundTasks,
-        db: Session = Depends(get_db),
-        body=Depends(remove_xss_json),
-        auth_user: AuthUser = Depends(find_auth_user),
-        user_id=Depends(valid_access_token)
-):
-    try:
-        # 提取请求数据
-        filename = body['filename']
-        file_name_desc = body['file_name_desc']
-        duty_unit = body['duty_unit']
-        if len(filename) == 0:
-            raise Exception()
-
-        file_path = f'/data/upload/mergefile/uploads/{filename}'
-
-        # 检查文件是否存在
-        if not os.path.isfile(file_path):
-            return JSONResponse(status_code=404, content={
-                'errcode': 404,
-                'errmsg': f'{filename}不存在'
-            })
-
-        # new_file = ThreeProofingResponsiblePersonImportFileStatus(
-        #     file_uuid=filename,
-        #     file_name=file_name_desc,
-        #     status='1',
-        #     remark='',
-        #     user_id=user_id
-        # )
-        # db.add(new_file)
-        # db.commit()
-        # background_tasks.add_task(import_data, db, file_path, user_id, new_file)
-        #
-        # db_czrz.log(db, auth_user, "系统管理", f"后台管理导入三防责任人管理人员信息成功", request.client.host)
-
-        # 返回创建成功的响应
-        return {
-            "code": 200,
-            "msg": "成功",
-            "data": None
-        }
-
-    except AppException as e:
-        return {
-            "code": 500,
-            "msg": e.msg
-        }
-
-    except Exception as e:
-        traceback.print_exc()
-        # 处理异常
-        db.rollback()
-        raise HTTPException(status_code=500, detail=str(e))
 
 
 # 本单位值班 导出
@@ -942,7 +997,7 @@ async def download_file(keywords:str =Query(None),
                     leaders.append(contact_info.name)
                 user_data.append(contact_info.name)
             data_list.append({
-                "单位名称": d.duty_unit,
+                "单位名称": dept_id_get_info(db,d.duty_unit).department_name,
                 "开始时间": d.start_time.strftime('%Y-%m-%d %H:%M:%S') if d.start_time else '',
                 "结束时间": d.end_time.strftime('%Y-%m-%d %H:%M:%S') if d.end_time else '',
                 "带班领导": "、".join(leaders),