-- ============================================================
-- 多币种迁移 SQL — 一次性执行
-- 数据库: accounting / pricing / llmage / product_management
-- ============================================================

-- ─── 1. accounting: currency + exchange_rate 表 + 初始数据 ───
CREATE TABLE IF NOT EXISTS accounting.currency (
    id VARCHAR(3) NOT NULL PRIMARY KEY,
    name VARCHAR(32) NOT NULL,
    symbol VARCHAR(8) DEFAULT '',
    decimal_places SMALLINT DEFAULT 2,
    is_base VARCHAR(1) DEFAULT '0',
    status VARCHAR(16) DEFAULT 'active'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT IGNORE INTO accounting.currency (id, name, symbol, decimal_places, is_base, status) VALUES
('CNY', '人民币', '¥', 2, '1', 'active'),
('USD', '美元', '$', 2, '0', 'active'),
('JPY', '日元', '¥', 0, '0', 'active'),
('GBP', '英镑', '£', 2, '0', 'active');

CREATE TABLE IF NOT EXISTS accounting.exchange_rate (
    id VARCHAR(32) NOT NULL PRIMARY KEY,
    from_currency VARCHAR(3) NOT NULL,
    to_currency VARCHAR(3) NOT NULL,
    buy_rate DOUBLE(14,6) DEFAULT 0,
    sell_rate DOUBLE(14,6) DEFAULT 0,
    mid_rate DOUBLE(14,6) DEFAULT 0,
    effective_date DATE NOT NULL,
    updated_at DATETIME DEFAULT NULL,
    UNIQUE KEY idx_pair_date (from_currency, to_currency, effective_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT IGNORE INTO accounting.exchange_rate (id, from_currency, to_currency, buy_rate, sell_rate, mid_rate, effective_date) VALUES
('er_usd_cny', 'USD', 'CNY', 7.150000, 7.250000, 7.200000, '2025-01-01'),
('er_jpy_cny', 'JPY', 'CNY', 0.048000, 0.049000, 0.048500, '2025-01-01'),
('er_gbp_cny', 'GBP', 'CNY', 9.100000, 9.250000, 9.175000, '2025-01-01'),
('er_cny_usd', 'CNY', 'USD', 0.137900, 0.139900, 0.138900, '2025-01-01');

INSERT IGNORE INTO accounting.appcodes (id, name, hierarchy_flg) VALUES ('currency_status', '币种状态', '0');
INSERT IGNORE INTO accounting.appcodes_kv (id, parentid, k, v) VALUES
('currency_status_active', 'currency_status', 'active', '启用'),
('currency_status_inactive', 'currency_status', 'inactive', '停用');

-- ─── 2. accounting: 账务表加币种字段 ───
ALTER TABLE accounting.account ADD COLUMN currency VARCHAR(3) DEFAULT 'CNY' AFTER org1id;
ALTER TABLE accounting.acc_detail ADD COLUMN currency VARCHAR(3) DEFAULT 'CNY' AFTER amount;
ALTER TABLE accounting.acc_detail ADD COLUMN exchange_rate DOUBLE(14,6) DEFAULT 1.0 AFTER currency;
ALTER TABLE accounting.acc_detail ADD COLUMN base_amount DOUBLE(20,2) DEFAULT 0 AFTER exchange_rate;
ALTER TABLE accounting.bill ADD COLUMN currency VARCHAR(3) DEFAULT 'CNY' AFTER amount;
ALTER TABLE accounting.bill ADD COLUMN base_amount DOUBLE(20,2) DEFAULT 0 AFTER currency;
ALTER TABLE accounting.bill_detail ADD COLUMN currency VARCHAR(3) DEFAULT 'CNY' AFTER amount;
ALTER TABLE accounting.bill_detail ADD COLUMN exchange_rate DOUBLE(14,6) DEFAULT 1.0 AFTER currency;
ALTER TABLE accounting.bill_detail ADD COLUMN base_amount DOUBLE(20,2) DEFAULT 0 AFTER exchange_rate;
ALTER TABLE accounting.ledger ADD COLUMN currency VARCHAR(3) DEFAULT 'CNY' AFTER amount;
ALTER TABLE accounting.ledger ADD COLUMN exchange_rate DOUBLE(14,6) DEFAULT 1.0 AFTER currency;
ALTER TABLE accounting.ledger ADD COLUMN base_amount DOUBLE(20,2) DEFAULT 0 AFTER exchange_rate;

-- ─── 3. pricing: 定价项目加币种 ───
ALTER TABLE pricing_program ADD COLUMN currency VARCHAR(3) DEFAULT 'CNY' AFTER description;

-- ─── 4. llmage: 使用记录加币种字段 ───
ALTER TABLE llmage.llmusage ADD COLUMN amount_currency VARCHAR(3) DEFAULT 'CNY' AFTER amount;
ALTER TABLE llmage.llmusage ADD COLUMN amount_base DOUBLE(20,2) DEFAULT 0 AFTER amount_currency;
ALTER TABLE llmage.llmusage ADD COLUMN cost_currency VARCHAR(3) DEFAULT 'CNY' AFTER cost;
ALTER TABLE llmage.llmusage ADD COLUMN cost_base DOUBLE(20,2) DEFAULT 0 AFTER cost_currency;

-- ─── 5. product_management: 订单表加币种 ───
ALTER TABLE product_management.biz_order ADD COLUMN currency VARCHAR(3) DEFAULT 'CNY' AFTER amount;
ALTER TABLE product_management.biz_orderdetail ADD COLUMN currency VARCHAR(3) DEFAULT 'CNY' AFTER trans_amount;
