-- 创建日结医保结算表
CREATE TABLE IF NOT EXISTS yb_day_end_settlement (
    id BIGSERIAL PRIMARY KEY,
    settlement_no VARCHAR(64) NOT NULL,
    settlement_date DATE NOT NULL,
    settlement_type VARCHAR(20) DEFAULT 'daily',
    insurance_type VARCHAR(50),
    total_visits INTEGER DEFAULT 0,
    total_amount NUMERIC(15,2) DEFAULT 0.00,
    insurance_pay_amount NUMERIC(15,2) DEFAULT 0.00,
    account_pay_amount NUMERIC(15,2) DEFAULT 0.00,
    personal_pay_amount NUMERIC(15,2) DEFAULT 0.00,
    fund_pay_sum_amount NUMERIC(15,2) DEFAULT 0.00,
    status CHAR(1) DEFAULT '0',
    operator VARCHAR(50),
    remark VARCHAR(500),
    create_by VARCHAR(64),
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_by VARCHAR(64),
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 添加注释
COMMENT ON TABLE yb_day_end_settlement IS '日结医保结算表';
COMMENT ON COLUMN yb_day_end_settlement.settlement_no IS '结算单号';
COMMENT ON COLUMN yb_day_end_settlement.settlement_date IS '结算日期';
COMMENT ON COLUMN yb_day_end_settlement.settlement_type IS '结算类型 (daily, weekly, monthly)';
COMMENT ON COLUMN yb_day_end_settlement.insurance_type IS '医保类型 (urbanEmployee, urbanRuralResident, newRuralCooperative, selfPaid)';
COMMENT ON COLUMN yb_day_end_settlement.total_visits IS '总人次';
COMMENT ON COLUMN yb_day_end_settlement.total_amount IS '总金额';
COMMENT ON COLUMN yb_day_end_settlement.insurance_pay_amount IS '医保统筹支付金额';
COMMENT ON COLUMN yb_day_end_settlement.account_pay_amount IS '个人账户支付金额';
COMMENT ON COLUMN yb_day_end_settlement.personal_pay_amount IS '个人自付金额';
COMMENT ON COLUMN yb_day_end_settlement.fund_pay_sum_amount IS '医保基金支付总额';
COMMENT ON COLUMN yb_day_end_settlement.status IS '状态 (0正常 1停用)';
COMMENT ON COLUMN yb_day_end_settlement.operator IS '操作员';
COMMENT ON COLUMN yb_day_end_settlement.remark IS '备注';
COMMENT ON COLUMN yb_day_end_settlement.create_by IS '创建者';
COMMENT ON COLUMN yb_day_end_settlement.create_time IS '创建时间';
COMMENT ON COLUMN yb_day_end_settlement.update_by IS '更新者';
COMMENT ON COLUMN yb_day_end_settlement.update_time IS '更新时间';

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_settlement_date ON yb_day_end_settlement(settlement_date);
CREATE INDEX IF NOT EXISTS idx_settlement_no ON yb_day_end_settlement(settlement_no);
CREATE INDEX IF NOT EXISTS idx_settlement_type ON yb_day_end_settlement(settlement_type);
CREATE INDEX IF NOT EXISTS idx_insurance_type ON yb_day_end_settlement(insurance_type);

-- 创建更新时间函数（如果不存在）
CREATE OR REPLACE FUNCTION update_modified_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.update_time = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ language 'plpgsql';

-- 创建更新时间触发器
DO $$
BEGIN
    DROP TRIGGER IF EXISTS update_yb_day_end_settlement_modtime ON yb_day_end_settlement;
    CREATE TRIGGER update_yb_day_end_settlement_modtime
        BEFORE UPDATE ON yb_day_end_settlement
        FOR EACH ROW EXECUTE FUNCTION update_modified_column();
END $$;

-- 插入示例数据
INSERT INTO yb_day_end_settlement (settlement_no, settlement_date, settlement_type, insurance_type, total_visits, total_amount, insurance_pay_amount, account_pay_amount, personal_pay_amount, fund_pay_sum_amount, status, operator, remark, create_by) VALUES
('YBDS20260202001', '2026-02-02', 'daily', 'urbanEmployee', 150, 150000.00, 120000.00, 15000.00, 15000.00, 135000.00, '0', 'admin', '2026年2月2日城镇职工医保日结', 'admin'),
('YBDS20260202002', '2026-02-02', 'daily', 'urbanRuralResident', 80, 80000.00, 64000.00, 8000.00, 8000.00, 72000.00, '0', 'admin', '2026年2月2日城乡居民医保日结', 'admin'),
('YBDS20260202003', '2026-02-02', 'daily', 'newRuralCooperative', 60, 60000.00, 48000.00, 6000.00, 6000.00, 54000.00, '0', 'admin', '2026年2月2日新农合医保日结', 'admin');