forked from iti-framework/iTi-Flask
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.0 KiB
Python
107 lines
3.0 KiB
Python
from apiflask import APIBlueprint
|
|
from iti.applications.extensions import db, sys_log
|
|
from iti.applications.common.utils import success, page_schema, page
|
|
from iti.applications.models import (
|
|
IotWorkshop,
|
|
IotWorkshopSchema,
|
|
)
|
|
from .schemas.workshop import (
|
|
WorkshopQuery,
|
|
WorkshopAddRequest,
|
|
WorkshopUpdateRequest,
|
|
)
|
|
from iti.applications.common import ModelFilter
|
|
from iti.applications.common.exceptions.biz_exp import BizException
|
|
from flask_jwt_extended import jwt_required, current_user
|
|
from sqlalchemy import select, delete, exists
|
|
from sqlalchemy.orm import noload
|
|
from iti.applications.common import permission
|
|
|
|
bp = APIBlueprint("iot_workshop", __name__, url_prefix="/workshop", tag="车间管理")
|
|
|
|
|
|
@bp.get("/list")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:workshop:list")
|
|
@bp.input(WorkshopQuery.Schema(partial=True), location="query")
|
|
@bp.output(IotWorkshopSchema(many=True))
|
|
def list_workshop(query_data: WorkshopQuery):
|
|
"""
|
|
获取车间列表
|
|
"""
|
|
|
|
return success(get_list_or_page(query_data))
|
|
|
|
|
|
@bp.get("/page")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:workshop:list")
|
|
@bp.input(WorkshopQuery.Schema(partial=True), location="query")
|
|
@bp.output(page_schema(IotWorkshopSchema(many=True)))
|
|
def page_user(query_data: WorkshopQuery):
|
|
"""
|
|
分页获取车间列表
|
|
"""
|
|
|
|
return page(get_list_or_page(query_data))
|
|
|
|
|
|
@bp.post("/add")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:workshop:add")
|
|
@bp.input(WorkshopAddRequest, location="json")
|
|
def add_workshop(json_data: dict):
|
|
"""
|
|
添加车间信息
|
|
"""
|
|
|
|
workshop = IotWorkshop(**json_data)
|
|
workshop.status = 0
|
|
db.session.add(workshop)
|
|
db.session.commit()
|
|
return success()
|
|
|
|
@bp.put("/<string:id>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:workshop:update")
|
|
@bp.input(WorkshopUpdateRequest(partial=True), location="json")
|
|
def update_workshop(id: str, json_data: dict):
|
|
"""
|
|
更新车间信息
|
|
"""
|
|
|
|
workshop = db.session.scalar(select(IotWorkshop).filter_by(id=id))
|
|
if not workshop:
|
|
raise BizException("车间信息不存在")
|
|
for key, value in json_data.items():
|
|
if value is not None:
|
|
setattr(workshop, key, value)
|
|
|
|
db.session.commit()
|
|
|
|
return success()
|
|
|
|
|
|
def get_list_or_page(query_data: WorkshopQuery):
|
|
"""
|
|
获取车间信息列表或分页
|
|
"""
|
|
query = select(IotWorkshop).order_by(IotWorkshop.created_at.desc())
|
|
if query_data.keyword:
|
|
kw = ModelFilter.escape_like(query_data.keyword)
|
|
query = query.filter(
|
|
IotWorkshop.workshop_name.like(f"%{kw}%")
|
|
| IotWorkshop.workshop_number.like(f"%{kw}%")
|
|
| IotWorkshop.director_name.like(f"%{kw}%")
|
|
)
|
|
if query_data.status:
|
|
query = query.filter(IotWorkshop.status == query_data.status)
|
|
if query_data.page and query_data.size:
|
|
return db.paginate(query, page=query_data.page, per_page=query_data.size)
|
|
else:
|
|
return db.session.scalars(query).all()
|