# 引入SparkSession用于模拟数据分析场景(虽然主系统是Django+MySQL,但可扩展此脚本进行离线分析)from pyspark.sql import SparkSessiondef analyze_repair_trends(): spark = SparkSession.builder \ .appName("RepairTrendAnalysis") \ .config("spark.sql.warehouse.dir", "file:///C:/temp/spark-warehouse") \ .getOrCreate() # 假设通过JDBC读取MySQL中的repair_orders表 jdbc_url = "jdbc:mysql://localhost:3306/university_logistics?user=root&password=123456" repair_df = spark.read.format("jdbc").options(url=jdbc_url, dbtable="repair_orders", driver="com.mysql.cj.jdbc.Driver").load() repair_df.createOrReplaceTempView("repairs") # 使用Spark SQL分析各楼宇报修数量 trend_result = spark.sql("SELECT location, COUNT(*) as repair_count FROM repairs GROUP BY location ORDER BY repair_count DESC") trend_result.show() spark.stop()def submit_repair(request): try: user_id = request.POST.get('user_id') contact = request.POST.get('contact') location = request.POST.get('location') description = request.POST.get('description') image = request.FILES.get('image') # 数据校验 if not all([user_id, contact, location, description]): return {'status': 'error', 'message': '信息不完整,请检查后提交'} # 创建报修单对象 new_order = RepairOrder() new_order.user_id = user_id new_order.contact = contact new_order.location = location new_order.description = description new_order.status = '待处理' # 处理图片上传 if image: save_path = f'uploads/repairs/{user_id}_{image.name}' with open(save_path, 'wb+') as destination: for chunk in image.chunks(): destination.write(chunk) new_order.image_path = save_path new_order.save() return {'status': 'success', 'message': '报修提交成功,请耐心等待处理', 'order_id': new_order.id} except Exception as e: print(f"提交报修失败: {e}") return {'status': 'error', 'message': '系统繁忙,请稍后再试'}def assign_repair(request): try: admin_id = request.session.get('admin_id') order_id = request.POST.get('order_id') staff_id = request.POST.get('staff_id') # 权限和参数校验 if not admin_id or not order_id or not staff_id: return {'status': 'error', 'message': '分配失败,参数或权限错误'} repair_order = RepairOrder.objects.get(id=order_id) if repair_order.status != '待处理': return {'status': 'error', 'message': '该工单已被处理,无法重复分配'} # 更新工单状态和负责人 repair_order.status = '处理中' repair_order.assigned_staff_id = staff_id repair_order.assigned_time = timezone.now() repair_order.save() # 此处可添加通知逻辑,例如给维修人员发送站内信或短信 return {'status': 'success', 'message': f'工单已成功分配给维修员 {staff_id}'} except RepairOrder.DoesNotExist: return {'status': 'error', 'message': '分配失败,未找到该报修单'} except Exception as e: print(f"分配工单失败: {e}") return {'status': 'error', 'message': '系统内部错误,分配失败'}