# 此处仅为示例,模拟使用Spark对历史预约数据进行离线分析,预测未来一周的热门社区from pyspark.sql import SparkSessionspark = SparkSession.builder.appName("ElderlyCareAnalysis").getOrCreate()# 假设appointments_df是一个包含历史预约数据的Spark DataFrame# appointments_df = spark.read.csv("hdfs://path/to/appointments.csv", header=True)# popular_communities = appointments_df.groupBy("community_id").count().orderBy("count", ascending=False)# popular_communities.show()def query_communities(request): location = request.GET.get('location', '') price_min = request.GET.get('price_min', 0) price_max = request.GET.get('price_max', 999999) facility = request.GET.getlist('facility', []) communities = Community.objects.all() if location: communities = communities.filter(address__icontains=location) if price_min: communities = communities.filter(price__gte=price_min) if price_max: communities = communities.filter(price__lte=price_max) if facility: communities = communities.filter(facilities__name__in=facility).distinct() community_list = list(communities.values('id', 'name', 'address', 'price', 'main_image', 'contact_phone')) return JsonResponse({'status': 'success', 'data': community_list})def book_appointment(request): if request.method == 'POST': user_id = request.user.id community_id = request.POST.get('community_id') appointment_date = request.POST.get('appointment_date') contact_phone = request.POST.get('contact_phone') if not all([community_id, appointment_date, contact_phone]): return JsonResponse({'status': 'error', 'message': '信息不完整'}) try: community = Community.objects.get(id=community_id) except Community.DoesNotExist: return JsonResponse({'status': 'error', 'message': '社区不存在'}) if Appointment.objects.filter(user_id=user_id, community=community, appointment_date=appointment_date).exists(): return JsonResponse({'status': 'error', 'message': '您已预约该社区在此日期的参观'}) appointment = Appointment.objects.create(user_id=user_id, community=community, appointment_date=appointment_date, contact_phone=contact_phone, status='待确认') return JsonResponse({'status': 'success', 'message': '预约成功,请等待社区联系您'})def update_community_info(request): if request.method == 'POST' and request.user.is_staff: community_id = request.POST.get('id') try: community = Community.objects.get(id=community_id) except Community.DoesNotExist: return JsonResponse({'status': 'error', 'message': '未找到该社区信息'}) community.name = request.POST.get('name', community.name) community.description = request.POST.get('description', community.description) community.address = request.POST.get('address', community.address) community.price = request.POST.get('price', community.price) community.contact_phone = request.POST.get('contact_phone', community.contact_phone) if 'main_image' in request.FILES: community.main_image = request.FILES['main_image'] community.save() return JsonResponse({'status': 'success', 'message': '社区信息更新成功'}) return JsonResponse({'status': 'error', 'message': '权限不足或请求错误'})