﻿CREATE DATABASE IF NOT EXISTS nursing_home DEFAULT CHARACTER SET utf8mb4;
USE nursing_home;

DROP TABLE IF EXISTS payment_record;
DROP TABLE IF EXISTS bill;
DROP TABLE IF EXISTS feedback;
DROP TABLE IF EXISTS notice;
DROP TABLE IF EXISTS handover;
DROP TABLE IF EXISTS health_record;
DROP TABLE IF EXISTS care_record;
DROP TABLE IF EXISTS schedule;
DROP TABLE IF EXISTS family_elder;
DROP TABLE IF EXISTS elder;
DROP TABLE IF EXISTS sys_user;

CREATE TABLE sys_user (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(50) NOT NULL UNIQUE,
  password VARCHAR(100) NOT NULL,
  name VARCHAR(50) NOT NULL,
  phone VARCHAR(30),
  role VARCHAR(20) NOT NULL,
  status TINYINT NOT NULL DEFAULT 1,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE elder (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(50) NOT NULL,
  gender VARCHAR(10),
  id_card VARCHAR(30) NOT NULL UNIQUE,
  birthday DATE,
  room_no VARCHAR(20),
  check_in_date DATE,
  care_level VARCHAR(20),
  status VARCHAR(20),
  remark VARCHAR(200)
);

CREATE TABLE family_elder (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  family_id BIGINT NOT NULL,
  elder_id BIGINT NOT NULL,
  relationship VARCHAR(20),
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_family (family_id),
  INDEX idx_elder (elder_id)
);

CREATE TABLE schedule (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  nurse_id BIGINT NOT NULL,
  date DATE NOT NULL,
  shift VARCHAR(20),
  task VARCHAR(200),
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_nurse_date (nurse_id, date)
);

CREATE TABLE care_record (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  elder_id BIGINT NOT NULL,
  nurse_id BIGINT NOT NULL,
  content VARCHAR(500),
  attachment_url VARCHAR(200),
  record_time DATETIME,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_elder (elder_id)
);

CREATE TABLE health_record (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  elder_id BIGINT NOT NULL,
  nurse_id BIGINT NOT NULL,
  temperature DECIMAL(4,1),
  bp_systolic INT,
  bp_diastolic INT,
  heart_rate INT,
  note VARCHAR(200),
  record_time DATETIME,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_elder (elder_id)
);

CREATE TABLE handover (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  nurse_id BIGINT NOT NULL,
  date DATE NOT NULL,
  content VARCHAR(500),
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_nurse (nurse_id)
);

CREATE TABLE notice (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  title VARCHAR(100) NOT NULL,
  content VARCHAR(1000),
  target_role VARCHAR(20),
  target_user_id BIGINT,
  created_by BIGINT,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE feedback (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  family_id BIGINT NOT NULL,
  elder_id BIGINT NOT NULL,
  type VARCHAR(20),
  content VARCHAR(500),
  rating INT,
  status VARCHAR(20),
  reply VARCHAR(500),
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE bill (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  elder_id BIGINT NOT NULL,
  month VARCHAR(7) NOT NULL,
  bed_fee DECIMAL(10,2),
  care_fee DECIMAL(10,2),
  meal_fee DECIMAL(10,2),
  other_fee DECIMAL(10,2),
  total DECIMAL(10,2),
  status VARCHAR(20),
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  paid_at DATETIME
);

CREATE TABLE payment_record (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  bill_id BIGINT NOT NULL,
  family_id BIGINT NOT NULL,
  amount DECIMAL(10,2) NOT NULL,
  method VARCHAR(20),
  paid_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO sys_user(username, password, name, phone, role, status)
VALUES ('admin', 'admin123', 'Administrator', '13800000000', 'ADMIN', 1);
