-- ============================================
-- SPA管理系统 - 店铺员工管理SQL
-- ============================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- 1. 店铺信息表
DROP TABLE IF EXISTS `spa_store`;
CREATE TABLE `spa_store` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `deleted_at` datetime(3) DEFAULT NULL,
  `store_no` varchar(50) NOT NULL COMMENT '店铺编号',
  `store_name` varchar(100) NOT NULL COMMENT '店铺名称',
  `store_type` tinyint DEFAULT 1 COMMENT '店铺类型 1直营 2加盟',
  `province` varchar(50) DEFAULT NULL COMMENT '省份',
  `city` varchar(50) DEFAULT NULL COMMENT '城市',
  `district` varchar(50) DEFAULT NULL COMMENT '区县',
  `address` varchar(500) DEFAULT NULL COMMENT '详细地址',
  `longitude` decimal(10,6) DEFAULT NULL COMMENT '经度',
  `latitude` decimal(10,6) DEFAULT NULL COMMENT '纬度',
  `phone` varchar(20) DEFAULT NULL COMMENT '联系电话',
  `business_hours` varchar(100) DEFAULT NULL COMMENT '营业时间',
  `images` json DEFAULT NULL COMMENT '店铺图片JSON',
  `facilities` json DEFAULT NULL COMMENT '设施JSON',
  `manager_id` bigint unsigned DEFAULT NULL COMMENT '店长ID',
  `status` tinyint DEFAULT 1 COMMENT '状态 1营业中 2休息中 3已关闭',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_store_no` (`store_no`),
  KEY `idx_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='店铺信息表';

-- 2. 房间信息表
DROP TABLE IF EXISTS `spa_room`;
CREATE TABLE `spa_room` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `deleted_at` datetime(3) DEFAULT NULL,
  `store_id` bigint unsigned NOT NULL COMMENT '店铺ID',
  `room_no` varchar(50) NOT NULL COMMENT '房间编号',
  `room_name` varchar(100) NOT NULL COMMENT '房间名称',
  `room_type` tinyint DEFAULT 1 COMMENT '房间类型 1单人 2双人 3VIP',
  `capacity` int DEFAULT 1 COMMENT '容纳人数',
  `facilities` json DEFAULT NULL COMMENT '设施JSON',
  `status` tinyint DEFAULT 1 COMMENT '状态 1空闲 2使用中 3维护中',
  PRIMARY KEY (`id`),
  KEY `idx_store_id` (`store_id`),
  KEY `idx_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='房间信息表';

-- 3. 房间预约表
DROP TABLE IF EXISTS `spa_room_booking`;
CREATE TABLE `spa_room_booking` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `room_id` bigint unsigned NOT NULL COMMENT '房间ID',
  `order_id` bigint unsigned NOT NULL COMMENT '订单ID',
  `booking_date` date NOT NULL COMMENT '预约日期',
  `start_time` time NOT NULL COMMENT '开始时间',
  `end_time` time NOT NULL COMMENT '结束时间',
  `status` tinyint DEFAULT 1 COMMENT '状态 1已预约 2使用中 3已完成 4已取消',
  PRIMARY KEY (`id`),
  KEY `idx_room_id` (`room_id`),
  KEY `idx_order_id` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='房间预约表';

-- 4. 部门表
DROP TABLE IF EXISTS `spa_department`;
CREATE TABLE `spa_department` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `deleted_at` datetime(3) DEFAULT NULL,
  `parent_id` bigint unsigned DEFAULT 0 COMMENT '父部门ID',
  `dept_name` varchar(100) NOT NULL COMMENT '部门名称',
  `dept_code` varchar(50) DEFAULT NULL COMMENT '部门编码',
  `leader_id` bigint unsigned DEFAULT NULL COMMENT '负责人ID',
  `sort` int DEFAULT 0 COMMENT '排序',
  `status` tinyint DEFAULT 1 COMMENT '状态 1正常 2停用',
  PRIMARY KEY (`id`),
  KEY `idx_parent_id` (`parent_id`),
  KEY `idx_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='部门表';

-- 5. 职位表
DROP TABLE IF EXISTS `spa_position`;
CREATE TABLE `spa_position` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `deleted_at` datetime(3) DEFAULT NULL,
  `position_name` varchar(100) NOT NULL COMMENT '职位名称',
  `position_code` varchar(50) DEFAULT NULL COMMENT '职位编码',
  `position_level` tinyint DEFAULT 1 COMMENT '职位级别',
  `base_salary` decimal(10,2) DEFAULT 0.00 COMMENT '基本工资',
  `sort` int DEFAULT 0 COMMENT '排序',
  `status` tinyint DEFAULT 1 COMMENT '状态 1正常 2停用',
  PRIMARY KEY (`id`),
  KEY `idx_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='职位表';

-- 6. 员工信息表
DROP TABLE IF EXISTS `spa_employee`;
CREATE TABLE `spa_employee` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `deleted_at` datetime(3) DEFAULT NULL,
  `user_id` bigint unsigned DEFAULT NULL COMMENT '关联用户ID',
  `employee_no` varchar(50) NOT NULL COMMENT '员工工号',
  `name` varchar(100) NOT NULL COMMENT '姓名',
  `gender` tinyint DEFAULT 0 COMMENT '性别 0未知 1男 2女',
  `phone` varchar(20) DEFAULT NULL COMMENT '手机号',
  `id_card` varchar(20) DEFAULT NULL COMMENT '身份证号',
  `avatar` varchar(500) DEFAULT NULL COMMENT '头像',
  `store_id` bigint unsigned DEFAULT NULL COMMENT '所属店铺ID',
  `dept_id` bigint unsigned DEFAULT NULL COMMENT '所属部门ID',
  `position_id` bigint unsigned DEFAULT NULL COMMENT '职位ID',
  `entry_date` date DEFAULT NULL COMMENT '入职日期',
  `leave_date` date DEFAULT NULL COMMENT '离职日期',
  `status` tinyint DEFAULT 1 COMMENT '状态 1在职 2离职',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_employee_no` (`employee_no`),
  KEY `idx_store_id` (`store_id`),
  KEY `idx_dept_id` (`dept_id`),
  KEY `idx_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工信息表';

-- 7. 考勤记录表
DROP TABLE IF EXISTS `spa_attendance`;
CREATE TABLE `spa_attendance` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `employee_id` bigint unsigned NOT NULL COMMENT '员工ID',
  `attendance_date` date NOT NULL COMMENT '考勤日期',
  `clock_in_time` datetime DEFAULT NULL COMMENT '上班打卡时间',
  `clock_out_time` datetime DEFAULT NULL COMMENT '下班打卡时间',
  `work_hours` decimal(5,2) DEFAULT 0.00 COMMENT '工作时长',
  `status` tinyint DEFAULT 1 COMMENT '状态 1正常 2迟到 3早退 4旷工 5请假',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  KEY `idx_employee_id` (`employee_id`),
  KEY `idx_attendance_date` (`attendance_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='考勤记录表';

-- 8. 请假记录表
DROP TABLE IF EXISTS `spa_leave`;
CREATE TABLE `spa_leave` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `employee_id` bigint unsigned NOT NULL COMMENT '员工ID',
  `leave_type` tinyint NOT NULL COMMENT '请假类型 1事假 2病假 3年假 4调休',
  `start_date` date NOT NULL COMMENT '开始日期',
  `end_date` date NOT NULL COMMENT '结束日期',
  `days` decimal(5,1) NOT NULL COMMENT '请假天数',
  `reason` text COMMENT '请假原因',
  `status` tinyint DEFAULT 1 COMMENT '状态 1待审核 2已批准 3已拒绝 4已取消',
  `audit_user_id` bigint unsigned DEFAULT NULL COMMENT '审核人ID',
  `audit_time` datetime DEFAULT NULL COMMENT '审核时间',
  `audit_remark` varchar(500) DEFAULT NULL COMMENT '审核备注',
  PRIMARY KEY (`id`),
  KEY `idx_employee_id` (`employee_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='请假记录表';

-- 9. 工资记录表
DROP TABLE IF EXISTS `spa_salary`;
CREATE TABLE `spa_salary` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `employee_id` bigint unsigned NOT NULL COMMENT '员工ID',
  `salary_month` varchar(7) NOT NULL COMMENT '工资月份 YYYY-MM',
  `base_salary` decimal(10,2) DEFAULT 0.00 COMMENT '基本工资',
  `performance_bonus` decimal(10,2) DEFAULT 0.00 COMMENT '绩效奖金',
  `commission` decimal(10,2) DEFAULT 0.00 COMMENT '提成',
  `allowance` decimal(10,2) DEFAULT 0.00 COMMENT '补贴',
  `deduction` decimal(10,2) DEFAULT 0.00 COMMENT '扣款',
  `total_salary` decimal(10,2) NOT NULL COMMENT '应发工资',
  `actual_salary` decimal(10,2) NOT NULL COMMENT '实发工资',
  `status` tinyint DEFAULT 1 COMMENT '状态 1待发放 2已发放',
  `pay_time` datetime DEFAULT NULL COMMENT '发放时间',
  `remark` text COMMENT '备注',
  PRIMARY KEY (`id`),
  KEY `idx_employee_id` (`employee_id`),
  KEY `idx_salary_month` (`salary_month`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='工资记录表';

SET FOREIGN_KEY_CHECKS = 1;
