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);

INSERT INTO sys_user(username, password, name, phone, role, status)
VALUES
('nurse1', 'nurse123', '护工刘敏', '13800000001', 'NURSE', 1),
('nurse2', 'nurse123', '护工张凯', '13800000002', 'NURSE', 1),
('family1', 'family123', '家属王伟', '13800000003', 'FAMILY', 1);

INSERT INTO elder(name, gender, id_card, birthday, room_no, check_in_date, care_level, status, remark)
VALUES
('陈国强', 'M', '110101194001010011', '1940-01-01', 'A101', '2023-01-10', '一级', '在住', '糖尿病史'),
('李秀兰', 'F', '110101194502020022', '1945-02-02', 'A102', '2023-02-15', '二级', '在住', '需要日常护理'),
('王德华', 'M', '110101195003030033', '1950-03-03', 'B201', '2023-03-20', '一级', '在住', '身体状况良好');

INSERT INTO family_elder(family_id, elder_id, relationship)
VALUES
(3, 1, '儿子'),
(3, 2, '女儿');

INSERT INTO schedule(nurse_id, date, shift, task)
VALUES
(2, '2024-06-01', '早班', 'A区日常巡查'),
(2, '2024-06-02', '夜班', 'A区夜间护理'),
(3, '2024-06-01', '中班', 'B区护理支持');

INSERT INTO care_record(elder_id, nurse_id, content, attachment_url, record_time)
VALUES
(1, 2, '协助用餐与日常清洁', NULL, '2024-06-01 09:30:00'),
(2, 3, '协助进行下肢活动训练', NULL, '2024-06-01 14:10:00');

INSERT INTO health_record(elder_id, nurse_id, temperature, bp_systolic, bp_diastolic, heart_rate, note, record_time)
VALUES
(1, 2, 36.6, 120, 80, 72, '正常', '2024-06-01 09:45:00'),
(2, 3, 36.8, 130, 85, 78, '血压略高', '2024-06-01 14:20:00');

INSERT INTO handover(nurse_id, date, content)
VALUES
(2, '2024-06-01', 'A区平稳，晚餐后关注陈国强'),
(3, '2024-06-01', 'B区训练完成，无异常情况');

INSERT INTO notice(title, content, target_role, target_user_id, created_by)
VALUES
('培训通知', '本周五10点进行消防演练培训', 'NURSE', NULL, 1),
('探视时间', '探视时间更新为 14:00-17:00', 'FAMILY', NULL, 1),
('全员会议', '周一上午召开月度会议', 'ALL', NULL, 1);

INSERT INTO feedback(family_id, elder_id, type, content, rating, status)
VALUES
(3, 1, 'SUGGESTION', '建议餐食增加蔬菜比例', 4, 'NEW');

INSERT INTO bill(elder_id, month, bed_fee, care_fee, meal_fee, other_fee, total, status)
VALUES
(1, '2024-05', 2000.00, 1500.00, 800.00, 0.00, 4300.00, 'UNPAID'),
(2, '2024-05', 2000.00, 1800.00, 800.00, 100.00, 4700.00, 'PAID');

INSERT INTO payment_record(bill_id, family_id, amount, method, paid_at)
VALUES
(2, 3, 4700.00, 'ONLINE', '2024-05-28 10:00:00');
