DROP TABLE IF EXISTS hrsa_tax_agent_emp;
CREATE TABLE hrsa_tax_agent_emp
(
    id            bigint(20) NOT NULL COMMENT 'ID',
    create_time   datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
    update_time   datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
    creator       bigint(20) NULL DEFAULT NULL COMMENT '创建人id',
    delete_type   int(11) NULL DEFAULT 0,
    tenant_key    varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '租户KEY',
    tax_agent_id  bigint(20) NULL DEFAULT NULL COMMENT '个税扣缴义务人的主键id',
    employee_id   bigint(20) NULL DEFAULT NULL COMMENT '人员信息表的主键id',
    employee_name varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
    PRIMARY KEY (id) USING BTREE,
    INDEX         idx_tenant_key(tenant_key) USING BTREE,
    INDEX         idx_tax_agent(tax_agent_id) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS hrsa_tax_agent_emp_change;
CREATE TABLE hrsa_tax_agent_emp_change
(
    id            bigint(20) NOT NULL COMMENT 'ID',
    create_time   datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
    update_time   datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
    creator       bigint(20) NULL DEFAULT NULL COMMENT '创建人id',
    delete_type   int(11) NULL DEFAULT 0 COMMENT '是否删除',
    tenant_key    varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '租户KEY',
    tax_agent_id  bigint(20) NOT NULL COMMENT '个税扣缴义务人的主键id',
    employee_id   bigint(20) NOT NULL COMMENT '人员信息表的主键id',
    change_type   int(11) NOT NULL DEFAULT 0 COMMENT '增量类型。1：新增、2：删除',
    employee_name varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
    module_type   int(4) NOT NULL DEFAULT 0,
    PRIMARY KEY (id) USING BTREE,
    INDEX         idx_tenant_key(tenant_key) USING BTREE,
    INDEX         idx_tax_agent(tax_agent_id) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;


DROP TABLE IF EXISTS hrsa_tax_agent_admin;
CREATE TABLE hrsa_tax_agent_admin
(
    id           bigint(20) NOT NULL COMMENT '主键',
    tax_agent_id bigint(20) NOT NULL COMMENT '个税扣缴义务人的主键id',
    employee_id  bigint(20) NOT NULL COMMENT '人员信息表的主键id',
    create_time  datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP (0) COMMENT '创建时间',
    update_time  datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP (0) ON UPDATE CURRENT_TIMESTAMP (0) COMMENT '更新时间',
    creator      bigint(20) NOT NULL DEFAULT 0 COMMENT '创建人',
    delete_type  int(11) NOT NULL DEFAULT 0 COMMENT '是否已删除。0：未删除、1：已删除',
    tenant_key   varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '租户ID',
    PRIMARY KEY (id) USING BTREE,
    INDEX        idx_tenant_key(tenant_key) USING BTREE,
    INDEX        idx_employee_id(employee_id) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '个税扣缴义务人管理员表' ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS hrsa_tax_agent_manage_range;
CREATE TABLE hrsa_tax_agent_manage_range
(
    id                     bigint(20) NOT NULL COMMENT '主键id',
    tax_agent_id           bigint(20) NOT NULL DEFAULT 0 COMMENT '个税扣缴义务人的id',
    employee_id            bigint(20) NOT NULL DEFAULT 0 COMMENT '人员表的主键id',
    tax_agent_sub_admin_id bigint(20) NOT NULL DEFAULT 0 COMMENT '个税扣缴义务人的分管理员的id',
    target_type            tinyint(4) NOT NULL DEFAULT 1 COMMENT '对象类型。1：人员、2：部门、3：岗位',
    target_id              bigint(20) NOT NULL DEFAULT 0 COMMENT '对象id',
    employee_status        varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
    include_type           int(4) NOT NULL DEFAULT 1,
    creator                bigint(20) NOT NULL DEFAULT 0 COMMENT '创建人',
    create_time            datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP (0) COMMENT '创建时间',
    update_time            datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP (0) ON UPDATE CURRENT_TIMESTAMP (0) COMMENT '更新时间',
    delete_type            int(11) NOT NULL DEFAULT 0 COMMENT '是否已删除。0：未删除、1：已删除',
    tenant_key             varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci  NOT NULL DEFAULT '' COMMENT '租户ID',
    range_type             int(4) NOT NULL DEFAULT 0,
    PRIMARY KEY (id) USING BTREE,
    INDEX                  idx_tenant(tenant_key) USING BTREE,
    INDEX                  idx_tax_agent_sub_admin_id(tax_agent_sub_admin_id) USING BTREE,
    INDEX                  idx_tenant_key(tenant_key) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '个税扣缴义务人的管理范围表' ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS hrsa_tax_agent_base;
CREATE TABLE hrsa_tax_agent_base
(
    id                bigint(20) NOT NULL COMMENT '主键',
    devolution_status int(11) NOT NULL DEFAULT 0 COMMENT '分权开启状态。0：关、1：开',
    create_time       datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP (0) COMMENT '创建时间',
    update_time       datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP (0) ON UPDATE CURRENT_TIMESTAMP (0) COMMENT '更新时间',
    creator           bigint(20) NOT NULL DEFAULT 0 COMMENT '创建人',
    delete_type       int(11) NOT NULL DEFAULT 0 COMMENT '是否已删除。0：未删除、1：已删除',
    tenant_key        varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '租户ID',
    PRIMARY KEY (id) USING BTREE,
    INDEX             idx_tenant_key(tenant_key) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '个税扣缴义务人基础信息表' ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS hrsa_tax_agent_sub_admin;
CREATE TABLE hrsa_tax_agent_sub_admin
(
    id           bigint(20) NOT NULL COMMENT '主键',
    tax_agent_id bigint(20) NOT NULL COMMENT '个税扣缴义务人的主键id',
    employee_id  bigint(20) NOT NULL COMMENT '人员信息表的主键id',
    description  varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '描述',
    create_time  datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    update_time  datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP (0) COMMENT '更新时间',
    creator      bigint(20) NOT NULL DEFAULT 0 COMMENT '创建人',
    delete_type  int(11) NOT NULL DEFAULT 0 COMMENT '是否已删除。0：未删除、1：已删除',
    tenant_key   varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '租户ID',
    remark       text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL,
    PRIMARY KEY (id) USING BTREE,
    INDEX        idx_tenant_key(tenant_key) USING BTREE,
    INDEX        idx_tax_agent_id(tax_agent_id) USING BTREE,
    INDEX        idx_employee_id(employee_id) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '个税扣缴义务人分管理员表' ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS hrsa_tax_agent_sub_admin_emp;
CREATE TABLE hrsa_tax_agent_sub_admin_emp
(
    id                     bigint(20) NOT NULL COMMENT 'ID',
    create_time            datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
    update_time            datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
    creator                bigint(20) NULL DEFAULT NULL COMMENT '创建人id',
    delete_type            int(11) NULL DEFAULT 0 COMMENT '是否删除',
    tenant_key             varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '租户KEY',
    tax_agent_id           bigint(20) NOT NULL DEFAULT 0 COMMENT '个税扣缴义务人的主键id',
    tax_agent_sub_admin_id bigint(20) NOT NULL DEFAULT 0 COMMENT '个税扣缴义务人的分管理员的id',
    employee_id            bigint(20) NOT NULL DEFAULT 0 COMMENT '人员信息表的主键id',
    employee_name          varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '人员姓名',
    PRIMARY KEY (id) USING BTREE,
    INDEX                  idx_tenant_key(tenant_key) USING BTREE,
    INDEX                  idx_tax_agent(tax_agent_id) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;



ALTER TABLE hrsa_tax_agent ADD COLUMN payment_agency varchar(255) NULL COMMENT '代缴机构' AFTER tenant_key;


ALTER TABLE hrsa_salary_sob ADD COLUMN tax_agent_id bigint(0) NULL COMMENT '个税扣缴义务人的主键id' AFTER tenant_key;


INSERT INTO hrsa_tax_agent_base(id, devolution_status, create_time, update_time, creator, delete_type, tenant_key) VALUES (1653303537239, 1, '2022-05-23 18:58:53', '2022-05-23 19:12:12', 1, 0, 'all_teams');

ALTER TABLE hrsa_bill_detail_temp ADD COLUMN payment_organization bigint(0) NULL COMMENT '个税扣缴义务人id' AFTER tenant_key;

ALTER TABLE hrsa_bill_detail ADD COLUMN payment_organization bigint(0) NULL COMMENT '个税扣缴义务人id' AFTER tenant_key;

ALTER TABLE hrsa_bill_batch ADD COLUMN payment_organization bigint(0) NULL COMMENT '个税扣缴义务人id' AFTER tenant_key;