cross_doctor/app/tmnet_db_func.py

301 lines
15 KiB
Python

from app.db_func_base import TableDbHelperBase
class TmnetDbHelper(TableDbHelperBase):
def __init__(self, pool):
self.db_pool = pool
self.DB_Name = 'tmnet'
def reload_cross_info(self, nodeid, last_reload_time):
sql1 = "select * from cross_ledger_update_info where nodeid = %s and crossid is not null" % nodeid
sql2 = "select * from road_ledger_update_info where nodeid = %s" % nodeid
crosses_list, cross_res_list = self.do_select(sql1), []
for cross_row in crosses_list:
if cross_row['update_time'] > last_reload_time:
cross_res_list.append(cross_row)
roads_list, road_res_list = self.do_select(sql2), []
for road_row in roads_list:
if road_row['update_time'] > last_reload_time:
road_res_list.append(road_row)
return cross_res_list, road_res_list
def query_phase_by_cross_list(self, citycode, cross_list):
conn, cursor = self.connect()
try:
sql = f'''select
cm.xl_crossid
from phasetable.cross_mapping as cm
join phasetable.day_schedule as ds on cm.jj_crossid = ds.crossid and cm.nodeid = cm.nodeid
where
cm.nodeid = %s and cm.xl_crossid in %s
group by cm.xl_crossid'''
print(cursor.mogrify(sql, (citycode, cross_list)))
cursor.execute(sql, (citycode, cross_list))
result = cursor.fetchall()
self.close(conn, cursor)
return result, None
except Exception as error:
self.close(conn, cursor)
return None, error
def query_cross_info(self, nodeid):
sql = "select * from `cross` where nodeid = %s" % nodeid
return self.do_select(sql)
def query_cross_info_new(self, citycode):
conn, cursor = self.connect()
try:
sql = f'''select * from `cross` where nodeid = %s'''
print(cursor.mogrify(sql, (citycode)))
cursor.execute(sql, (citycode))
result = cursor.fetchall()
self.close(conn, cursor)
return result, None
except Exception as error:
self.close(conn, cursor)
return None, error
def query_cross_list_sql(self, nodeid, area_id):
sql = """
select
if(t2.name is not null, t2.name, t1.name) as name,
t1.crossid,
if (t2.location is not null, t2.location, t1.location) as location,
t1.nodeid,
t1.area_id
from (select name,crossid, location,nodeid, area_id from `cross` where nodeid = %s and area_id = %s and at_edge=0 and isdeleted=0 ) as t1
left join (select name,crossid, location,nodeid, area_id from `cross_ledger_update_info` where nodeid = %s and area_id = %s and at_edge=0 and isdeleted=0 ) as t2 on t1.crossid=t2.crossid
""" % (nodeid, area_id, nodeid, area_id)
cross_list = self.do_select(sql)
virtual_cross_sql = f'select name, crossid, location, nodeid, area_id from user_defined_cross where nodeid = {nodeid} and area_id = {area_id}'
virtual_cross_list = self.do_select(virtual_cross_sql)
cross_list.extend(virtual_cross_list)
if area_id != 0:
area_cross_sql = f'select * from bound_crosses where nodeid = {nodeid} and area_id = {area_id}'
area_crosses = self.do_select(area_cross_sql)
area_cross_list = [cross['crossid'] for cross in area_crosses]
cross_list = [cross for cross in cross_list if cross['crossid'] in area_cross_list]
return cross_list
def query_cross_inroads(self, crossid, nodeid):
sql = """
select
t1.roadid,
if(t2.from_crossid is not null, t2.from_crossid, t1.from_crossid) as from_crossid,
if(t2.to_crossid is not null, t2.to_crossid, t1.to_crossid) as to_crossid,
if(t2.name is not null, t2.name, t1.name) as name,
if(t2.src_direct is not null, t2.src_direct, t1.src_direct) as src_direct,
if(t2.lane_turn_info is not null, t2.lane_turn_info, t1.lane_turn_info) as lane_turn_info
from
(select * from road where nodeid = '%s' and recordstate=0 and to_crossid='%s' and (is_sup_road is null or is_sup_road<>1)) t1
left join
(select * from road_ledger_update_info where nodeid = '%s' and recordstate=0 and to_crossid='%s' and (is_sup_road is null or is_sup_road<>1)) t2
on t1.roadid = t2.roadid;
""" % (nodeid, crossid, nodeid, crossid)
return self.do_select(sql)
def query_base_info(self, nodeid, area_id):
sql = "select * from ledger.leger_base_info where nodeid= %d and area_id = %s" % (int(nodeid), int(area_id))
return self.do_select(sql)
def query_city_tp_info(self, nodeid, area_id):
sql = f"select tp_desc, peak_tp from cross_doctor_config.area_tp_config where nodeid = {nodeid} and area_id = {area_id}"
return self.do_select(sql)
def query_all_cross_examine_records(self, yes_str):
sql = "select * from cross_doctor_matedata.cross_phase_problems_record where date(update_time) = '%s' and end_date is null" % yes_str
return self.do_select(sql)
def insert_cross_examine_records(self, insert_list):
conn, cursor = self.connect()
try:
sql = """insert into cross_doctor_matedata.cross_phase_problems_record(start_hm,end_hm,crossid,phase_type, phase_detail,cont_times,final_state,level_color,first_date,change_red_daynums)
values(%s, %s, %s, %s,%s, %s, %s, %s,%s, %s)
"""
values = [
(
d['start_hm'], d['end_hm'], d['crossid'], d['phase_type'], d['phase_detail'], d['cont_times'],
d['final_state'],
d['level_color'], d['first_date'], d['change_red_daynums']
) for d in insert_list
]
ret = cursor.executemany(sql, values)
if ret == len(insert_list):
conn.commit()
self.close(conn, cursor)
return ret, None
else:
conn.rollback()
self.close(conn, cursor)
return 0, "insert_cross_examine_records error"
except Exception as e:
conn.rollback()
self.close(conn, cursor)
return 0, e
def update_cross_examine_records(self, update_list):
conn, cursor = self.connect()
try:
sql = """
update cross_doctor_matedata.cross_phase_problems_record
set phase_type = %s, phase_detail = %s, cont_times = %s, final_state = %s, level_color = %s, change_red_daynums = %s,
end_date = %s where crossid = %s and start_hm = %s and first_date = %s
"""
values = [
(
d['phase_type'], d['phase_detail'], d['cont_times'], d['final_state'], d['level_color'],
d['change_red_daynums'], d['end_date'], d['crossid'], d['start_hm'], d['first_date']
) for d in update_list
]
ret = cursor.executemany(sql, values)
if ret == len(update_list):
conn.commit()
self.close(conn, cursor)
return ret, None
else:
conn.rollback()
self.close(conn, cursor)
return ret, None
except Exception as e:
conn.rollback()
self.close(conn, cursor)
return 0, e
def query_cross_examine_records(self, start_hm, first_date, crossid, end_date):
sql = """
select * from cross_doctor_matedata.cross_phase_problems_record where crossid = '%s' and start_hm = '%s' and first_date <= %s and (end_date >= '%s' or end_date is null)
""" % (crossid, start_hm, first_date, end_date)
return self.do_select(sql)
def query_out_cross(self, inroadid):
sql = "select from_crossid, from_road_length from road where roadid = '%s'" % inroadid
return self.do_select(sql)
def query_next_cross_info(self, nodeid, area_id, crossid):
sql = """
select
if(t2.name is not null, t2.name, t1.name) as name,
t1.crossid,
if (t2.location is not null, t2.location, t1.location) as location,
t1.nodeid,
t1.area_id
from (select name,crossid, location,nodeid, area_id from `cross` where nodeid = %s and area_id = %s and at_edge=0 and isdeleted=0 and crossid = '%s') as t1
left join (select name,crossid, location,nodeid, area_id from `cross_ledger_update_info` where nodeid = %s and area_id = %s and at_edge=0 and isdeleted=0 and crossid = '%s') as t2 on t1.crossid=t2.crossid
""" % (nodeid, area_id, crossid, nodeid, area_id, crossid)
return self.do_select(sql)
def query_cross_list_road(self, nodeid: str, area_id: str):
"""
查询bound_cross的路口和进口道
包括虚拟路口和虚拟路段
"""
conn, cursor = self.connect()
try:
sql = """select IFNULL(clui.name, c2.name) as name,
c.crossid,
IFNULL(clui.location, c.location) as location,
c.nodeid,
c.area_id,
IFNULL(r.roadid,IFNULL(rlui.roadid,udr.roadid)) as roadid,
IFNULL(rlui.from_crossid, IFNULL(r.from_crossid,udr.from_crossid)) as from_crossid,
IFNULL(rlui.to_crossid, IFNULL(r.to_crossid,udr.to_crossid)) as to_crossid,
IFNULL(rlui.name, IFNULL(r.name,udr.name)) as road_name,
IFNULL(rlui.src_direct, IFNULL(r.src_direct,udr.src_direct)) as src_direct
from `bound_crosses` as c
join `cross` c2 on c2.crossid = c.crossid and c2.nodeid = %s and c2.area_id = %s
left join `cross_ledger_update_info` as clui on clui.crossid = c.crossid and clui.nodeid = %s and clui.area_id = %s
left join `road` as r on r.nodeid = %s and r.to_crossid = c.crossid and r.recordstate = 0 and (r.is_sup_road is null or r.is_sup_road <> 1)
left join `road_ledger_update_info` as rlui on rlui.nodeid = %s and rlui.recordstate = r.recordstate and rlui.to_crossid = r.to_crossid and (rlui.is_sup_road is null or rlui.is_sup_road <> 1)
left join `user_defined_roads` as udr on udr.nodeid = %s and udr.recordstate = 0 and udr.to_crossid = c2.crossid and (udr.is_sup_road is null or udr.is_sup_road <> 1)
where c.nodeid = %s
and c.area_id = %s
union all
select udc.name,
udc.crossid,
udc.location,
c.nodeid,
c.area_id,
IFNULL(r.roadid,IFNULL(rlui.roadid,udr.roadid)) as roadid,
IFNULL(rlui.from_crossid, IFNULL(r.from_crossid,udr.from_crossid)) as from_crossid,
IFNULL(rlui.to_crossid, IFNULL(r.to_crossid,udr.to_crossid)) as to_crossid,
IFNULL(rlui.name, IFNULL(r.name,udr.name)) as road_name,
IFNULL(rlui.src_direct, IFNULL(r.src_direct,udr.src_direct)) as src_direct
from `bound_crosses` as c
join `user_defined_cross` udc on udc.crossid = c.crossid and udc.nodeid = %s and udc.area_id = %s
left join `road` as r on r.nodeid = %s and r.to_crossid = udc.crossid and r.recordstate = 0 and (r.is_sup_road is null or r.is_sup_road <> 1)
left join `road_ledger_update_info` as rlui on rlui.nodeid = %s and rlui.recordstate = 0 and rlui.to_crossid = r.to_crossid and (rlui.is_sup_road is null or rlui.is_sup_road <> 1)
left join `user_defined_roads` as udr on udr.nodeid = %s and udr.recordstate = 0 and udr.to_crossid = c.crossid and (udr.is_sup_road is null or udr.is_sup_road <> 1)
where c.nodeid = %s
and c.area_id = %s"""
print(cursor.mogrify(sql, (
nodeid, area_id, nodeid, area_id, nodeid, nodeid, nodeid, int(nodeid), int(area_id), nodeid, area_id,
nodeid, nodeid, nodeid, int(nodeid), int(area_id))))
cursor.execute(sql, (
nodeid, area_id, nodeid, area_id, nodeid, nodeid, nodeid, int(nodeid), int(area_id), nodeid, area_id,
nodeid, nodeid, nodeid, int(nodeid), int(area_id)))
result = cursor.fetchall()
self.close(conn, cursor)
return result, None
except Exception as error:
self.close(conn, cursor)
return None, error
def query_cross_info_all(self, crossids: []):
conn, cursor = self.connect()
try:
sql = f'''select IFNULL(clui.name, c2.name) as name,
c2.crossid,
IFNULL(clui.location, c2.location) as location,
c2.nodeid,
c2.area_id
from `cross` as c2
left join `cross_ledger_update_info` as clui on clui.crossid = c2.crossid
where c2.crossid in %s
union all
select udc.name,
udc.crossid,
udc.location,
udc.nodeid,
udc.area_id
from `user_defined_cross` as udc
where udc.crossid in %s'''
print(cursor.mogrify(sql, (crossids, crossids)))
cursor.execute(sql, (crossids, crossids))
# 获取所有查询结果
result = cursor.fetchall()
self.close(conn, cursor)
return result, None
except Exception as e:
self.close(conn, cursor)
return None, e
def query_road_info_all(self, roadids: []):
conn, cursor = self.connect()
try:
sql = f'''select
IFNULL(rlui.name, r.name) as name,
IFNULL(rlui.from_crossid, r.from_crossid) as from_crossid,
IFNULL(rlui.to_crossid, r.to_crossid) as to_crossid,
IFNULL(rlui.src_direct, r.src_direct) as src_direct,
r.nodeid
from `road` as r
left join `road_ledger_update_info` as rlui on rlui.roadid = r.roadid
where r.roadid in %s
union all
select name
from_crossid,
to_crossid,
src_direct,
nodeid
from `user_defined_roads`
where roadid in %s'''
print(cursor.mogrify(sql, (roadids, roadids)))
cursor.execute(sql, (roadids, roadids))
# 获取所有查询结果
result = cursor.fetchall()
self.close(conn, cursor)
return result, None
except Exception as e:
self.close(conn, cursor)
return None, e