from django.http import JsonResponse from django.db.models import Q from django.views.decorators.csrf import csrf_exempt import json from ai.models import * from ai.utils.show_process import show_main_process from ai.utils.show_func import get_cinema_show_result_func from ai.utils.basic_func import * # 手动触发ai排片 @csrf_exempt def manual_general_show(request): zz_code = request.GET.dict().get('cinema_code') result = show_main_process(zz_code) result_dict = { 'status': 'success' if result else 'fail', 'message': '生成成功' if result else '生成失败', } return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False}) # 更新指定日期的排片 @csrf_exempt def get_cinema_show_result(request): zz_code = request.GET.dict().get('cinema_code') show_date = request.GET.dict().get('show_date') print(zz_code, show_date) show, income = get_cinema_show_result_func(zz_code, show_date) result_dict = { 'status': 'success', 'message': '生成成功', 'show': show, 'income': income, } return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False}) # 清除redis锁 @csrf_exempt def clear_lock(request): clear_lock_func() result_dict = { 'status': 'success', 'message': '完成', } return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False}) @csrf_exempt def report(request): zz_code = request.GET.dict().get('cinema_code') show_date = request.GET.dict().get('show_date') force = request.GET.dict().get('force') print(zz_code, show_date) cinema = TestCinema.objects.filter(zz_code=zz_code).first() # 获取真实排片和销售数据 check_point = datetime.datetime.strftime( datetime.datetime.strptime(show_date, '%Y-%m-%d') + datetime.timedelta(hours=30), '%Y-%m-%d %H:%M:%S') last_real_data = AiShow.objects.filter(Q(is_ai_show=False) & Q(zz_code=zz_code) & Q(show_date=show_date) & Q(updated_at__gt=check_point)).order_by('-id').first() if (not last_real_data) or (force == '1'): get_cinema_show_result_func(zz_code, show_date) last_ai_data = AiShow.objects.filter(Q(is_ai_show=True) & Q(zz_code=zz_code) & Q(show_date=show_date)).order_by('-id').first() if not last_ai_data: result_dict = { 'status': 'Failure', 'message': '没有查询到有效的智能排片数据,请调整日期和影院后再试!'} return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False}) last_real_data = AiShow.objects.filter(Q(is_ai_show=False) & Q(zz_code=zz_code) & Q(show_date=show_date)).order_by('-id').first() ai_dict = scv_to_obj(last_ai_data.show) real_dict = scv_to_obj(last_real_data.show) print(ai_dict, real_dict) # 获取影厅数据 hall = CinemaHall.objects.filter(cinema_code=zz_code).all() # 准备数据 data = dict() for h in hall: hall_dict = {'hall_name': h.hall_name, 'hall_id': h.hall_id, 'ai': [], 'real': []} for ai in ai_dict: if ai['影厅id'] == str(h.hall_id): hall_dict['ai'].append({ 'movie_name': ai['影片别名'], 'movie_id': ai['本地影片id'], 'language': ai['语言'], 'show_date': ai['放映日期'], 'start': ai['开始时间'], 'end': ai['结束时间'], 'length': ai['片长'], 'duration': ai['场间'] }) for real in real_dict: if real['影厅id'] == str(h.hall_id): hall_dict['real'].append({ 'movie_name': real['影片别名'], 'movie_id': real['本地影片id'], 'language': real['语言'], 'show_date': real['放映日期'], 'start': real['开始时间'], 'end': real['结束时间'], 'length': real['片长'], 'duration': real['场间'] }) data[h.hall_id] = hall_dict # 格式化输出 output_list = [] output_csv = [ ['影厅别名', '影片别名', '语言', '开始时间', '结束时间', '片长', '场间', '', '影厅别名', '影片别名', '语言', '开始时间', '结束时间', '片长', '场间']] for v in data.values(): if len(v['ai']) == 0 and len(v['real']) == 0: output_list.append([v['hall_name'], show_date, '', '', '', '', '', '', '', '', '', '', '', '', '', '']) output_csv.append([v['hall_name'], '', '', '', '', '', '', '', v['hall_name'], '', '', '', '', '', '']) if len(v['ai']) >= len(v['real']): for i in range(len(v['ai'])): if i < len(v['real']): a = v['ai'][i] r = v['real'][i] output_list.append( [v['hall_name'], show_date, a['movie_name'], a['movie_id'], a['language'], a['start'], a['end'], a['length'], a['duration'], r['movie_name'], r['movie_id'], r['language'], r['start'], r['end'], r['length'], r['duration']] ) output_csv.append( [v['hall_name'], a['movie_name'], a['language'], a['start'], a['end'], a['length'], a['duration'], '', v['hall_name'], r['movie_name'], r['language'], r['start'], r['end'], r['length'], r['duration']] ) else: a = v['ai'][i] output_list.append( [v['hall_name'], show_date, a['movie_name'], a['movie_id'], a['language'], a['start'], a['end'], a['length'], a['duration'], '', '', '', '', '', '', ''] ) output_csv.append( [v['hall_name'], a['movie_name'], a['language'], a['start'], a['end'], a['length'], a['duration'], '', '', '', '', '', '', '', ''] ) else: for i in range(len(v['real'])): if i < len(v['ai']): a = v['ai'][i] r = v['real'][i] output_list.append( [v['hall_name'], show_date, a['movie_name'], a['movie_id'], a['language'], a['start'], a['end'], a['length'], a['duration'], r['movie_name'], r['movie_id'], r['language'], r['start'], r['end'], r['length'], r['duration']] ) output_csv.append( [v['hall_name'], a['movie_name'], a['language'], a['start'], a['end'], a['length'], a['duration'], '', v['hall_name'], r['movie_name'], r['language'], r['start'], r['end'], r['length'], r['duration']] ) else: r = v['real'][i] output_list.append( [v['hall_name'], show_date, '', '', '', '', '', '', '', r['movie_name'], r['movie_id'], r['language'], r['start'], r['end'], r['length'], r['duration']] ) output_csv.append( [v['hall_name'], '', '', '', '', '', '', '', v['hall_name'], r['movie_name'], r['language'], r['start'], r['end'], r['length'], r['duration']] ) # 返回 result_dict = { 'status': 'success', 'message': '成功', 'data': { 'cinema_name': last_ai_data.cinema, 'zz_code': last_ai_data.zz_code, 'real_sales': last_real_data.sales, 'ai_sales': last_ai_data.sales, 'take_times': last_ai_data.take_times, 'take_tokens': json.loads(last_ai_data.take_tokens)['total_tokens'], 'csv': list_to_csv(output_csv), 'objects': output_list, 'prompt': last_ai_data.prompt } } print(result_dict) return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False})