| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- """Export API routes for Excel report generation."""
- from flask import request, send_file
- from flask_restx import Namespace, Resource, fields
- from app.services.export_service import ExportService
- from app.utils.auth_decorator import require_auth
- export_ns = Namespace('export', description='Excel导出接口')
- # Response models for Swagger documentation
- error_response = export_ns.model('ExportErrorResponse', {
- 'success': fields.Boolean(description='操作是否成功'),
- 'error': fields.String(description='错误信息'),
- 'code': fields.String(description='错误代码')
- })
- @export_ns.route('/monthly')
- class MonthlyExport(Resource):
- """Resource for monthly Excel export."""
-
- @export_ns.doc('export_monthly')
- @export_ns.param('year', '年份 (例如: 2024)', required=True, type=int)
- @export_ns.param('month', '月份 (1-12)', required=True, type=int)
- @export_ns.response(200, 'Excel文件下载')
- @export_ns.response(400, '参数错误', error_response)
- @export_ns.response(500, '服务器错误', error_response)
- @export_ns.produces(['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'])
- @require_auth
- def get(self):
- """导出月度工作报表Excel
-
- 生成包含以下内容的Excel文件:
- - 明细表: 人员、日期、物品、单价、数量、总价
- - 月度汇总: 每人总金额及合计
- """
- # Get and validate parameters
- year = request.args.get('year', type=int)
- month = request.args.get('month', type=int)
-
- if year is None:
- return {
- 'success': False,
- 'error': 'Year parameter is required',
- 'code': 'VALIDATION_ERROR'
- }, 400
-
- if month is None:
- return {
- 'success': False,
- 'error': 'Month parameter is required',
- 'code': 'VALIDATION_ERROR'
- }, 400
-
- try:
- excel_file, error = ExportService.export_monthly(year, month)
-
- if error:
- return {
- 'success': False,
- 'error': error,
- 'code': 'VALIDATION_ERROR'
- }, 400
-
- filename = f'work_report_{year}_{month:02d}.xlsx'
-
- return send_file(
- excel_file,
- mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- as_attachment=True,
- download_name=filename
- )
- except Exception as e:
- return {
- 'success': False,
- 'error': f'Failed to generate Excel: {str(e)}',
- 'code': 'SERVER_ERROR'
- }, 500
- @export_ns.route('/yearly')
- class YearlyExport(Resource):
- """Resource for yearly Excel export."""
-
- @export_ns.doc('export_yearly')
- @export_ns.param('year', '年份 (例如: 2024)', required=True, type=int)
- @export_ns.response(200, 'Excel文件下载')
- @export_ns.response(400, '参数错误', error_response)
- @export_ns.response(500, '服务器错误', error_response)
- @export_ns.produces(['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'])
- @require_auth
- def get(self):
- """导出年度工作报表Excel
-
- 生成包含以下内容的Excel文件:
- - 明细表: 人员、日期、物品、单价、数量、总价
- - 年度汇总: 每人按月统计及年度合计
- """
- # Get and validate parameters
- year = request.args.get('year', type=int)
-
- if year is None:
- return {
- 'success': False,
- 'error': 'Year parameter is required',
- 'code': 'VALIDATION_ERROR'
- }, 400
-
- try:
- excel_file, error = ExportService.export_yearly(year)
-
- if error:
- return {
- 'success': False,
- 'error': error,
- 'code': 'VALIDATION_ERROR'
- }, 400
-
- filename = f'work_report_{year}.xlsx'
-
- return send_file(
- excel_file,
- mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- as_attachment=True,
- download_name=filename
- )
- except Exception as e:
- return {
- 'success': False,
- 'error': f'Failed to generate Excel: {str(e)}',
- 'code': 'SERVER_ERROR'
- }, 500
|