-- 报销模块表结构
-- 执行前请确认当前数据库字符集为 utf8mb4

CREATE TABLE IF NOT EXISTS `yz_backend_approval_flows` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `tid` int(11) NOT NULL DEFAULT '0' COMMENT '租户ID',
  `uid` int(11) DEFAULT NULL COMMENT '用户ID(数据隔离)',
  `flow_name` varchar(100) NOT NULL DEFAULT '' COMMENT '流程名称',
  `step_order` tinyint(1) NOT NULL DEFAULT '0' COMMENT '步骤顺序(从1开始)',
  `approver_role` varchar(50) NOT NULL DEFAULT '' COMMENT '审批角色(如: dept_manager, finance, gm)',
  `approver_rule` varchar(200) NOT NULL DEFAULT '' COMMENT '审批人规则(如: 部门主管, 财务总监)',
  `timeout_hours` int(11) NOT NULL DEFAULT '48' COMMENT '超时提醒时间(小时)',
  `is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用：0-禁用 1-启用',
  `is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除：0-否 1-是',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `delete_time` datetime DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_flow_name` (`flow_name`) USING BTREE,
  KEY `idx_step_order` (`step_order`) USING BTREE,
  KEY `idx_is_active` (`is_active`) USING BTREE,
  KEY `idx_create_time` (`create_time`) USING BTREE,
  KEY `idx_delete_time` (`delete_time`) USING BTREE,
  KEY `idx_tid` (`tid`) USING BTREE,
  KEY `idx_uid` (`uid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='审批流程配置表';

CREATE TABLE IF NOT EXISTS `yz_backend_reimbursements` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `tid` int(11) NOT NULL DEFAULT '0' COMMENT '租户ID',
  `uid` int(11) DEFAULT NULL COMMENT '用户ID(数据隔离)',
  `user_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '申请人ID',
  `department_id` int(11) unsigned DEFAULT '0' COMMENT '部门ID',
  `total_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '总金额',
  `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态：0-草稿 1-审批中 2-已通过 3-已驳回 4-已撤回 5-已打款',
  `apply_date` date NOT NULL COMMENT '申请日期',
  `description` text COMMENT '备注说明',
  `current_approver_id` int(11) unsigned DEFAULT '0' COMMENT '当前审批人ID',
  `current_step` tinyint(1) NOT NULL DEFAULT '0' COMMENT '当前审批步骤(0:未提交)',
  `is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除：0-否 1-是',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `delete_time` datetime DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_user_id` (`user_id`) USING BTREE,
  KEY `idx_department_id` (`department_id`) USING BTREE,
  KEY `idx_status` (`status`) USING BTREE,
  KEY `idx_apply_date` (`apply_date`) USING BTREE,
  KEY `idx_current_approver_id` (`current_approver_id`) USING BTREE,
  KEY `idx_create_time` (`create_time`) USING BTREE,
  KEY `idx_update_time` (`update_time`) USING BTREE,
  KEY `idx_delete_time` (`delete_time`) USING BTREE,
  KEY `idx_tid` (`tid`) USING BTREE,
  KEY `idx_uid` (`uid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='报销单主表';

CREATE TABLE IF NOT EXISTS `yz_backend_reimbursement_items` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `tid` int(11) NOT NULL DEFAULT '0' COMMENT '租户ID',
  `uid` int(11) DEFAULT NULL COMMENT '用户ID(数据隔离)',
  `reimbursement_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '关联报销单ID',
  `expense_type` varchar(50) NOT NULL DEFAULT '' COMMENT '费用类型(具体子类)',
  `expense_date` date NOT NULL COMMENT '费用发生日期',
  `amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '金额',
  `quantity` int(11) NOT NULL DEFAULT '1' COMMENT '数量',
  `unit_price` decimal(10,2) DEFAULT '0.00' COMMENT '单价',
  `description` varchar(200) NOT NULL DEFAULT '' COMMENT '费用描述',
  `receipt_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否有发票：0-否 1-是',
  `is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除：0-否 1-是',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `delete_time` datetime DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_reimbursement_id` (`reimbursement_id`) USING BTREE,
  KEY `idx_expense_type` (`expense_type`) USING BTREE,
  KEY `idx_expense_date` (`expense_date`) USING BTREE,
  KEY `idx_create_time` (`create_time`) USING BTREE,
  KEY `idx_delete_time` (`delete_time`) USING BTREE,
  KEY `idx_tid` (`tid`) USING BTREE,
  KEY `idx_uid` (`uid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='报销费用明细表';

CREATE TABLE IF NOT EXISTS `yz_backend_approval_records` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `tid` int(11) NOT NULL DEFAULT '0' COMMENT '租户ID',
  `uid` int(11) DEFAULT NULL COMMENT '用户ID(数据隔离)',
  `reimbursement_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '关联报销单ID',
  `approver_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '审批人ID',
  `approver_name` varchar(100) NOT NULL DEFAULT '' COMMENT '审批人姓名(冗余)',
  `action` tinyint(1) NOT NULL DEFAULT '0' COMMENT '操作：1-提交 2-通过 3-驳回 4-转审 5-撤回',
  `comment` text COMMENT '审批意见',
  `step` tinyint(1) NOT NULL DEFAULT '0' COMMENT '审批步骤序号',
  `prev_step` tinyint(1) NOT NULL DEFAULT '0' COMMENT '上一步骤序号',
  `is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除：0-否 1-是',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `delete_time` datetime DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_reimbursement_id` (`reimbursement_id`) USING BTREE,
  KEY `idx_approver_id` (`approver_id`) USING BTREE,
  KEY `idx_action` (`action`) USING BTREE,
  KEY `idx_step` (`step`) USING BTREE,
  KEY `idx_create_time` (`create_time`) USING BTREE,
  KEY `idx_delete_time` (`delete_time`) USING BTREE,
  KEY `idx_tid` (`tid`) USING BTREE,
  KEY `idx_uid` (`uid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='审批记录表';
