import json import os from flask import request, jsonify, g from flask_restx import Resource, Namespace, reqparse from sqlalchemy import select, insert, update, delete from sqlalchemy.orm import Session from werkzeug.datastructures import FileStorage from app.configs.config import TEMPLATE_FILE_PATH, TEMPLATE_FILE_URL from app.database import engine from app.defines import StatesCode, Module, OperationType from app.modle.template import Template from app.utils.jwt_util import login_required from app.utils.save_log import save_log from app.utils.util import to_dict, cn_now ns = Namespace('data', description='数据管理接口') template_list = reqparse.RequestParser(bundle_errors=True) template_list.add_argument(name='page_size', type=int, location='args', required=False, help='每页记录数量,默认:20') template_list.add_argument(name='page', type=int, location='args', required=False, help='第几页') template_list.add_argument(name='template_name', type=str, location='args', required=False, help='模版名称') template_list.add_argument(name='template_format', type=str, location='args', required=False, help='模版格式') template_list.add_argument(name='template_type', type=str, location='args', required=False, help='模版类型 0:报表,1:报告') template_list.add_argument(name='report_type', type=str, location='args', required=False, help='报告类型') @ns.route('/template_list') class TemplateConfigListApi(Resource): method_decorators = [login_required] @ns.doc(id='template_list', description='模版列表') @ns.expect(template_list) def get(self): """获取模版列表""" page_size = int(request.args.get('page_size', 20)) page = int(request.args.get('page', 1)) template_name = request.args.get('template_name') template_format = request.args.get('template_format') report_type = request.args.get('report_type') template_type = request.args.get('template_type') with Session(engine) as session: stmt = select(Template).where(Template.template_type == template_type) if template_name: stmt.where(Template.name == template_name) if template_format: stmt.where(Template.format == template_format) if report_type: stmt.where(Template.report_type == report_type) stmt.offset(page_size * (page - 1)).limit(page_size) results = session.execute(stmt).scalars().all() save_log(request, Module.DATA, OperationType.INQUIRE, StatesCode.SUCCESS) return jsonify(code=StatesCode.SUCCESS, message='成功', data=to_dict(results)) template = reqparse.RequestParser(bundle_errors=True) template.add_argument(name='template_id', type=int, location='args', required=False, help='模版id') template_details = reqparse.RequestParser(bundle_errors=True) template_details.add_argument(name='template_id', type=int, location='form', required=False, help='模版id') template_details.add_argument(name='template_name', type=str, location='form', required=False, help='模版名称') template_details.add_argument(name='template_format', type=str, location='form', required=False, help='模版格式') template_details.add_argument(name='report_type', type=str, location='form', required=False, help='报告类型') template_details.add_argument(name='introduction', type=str, location='form', required=False, help='模版简介') template_details.add_argument(name='template_file', type=FileStorage, location='files', required=False, help='模版文件') template_details.add_argument(name='template_type', type=int, location='files', required=False, help='模版类型 0:报表,1:报告') @ns.route('/template') class TemplateConfigApi(Resource): # method_decorators = [login_required] @ns.doc(id='get_template', description='获取模版详情') @ns.expect(template) def get(self): """获取模版详情""" template_id = request.args.get('template_id') if template_id is None: return jsonify(code=StatesCode.UNKNOWN_ERROR, message='模版id不能为空') with Session(engine) as session: stmt = select(Template).where(Template.id == template_id) results = session.execute(stmt).scalars().all() save_log(request, Module.DATA, OperationType.INQUIRE, StatesCode.SUCCESS) return jsonify(code=StatesCode.SUCCESS, message='获取成功', data=to_dict(results)) @ns.doc(id='add_template', description='添加报表模版') @ns.expect(template_details) def post(self): """添加模版""" template_name = request.form.get('template_name') template_format = request.form.get('template_format') introduction = request.form.get('introduction') template_file = request.files.get('template_file') report_type = request.form.get('report_type') template_type = request.form.get('template_type') if template_name is None and template_type is None: return jsonify(code=StatesCode.UNKNOWN_ERROR, message='报表模版名和模版类型不能为空') # 模版存储(url), if template_file: template_file.save(os.path.join(TEMPLATE_FILE_PATH, template_file.filename)) template_url = TEMPLATE_FILE_URL + template_file.filename else: template_url = None with Session(engine) as session: # 判断是否存在 stmt = select(Template).where(Template.name == template_name) result = session.execute(stmt).scalars().first() if result: return jsonify(code=StatesCode.UNKNOWN_ERROR, message="模版已存在") # 添加 stmt = insert(Template).values( name=template_name, format=template_format, Introduction=introduction, report_type=report_type, creator=g.user_name, create_time=cn_now(), update_time=cn_now(), template_url=template_url, template_type=template_type ) session.execute(stmt) session.commit() save_log(request, Module.DATA, OperationType.ADD, StatesCode.SUCCESS) return jsonify(code=StatesCode.SUCCESS, message='添加成功') @ns.doc(id='update_templatet', description='修改模版信息') @ns.expect(template_details) def put(self): """修改模版""" template_id = request.form.get('template_id') template_name = request.form.get('template_name') template_format = request.form.get('template_format') introduction = request.form.get('introduction') template_file = request.files.get('template_file') report_type = request.form.get('report_type') template_type = request.form.get('template_type') if template_id is None or template_name is None: return jsonify(code=StatesCode.UNKNOWN_ERROR, message="模版id和模版名不能为空") # 报表模版存储(url), if template_file: template_file.save(os.path.join(TEMPLATE_FILE_PATH, template_file.filename)) template_url = TEMPLATE_FILE_URL + template_file.filename else: template_url = None with Session(engine) as session: stmt = update(Template).where(Template.id == template_id).values( name=template_name, format=template_format, Introduction=introduction, report_type=report_type, creator=g.user_name, create_time=cn_now(), update_time=cn_now(), template_url=template_url, template_type=template_type ) session.execute(stmt) session.commit() save_log(request, Module.DATA, OperationType.UPDATE, StatesCode.SUCCESS) return jsonify(code=StatesCode.SUCCESS, message='修改成功') @ns.doc(id='delete_template', description='删除模版') @ns.expect(template_details) def delete(self): """删除模版""" template_id = request.form.get('template_id') if template_id is None: return jsonify(code=StatesCode.UNKNOWN_ERROR, message="模版id不能为空") with Session(engine) as session: stmt = delete(Template).where(Template.id == template_id) session.execute(stmt) session.commit() save_log(request, Module.DATA, OperationType.DELETE, StatesCode.SUCCESS) return jsonify(code=StatesCode.SUCCESS, message='删除成功') batch_delete_template = reqparse.RequestParser(bundle_errors=True) batch_delete_template.add_argument(name='template_ids', type=str, required=True, location='form', help='模版id列表') @ns.route('/batch_delete_template') class BatchTemplateConfigApi(Resource): # method_decorators = [login_required] @ns.doc(id='batch_delete_template', description='批量删除模版') @ns.expect(batch_delete_template) def delete(self): """批量删除模版""" template_ids = request.form.get('template_ids') if template_ids is not None: template_ids = json.loads(template_ids) else: return jsonify(code=StatesCode.UNKNOWN_ERROR, message='模版id不能为空') with Session(engine) as session: stmt = select(Template).where(Template.id.in_(template_ids)) results = session.execute(stmt).scalars().all() for result in results: session.delete(result) session.commit() save_log(request, Module.DATA, OperationType.BATCH_DELETE, StatesCode.SUCCESS) return jsonify(code=StatesCode.SUCCESS, message='批量删除成功') @ns.route('/company') class CompanyApi(Resource): def get(self): """获取公司基本信息""" return jsonify(code=StatesCode.SUCCESS, message='获取成功', data='') def put(self): """修改公司基本信息""" return jsonify(code=StatesCode.SUCCESS, message='修改成功', data='') @ns.route('/building_list') class BuildingListApi(Resource): def get(self): """获取楼宇列表""" return jsonify(code=StatesCode.SUCCESS, message='获取成功', data='') @ns.route('/building') class BuildingApi(Resource): def get(self): """获取楼宇信息""" return jsonify(code=StatesCode.SUCCESS, message='获取成功', data='') def post(self): """添加楼宇""" return jsonify(code=StatesCode.SUCCESS, message='添加成功', data='') def put(self): """修改楼宇""" return jsonify(code=StatesCode.SUCCESS, message='修改成功', data='') def delete(self): """删除楼宇""" return jsonify(code=StatesCode.SUCCESS, message='删除成功', data='') @ns.route('/underlying_system') class UnderlyingSystemMessageApi(Resource): def post(self): """添加底层系统信息""" return jsonify(code=StatesCode.SUCCESS, message='添加成功', data='')