-- 1. 社保/公积金模板主表
CREATE TABLE `oa_insurance_template`
(
    `insurance_template_id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '模板ID',
    `template_name`         VARCHAR(100) NOT NULL COMMENT '模板名称',
    `del_flag`              TINYINT(1)   NOT NULL DEFAULT 0 COMMENT '删除标志 0=未删,1=已删',
    `remark`                VARCHAR(255) COMMENT '备注',
    `created_by`            VARCHAR(50) COMMENT '创建人',
    `create_time`           DATETIME              DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_by`            VARCHAR(50) COMMENT '更新人',
    `update_time`           DATETIME              DEFAULT CURRENT_TIMESTAMP
        ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4 COMMENT ='社保/公积金模板主表';

-- 2. 社保/公积金模板明细表
CREATE TABLE `oa_insurance_template_detail`
(
    `insurance_detail_id`   BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '明细ID',
    `insurance_template_id` BIGINT         NOT NULL COMMENT '模板ID → oa_insurance_template.insurance_template_id',
    `insurance_type`        VARCHAR(50)    NOT NULL COMMENT '险种代码，如 pension、unemployment…',
    `amount`                DECIMAL(12, 2) NOT NULL COMMENT '金额或比例',
    `del_flag`              TINYINT(1)     NOT NULL DEFAULT 0 COMMENT '删除标志',
    `remark`                VARCHAR(255) COMMENT '备注',
    `created_by`            VARCHAR(50) COMMENT '创建人',
    `create_time`           DATETIME                DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_by`            VARCHAR(50) COMMENT '更新人',
    `update_time`           DATETIME                DEFAULT CURRENT_TIMESTAMP
        ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX (`insurance_template_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4 COMMENT ='社保/公积金模板明细表';

-- 3. 薪资模板主表
CREATE TABLE `oa_salary_template`
(
    `salary_template_id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '模板ID',
    `template_name`      VARCHAR(100) NOT NULL COMMENT '模板名称',
    `del_flag`           TINYINT(1)   NOT NULL DEFAULT 0 COMMENT '删除标志',
    `remark`             VARCHAR(255) COMMENT '备注',
    `created_by`         VARCHAR(50) COMMENT '创建人',
    `create_time`        DATETIME              DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_by`         VARCHAR(50) COMMENT '更新人',
    `update_time`        DATETIME              DEFAULT CURRENT_TIMESTAMP
        ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4 COMMENT ='薪资模板主表';

-- 4. 薪资模板明细表（金额与费率分开）
CREATE TABLE `oa_salary_template_detail`
(
    `salary_detail_id`   BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '明细ID',
    `salary_template_id` BIGINT      NOT NULL COMMENT '模板ID → oa_salary_template.salary_template_id',
    `salary_item`        VARCHAR(50) NOT NULL COMMENT '项目代码，如 basic_salary…',
    `amount`             DECIMAL(12, 2)       DEFAULT NULL COMMENT '固定金额，仅针对金额项',
    `rate`               DECIMAL(12, 4)       DEFAULT NULL COMMENT '费率，仅针对费率项',
    `del_flag`           TINYINT(1)  NOT NULL DEFAULT 0 COMMENT '删除标志',
    `remark`             VARCHAR(255) COMMENT '备注',
    `created_by`         VARCHAR(50) COMMENT '创建人',
    `create_time`        DATETIME             DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_by`         VARCHAR(50) COMMENT '更新人',
    `update_time`        DATETIME             DEFAULT CURRENT_TIMESTAMP
        ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX (`salary_template_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4 COMMENT ='薪资模板明细表';

-- 5. 员工模板绑定及发放记录表
CREATE TABLE `oa_employee_template_binding`
(
    `binding_id`            BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '记录ID',
    `employee_id`           BIGINT         NOT NULL COMMENT '员工ID → oa_employee.employee_id',
    `insurance_template_id` BIGINT         NOT NULL COMMENT '保险模板ID → oa_insurance_template.insurance_template_id',
    `salary_template_id`    BIGINT         NOT NULL COMMENT '薪资模板ID → oa_salary_template.salary_template_id',
    `pay_year`              SMALLINT       NOT NULL COMMENT '发放年度',
    `pay_month`             TINYINT        NOT NULL COMMENT '发放月份',
    `net_salary`            DECIMAL(12, 2) NOT NULL COMMENT '实发工资',
    `total_company_cost`    DECIMAL(12, 2) NOT NULL COMMENT '单位总支出',
    `status`                VARCHAR(20)    NOT NULL COMMENT '状态（已发/待发）',
    `del_flag`              TINYINT(1)     NOT NULL DEFAULT 0 COMMENT '删除标志',
    `remark`                VARCHAR(255) COMMENT '备注',
    `created_by`            VARCHAR(50) COMMENT '创建人',
    `create_time`           DATETIME                DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_by`            VARCHAR(50) COMMENT '更新人',
    `update_time`           DATETIME                DEFAULT CURRENT_TIMESTAMP
        ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX (`employee_id`),
    INDEX (`insurance_template_id`),
    INDEX (`salary_template_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4 COMMENT ='员工模板绑定及月度发放记录表';

-- 6. 绑定记录明细表（每次发放的模板明细实际金额）
CREATE TABLE `oa_binding_item_detail`
(
    `binding_item_id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '明细ID',
    `binding_id`      BIGINT                      NOT NULL COMMENT '绑定记录ID → oa_employee_template_binding.binding_id',
    `template_type`   ENUM ('insurance','salary') NOT NULL COMMENT '模板类型',
    `item_detail_id`  BIGINT                      NOT NULL COMMENT '明细ID → oa_insurance_template_detail.insurance_detail_id 或 oa_salary_template_detail.salary_detail_id',
    `paid_amount`     DECIMAL(12, 2)              NOT NULL COMMENT '实际缴纳/支付金额',
    `del_flag`        TINYINT(1)                  NOT NULL DEFAULT 0 COMMENT '删除标志',
    `remark`          VARCHAR(255) COMMENT '备注',
    `created_by`      VARCHAR(50) COMMENT '创建人',
    `create_time`     DATETIME                             DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_by`      VARCHAR(50) COMMENT '更新人',
    `update_time`     DATETIME                             DEFAULT CURRENT_TIMESTAMP
        ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX (`binding_id`),
    INDEX (`template_type`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4 COMMENT ='绑定记录明细表';
