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 * from ( 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,rlui.roadid) as roadid, IFNULL(rlui.from_crossid, r.from_crossid) as from_crossid, IFNULL(rlui.to_crossid, r.to_crossid) as to_crossid, IFNULL(rlui.name, r.name) as road_name, IFNULL(rlui.src_direct, r.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.roadid = r.roadid and 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) where c.nodeid = %s and c.area_id = %s union all select IFNULL(clui.name, c2.name) as name, c.crossid, IFNULL(clui.location, c.location) as location, c.nodeid, c.area_id, udr.roadid, udr.from_crossid, udr.to_crossid, udr.name, udr.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 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,rlui.roadid) as roadid, IFNULL(rlui.from_crossid, r.from_crossid) as from_crossid, IFNULL(rlui.to_crossid, r.to_crossid) as to_crossid, IFNULL(rlui.name, r.name) as road_name, IFNULL(rlui.src_direct, r.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.roadid = r.roadid and 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) where c.nodeid = %s and c.area_id = %s union all select udc.name, udc.crossid, udc.location, c.nodeid, c.area_id, udr.roadid, udr.from_crossid, udr.to_crossid, udr.name as road_name, udr.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 `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) as t where t.src_direct is not NULL""" print(cursor.mogrify(sql, ( nodeid, area_id, nodeid, area_id, nodeid, nodeid, int(nodeid), int(area_id), nodeid, area_id, nodeid, area_id, nodeid, int(nodeid), int(area_id), nodeid, area_id, nodeid, nodeid, int(nodeid), int(area_id), nodeid, area_id, nodeid, int(nodeid), int(area_id)))) cursor.execute(sql, ( nodeid, area_id, nodeid, area_id, nodeid, nodeid, int(nodeid), int(area_id), nodeid, area_id, nodeid, area_id, nodeid, int(nodeid), int(area_id), nodeid, area_id, nodeid, nodeid, int(nodeid), int(area_id), nodeid, area_id, 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 def query_routing_crosses(self, nodeid, area_id): # and is_routing_inspection = 1 sql = """ select t1.crossid, if(t2.name is not null, t2.name, t1.name) as name, if(t2.cross_no is not null, t2.cross_no, t1.cross_no) as crossno, if(t2.company is not null, t2.company, t1.company) as company, if(t2.location is not null, t2.location, t1.location) as location, t1.nodeid, t1.area_id from (select crossid, name, location, nodeid, area_id, cross_no, company from `cross` where nodeid = %s and area_id = %s) t1 left join (select crossid, name, location, nodeid, area_id, cross_no, company from `cross_ledger_update_info` where nodeid = %s and area_id = %s ) t2 on t1.crossid = t2.crossid where t1.area_id = %s and t1.nodeid = %s and t1.crossid in (select crossid from bound_crosses where area_id = %s and nodeid = %s and is_routing_inspection = 1) union all select crossid, name, location, nodeid, area_id, cross_no, company from user_defined_cross where area_id = %s and nodeid = %s and crossid in (select crossid from bound_crosses where area_id = %s and nodeid = %s and is_routing_inspection = 1) """ % (nodeid, area_id, nodeid, area_id, area_id, nodeid, area_id, nodeid, area_id, nodeid, area_id, nodeid) return self.do_select(sql) def query_crosses_slc_company_info(self, nodeid, area_id, crossid_list): crossids = "'" + "', '".join(item for item in crossid_list) + "'" sql = """ select t3.slc_company, count(1) as num from (select if((t1.slc_company is null or t2.slc_company != t1.slc_company), t2.slc_company, t1.slc_company) as slc_company from (select crossid, slc_company from `cross` where nodeid = %s and area_id = %s and crossid in (%s)) t1 left join (select crossid, slc_company from `cross_ledger_update_info` where nodeid = %s and area_id = %s and crossid in (%s)) t2 on t1.crossid = t2.crossid) t3 group by t3.slc_company """ % (nodeid, area_id, crossids, nodeid, area_id, crossids) return self.do_select(sql) def query_crosses_internet_info(self, nodeid, area_id, crossid_list): crossids = "'" + "', '".join(item for item in crossid_list) + "'" sql = """ select t3.internet, count(1) as num from (select if((t1.internet is null or t2.internet != t1.internet), t2.internet, t1.internet) as internet from (select crossid, internet from `cross` where nodeid = %s and area_id = %s and crossid in (%s)) t1 left join (select crossid, internet from `cross_ledger_update_info` where nodeid = %s and area_id = %s and crossid in (%s)) t2 on t1.crossid = t2.crossid) t3 group by t3.internet """ % (nodeid, area_id, crossids, nodeid, area_id, crossids) return self.do_select(sql) def check_reverse_turn(self, roads): roads = "'" + "', '".join(item for item in roads) + "'" sql = """ select sum(reverse_turn) as num from road_ledger_update_info where roadid in (%s) """ % (roads) res = self.do_select(sql) check_res = 0 if res: check_res = res[0]['num'] return check_res def query_inroads_by_crossids(self, nodeid,crossid_list): crossids = "'" + "', '".join(item for item in crossid_list) + "'" 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 in (%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 in (%s) and (is_sup_road is null or is_sup_road<>1)) t2 on t1.roadid = t2.roadid; """ % (nodeid, crossids, nodeid, crossids) return self.do_select(sql) def calc_has_reversible_lane_crosses(self, crossid_list): crossids = "'" + "', '".join(item for item in crossid_list) + "'" sql = """ select count(1) as num from reversible_lanes_info where crossid in (%s) """ % (crossids) return self.do_select(sql) def query_cross_ledger_info(self, crossid, nodeid, area_id): sql = """ select t1.crossid, if(t2.name is not null, t2.name, t1.name) as name, if(t2.location is not null, t2.location, t1.location) as location, if(t2.internet is not null, t2.internet, t1.internet) as internet, if(t2.cross_model is not null, t2.cross_model, t1.cross_model) as cross_model, if(t2.slc_company is not null, t2.slc_company, t1.slc_company) as slc_company, t1.nodeid, t1.area_id from (select crossid, name, location, nodeid, area_id from `cross` where nodeid = %s and area_id = %s and crossid = '%s') t1 left join (select crossid, name, location, nodeid, area_id from `cross_ledger_update_info` where nodeid = %s and area_id = %s and crossid = '%s') t2 on t1.crossid = t2.crossid where t1.area_id = %s and t1.nodeid = %s """ % (nodeid, area_id, crossid, nodeid, area_id, crossid, area_id, nodeid) return self.do_select(sql) def query_virtual_roads(self, crossids): crossids = "'" + "', '".join(item for item in crossids) + "'" sql = """ select roadid, reverse_turn from user_defined_roads where to_crossid in (%s) """ % (crossids) return self.do_select(sql) def query_virtual_cross_info(self, crossid, nodeid, area_id): sql = """ select * from user_defined_cross where nodeid = %s and area_id = %s and crossid = '%s' """ % (nodeid, area_id, crossid) return self.do_select(sql) def query_cross_survey_job(self, crossid, area_id): sql = """ select * from cross_survey.cross_survey_jobs where crossid = '%s' and area_id = %s """ % (crossid, area_id) return self.do_select(sql) def insert_cross_survey_job(self, values): sql = """ insert into cross_survey.cross_survey_jobs (crossid, start_day, end_day, status, done_inroads, inroads_dir, nodeid, area_id) values(%s, %s, %s, %s, %s, %s, %s, %s) """ return self.do_executemany(sql, values) def query_survey_job_info_by_id(self, job_id): sql = """ select * from cross_survey.cross_survey_jobs where id = %s """ % (job_id) return self.do_select(sql) def del_cross_survey_job(self, job_id): sql = """ delete from cross_survey.cross_survey_jobs where id = %s """ % (job_id) return self.do_execute(sql) def query_survey_job_info_by_area_id(self, area_id): sql = """ select id, crossid, start_day, end_day, update_time,status,create_time from cross_survey.cross_survey_jobs where area_id = %s """ % (area_id) return self.do_select(sql) def update_cross_survey_job_status(self, job_info, jobid, done_inroads=None): if not done_inroads: done_inroads = job_info[0]['done_inroads'].replace('1', '0') sql = """ update cross_survey.cross_survey_jobs set status = 0, done_inroads = '%s' where id = %s """ % (done_inroads, jobid) return self.do_execute(sql)