diff --git a/api/api_v1/endpoints/worker_info.py b/api/api_v1/endpoints/worker_info.py index b577926..0070760 100644 --- a/api/api_v1/endpoints/worker_info.py +++ b/api/api_v1/endpoints/worker_info.py @@ -107,3 +107,43 @@ async def update_worker( data = await crud.worker_info.update_worker(db, data_in.worker_query, data_in.data_in) return schemas.Msg(code=200, msg='ok', data=data) + + +# 添加统计数据 +@router.post("/add_count_info") +async def add_count_info( + request: Request, + data_in: schemas.CountInWorkerQuery, + db: AsyncIOMotorDatabase = Depends(get_database) +) -> schemas.Msg: + """ 增加一条统计数据 """ + count_id = data_in.count_id + in_data = await crud.count_in_worker.find_one_count(db, count_id) + # 本月数据已添加 + if in_data: + return schemas.Msg(code=-10, msg='本月数据已添加', data=None) + worker_data = await crud.worker_info.find_worker_some(db, findlist=['name', 'work_in']) + insert_data = { + 'count_id': count_id + } + for chk_data in worker_data: + worker_in = chk_data['work_in'] + key = str(datetime.strptime(worker_in, '%Y-%m-%d').year) + if key not in insert_data: + insert_data[key] = 1 + continue + insert_data[key] += 1 + data = await crud.count_in_worker.insert_count_info(db, insert_data) + return schemas.Msg(code=200, msg='ok', data=data) + + +# 查询统计数据 +@router.post("/find_count_info") +async def find_count_info( + request: Request, + db: AsyncIOMotorDatabase = Depends(get_database) +) -> schemas.Msg: + """ 查询所有统计数据 """ + + data = await crud.count_in_worker.find_count_some(db) + return schemas.Msg(code=200, msg='ok', data=data) diff --git a/crud/__init__.py b/crud/__init__.py index 6feea1e..c467ad4 100644 --- a/crud/__init__.py +++ b/crud/__init__.py @@ -29,3 +29,4 @@ from .crud_operate_log import operate_log from .crud_interview_record import interview_record from .crud_worker_info import worker_info from .crud_owner_info import owner_info +from .crud_count_in_worker import count_in_worker diff --git a/crud/crud_count_in_worker.py b/crud/crud_count_in_worker.py new file mode 100644 index 0000000..170a5e9 --- /dev/null +++ b/crud/crud_count_in_worker.py @@ -0,0 +1,33 @@ +from motor.motor_asyncio import AsyncIOMotorDatabase +import schemas +from crud.base import CRUDBase + +__all__ = 'count_in_worker', + + +class CRUDCountInWorker(CRUDBase): + # 获取所有在职人员统计数据 + async def all_count_info(self, db: AsyncIOMotorDatabase): + return await self.find_many(db, {}, {'_id': 0}) + + # 获取指定字段 + async def find_count_some(self, db: AsyncIOMotorDatabase, findlist=[]): + findWhere = {'_id': 0} + if findlist: + for key in findlist: + findWhere.update({ + key: 1 + }) + return await self.find_many(db, {}, findWhere) + + # 查询单条数据 + async def find_one_count(self, db: AsyncIOMotorDatabase, count_id): + return await self.find_one(db, {'count_id': count_id}, {'_id': 0}) + + # 插入一条新的在职人员统计数据 + async def insert_count_info(self, db: AsyncIOMotorDatabase, insertdata): + + await self.insert_one(db, insertdata) + + +count_in_worker = CRUDCountInWorker('count_in_worker') diff --git a/schemas/__init__.py b/schemas/__init__.py index 4639be1..4d6ce63 100644 --- a/schemas/__init__.py +++ b/schemas/__init__.py @@ -35,3 +35,4 @@ from .interview_modes import * from .interview_record import * from .worker import * from .owner_info import * +from .count_in_worker import * diff --git a/schemas/count_in_worker.py b/schemas/count_in_worker.py new file mode 100644 index 0000000..a8bfe59 --- /dev/null +++ b/schemas/count_in_worker.py @@ -0,0 +1,12 @@ +from typing import Dict, List, Union, Any +from pydantic import BaseModel +from datetime import datetime + + +def get_id(): + return datetime.strftime(datetime.now(), '%Y-%m') + + +# 统计在职人员数据添加格式 +class CountInWorkerQuery(BaseModel): + count_id: str = get_id() diff --git a/接口文档/在职年份统计接口文档.txt b/接口文档/在职年份统计接口文档.txt new file mode 100644 index 0000000..fb687e1 --- /dev/null +++ b/接口文档/在职年份统计接口文档.txt @@ -0,0 +1,45 @@ +·: /api/v1/worker/add_count_info # һͳݽӿ + +: # +{} + + +ֵ1: +{ + "code": 200, + "msg": "ok", + "data": null +} + +ֵ2: +{ + "code": -10, + "msg": "", + "data": null +} + + + +·: /api/v1/worker/find_count_info # ѯͳݽӿ + +: # +{} + + +ֵ: +{ + "code": 200, + "msg": "ok", + "data": [ + { + "2017": 1, # ְݶӦ + "2021": 3, # ְݶӦ + "count_id": "2022-07" # ͳ + }, + { + "2017": 1, + "2021": 3, + "count_id": "2022-06" + } + ] +} \ No newline at end of file