from unitls.LogerinTxt import app_logger

from .flightDB import flightDB


def UpdatePeopleScheduleFromOnline(localdatabase:flightDB,onlinedatabase:flightDB):
    sts=False
    dydict={"放行":"放行", "工1":"勤务1", "工2":"勤务2","工3":"工1","工4":"工2","班组长":"工3","备注":"附加消息"}
    try:
        needupdate=onlinedatabase.queryTabel('change_log_onlineflight','*',"状态 ='未同步'")
        if needupdate:
            for singledata in needupdate:
                res = localdatabase.getSingledata("编号", "peopleSchedule%s" % singledata[7])
                if singledata[1] in str(res) :
                    localdatabase.upDateItem("peopleSchedule%s" % singledata[7] , {"%s" % dydict[singledata[2]] : "'%s'" % singledata[4] } ,"编号='%s'" % singledata[1])
                else:
                    dic = {"编号" : singledata[1], "航班编号" : singledata[1][:-4], "显示模式" : "A", "%s" % dydict[singledata[2]]:"%s" % singledata[4] }
                    for key in dydict.values() :
                        if key not in dic.keys() :
                            dic[key] = ''
                    localdatabase.insertData("peopleSchedule%s" % singledata[7] , dic)
                newdic = {"航班编号" : "%s" % singledata[1], "提示信息": "" , "信息状态" : "","警告信息" : f"【{singledata[1]}】收到云端在线编辑变更：{singledata[2]}由{singledata[3]}变更为{singledata[4]}，变更人：{singledata[6]}，请进行识别和处理！" ,"产生时间" : "%s" % singledata[5] , "处理人" : "" , "处理时间" : "" , "变更字段" : "人员安排云端编辑写入" , "显示对象" : "" , "提示内容" : "" }
                localdatabase.insertData("logs", newdic)
                onlinedatabase.upDateItem("change_log_onlineflight" , {"状态" : "'已同步'" } , "taskid='%s'" % singledata[1])
                sts=True
        return sts
    except Exception as e:
        app_logger.log_error(e)
        return sts