增加文件

main
rogersun 2 weeks ago
parent 86ec8db612
commit 82f13d14ac
  1. 36
      ai/utils/basic_func.py
  2. 45
      ai/utils/show_func.py
  3. 8
      ai/views.py

@ -0,0 +1,36 @@
import datetime
import csv
from io import StringIO
from django_redis import get_redis_connection
# 清除redis锁
def clear_lock_func():
redis_conn = get_redis_connection()
redis_key = f'ai_show{datetime.date.today().strftime("%Y%m%d")}'
if redis_conn.exists(redis_key):
redis_conn.delete(redis_key)
# 将csv转化成字典
def scv_to_obj(csv_data):
f = StringIO(csv_data)
reader = csv.DictReader(f)
return list(reader)
# 列表转csv
def list_to_csv(rows, include_header=True):
"""
将嵌套列表转换为 CSV 字符串
:param rows: 输入数据格式为 [['col1','col2'], ['val1','val2'], ...]
:param include_header: 若为 True rows[0] 作为表头否则全部当作数据行
"""
output = StringIO()
writer = csv.writer(output, lineterminator='\r\n')
if include_header and rows:
writer.writerow(rows[0])
writer.writerows(rows[1:])
else:
writer.writerows(rows)
return output.getvalue()

@ -0,0 +1,45 @@
from django.db.models import Q
import datetime
from ai.models import *
from ai.utils.show_database import GetData
def get_cinema_show_result_func(_zz_code, _show_date):
cinema = TestCinema.objects.filter(zz_code=_zz_code).first()
start = datetime.datetime.strftime(datetime.datetime.strptime(_show_date, '%Y-%m-%d') + datetime.timedelta(hours=6),
'%Y-%m-%d %H:%M:%S')
end = datetime.datetime.strftime(
datetime.datetime.strptime(_show_date, '%Y-%m-%d') + datetime.timedelta(hours=29, minutes=59, seconds=59),
'%Y-%m-%d %H:%M:%S')
show = ''
income = ''
if cinema:
print(cinema)
data = GetData(cinema)
show = data.get_show_data(start, end)
income = data.get_total_income(start, end)
print(show, income)
show_db = AiShow.objects.filter(Q(is_ai_show=False) & Q(zz_code=_zz_code) & Q(show_date=_show_date)).order_by(
'-id').first()
if show_db:
print('show_db', show_db)
show_db.show = show
show_db.income = income
show_db.save()
else:
try:
AiShow.objects.create(
cinema=cinema.name,
zz_code=cinema.zz_code,
show_date=_show_date,
is_ai_show=False,
show=show,
sales=income,
prompt='',
result='',
message='',
take_times=0,
take_tokens='0'
)
except Exception as e:
print(e)
return show, income

@ -60,9 +60,15 @@ def report(request):
Q(updated_at__gt=check_point)).order_by('-id').first() Q(updated_at__gt=check_point)).order_by('-id').first()
if not last_real_data: if not last_real_data:
get_cinema_show_result_func(zz_code, show_date) get_cinema_show_result_func(zz_code, show_date)
last_ai_data = AiShow.objects.filter(Q(is_ai_show=True) & last_ai_data = AiShow.objects.filter(Q(is_ai_show=True) &
Q(zz_code=zz_code) & Q(zz_code=zz_code) &
Q(show_date=show_date)).order_by('-id').first() 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) & last_real_data = AiShow.objects.filter(Q(is_ai_show=False) &
Q(zz_code=zz_code) & Q(zz_code=zz_code) &
Q(show_date=show_date)).order_by('-id').first() Q(show_date=show_date)).order_by('-id').first()
@ -162,7 +168,7 @@ def report(request):
# 返回 # 返回
result_dict = { result_dict = {
'status': 'success', 'status': 'success',
'message': '', 'message': '',
'data': { 'data': {
'cinema_name': last_ai_data.cinema, 'cinema_name': last_ai_data.cinema,
'zz_code': last_ai_data.zz_code, 'zz_code': last_ai_data.zz_code,

Loading…
Cancel
Save