-- 如果表不存在则创建表，如果存在则添加缺失的列

-- 检查并创建appointment表或添加缺失列
CREATE TABLE IF NOT EXISTS appointment (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    customer_id BIGINT NOT NULL,
    pet_id BIGINT NOT NULL,
    doctor_id BIGINT,
    department VARCHAR(50),
    appointment_date DATE,
    time_slot VARCHAR(20), -- 添加这个缺失的列
    status VARCHAR(20) DEFAULT 'PENDING',
    remark TEXT,
    cancel_time TIMESTAMP NULL,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建visit表
CREATE TABLE IF NOT EXISTS visit (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    appointment_id BIGINT,
    customer_id BIGINT NOT NULL,
    pet_id BIGINT NOT NULL,
    doctor_id BIGINT NOT NULL,
    symptoms TEXT,
    diagnosis TEXT,
    treatment_plan TEXT,
    status VARCHAR(20) DEFAULT 'PENDING',
    total_amount DECIMAL(10,2),
    payment_status VARCHAR(20) DEFAULT 'UNPAID',
    payment_method VARCHAR(20),
    payment_time TIMESTAMP NULL,
    start_time TIMESTAMP,
    end_time TIMESTAMP,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建prescription表
CREATE TABLE IF NOT EXISTS prescription (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    visit_id BIGINT,
    doctor_id BIGINT,
    customer_id BIGINT,
    total_amount DECIMAL(10,2),
    status VARCHAR(20) DEFAULT 'DRAFT',
    remark TEXT,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建prescription_item表
CREATE TABLE IF NOT EXISTS prescription_item (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    prescription_id BIGINT NOT NULL,
    drug_id BIGINT NOT NULL,
    drug_name VARCHAR(100),
    specification VARCHAR(100),
    quantity INT NOT NULL,
    usage_desc VARCHAR(200),
    days INT,
    unit_price DECIMAL(10,2),
    subtotal DECIMAL(10,2),
    dosage VARCHAR(100),
    frequency VARCHAR(50),
    duration VARCHAR(50),
    usage_instructions TEXT,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建order_info表
CREATE TABLE IF NOT EXISTS order_info (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    visit_id BIGINT,
    customer_id BIGINT NOT NULL,
    amount DECIMAL(10,2),
    status VARCHAR(20) DEFAULT 'UNPAID',
    payment_method VARCHAR(20),
    payment_time TIMESTAMP NULL,
    remark TEXT,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建drug表
CREATE TABLE IF NOT EXISTS drug (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    category VARCHAR(50),
    manufacturer VARCHAR(100),
    specification VARCHAR(100),
    unit_price DECIMAL(10,2),
    stock_quantity INT DEFAULT 0,
    stock INT DEFAULT 0,
    alert_threshold INT,
    purchase_price DECIMAL(10,2),
    sale_price DECIMAL(10,2),
    approval_number VARCHAR(100),
    expiry_date TIMESTAMP NULL,
    unit VARCHAR(20),
    status INT DEFAULT 1,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建pet表
CREATE TABLE IF NOT EXISTS pet (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    owner_id BIGINT NOT NULL,
    name VARCHAR(50) NOT NULL,
    species VARCHAR(50),
    breed VARCHAR(100),
    gender VARCHAR(10), -- 修改为VARCHAR以支持MALE/FEMALE
    birthday DATE, -- 添加birthday字段
    age INT, -- 添加age字段
    weight DOUBLE, -- 添加weight字段
    photo VARCHAR(255), -- 添加photo字段
    remark TEXT, -- 添加remark字段
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 为已存在的pet表添加age列
ALTER TABLE pet ADD COLUMN IF NOT EXISTS age INT AFTER birthday;

-- 检查并创建doctor表
CREATE TABLE IF NOT EXISTS doctor (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    department VARCHAR(50),
    title VARCHAR(50),
    phone VARCHAR(20),
    email VARCHAR(100),
    avatar VARCHAR(255),
    status INT DEFAULT 1,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建user表
CREATE TABLE IF NOT EXISTS `user` (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    phone VARCHAR(20),
    email VARCHAR(100),
    password VARCHAR(255) NOT NULL,
    role VARCHAR(20) DEFAULT 'CUSTOMER',
    status INT DEFAULT 1,
    avatar VARCHAR(255),
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建medical_record表
CREATE TABLE IF NOT EXISTS medical_record (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    visit_id BIGINT NOT NULL,
    chief_complaint TEXT,
    present_illness TEXT,
    physical_examination TEXT,
    examination_results TEXT,
    diagnosis TEXT,
    treatment_plan TEXT,
    advice TEXT,
    status VARCHAR(20),
    record_type VARCHAR(50), -- CHECKUP体检, DIAGNOSIS诊断, TREATMENT治疗
    content TEXT,
    attachment_urls TEXT,
    doctor_id BIGINT,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建message表
CREATE TABLE IF NOT EXISTS message (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT,
    user_name VARCHAR(100),
    contact VARCHAR(100),
    title VARCHAR(200),
    sender_id BIGINT,
    receiver_id BIGINT NOT NULL,
    content TEXT NOT NULL,
    type VARCHAR(20) DEFAULT 'NOTICE', -- NOTICE通知, CHAT聊天
    status VARCHAR(20) DEFAULT 'UNREAD', -- UNREAD未读, READ已读
    reply TEXT,
    reply_time TIMESTAMP NULL,
    reply_user_id BIGINT,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建notice表
CREATE TABLE IF NOT EXISTS notice (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(200) NOT NULL,
    content TEXT NOT NULL,
    is_top INT DEFAULT 0,
    publisher_id BIGINT NOT NULL,
    publish_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    status INT DEFAULT 1,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建report表
CREATE TABLE IF NOT EXISTS report (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    visit_id BIGINT,
    pet_id BIGINT,
    type VARCHAR(50),
    title VARCHAR(200),
    summary TEXT,
    attachment_url VARCHAR(255),
    doctor_id BIGINT,
    report_type VARCHAR(50) DEFAULT 'DIAGNOSIS', -- REVENUE收入, CUSTOMER客户, PET宠物, DRUG药品, DIAGNOSIS检查报告
    report_data JSON,
    period_start DATE,
    period_end DATE,
    generated_by BIGINT,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 为已存在的report表修改report_type默认值
ALTER TABLE report MODIFY COLUMN report_type VARCHAR(50) DEFAULT 'DIAGNOSIS';

-- 检查并创建stock_in表
CREATE TABLE IF NOT EXISTS stock_in (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    drug_id BIGINT NOT NULL,
    quantity INT NOT NULL,
    unit_price DECIMAL(10,2),
    supplier VARCHAR(100),
    operator_id BIGINT,
    stock_in_time TIMESTAMP NULL,
    remark TEXT,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建stock_out表
CREATE TABLE IF NOT EXISTS stock_out (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    drug_id BIGINT NOT NULL,
    prescription_id BIGINT,
    quantity INT NOT NULL,
    unit_price DECIMAL(10,2),
    purpose VARCHAR(100), -- 用途：销售、损耗等
    operator_id BIGINT,
    stock_out_time TIMESTAMP NULL,
    remark TEXT,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);

-- 检查并创建vaccine_record表
CREATE TABLE IF NOT EXISTS vaccine_record (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    pet_id BIGINT NOT NULL,
    type VARCHAR(20), -- VACCINE疫苗, DEWORMING驱虫
    item_name VARCHAR(100),
    execute_date DATE,
    next_reminder_date DATE,
    vaccine_name VARCHAR(100) NOT NULL,
    dose_number INT,
    injection_date DATE,
    next_appointment_date DATE,
    doctor_id BIGINT,
    remark TEXT,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted INT DEFAULT 0
);
