把较复杂的sql提出成cinema_sql, 封装数据库查询逻辑db()

main
RogerWork 11 months ago
parent 7f005cc298
commit b02c3aba3d
  1. 52
      mock/cinema_sql.py
  2. 24
      mock/mock_templates/get_overtime_ticket_status_tmp.py
  3. 7
      mock/models.py
  4. 99
      mock/views.py

@ -0,0 +1,52 @@
FROM_SELL_ADD_GET_SHOW_SQL = '''
SELECT csa.cinema_sell_add_showid AS show_id,
cms.cinema_movie_name AS name,
cms.cinema_movie_show_start_time AS start_time,
chi.cinema_hall_name AS hall_name
FROM cinema_sell_add csa
LEFT JOIN cinema_hall_info chi ON csa.cinema_sell_add_hall = chi.cinema_hall_id
LEFT JOIN cinema_movie_show cms ON csa.cinema_sell_add_showid = cms.cinema_movie_show_id
GROUP BY csa.cinema_sell_add_showid ORDER BY cms.cinema_movie_show_start_time DESC;
'''
FROM_REFUND_GET_SHOW_SQL = '''
SELECT csl.cinema_movie_show_id AS show_id,
csl.cinema_movie_name AS name,
csl.cinema_movie_show_start_time AS start_time,
chi.cinema_hall_name AS hall_name
FROM cinema_sell_log csl
LEFT JOIN cinema_hall_info chi ON csl.cinema_hall_id = chi.cinema_hall_id
WHERE csl.cinema_sell_id IN (SELECT cinema_sell_id FROM cinema_refund_order_detail)
GROUP BY cinema_movie_id ORDER BY csl.cinema_movie_show_start_time DESC;
'''
UPDATE_ZZ_PLATFORM_HOST_SQL = '''
UPDATE cine.cinema_common_info
SET cinema_common_info_val = %s WHERE cinema_common_info_key = 'zz_platform_host';
'''
GET_ZZ_PLATFORM_HOST_SQL = '''
SELECT * FROM cine.cinema_common_info WHERE cinema_common_info_key = 'zz_platform_host';
'''
GET_OVERTIME_SELL_TICKET_SQL = """
SELECT csad.id,
csad.ticket_no AS ticket_no,
csa.cinema_sell_add_status AS add_status,
csa.zz_audit_status AS audit_status,
csa.cinema_sell_add_showid AS show_id
FROM cinema_sell_add_detail csad
LEFT JOIN cinema_sell_add csa ON csad.cinema_sell_add_id = csa.cinema_sell_add_id
WHERE csad.ticket_no <> ''
AND csa.cinema_sell_add_showid = %s
ORDER BY csad.id DESC;
"""
GET_OVERTIME_REFUND_TICKET_SQL = """
SELECT crod.id, crod.cinema_sell_id, crod.refund_status, cslei.ticket_no
FROM cinema_refund_order_detail crod
LEFT JOIN cinema_sell_log_ext_info cslei ON crod.cinema_sell_id = cslei.cinema_sell_id
WHERE refund_order_id IN (SELECT cinema_refund_order.refund_order_id FROM cinema_refund_order WHERE show_id = %s)
ORDER BY id DESC;
"""

@ -1,6 +1,7 @@
import pymysql
from pymysql.cursors import DictCursor
from mock.models import ZZMockModel
from mock.cinema_sql import *
# 转换sessionCode为show_id
@ -58,19 +59,7 @@ def get(cinema_data, request):
if overtime_type == '0':
# zz_audit_status 专资审核状态 1-已审核 2-已驳回 3-审核中 4-已补登 5-未提交 6-提交失败
# cinema_sell_add_status 补登状态 1-已审核 2-已驳回 3-审核中,
sell_sql = """
SELECT csad.id,
csad.ticket_no AS ticket_no,
csa.cinema_sell_add_status AS add_status,
csa.zz_audit_status AS audit_status,
csa.cinema_sell_add_showid AS show_id
FROM cinema_sell_add_detail csad
LEFT JOIN cinema_sell_add csa ON csad.cinema_sell_add_id = csa.cinema_sell_add_id
WHERE csad.ticket_no <> ''
AND csa.cinema_sell_add_showid = %s
ORDER BY csad.id DESC;
"""
db_cursor.execute(sell_sql, (target_show_id,))
db_cursor.execute(GET_OVERTIME_SELL_TICKET_SQL, (target_show_id,))
sell_tickets = db_cursor.fetchall()
for ticket in sell_tickets:
sell_status = str(ticket['audit_status'])
@ -91,14 +80,7 @@ def get(cinema_data, request):
# 处理过场退票
if overtime_type == '1':
# cinema_refund_order_detail
refund_sql = """
SELECT crod.id, crod.cinema_sell_id, crod.refund_status, cslei.ticket_no
FROM cinema_refund_order_detail crod
LEFT JOIN cinema_sell_log_ext_info cslei ON crod.cinema_sell_id = cslei.cinema_sell_id
WHERE refund_order_id IN (SELECT cinema_refund_order.refund_order_id FROM cinema_refund_order WHERE show_id = %s)
ORDER BY id DESC;
"""
db_cursor.execute(refund_sql, (target_show_id,))
db_cursor.execute(GET_OVERTIME_REFUND_TICKET_SQL, (target_show_id,))
refund_tickets = db_cursor.fetchall()
for ticket in refund_tickets:

@ -15,9 +15,10 @@ class ZZMockModel(models.Model):
getOvertimeTicketStatus = models.BooleanField(verbose_name='超时票务受理情况查询接口', null=False,
help_text='超时票务受理情况查询接口')
validError = models.BooleanField(verbose_name='数据清洗错误查询接口', null=False, help_text='数据清洗错误查询接口')
auditShowId = models.CharField(verbose_name='需要审核的过场场次编码', null=True, max_length=20, help_text='需要审核的过场场次编码')
auditStatus = models.CharField(verbose_name='需要审核的过场场次状态', null=True, max_length=3, help_text='需要审核的过场场次状态')
auditShowId = models.CharField(verbose_name='需要审核的过场场次编码', null=True, max_length=20,
help_text='需要审核的过场场次编码')
auditStatus = models.CharField(verbose_name='需要审核的过场场次状态', null=True, max_length=3,
help_text='需要审核的过场场次状态')
def __str__(self):
return self.ip

@ -7,7 +7,10 @@ from update.models import Cinema
from django.views.decorators.csrf import csrf_exempt
from pymysql.cursors import DictCursor
import pymysql
from django.core.management.commands.runserver import Command as runserver
import socket
from mock.mock_templates import *
from cinema_sql import *
ZZ_URL = 'https://zzcs.yinghezhong.com'
@ -103,10 +106,8 @@ def valid_error(request):
return mock_service.bypass(request)
# 获取过场信息
def get_overtime_show(request):
query_params = request.query_params.dict()
ip = query_params.get('ip')
# 封装数据库操作
def db(ip, sql, params=None):
cinema_data = Cinema.objects.filter(ip=ip).first()
db_config = {
'host': cinema_data.ip,
@ -115,44 +116,40 @@ def get_overtime_show(request):
'database': 'cine',
'connect_timeout': 5,
}
# 获取过场售票
sql_str = '''
SELECT csa.cinema_sell_add_showid AS show_id,
cms.cinema_movie_name AS name,
cms.cinema_movie_show_start_time AS start_time,
chi.cinema_hall_name AS hall_name
FROM cinema_sell_add csa
LEFT JOIN cinema_hall_info chi ON csa.cinema_sell_add_hall = chi.cinema_hall_id
LEFT JOIN cinema_movie_show cms ON csa.cinema_sell_add_showid = cms.cinema_movie_show_id
GROUP BY csa.cinema_sell_add_showid ORDER BY cms.cinema_movie_show_start_time DESC;
'''
db_conn = pymysql.Connect(**db_config)
db_cursor = db_conn.cursor(cursor=DictCursor)
db_cursor.execute(sql_str)
show_data = db_cursor.fetchall()
result, data = 0, []
if sql.startswith('UPDATE'):
result = db_cursor.execute(sql, params)
data = db_cursor.fetchall()
db_conn.commit()
if sql.startswith('SELECT'):
if params is None:
result = db_cursor.execute(sql)
else:
result = db_cursor.execute(sql, params)
data = db_cursor.fetchall()
return {'result': result, 'data': data}
# 获取过场信息
def get_overtime_show(request):
ip = request.query_params.dict().get('ip')
# 获取过场售票
db_result = db(ip, FROM_SELL_ADD_GET_SHOW_SQL)
sell_show_data = db_result['data']
sell_show_list = []
for show in show_data:
for show in sell_show_data:
sell_show_list.append({'id': show['show_id'],
'show': show['name'] + ' ' + show['start_time'] + ' ' + show[
'hall_name'] if show['hall_name'] is not None else ''
})
# 获取过场退票场次
sql_str = '''
SELECT csl.cinema_movie_show_id AS show_id,
csl.cinema_movie_name AS name,
csl.cinema_movie_show_start_time AS start_time,
chi.cinema_hall_name AS hall_name
FROM cinema_sell_log csl
LEFT JOIN cinema_hall_info chi ON csl.cinema_hall_id = chi.cinema_hall_id
WHERE csl.cinema_sell_id IN (SELECT cinema_sell_id FROM cinema_refund_order_detail)
GROUP BY cinema_movie_id ORDER BY csl.cinema_movie_show_start_time DESC;
'''
db_conn = pymysql.Connect(**db_config)
db_cursor = db_conn.cursor(cursor=DictCursor)
db_cursor.execute(sql_str)
show_data = db_cursor.fetchall()
db_result = db(ip, FROM_REFUND_GET_SHOW_SQL)
refund_show_data = db_result['data']
refund_show_list = []
for show in show_data:
for show in refund_show_data:
refund_show_list.append({'id': show['show_id'],
'show': show['name'] + ' ' + show['start_time'] + ' ' + show[
'hall_name'] if show['hall_name'] is not None else ''
@ -164,3 +161,39 @@ def get_overtime_show(request):
def get_response(request, _api):
_ip = request.query_params.dict().get('ip')
return mock_service.mock(_ip, _api)
# 获取本机 IP 地址:
def get_local_ip():
r = runserver()
ip = socket.gethostbyname(socket.gethostname())
print(ip)
return ip
# 启动mock
def enable_mock(request):
ip = request.query_params.dict().get('ip')
server_ip = get_local_ip()
current_host = db(ip, GET_ZZ_PLATFORM_HOST_SQL)['data'][0]['cinema_common_info_val']
if current_host == 'https://zzcs.yinghezhong.com' or current_host == 'https://dy.yinghezhong.com':
db_result = db(ip, UPDATE_ZZ_PLATFORM_HOST_SQL, (f'http://{server_ip}:8000',))
if db_result['result'] > 0:
return JsonResponse({'status': 'success'})
if current_host == f'http://{server_ip}:8000':
return JsonResponse({'status': 'success'})
return JsonResponse({'status': 'fail'})
# 停用mock服务
def disable_mock(request):
ip = request.query_params.dict().get('ip')
server_ip = get_local_ip()
current_host = db(ip, GET_ZZ_PLATFORM_HOST_SQL)['data'][0]['cinema_common_info_val']
if current_host == 'https://zzcs.yinghezhong.com' or current_host == 'https://dy.yinghezhong.com':
return JsonResponse({'status': 'success'})
if current_host == f'http://{server_ip}:8000':
db_result = db(ip, UPDATE_ZZ_PLATFORM_HOST_SQL, ('https://zzcs.yinghezhong.com',))
if db_result['result'] > 0:
return JsonResponse({'status': 'success'})
return JsonResponse({'status': 'fail'})

Loading…
Cancel
Save