# -*- coding: utf-8 -*-
from sqlalchemy import String, Column, Integer,DateTime,Text,Date,Time,ForeignKey
from database import Base
from sqlalchemy.orm import relationship
from datetime import datetime


class JobConfigEntity(Base):
    __tablename__ = "tp_job_config"

    id = Column(Integer, primary_key=True, autoincrement=True,comment="id")
    jobName = Column(String(255), nullable=False,comment="任务名称")
    groupName = Column (String(255),default='',comment="组名称")
    enable = Column(Integer,default=True,comment="启用状态")
    create_time = Column(DateTime, default=datetime.now,comment="任务创建时间")
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now,comment="任务最后修改时间")
    triggerType = Column(String(2),comment="更新机制 1:手动触发;2:每时;3:每天;4:每周;5:每月;6:每年")
    readerPreSQL = Column(String(255),default='',comment="源库前置SQL")
    readerSQL = Column(Text, nullable=False,comment="源库取数SQL")   # {sql} order by create_time limit ,writerBatchSize  {sql}:select * from xxx where
    readerPostSQL = Column(String(255),default='',comment="源库后置SQL")
    writerPreSQL = Column(String(255),default='',comment="目标库前置SQL") #清表动作
    writerPostSQL = Column(String(255),default='',comment="目标库后置SQL")
    columns = Column(Text,comment="入库列")
    fromDbId = Column(String(100),nullable = False,comment = "源库ID")
    toDbId = Column(String(100),nullable = False,comment = "目标库ID")
    toTableName = Column(String(100),nullable=False,comment="目标表")
    toTablePKColumns = Column(String(50),comment="目标表主键字段")  # 按,分隔开
    writerBatchSize = Column(Integer,default=0,comment="每次事务写记录数")  # divmod(total, size)
    defaultParamsMapJson = Column(String(100),default='',comment="默认参数值json串") #{"create_time":"2024-07-26 00:00:00"}
    incrementColumns = Column(String(50),comment="增量字段") # create_time


    class Config:
        orm_mode = True


class JobHistoryEntity(Base):
    __tablename__ = "tp_job_history"

    id = Column(Integer, primary_key=True, autoincrement=True,comment="id")
    configId = Column(Integer, comment="configid")
    jobName = Column(String(255), nullable=False,comment="任务名称")
    postStepId = Column(Integer, nullable=False, comment="postStepId")
    postStepState = Column(Integer,comment="任务状态 1:执行中;2:成功;3:失败")
    preStepSuccessRecord = Column(Integer, comment="读库数")
    postStepSuccessRecord = Column(Integer, comment="入库数")
    create_time = Column(DateTime, default=datetime.now,comment="任务开始执行时间")
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now,comment="任务最后更新时间")
    toTableName = Column(String(100),nullable=False,comment="目标表")
    totalCostsMS = Column(Integer, comment="执行总耗时ms")

    class Config:
        orm_mode = True

class JobStepEntity(Base):
    __tablename__ = "tp_job_step"

    id = Column(Integer, primary_key=True, autoincrement=True,comment="id")
    jobHisId = Column(Integer, comment="jobhisid")
    state = Column(Integer,comment="任务状态 1:执行中;2:成功;3:失败")
    create_time = Column(DateTime, default=datetime.now,comment="任务开始执行时间")
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now,comment="任务最后更新时间")
    paramsMapJson = Column(String(100),default='',comment="已合并参数JSON") # {"create_time":"2024-07-26 00:00:00"}
    incrementColumnMaxValueJson = Column(String(100),default='',comment="执行结束增量字段最大值") # tmp
    totalReadRecords = Column(Integer, comment="总读取记录行数")# select count(*) from ({sql})A
    totalWriteSuccessRecords = Column(Integer, comment="总成功写入行数")
    totalErrorRecords = Column(Integer, comment="总失败记录行数")
    totalCostsMS = Column(Integer, comment="执行总耗时ms")

    class Config:
        orm_mode = True