From abd907dd86a61823388a7a22a6c082cd3ac25784 Mon Sep 17 00:00:00 2001 From: rogersun Date: Tue, 16 Jun 2026 12:30:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=A8=A1=E6=9D=BF=E5=AF=B9?= =?UTF-8?q?=E6=AF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0008_aishow_temp_date_aishow_template.py | 23 ++++ ai/models.py | 2 + ai/utils/basic_func.py | 99 ++++++++++++++++- ai/utils/show_func.py | 17 ++- ai/utils/show_process.py | 53 +++++---- ai/views.py | 105 ++---------------- 6 files changed, 181 insertions(+), 118 deletions(-) create mode 100644 ai/migrations/0008_aishow_temp_date_aishow_template.py diff --git a/ai/migrations/0008_aishow_temp_date_aishow_template.py b/ai/migrations/0008_aishow_temp_date_aishow_template.py new file mode 100644 index 0000000..0531050 --- /dev/null +++ b/ai/migrations/0008_aishow_temp_date_aishow_template.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.7 on 2026-06-16 01:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('ai', '0007_aishow_prompt_version'), + ] + + operations = [ + migrations.AddField( + model_name='aishow', + name='temp_date', + field=models.CharField(default='', max_length=50), + ), + migrations.AddField( + model_name='aishow', + name='template', + field=models.TextField(default=''), + ), + ] diff --git a/ai/models.py b/ai/models.py index 1ed4bf8..b82729b 100644 --- a/ai/models.py +++ b/ai/models.py @@ -41,6 +41,8 @@ class AiShow(models.Model): zz_code = models.CharField(max_length=50) show_date = models.DateField() is_ai_show = models.BooleanField(default=True) + template = models.TextField(default='') + temp_date = models.CharField(max_length=50, default='') show = models.TextField() sales = models.CharField(max_length=50) prompt = models.TextField() diff --git a/ai/utils/basic_func.py b/ai/utils/basic_func.py index 9f91136..bdf9f3d 100644 --- a/ai/utils/basic_func.py +++ b/ai/utils/basic_func.py @@ -2,6 +2,7 @@ import datetime import csv from io import StringIO from django_redis import get_redis_connection +from ai.models import CinemaHall # 清除redis锁 @@ -10,10 +11,11 @@ def clear_lock_func(): redis_key = f'ai_show{datetime.date.today().strftime("%Y%m%d")}' if redis_conn.exists(redis_key): redis_conn.delete(redis_key) + print(f'清除{redis_key}成功') # 将csv转化成字典 -def scv_to_obj(csv_data): +def csv_to_obj(csv_data): f = StringIO(csv_data) reader = csv.DictReader(f) return list(reader) @@ -34,3 +36,98 @@ def list_to_csv(rows, include_header=True): else: writer.writerows(rows) return output.getvalue() + + +# 格式化数据 +def format_show_data(cinema_code:str, show_date:str, data_left:dict, data_right:dict): + # 获取影厅数据 + hall = CinemaHall.objects.filter(cinema_code=cinema_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 data_left: + 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 data_right: + 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']] + ) + return output_list, output_csv \ No newline at end of file diff --git a/ai/utils/show_func.py b/ai/utils/show_func.py index 3122500..8ac58bb 100644 --- a/ai/utils/show_func.py +++ b/ai/utils/show_func.py @@ -2,6 +2,8 @@ from django.db.models import Q import datetime from ai.models import * from ai.utils.show_database import GetData +from ai.utils.datetime_format import * + def get_cinema_show_result_func(_zz_code, _show_date): cinema = TestCinema.objects.filter(zz_code=_zz_code).first() @@ -42,4 +44,17 @@ def get_cinema_show_result_func(_zz_code, _show_date): ) except Exception as e: print(e) - return show, income \ No newline at end of file + return show, income + +# 获取模板日期排片数据 +def get_template_show(cinema, show_date): + history_start, history_end, target_start, target_end = get_data_datetime(show_date) + data = GetData(cinema) + temp_date_obj = data.get_template_date(history_start, history_end) + temp_date = datetime.datetime.strftime(temp_date_obj, '%Y-%m-%d') + temp_start = datetime.datetime.strftime(temp_date_obj + datetime.timedelta(hours=6), '%Y-%m-%d %H:%M:%S') + temp_end = datetime.datetime.strftime(temp_date_obj + datetime.timedelta(hours=29, minutes=59, seconds=59), + '%Y-%m-%d %H:%M:%S') + temp_show = data.get_show_data(temp_start, temp_end) + print('get_template_show', temp_date, temp_show) + return temp_show, temp_date diff --git a/ai/utils/show_process.py b/ai/utils/show_process.py index e48d6ad..3297f05 100644 --- a/ai/utils/show_process.py +++ b/ai/utils/show_process.py @@ -1,5 +1,8 @@ +import json + from ai.models import * from ai.utils.show_prompt import * +from ai.utils.show_func import * import datetime from django_redis import get_redis_connection from django.db.models import Q @@ -27,34 +30,40 @@ def show_main_process(zz_code=None): for cinema in test_cinema_list: print(cinema.name) show_ai = ShowAI(cinema, show_date) + template_show, template_date = get_template_show(cinema, show_date) prompt = show_ai.general_prompt() - start = datetime.datetime.now() + start = datetime.datetime.now() # 开始计时 result, message, tokens = show_ai.get_show_result_ai() - end = datetime.datetime.now() + end = datetime.datetime.now() # 结束计时 print('prompt:', prompt) print('result:', result) print('message:', message) print('tokens:', tokens) # 获取排片数据 - # 方式一 - _show = next((s for s in result.split('------') if s.startswith('\n影厅别名,影厅id')), '') - _show = _show.strip() - # 方式二 - if _show == '': - _show = next((s for s in result.split('------') if s.startswith('\n```csv\n影厅别名,')), '') - _show = _show.replace('```', '').replace('csv', '').strip() - # 方式三 - if _show == '': - # 正则提取中间内容 - csv_pattern = r"```csv\s*([\s\S]*?)```" - match = re.search(csv_pattern, result) - if match: - _show = match.group(1) - print('_show:', _show) - # 获取销售额 - _sales = next((s for s in result.split('\n') if re.search(r'\d{3,6}元', s)), '') - _sales = str(re.findall(r'\d{3,6}元', _sales)[-1]) - print('_sales:', _sales) + result_obj = json.loads(result) + # 预测排片数据 + _show = result_obj['show'].replace('```', '').replace('csv', '').strip() + # 预测销售数据 + _sales = str(result_obj['income']).strip() + # # 方式一 + # _show = next((s for s in result.split('------') if s.startswith('\n影厅别名,影厅id')), '') + # _show = _show.strip() + # # 方式二 + # if _show == '': + # _show = next((s for s in result.split('------') if s.startswith('\n```csv\n影厅别名,')), '') + # _show = _show.replace('```', '').replace('csv', '').strip() + # # 方式三 + # if _show == '': + # # 正则提取中间内容 + # csv_pattern = r"```csv\s*([\s\S]*?)```" + # match = re.search(csv_pattern, result) + # if match: + # _show = match.group(1) + # print('_show:', _show) + # # 获取销售额 + # _sales = next((s for s in result.split('\n') if re.search(r'\d{3,6}元', s)), '') + # _sales = str(re.findall(r'\d{3,6}元', _sales)[-1]) + # print('_sales:', _sales) # 处理返回结果 try: AiShow.objects.create( @@ -62,6 +71,8 @@ def show_main_process(zz_code=None): zz_code=cinema.zz_code, show_date=show_date, is_ai_show=True, + template = template_show, + temp_date=template_date, show=_show, sales=_sales, prompt=prompt, diff --git a/ai/views.py b/ai/views.py index 4376b6f..a0b420b 100644 --- a/ai/views.py +++ b/ai/views.py @@ -75,99 +75,12 @@ def report(request): 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']] - ) + ai_dict = csv_to_obj(last_ai_data.show) + real_dict = csv_to_obj(last_real_data.show) + temp_dict = csv_to_obj(last_ai_data.template) + print(ai_dict, real_dict, temp_dict) + output_list_ai_real, output_csv_ai_real = format_show_data(zz_code, show_date, ai_dict, real_dict) + output_list_ai_temp, output_csv_ai_temp = format_show_data(zz_code, show_date, ai_dict, temp_dict) # 返回 result_dict = { 'status': 'success', @@ -179,8 +92,10 @@ def report(request): '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, + 'csv': list_to_csv(output_csv_ai_real), + 'real_obj': output_list_ai_real, + 'template_obj': output_list_ai_temp, + 'template_date':last_ai_data.temp_date, 'prompt': last_ai_data.prompt, 'result': last_ai_data.result, }