
from unitls.LogerinTxt import app_logger
from .flightDB import flightDB


def insertWorkjob(database:flightDB,workjobdata,taskid):
    try:
        A=["MDTITLE_C","WORKSTEP","BASE_CODE","PLANSTD"]
        num=1
        MDTITLE_C=""
        WORKSTEP=""
        PLANSTD=""
        BASE_CODE_now=""
        if len(workjobdata) != 0:
            for row in workjobdata:
                MDTITLE_C_now=row["MDTITLE_C"]  if "MDTITLE_C" in row.keys() else ""
                WORKSTEP_now = row["WORKSTEP"] if "WORKSTEP" in row.keys() else ""
                BASE_CODE_now = row["BASE_CODE"] if "BASE_CODE" in row.keys() else ""
                PLANSTD_now = row["PLANSTD"] if "PLANSTD" in row.keys() else ""
                if MDTITLE_C !="":
                    MDTITLE_C=MDTITLE_C+"\n------------------------------------------------\n"+"工作包%s内容："%num+"\n"+MDTITLE_C_now
                    WORKSTEP=WORKSTEP+"\n------------------------------------------------\n"+"工作包%s步骤："%num+"\n"+WORKSTEP_now
                    PLANSTD=PLANSTD+"\n------------------------------------------------\n"+"工作包%s计划开始时间："%num+"\n"+PLANSTD_now
                else:
                    MDTITLE_C ="工作包%s内容：" % num + "\n" + MDTITLE_C_now
                    WORKSTEP ="工作包%s步骤：" % num + "\n" + WORKSTEP_now
                    PLANSTD ="工作包%s计划开始时间：" % num + "\n" + PLANSTD_now
                num+=1
            newdic={"基地":"'%s'"%BASE_CODE_now,"开始时间":"'%s'"%PLANSTD,"项目描述":"'%s'"%MDTITLE_C.replace("\"","*").replace("#","*").replace("\\",";").replace("\'","*").replace("/","、"),"工作步骤":"'%s'"%WORKSTEP.replace("\'","*").replace("\\",";").replace("/","、").replace("\"","*").replace("#","*")}
            database.lazyUpdateItem('workjob', newdic, "任务编号='%s'" %taskid)
        res={"返回值":"ok"}
        database.FunctionCommit()
        return res
    except Exception as e:
        app_logger.log_error(e)
        res = {"返回值": "fail"}
        database.FunctionCommit()
        return res
