Commit 57c61270 by Aeolus

统计机柜单日租借量,使用时长分析接口优化

统计机柜单日租借量接口增加传入日期参数
使用时长分析优化sql异常
parent a5d23ca1
...@@ -88,13 +88,13 @@ def user_rent_time(): ...@@ -88,13 +88,13 @@ def user_rent_time():
tmp_data = [] tmp_data = []
sql = """ sql = """
select count(1) as number from(SELECT id FROM production select count(1) as number from(SELECT id FROM production
where spot_id ='{spot_id}' and total >0 and return_time is not null and time_to_sec(timediff(return_time, created_at))<3600) as rs where spot_id ='{spot_id}' and total >0 and return_time is not null and (UNIX_TIMESTAMP(return_time) - UNIX_TIMESTAMP(created_at))<3600) as rs
union all union all
select count(1) as number from(SELECT id FROM production select count(1) as number from(SELECT id FROM production
where spot_id ='{spot_id}' and total >0 and return_time is not null and time_to_sec(timediff(return_time, created_at))>3600 and time_to_sec(timediff(return_time, created_at))<=7200) as rs where spot_id ='{spot_id}' and total >0 and return_time is not null and (UNIX_TIMESTAMP(return_time) - UNIX_TIMESTAMP(created_at))>3600 and (UNIX_TIMESTAMP(return_time) - UNIX_TIMESTAMP(created_at))<=7200) as rs
union all union all
select count(1) as number from(SELECT id FROM production select count(1) as number from(SELECT id FROM production
where spot_id ='{spot_id}' and total >0 and return_time is not null and time_to_sec(timediff(return_time, created_at))<7200) as rs; where spot_id ='{spot_id}' and total >0 and return_time is not null and (UNIX_TIMESTAMP(return_time) - UNIX_TIMESTAMP(created_at))<7200) as rs;
""".format(spot_id=spot_id) """.format(spot_id=spot_id)
result = db.session.execute(sql).fetchall() result = db.session.execute(sql).fetchall()
if result: if result:
...@@ -156,6 +156,7 @@ def get_machine_rent_number(): ...@@ -156,6 +156,7 @@ def get_machine_rent_number():
return jsonify(OPERATE_LEVEL_ERROR) return jsonify(OPERATE_LEVEL_ERROR)
json_data = request.get_json() json_data = request.get_json()
spot_id = int(json_data['spot_id']) if 'spot_id' in json_data else 0 spot_id = int(json_data['spot_id']) if 'spot_id' in json_data else 0
query_date = json_data.get('query_date', datetime.datetime.now().strftime("%Y-%m-%d"))
machine_info = Machine.query.filter(Machine.spot_id == spot_id).all() machine_info = Machine.query.filter(Machine.spot_id == spot_id).all()
machines = {} machines = {}
...@@ -176,7 +177,7 @@ def get_machine_rent_number(): ...@@ -176,7 +177,7 @@ def get_machine_rent_number():
and total>0 and total>0
group by rent_machine_id group by rent_machine_id
""".format(sql_spot_id, datetime.datetime.now().strftime("%Y-%m-%d")) """.format(sql_spot_id, query_date)
result = db.session.execute(sql).fetchall() result = db.session.execute(sql).fetchall()
if result: if result:
for i in result: for i in result:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment