def submit_record(user, jobs, plans): db = Database() comands = [] jps, keys, texts = [{}, {}], ["job", "plan"], ["任务", "计划"] for i, rs in enumerate([jobs, plans]): for r in rs: w = WorkRecord() w.load_data(user, r) sql, params = w.get_sql(i) comands.append([sql, params]) if w.task_id == None: if w.task == "": return False, "任务不能为空!" continue if int(w.task_id) == -1: continue if w.task_id in jps[i]: return False, "%s项重复!" % texts[i] jps[i][w.task_id] = w.progress flag, msg = check_record(w, jps[0], db, keys[i]) if not flag: return flag, msg '''End For 2''' '''End For 1''' flag, data = db.Transaction(comands) if not flag: data = "请检查数据问题或今日是否已提交" return flag, data
def add_change(user, t_id, plan_time, progress, reason): db = Database() '''获取任务基本信息''' sql = "select planEndTime, multiUser from task where id=%s" cnx, cursor = db.Query(sql, (t_id, )) src_time, multi_user = cursor.fetchone() '''获取历史变更信息''' sql = "select id, executor, createTime from plan_change where taskId=%s order by createTime desc limit 1 for update" cursor = db.select(sql, (t_id, )) line = cursor.fetchone() flag = True if line: _, executor, tm = line '''协同任务或该用户已提交计划未执行''' if multi_user or executor == user: sql = "select count(*) from record_day where taskId=%s and workday>%s " db.select(sql, (t_id, tm), cursor) count = cursor.fetchone()[0] if count == 0: flag = False '''End If 2''' '''End If 1''' '''有已更改的计划暂未实施,不能新增''' if not flag: cursor.close() return False, "该任务有已更改的计划暂未实施,不能新增''" sql = "insert into plan_change (taskId, srcTime, planTime, changeUser, executor, progress, reason, multiUser) \ values (%s, %s, %s, %s, %s, %s, %s, %s)" params = (t_id, src_time, plan_time, user, user, progress, reason, multi_user) flag, _ = db.Transaction([[sql, params]], cnx, cursor) return flag, "操作失败"
def request_rumor_type_data(rumor_type=0): error_times = 0 db = Database() i = 0 while i < 5: i += 1 L.info("Preparing for type {} page {}".format(str(rumor_type), str(i))) time.sleep(3) rst = request_data( "https://lab.isaaclin.cn/nCoV/api/rumors?num=50&rumorType={}&page={}" .format(str(rumor_type), str(i)), str(i)) if rst['success'] == False: if error_times == 10: break error_times += 1 L.info( "This is the {} times fail at getting page {}, will try it again." .format(str(error_times), str(i))) i -= 1 continue else: error_times = 0 if not rst['results']: L.info( "Collecting type {} rumor data finished.".format(rumor_type)) break comands = [] for line in rst['results']: ks = line.keys() params = line['title'] sql = "select * from rumor where title ='{}'".format(params) data = db.select(sql) if not data: sql = "insert into rumor (" + ','.join( ks) + ") values (" + ', '.join(['%s' for k in ks]) + ")" params = [line[k] for k in ks] comands.append([sql, params]) try: db.Transaction(comands) L.info("Writing database for {} rumor.".format(str(len(comands)))) except Exception as e: if error_times == 10: break error_times += 1 L.info( "This is the {} times fail at wirting database due to {}, will try this page again." .format(str(error_times), str(e))) i -= 1 continue if error_times == 10: L.info("Collecting rumor data finished with error.") else: L.info("Collecting rumor data finished.")
def import_region(): db = Database() lines = [] levels = {'province': 1, 'city': 2, 'district': 3} with open("geojson/adcode-map.json", encoding='utf8') as load_f: dt = json.load(load_f) for item in dt: sql = "insert into region (name, code, level) values (%s, %s, %s)" param = (item['name'], item['adcode'], levels[item['level']]) lines.append([sql, param]) db.Transaction(lines)
def login_out(user, ip='', address=''): del_token(user) db = Database() sqlU, paramsU = ( "update user set onlineStatus = %s where userName = %s"), (0, user) sqlL, paramsL = ("insert into logs (user, ip, style, createTime, address) values (%s, %s, 1, %s, %s)"), \ (user, ip, TM(), address) db.Transaction([[sqlU, paramsU], [sqlL, paramsL]]) return {}
def add_city_code(): db = Database() sql = "select id, region_name, region_parent FROM patients WHERE region_level=2 and region_code=0" comands = [] for (id_, name, parent) in db.select(sql): code = check_city(name, parent) if not code: print(parent, name, code) continue sql = "update patients set region_code=%s where id=%s" comands.append([sql, (code, id_)]) db.Transaction(comands)
def import_region_special(): db = Database() lines = [] levels = {'province': 1, 'city': 2, 'district': 3} for key in ["110000", "500000", "120000", "310000"]: with open("geojson/" + key + ".json", encoding='utf8') as load_f: dt = json.load(load_f) for item in dt["features"]: sql = "insert into region (name, code, level, parent) values (%s, %s, %s, %s)" param = (item['properties']['name'], item['id'], 2, key) lines.append([sql, param]) db.Transaction(lines)
def import_data_from_files(): db = Database() idx = 0 for filename in get_files(): idx += 1 print(filename) commands = load_file_province(filename) db.Transaction(commands) print(idx, "OK! ") print("Finished!") for c in Position.Errors: print("未识别的城市:{}, {}处".format(c, Position.Errors[c]))
def request_data_time_series(): L.info("Start update data time series.") needed = {} for p in SP: if p['name'] == '香港特别行政区': needed['香港'] = p if p['name'] == '澳门特别行政区': needed['澳门'] = p if p['name'] == '台湾省': needed['台湾'] = p needed[p['name']] = p with open('./json/DXYArea-TimeSeries.json', 'r') as file: data = json.load(file) db = Database() history = getLatest(db) lines = [] for province in data: if province["provinceName"] in needed: p = needed[province["provinceName"]] translate([province], p, lines) L.info("\tRead {} lines from file".format(len(lines))) comands = [] for line in lines: '''排除已有历史数据''' key = '_'.join([ str(line['region_code']), line['region_name'], str(line['region_parent']) ]) if key in history and not date_less(history[key], line['data_date']): continue ks = line.keys() sql = "insert into patients (" + ','.join( ks) + ") values (" + ', '.join(['%s' for k in ks]) + ")" params = [line[k] for k in ks] comands.append([sql, params]) L.info("\tNew data lines count:" + str(len(comands))) if len(comands) > 0: db.Transaction(comands)
def request_data_province(): names = {'香港特别行政区': '香港', '澳门特别行政区': '澳门', '台湾省': '台湾'} url = "https://lab.isaaclin.cn/nCoV/api/area?latest=1&province=" db = Database() history = getLatest(db) idx = 0 for p in SP: idx += 1 # if idx <= 23: continue purl = url + parse.quote(names.get(p['name'], p['name'])) rst = request_data(purl, p['name']) if not rst: continue data, lines = rst['results'], [] translate(data, p, lines) L.info("Get data collects count:" + str(len(data))) comands = [] for line in lines: '''排除已有历史数据''' key = '_'.join([ str(line['region_code']), line['region_name'], str(line['region_parent']) ]) if key in history and not date_less(history[key], line['data_date']): continue ks = line.keys() sql = "insert into patients (" + ','.join( ks) + ") values (" + ', '.join(['%s' for k in ks]) + ")" params = [line[k] for k in ks] comands.append([sql, params]) L.info("New data lines count:" + str(len(comands))) if len(comands) > 0: db.Transaction(comands) L.info("{}\t {} finished!".format(idx, p['name'])) time.sleep(3)
def user_login(user, passwd, ip, address='', isApp=None): db = Database() sql = ("select password, status, level, nickname, type, id from user \ where username = %s and isDel!=1") params = (user, ) L.info("user {} login from ip: {}".format(user, ip)) dt = db.selectEx(sql, params) rst = {'error': 1, 'message': 'success', 'data': {}} print(dt) if len(dt) == 0: rst['message'] = '用户不存在' elif dt[0][0] != passwd and get_md5(dt[0][0]) != passwd: rst['message'] = '密码错误' else: if dt[0][1] != 0: rst['message'] = '用户登陆受限' else: rst['error'] = 0 rst['data'] = { 'level': dt[0][2], 'type': dt[0][4], 'id': dt[0][5], 'nickname': dt[0][3].encode("utf8") if dt[0][3] else '未命名' } '''End If''' '''登陆成功''' if rst['error'] == 0: sqlU, paramsU = ( "update user set loginTime = %s, onlineStatus = %s where \ username = %s"), (TM(), 1, user) sqlL, paramsL = ("insert into logs (user, ip, address, client) values (%s, %s, %s, %s)"), \ (user, ip, address, 'app' if isApp else 'pc') db.Transaction([[sqlU, paramsU], [sqlL, paramsL]]) return rst